Skip to content

Commit

Permalink
fix(ComboBox): bring IME support back (carbon-design-system#4952)
Browse files Browse the repository at this point in the history
This change backs out carbon-design-system#3646 partially given moving `inputValue` state
syncing code from `handleOnInputValueChange` to `handleOnStateChange`
causes in-composition string (of IME) getting lost, because the
different rendering sequence yielded by such change causes Downshift to
(temporarily) render an older (stale) value to the `<input>`.

Fixes carbon-design-system#4947.
  • Loading branch information
asudoh authored and joshblack committed Jan 14, 2020
1 parent 4a09563 commit f788410
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
8 changes: 2 additions & 6 deletions packages/react/src/components/ComboBox/ComboBox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,10 @@ describe('ComboBox', () => {
it('should set `inputValue` to an empty string if a falsey-y value is given', () => {
const wrapper = mount(<ComboBox {...mockProps} />);

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

wrapper
.instance()
.handleOnStateChange({ inputValue: null }, downshiftActions);
wrapper.instance().handleOnInputValueChange(null, downshiftActions);
expect(wrapper.state('inputValue')).toBe('');
});
});
Expand Down
31 changes: 17 additions & 14 deletions packages/react/src/components/ComboBox/ComboBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,24 +220,26 @@ export default class ComboBox extends React.Component {
}
};

handleOnInputValueChange = inputValue => {
const { onInputChange } = this.props;

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);
}
}
);
}
};

Expand Down Expand Up @@ -301,6 +303,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}
Expand Down

0 comments on commit f788410

Please sign in to comment.