Skip to content

Commit

Permalink
Merge pull request #2133 from tehbi4/master
Browse files Browse the repository at this point in the history
fixed: async doesn't change value when onInputChange returns a value
  • Loading branch information
JedWatson committed Nov 16, 2017
2 parents 6cc59dd + e71b7a9 commit c22abbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,17 @@ export default class Async extends Component {

onInputChange (inputValue) {
const { ignoreAccents, ignoreCase, onInputChange } = this.props;
let transformedInputValue = inputValue;
let newInputValue = inputValue;

if (onInputChange) {
const value = onInputChange(newInputValue);
// Note: != used deliberately here to catch undefined and null
if (value != null && typeof value !== 'object') {
newInputValue = '' + value;
}
}

let transformedInputValue = newInputValue;

if (ignoreAccents) {
transformedInputValue = stripDiacritics(transformedInputValue);
Expand All @@ -148,15 +158,11 @@ export default class Async extends Component {
transformedInputValue = transformedInputValue.toLowerCase();
}

if (onInputChange) {
onInputChange(transformedInputValue);
}

this.setState({ inputValue });
this.setState({ inputValue: newInputValue });
this.loadOptions(transformedInputValue);

// Return the original input value to avoid modifying the user's view of the input while typing.
return inputValue;
// Return new input value, but without applying toLowerCase() to avoid modifying the user's view case of the input while typing.
return newInputValue;
}

noResultsText() {
Expand Down
11 changes: 10 additions & 1 deletion test/Async-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('Async', () => {
typeSearchText('te');
return expect(asyncNode.textContent, 'to contain', 'Loading');
});

it('caches the result of all option fetches', (cb) => {
const res = {
t: createOptionsResponse(['t']),
Expand Down Expand Up @@ -475,6 +475,15 @@ describe('Async', () => {
typeSearchText('a');
return expect(onInputChange, 'was called times', 1);
});

it('should change the value when onInputChange returns a value', () => {
const onInputChange = sinon.stub().returns('2');
const instance = createControl({
onInputChange,
});
typeSearchText('1');
return expect(filterInputNode.value, 'to equal', '2');
});
});

describe('.focus()', () => {
Expand Down

0 comments on commit c22abbd

Please sign in to comment.