Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed: async doesn't change value when onInputChange returns a value #2133

Merged
merged 1 commit into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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