Skip to content

Commit ce17c6d

Browse files
authored
Ensure children prop of Field component can be a render prop (#2941)
* ensure `children` prop can be a render prop * update changelog
1 parent f2bc6fd commit ce17c6d

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Expose `disabled` state on `<Tab />` component ([#2918](https://github.com/tailwindlabs/headlessui/pull/2918))
1313
- Prevent default behaviour when clicking outside of a `Dialog.Panel` ([#2919](https://github.com/tailwindlabs/headlessui/pull/2919))
1414
- Use `isFocused` instead of `isFocusVisible` for `Input` and `Textarea` components ([#2940](https://github.com/tailwindlabs/headlessui/pull/2940))
15+
- Ensure `children` prop of `Field` component can be a render prop ([#2941](https://github.com/tailwindlabs/headlessui/pull/2941))
1516

1617
## [2.0.0-alpha.4] - 2024-01-03
1718

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ describe('Rendering', () => {
1414
expect(container.firstChild).not.toHaveAttribute('aria-disabled', 'true')
1515
})
1616

17+
it('should render a `Field` component with a render prop', async () => {
18+
let { container } = render(
19+
<Field>
20+
{(slot) => {
21+
return (
22+
<div data-slot={JSON.stringify(slot)}>
23+
<input />
24+
</div>
25+
)
26+
}}
27+
</Field>
28+
)
29+
30+
expect(container.querySelector('[data-slot]')?.getAttribute('data-slot')).toEqual(
31+
JSON.stringify({ disabled: false })
32+
)
33+
expect(container.firstChild).not.toHaveAttribute('aria-disabled', 'true')
34+
})
35+
1736
it('should add `aria-disabled` when a `Field` is disabled', async () => {
1837
let { container } = render(
1938
<Field disabled>

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ function FieldFn<TTag extends ElementType = typeof DEFAULT_FIELD_TAG>(
5353
ourProps,
5454
theirProps: {
5555
...theirProps,
56-
children: <FormFieldsProvider>{theirProps.children}</FormFieldsProvider>,
56+
children: (
57+
<FormFieldsProvider>
58+
{typeof theirProps.children === 'function'
59+
? theirProps.children(slot)
60+
: theirProps.children}
61+
</FormFieldsProvider>
62+
),
5763
},
5864
slot,
5965
defaultTag: DEFAULT_FIELD_TAG,

0 commit comments

Comments
 (0)