Skip to content

Commit

Permalink
[Issue 157] move onConfirm to be after state
Browse files Browse the repository at this point in the history
Let the state be set with updated value then trigger
`onConfirm`
  • Loading branch information
Sam Tsai authored and colinrotherham committed Jan 17, 2024
1 parent 24e5bce commit d2e80b9
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dist/accessible-autocomplete.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/accessible-autocomplete.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/accessible-autocomplete.preact.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/accessible-autocomplete.preact.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/accessible-autocomplete.react.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/lib/accessible-autocomplete.react.min.js.map

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,22 @@ export default class Autocomplete extends Component {

handleComponentBlur (newState) {
const { options, query, selected } = this.state
let newQuery
let newQuery, setStateCallback

if (this.props.confirmOnBlur) {
newQuery = newState.query || query
this.props.onConfirm(options[selected])
setStateCallback = this.props.onConfirm(options[selected])
} else {
newQuery = query
setStateCallback = () => {}
}
this.setState({
focused: null,
menuOpen: newState.menuOpen || false,
query: newQuery,
selected: null,
validChoiceMade: this.isQueryAnOption(newQuery, options)
})
}, setStateCallback)
}

handleListMouseLeave (event) {
Expand Down Expand Up @@ -280,15 +282,14 @@ export default class Autocomplete extends Component {
handleOptionClick (event, index) {
const selectedOption = this.state.options[index]
const newQuery = this.templateInputValue(selectedOption)
this.props.onConfirm(selectedOption)
this.setState({
focused: -1,
hovered: null,
menuOpen: false,
query: newQuery,
selected: -1,
validChoiceMade: true
})
}, this.props.onConfirm(selectedOption))
this.forceUpdate()
}

Expand Down

0 comments on commit d2e80b9

Please sign in to comment.