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

fix(ComboBox): switch from onInputValueChange to onStateChange #3646

Merged
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
10 changes: 7 additions & 3 deletions packages/react/src/components/ComboBox/ComboBox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const downshiftActions = {
setHighlightedIndex: jest.fn(),
};
const clearInput = wrapper =>
wrapper.instance().handleOnInputValueChange('', downshiftActions);
wrapper.instance().handleOnStateChange({ inputValue: '' }, downshiftActions);

describe('ComboBox', () => {
let mockProps;
Expand Down Expand Up @@ -156,10 +156,14 @@ describe('ComboBox', () => {
it('should set `inputValue` to an empty string if a falsey-y value is given', () => {
const wrapper = mount(<ComboBox {...mockProps} />);

wrapper.instance().handleOnInputValueChange('foo', downshiftActions);
wrapper
.instance()
.handleOnStateChange({ inputValue: 'foo' }, downshiftActions);
expect(wrapper.state('inputValue')).toBe('foo');

wrapper.instance().handleOnInputValueChange(null, downshiftActions);
wrapper
.instance()
.handleOnStateChange({ inputValue: null }, downshiftActions);
expect(wrapper.state('inputValue')).toBe('');
});
});
Expand Down
35 changes: 19 additions & 16 deletions packages/react/src/components/ComboBox/ComboBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,25 @@ export default class ComboBox extends React.Component {
event.stopPropagation();
};

handleOnInputValueChange = (inputValue, { setHighlightedIndex }) => {
const { onInputChange } = this.props;

setHighlightedIndex(findHighlightedIndex(this.props, inputValue));

this.setState(
() => ({
// Default to empty string if we have a false-y `inputValue`
inputValue: inputValue || '',
}),
() => {
if (onInputChange) {
onInputChange(inputValue);
handleOnStateChange = (newState, { setHighlightedIndex }) => {
if (Object.prototype.hasOwnProperty.call(newState, 'inputValue')) {
const { inputValue } = newState;
const { onInputChange } = this.props;

setHighlightedIndex(findHighlightedIndex(this.props, inputValue));

this.setState(
() => ({
// Default to empty string if we have a false-y `inputValue`
inputValue: inputValue || '',
}),
() => {
if (onInputChange) {
onInputChange(inputValue);
}
}
}
);
);
}
};

onToggleClick = isOpen => event => {
Expand Down Expand Up @@ -276,7 +279,7 @@ export default class ComboBox extends React.Component {
<Downshift
{...downshiftProps}
onChange={this.handleOnChange}
onInputValueChange={this.handleOnInputValueChange}
onStateChange={this.handleOnStateChange}
inputValue={this.state.inputValue || ''}
itemToString={itemToString}
defaultSelectedItem={initialSelectedItem}>
Expand Down