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

Only handle form reset when defaultValue is used #3240

Merged
merged 3 commits into from
May 24, 2024

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.
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

@RobinMalfait RobinMalfait merged commit 8c3499c into main May 24, 2024
8 checks passed
@RobinMalfait RobinMalfait deleted the fix/default-value-and-resets branch May 24, 2024 22:26
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.

1 participant