-
-
Notifications
You must be signed in to change notification settings - Fork 264
[combobox][autocomplete] Fix controlled input value updates #2707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
commit: |
| onCompositionStart(event) { | ||
| isComposingRef.current = true; | ||
| setComposingValue(event.currentTarget.value); | ||
| }, | ||
| onCompositionEnd(event) { | ||
| isComposingRef.current = false; | ||
| const next = event.currentTarget.value; | ||
| setComposingValue(null); | ||
| store.state.setInputValue( | ||
| next, | ||
| createBaseUIEventDetails('input-change', event.nativeEvent), | ||
| ); | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc: @mj12albert to test with composed characters, none of this should be necessary now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still works correctly~
Also noticed in the async search autocomplete demo undo/redo using the keyboard (after typing some stuff) is messed up but works correctly in this PR now
Bundle size report
|
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
romgrk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach isn't very clean. I would suggest either:
- Add a callback setter in
useControlledto allow updating the store at the same time as the local state hook - Create a
useControlledthat uses the store instead of auseStatehook.
We're mixing reactivity models with the useControlled hook.
|
@romgrk I think the problem here is that it's the user's external The store can't update at the same time in controlled mode; it has to be derived? const [value, setValue] = React.useState('');
<Autocomplete.Root value={value} onValueChange={setValue}>I suppose a workaround would be to set the state in the store manually in |
|
Right, that's annoying. Have you tried updating the store in the render phase in reaction to the controlled prop change? React might be fine with it, considering it's an external store. Otherwise I guess the additional context could work. Anyway I'm quite unhappy about having to use |
|
Nope, it errors because it tries to update another component as the root is rendering:
I agree the effect is annoying to synchronize values but I wonder how we can restructure the code to avoid it - it seems difficult |
|
I found an issue when pressing Enter to pick the first IME suggestion doesn't work: Screen.Recording.2025-09-10.at.10.36.59.PM.movSame in all 3 browsers and also happens in master, using a number key to pick the first option works so it's not that bad |
|
Screen.Recording.2025-09-11.at.12.46.03.AM.movHere's the same input on master: Screen.Recording.2025-09-11.at.12.50.15.AM.mov |
c7b159b to
947900d
Compare
947900d to
9dace8e
Compare
|
@mj12albert ah right, makes sense - the composition handling also fixed the issue Material UI has without the handling added. I've restored it back. |
mj12albert
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Controlled mode works correctly now, though better add a test for the correct caret position when inserting text in the middle of the input string ~
d66a9c3 to
284041b
Compare
Fixes #2703
Previously:
store.state.setInputValue(...)inputValuefromuseControlledReact.useEffect+store.apply({ inputValue })executesinputValueupdates too late inCombobox.Input- it must be sync (from theonChangecall) for the caret position to be preserved.Now:
store.state.setInputValue(...)inputValuefromuseControlledcc: @romgrk maybe you know of a better way to update the store based on derived values from other hooks, or perhaps a new context provider is required just for this special case.