Skip to content

Conversation

@RobinMalfait
Copy link
Member

This PR fixes a bug where it would call the onChange of a <Listbox> or <Combobox> component when used inside of a <form> even if no defaultValue was provided. This also happened for the <Checkbox> and <Switch> component but for the defaultChecked prop.

This means that the onChange would be called with undefined which your application might not expect.

This PR only calls onChange with the defaultValue (or defaultChecked for <Checkbox> or <Switch> components) if it is provided.

This PR also has a small refactor to ensure we can safely use defaultChecked and defaultValue in dependency arrays of hooks.

This allows us to have a guaranteed `default value` that never changes
unless the component re-mounts.

Since the hook returns a stable value, we can safely include it in
dependency arrays of certain hooks.

Before this change, including this is in the dependency arrays it would
cause a trigger or change of the hook when the `defaultValue` changes
but we never want that.
…provided

If a `defaultValue` is provided, then the reset will be handled and the
`onChange` will be called with this value.

If no `defaultValue` was provided, we won't handle the `reset`,
otherwise we would call the `onChange` with `undefined` which is
incorrect.
@vercel
Copy link

vercel bot commented May 24, 2024

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

Name Status Preview Comments Updated (UTC)
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 24, 2024 10:02pm
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 24, 2024 10:02pm

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.

2 participants