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

Make form components uncontrollable #1683

Merged
merged 5 commits into from Aug 1, 2022

Conversation

RobinMalfait
Copy link
Collaborator

This PR allows our form related components (Switch, RadioGroup, Listbox and Combobox) to be both controlled and uncontrolled (new).

This has the benefit that you don't have to do any wiring yourself if you just want to choose values from a know list and use the good old <form> element to submit your values.

Add a name prop to those components (more info: Listbox example https://headlessui.com/react/listbox#using-with-html-forms) and you should be good to go!

@vercel
Copy link

vercel bot commented Jul 15, 2022

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

Name Status Preview Updated
headlessui-react ✅ Ready (Inspect) Visit Preview Sep 1, 2022 at 10:19AM (UTC)
headlessui-vue ✅ Ready (Inspect) Visit Preview Sep 1, 2022 at 10:19AM (UTC)

A few versions ago we introduced compatibility with the native `form`
element. This means that behind the scenes we render hidden inputs that
are kept in sync which allows you to submit your normal form and get
data via `new FormData(event.currentTarget)`.

Before this change every form related component (Switch, RadioGroup,
Listbox and Combobox) always had to be passed a `value` and an
`onChange` regardless of this change.

This change will allow you to not even use the `value` and the
`onChange` at all and keep it completely uncontrolled.

This has some changes:

- `value` is made optional
- `onChange` is made optional (but will still be called if passed
  regardless of being controlled or uncontrolled)
- `defaultValue` got added so that you can still pre-fill your values
  with known values.
- `value` render prop got exposed so that you can still use this while
  rendering.

This should also make it completely compatible with tools like Remix
without wiring up your own state.
@RobinMalfait RobinMalfait force-pushed the feat/uncontrolled-form-components branch from b4d9ce7 to de8474f Compare August 1, 2022 10:17
@RobinMalfait RobinMalfait changed the title [WIP] Make form components uncontrollable Make form components uncontrollable Aug 1, 2022
@RobinMalfait RobinMalfait merged commit 1831832 into main Aug 1, 2022
@RobinMalfait RobinMalfait deleted the feat/uncontrolled-form-components branch August 1, 2022 10:37
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.

None yet

1 participant