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

Render hidden form input fields for Checkbox, Switch and RadioGroup components #3095

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix cursor position when re-focusing the `ComboboxInput` component ([#3065](https://github.com/tailwindlabs/headlessui/pull/3065))
- Keep focus inside of the `<ComboboxInput />` component ([#3073](https://github.com/tailwindlabs/headlessui/pull/3073))
- Fix enter transitions for the `Transition` component ([#3074](https://github.com/tailwindlabs/headlessui/pull/3074))
- Render hidden form input fields for `Checkbox`, `Switch` and `RadioGroup` components ([#3095](https://github.com/tailwindlabs/headlessui/pull/3095))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ describe('Form submissions', () => {
</form>
)

let checkbox = document.querySelector('[id^="headlessui-checkbox-"]') as HTMLInputElement

// Focus the checkbox
await focus(getCheckbox())
await focus(checkbox)

// Submit
await press(Keys.Enter)
Expand All @@ -145,7 +147,7 @@ describe('Form submissions', () => {
expect(handleSubmission).toHaveBeenLastCalledWith({})

// Toggle
await click(getCheckbox())
await click(checkbox)

// Submit
await press(Keys.Enter)
Expand All @@ -154,7 +156,7 @@ describe('Form submissions', () => {
expect(handleSubmission).toHaveBeenLastCalledWith({ notifications: 'on' })

// Toggle
await click(getCheckbox())
await click(checkbox)

// Submit
await press(Keys.Enter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ function CheckboxFn<TTag extends ElementType = typeof DEFAULT_CHECKBOX_TAG, TTyp
{name != null && (
<FormFields
disabled={disabled}
data={checked ? { [name]: value || 'on' } : {}}
data={{ [name]: value || 'on' }}
overrides={{ type: 'checkbox', checked }}
form={form}
onReset={reset}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ function RadioGroupFn<TTag extends ElementType = typeof DEFAULT_RADIO_GROUP_TAG,
{name != null && (
<FormFields
disabled={disabled}
data={value != null ? { [name]: value || 'on' } : {}}
data={{ [name]: value || 'on' }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it doesn't technically matter but it seems weird to me that this produces a hidden radio input where the value="on" (it's not checked so it's not going to show up in form data).

I feel like I would've expected to see three radios in the DOM with the same name, the three values, and only the appropriate one checked. Instead of mutating a single radio.

Maybe it doesn't matter though? Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit odd for sure, but if you submit a form with <input type="radio" name="foo" /> without a value, then it will be submitted with a value of "on".

We could try to move the hidden inputs to each Radio component itself, but only the checked ones will be submitted anyway so this feels better. Then we don't have to render n hidden radio inputs.

overrides={{ type: 'radio', checked: value != null }}
form={form}
onReset={reset}
/>
Expand Down
3 changes: 2 additions & 1 deletion packages/@headlessui-react/src/components/switch/switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ function SwitchFn<TTag extends ElementType = typeof DEFAULT_SWITCH_TAG>(
{name != null && (
<FormFields
disabled={disabled}
data={checked ? { [name]: value || 'on' } : {}}
data={{ [name]: value || 'on' }}
overrides={{ type: 'checkbox', checked }}
form={form}
onReset={reset}
/>
Expand Down
3 changes: 3 additions & 0 deletions packages/@headlessui-react/src/internal/form-fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ export function FormFields({
form: formId,
disabled,
onReset,
overrides,
}: {
data: Record<string, any>
overrides?: Record<string, any>
form?: string
disabled?: boolean
onReset?: (e: Event) => void
Expand Down Expand Up @@ -66,6 +68,7 @@ export function FormFields({
disabled,
name,
value,
...overrides,
})}
/>
)
Expand Down
Loading