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

Allow custom id attribute to <Listbox.Button /> #2020

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions packages/@headlessui-react/src/components/listbox/listbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,16 @@ describe('Rendering', () => {

expect(getListboxButton()).not.toHaveAttribute('type')
})

it('should support a custom `id` attribute', async () => {
render(
<Listbox value={null} onChange={console.log}>
<Listbox.Button id="my-custom-id">Trigger</Listbox.Button>
</Listbox>
)

expect(getListboxButton()).toHaveAttribute('id', 'my-custom-id')
})
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ let Button = forwardRefWithAs(function Button<TTag extends ElementType = typeof
let actions = useActions('Listbox.Button')
let buttonRef = useSyncRefs(data.buttonRef, ref)

let id = `headlessui-listbox-button-${useId()}`
let id = props.id ?? `headlessui-listbox-button-${useId()}`
let d = useDisposables()

let handleKeyDown = useEvent((event: ReactKeyboardEvent<HTMLButtonElement>) => {
Expand Down