Skip to content

Commit 00f84ac

Browse files
authored
Render hidden form input fields for Checkbox, Switch and RadioGroup components (#3095)
* add `overrides` prop to internal `FormFields` By default, the hidden form fields render an `<input type="hidden" />`. However, we have some components where we explicitly want to change the type for example `checkbox` or `radio` types. I first tried to encode this information in the data itself. That requires us to use symbols so that we don't accidentally choose a key that actually exists in the data. An overrides key solves this for our use cases. The component is also internal, so nothing is exposed to the user. * always render hidden form elements In case of checkboxes and radio elements, we will only render the hidden inputs if: 1. You pass a `name` prop to make it form-aware 2. When the checkbox / radio is "checked" This is now changed to always render the hidden input as long as the `name` prop is passed to make it form-aware. * update changelog
1 parent 92a69ef commit 00f84ac

File tree

6 files changed

+15
-6
lines changed

6 files changed

+15
-6
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
- Fix cursor position when re-focusing the `ComboboxInput` component ([#3065](https://github.com/tailwindlabs/headlessui/pull/3065))
2525
- Keep focus inside of the `<ComboboxInput />` component ([#3073](https://github.com/tailwindlabs/headlessui/pull/3073))
2626
- Fix enter transitions for the `Transition` component ([#3074](https://github.com/tailwindlabs/headlessui/pull/3074))
27+
- Render hidden form input fields for `Checkbox`, `Switch` and `RadioGroup` components ([#3095](https://github.com/tailwindlabs/headlessui/pull/3095))
2728

2829
### Changed
2930

packages/@headlessui-react/src/components/checkbox/checkbox.test.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ describe('Form submissions', () => {
135135
</form>
136136
)
137137

138+
let checkbox = document.querySelector('[id^="headlessui-checkbox-"]') as HTMLInputElement
139+
138140
// Focus the checkbox
139-
await focus(getCheckbox())
141+
await focus(checkbox)
140142

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

147149
// Toggle
148-
await click(getCheckbox())
150+
await click(checkbox)
149151

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

156158
// Toggle
157-
await click(getCheckbox())
159+
await click(checkbox)
158160

159161
// Submit
160162
await press(Keys.Enter)

packages/@headlessui-react/src/components/checkbox/checkbox.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ function CheckboxFn<TTag extends ElementType = typeof DEFAULT_CHECKBOX_TAG, TTyp
174174
{name != null && (
175175
<FormFields
176176
disabled={disabled}
177-
data={checked ? { [name]: value || 'on' } : {}}
177+
data={{ [name]: value || 'on' }}
178+
overrides={{ type: 'checkbox', checked }}
178179
form={form}
179180
onReset={reset}
180181
/>

packages/@headlessui-react/src/components/radio-group/radio-group.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ function RadioGroupFn<TTag extends ElementType = typeof DEFAULT_RADIO_GROUP_TAG,
315315
{name != null && (
316316
<FormFields
317317
disabled={disabled}
318-
data={value != null ? { [name]: value || 'on' } : {}}
318+
data={{ [name]: value || 'on' }}
319+
overrides={{ type: 'radio', checked: value != null }}
319320
form={form}
320321
onReset={reset}
321322
/>

packages/@headlessui-react/src/components/switch/switch.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ function SwitchFn<TTag extends ElementType = typeof DEFAULT_SWITCH_TAG>(
240240
{name != null && (
241241
<FormFields
242242
disabled={disabled}
243-
data={checked ? { [name]: value || 'on' } : {}}
243+
data={{ [name]: value || 'on' }}
244+
overrides={{ type: 'checkbox', checked }}
244245
form={form}
245246
onReset={reset}
246247
/>

packages/@headlessui-react/src/internal/form-fields.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ export function FormFields({
3333
form: formId,
3434
disabled,
3535
onReset,
36+
overrides,
3637
}: {
3738
data: Record<string, any>
39+
overrides?: Record<string, any>
3840
form?: string
3941
disabled?: boolean
4042
onReset?: (e: Event) => void
@@ -66,6 +68,7 @@ export function FormFields({
6668
disabled,
6769
name,
6870
value,
71+
...overrides,
6972
})}
7073
/>
7174
)

0 commit comments

Comments
 (0)