Issue 157: trigger onConfirm after set state#169
Issue 157: trigger onConfirm after set state#169samtsai wants to merge 1 commit intoalphagov:mainfrom
onConfirm after set state#169Conversation
2b24f4f to
5344b08
Compare
|
Have some linting errors, let me fix |
5344b08 to
62022fc
Compare
src/autocomplete.js
Outdated
| query: newQuery, | ||
| selected: null | ||
| }) | ||
| }, this.props.confirmOnBlur ? this.props.onConfirm(newQuery) : null) |
There was a problem hiding this comment.
@tvararu I wasn't quite sure how the selected property was being used so I opted to just send newQuery. During testing, selected was -1 so option[selected] was not returning what I expected.
There was a problem hiding this comment.
I opted to just send
newQuery
👍 That's fine. options[selected] was weird given that newQuery exists in the same scope.
The second argument to setState should be a callback function. You're passing the result of the onConfirm() invocation. So your code (when this.props.confirmOnBlur === true) at runtime is:
this.setState({
focused: null,
menuOpen: newState.menuOpen || false,
query: newQuery,
selected: null
}, undefined)With the added side effect of running onConfirm in order to produce the undefined argument for setState, which shouldn't actually solve the underlying issue (if it did, that's curious).
Instead you want this (added () =>):
this.setState({
focused: null,
menuOpen: newState.menuOpen || false,
query: newQuery,
selected: null
}, () => this.props.confirmOnBlur ? this.props.onConfirm(newQuery) : null)Also apply the same change to the other place where this is used. :)
There was a problem hiding this comment.
options[selected]was weird given thatnewQueryexists in the same scope.
Actually, disregard what I said. It should be options[selected]. options[-1] returning undefined is correct behaviour. In which tests did you find this to be unexpected?
There was a problem hiding this comment.
Thanks @tvararu, I will correct the callback application, but as far as sending options[selected] to onConfirm, wouldn't that be misleading? I thought onConfirm is meant to receive the selected query, in which case options[-1] was giving undefined.
What I mean is, in normal usage the selected property was not being updated in the state in all cases or would come back as -1. I was just using any of the example Autocompletes.
There was a problem hiding this comment.
@samtsai it's meant to receive the selected option which can be an object. query is just a printable string that gets inlined as part of the input. For most usecases these two concepts are identical, but the autocomplete does support more advanced use cases using the templates.inputValue option.
There was a problem hiding this comment.
Ok makes sense, I'll need to take a look at this a little longer, going to be on vacation rest of this weekend, so I'll pick it up next week.
Likely, I'll want to update my tests to reflect your comments.
src/autocomplete.js
Outdated
| query: newQuery, | ||
| selected: null | ||
| }) | ||
| }, this.props.confirmOnBlur ? this.props.onConfirm(newQuery) : null) |
There was a problem hiding this comment.
I opted to just send
newQuery
👍 That's fine. options[selected] was weird given that newQuery exists in the same scope.
The second argument to setState should be a callback function. You're passing the result of the onConfirm() invocation. So your code (when this.props.confirmOnBlur === true) at runtime is:
this.setState({
focused: null,
menuOpen: newState.menuOpen || false,
query: newQuery,
selected: null
}, undefined)With the added side effect of running onConfirm in order to produce the undefined argument for setState, which shouldn't actually solve the underlying issue (if it did, that's curious).
Instead you want this (added () =>):
this.setState({
focused: null,
menuOpen: newState.menuOpen || false,
query: newQuery,
selected: null
}, () => this.props.confirmOnBlur ? this.props.onConfirm(newQuery) : null)Also apply the same change to the other place where this is used. :)
|
See comment, looks good otherwise. 👍 |
62022fc to
2c70526
Compare
| if (this.props.confirmOnBlur) { | ||
| newQuery = newState.query || query | ||
| this.props.onConfirm(options[selected]) | ||
| setStateCallback = this.props.onConfirm(options[selected]) |
There was a problem hiding this comment.
This should be:
setStateCallback = () => this.props.onConfirm(options[selected]) | query: newQuery, | ||
| selected: -1 | ||
| }) | ||
| }, this.props.onConfirm(selectedOption)) |
There was a problem hiding this comment.
This should be:
}, () => this.props.onConfirm(selectedOption))|
Should this be documented somewhere? Edit: Actually, I've reread it this does not introduce anything new. 👍 |
2c70526 to
ad7fc95
Compare
|
Any update on this? Thanks! |
Let the state be set with updated value then trigger `onConfirm`
ad7fc95 to
d2e80b9
Compare
|
I've rebased this one with |
onConfirmis currently triggered beforesetStateis called,to keep proper state with what is sent to
onConfirm,setStateshould be called then
onConfirm.This is to resolve #157