Skip to content

Commit

Permalink
Ensure children prop of Field component can be a render prop (#2941)
Browse files Browse the repository at this point in the history
* ensure `children` prop can be a render prop

* update changelog
  • Loading branch information
RobinMalfait committed Jan 23, 2024
1 parent f2bc6fd commit ce17c6d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Expose `disabled` state on `<Tab />` component ([#2918](https://github.com/tailwindlabs/headlessui/pull/2918))
- Prevent default behaviour when clicking outside of a `Dialog.Panel` ([#2919](https://github.com/tailwindlabs/headlessui/pull/2919))
- Use `isFocused` instead of `isFocusVisible` for `Input` and `Textarea` components ([#2940](https://github.com/tailwindlabs/headlessui/pull/2940))
- Ensure `children` prop of `Field` component can be a render prop ([#2941](https://github.com/tailwindlabs/headlessui/pull/2941))

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

Expand Down
19 changes: 19 additions & 0 deletions packages/@headlessui-react/src/components/field/field.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ describe('Rendering', () => {
expect(container.firstChild).not.toHaveAttribute('aria-disabled', 'true')
})

it('should render a `Field` component with a render prop', async () => {
let { container } = render(
<Field>
{(slot) => {
return (
<div data-slot={JSON.stringify(slot)}>
<input />
</div>
)
}}
</Field>
)

expect(container.querySelector('[data-slot]')?.getAttribute('data-slot')).toEqual(
JSON.stringify({ disabled: false })
)
expect(container.firstChild).not.toHaveAttribute('aria-disabled', 'true')
})

it('should add `aria-disabled` when a `Field` is disabled', async () => {
let { container } = render(
<Field disabled>
Expand Down
8 changes: 7 additions & 1 deletion packages/@headlessui-react/src/components/field/field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ function FieldFn<TTag extends ElementType = typeof DEFAULT_FIELD_TAG>(
ourProps,
theirProps: {
...theirProps,
children: <FormFieldsProvider>{theirProps.children}</FormFieldsProvider>,
children: (
<FormFieldsProvider>
{typeof theirProps.children === 'function'
? theirProps.children(slot)
: theirProps.children}
</FormFieldsProvider>
),
},
slot,
defaultTag: DEFAULT_FIELD_TAG,
Expand Down

2 comments on commit ce17c6d

@vercel
Copy link

@vercel vercel bot commented on ce17c6d Jan 23, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

headlessui-vue – ./playgrounds/vue

headlessui-vue-tailwindlabs.vercel.app
headlessui-vue.vercel.app
headlessui-vue-git-main-tailwindlabs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ce17c6d Jan 23, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

headlessui-react – ./playgrounds/react

headlessui-react.vercel.app
headlessui-react-tailwindlabs.vercel.app
headlessui-react-git-main-tailwindlabs.vercel.app

Please sign in to comment.