Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit 3f83a92

Browse files
CMTegnerwhichsteveyp
authored andcommitted
[fixed] Select highlighted item on input click (#84)
While trying to debug #80 I discovered that clicking the input field to "confirm" a suggestion didn't work as expected. The problem: 1. Navigate to https://reactcommunity.org/react-autocomplete/static-data/ 2. Type 'a' in input field 3. Observe that the input has been filled with 'Alabama' 4. Click anywhere inside the input field 5. Observe that the highlighted selection is gone, and the menu has been closed 6. Click/tab outside the input 7. Observe that the input value has been reverted to 'a' This change ensures that clicking in the input field has the same effect as clicking the highlighted item in the suggestion menu.
1 parent 1715443 commit 3f83a92

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/Autocomplete.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ let Autocomplete = React.createClass({
305305
handleInputClick () {
306306
if (this.isInputFocused() && this.state.isOpen === false)
307307
this.setState({ isOpen: true })
308+
else if (this.state.highlightedIndex !== null)
309+
this.selectItemFromMouse(this.getFilteredItems()[this.state.highlightedIndex])
308310
},
309311

310312
render () {

lib/__tests__/Autocomplete-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,27 @@ describe('Autocomplete kewDown->Escape event handlers', () => {
244244

245245
});
246246

247+
describe('Autocomplete click event handlers', () => {
248+
249+
var autocompleteWrapper = mount(AutocompleteComponentJSX({}));
250+
var autocompleteInputWrapper = autocompleteWrapper.find('input');
251+
252+
it('should update input value from selected menu item and close the menu', () => {
253+
autocompleteWrapper.setState({ isOpen: true });
254+
autocompleteInputWrapper.simulate('focus');
255+
autocompleteInputWrapper.simulate('change', { target: { value: 'Ar' } });
256+
257+
// simulate keyUp of last key, triggering autocomplete suggestion + selection of the suggestion in the menu
258+
autocompleteInputWrapper.simulate('keyUp', { key : 'r', keyCode: 82, which: 82 });
259+
260+
// Click inside input, updating state.value with the selected Autocomplete suggestion
261+
autocompleteInputWrapper.simulate('click');
262+
expect(autocompleteWrapper.state('value')).to.equal('Arizona');
263+
expect(autocompleteWrapper.state('isOpen')).to.be.false;
264+
});
265+
266+
});
267+
247268
// Component method unit tests
248269
describe('Autocomplete#renderMenu', () => {
249270

0 commit comments

Comments
 (0)