Skip to content
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

Improve syncing of the Combobox.Input value #2042

Merged
merged 4 commits into from
Nov 23, 2022
Merged

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Nov 23, 2022

This PR improves the syncing of the Combobox.Input value.

Syncing of the input should happen when the value changes internally or externally

I also got rid of the manually dispatching of the change event if the value changes from internally.

I think the correct mental model is:

  • That the Combobox.Input value should change if the selected value changes from the outside (props) or from the inside.
    • Note: It should not do that if you are currently typing (once you choose a new value it will re-sync, once you reset (escape / outside click) it will also sync again).
  • The onChange/onInput of the Combobox.Input itself should only be called if you as the user type something. Not when the value is "synced" based on the selected value.

The manual dispatching of events tried to solve an issue (#1875), but I think this can be solved in 2 other ways that make a bit more sense:

  1. (Today) Use the onBlur on the input to reset the query value to filter options. E.g.: <Combobox.Input onBlur={() => setQuery('')} />
  2. (In the future) Use an exposed onClose (or similar) event to reset your query value.

Fixes: #1997

…or externally

I also got rid of the manually dispatching of the change event if the
value changes from internally.

I think the correct mental model is:
- That the `Combobox.Input` value should change if the selected value
  changes from the outside or from the inside.
  - Note: It should _not_ do that if you are currently typing (once you
    choose a new value it will re-sync, once you reset (escape / outside
    click) it will also sync again).
- The `onChange`/`onInput` of the `Combobox.Input` itself should only be
  called if you as the user type something. Not when the value is
  "synced" based on the selected value. We were currently manually
  dispatching events which works (to a certain extend) but smelled a bit
  fishy to me.

The manual dispatching of events tried to solve an issue
(#1875), but I think
this can be solved in 2 other ways that make a bit more sense:

1. (Today) Use the `onBlur` on the input to reset the `query` value to filter
   options.
2. (In the future)  Use an exposed `onClose` (or similar) event to reset
   your `query` value.
@vercel
Copy link

vercel bot commented Nov 23, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
headlessui-react ✅ Ready (Inspect) Visit Preview Nov 23, 2022 at 3:02PM (UTC)
headlessui-vue ✅ Ready (Inspect) Visit Preview Nov 23, 2022 at 3:02PM (UTC)

@stefanprobst
Copy link

@RobinMalfait do you have an example for the mentioned onBlur approach? i added this to my reproduction for #1875 (see https://github.com/stefanprobst/headlessui-combobox) and while it clears the input field, it seems i now cannot actually select a value.

@SergSpice
Copy link

Thank you a lot i was pulling my hair out because of this and i just had to install the newest version of the lib

@stefanprobst
Copy link

@RobinMalfait should i open a new issue for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Controlled Comobox shows only one option after selecting
3 participants