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

Commit cf13fb3

Browse files
committed
Delay re-focus until all actions have been processed (#240)
This is a fix for when trying to select an item while the input was off-screen: The focus was being reset immediately after it had been lost, causing the view- port to scroll to the input field before the item's onClick handler had been processed. This was a problem because if the cursor moved as much as one pixel in the meantime it would change the highlightedIndex, causing a re-render (because it would now likely be hovering over a different item) and aborting the chain of actions (i.e. the item would not be selected).
1 parent 534a49a commit cf13fb3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/Autocomplete.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ let Autocomplete = React.createClass({
197197
if (prevState.isOpen !== this.state.isOpen) {
198198
this.props.onMenuVisibilityChange(this.state.isOpen)
199199
}
200+
// Capture the input's focus as long as the ignoreBlur flag is set
201+
if (this._ignoreBlur) {
202+
this.refs.input.focus()
203+
}
200204
},
201205

202206
exposeAPI(el) {
@@ -376,11 +380,13 @@ let Autocomplete = React.createClass({
376380

377381
selectItemFromMouse(item) {
378382
const value = this.props.getItemValue(item)
379-
this.setIgnoreBlur(false)
380383
this.setState({
381384
isOpen: false,
382385
highlightedIndex: null
383386
}, () => {
387+
// Clear the ignoreBlur flag after the component has
388+
// updated to release control over the input's focus
389+
this.setIgnoreBlur(false)
384390
this.props.onSelect(value, item)
385391
})
386392
},
@@ -418,7 +424,6 @@ let Autocomplete = React.createClass({
418424

419425
handleInputBlur(event) {
420426
if (this._ignoreBlur) {
421-
this.refs.input.focus()
422427
return
423428
}
424429
this.setState({

0 commit comments

Comments
 (0)