Skip to content

Commit

Permalink
fix(Selection): correctly link id with label (#2911)
Browse files Browse the repository at this point in the history
Fixes #2842
  • Loading branch information
tujoworker authored Nov 17, 2023
1 parent 6332903 commit 71ad30d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function Selection(props: Props) {
>
{options.map((option, i) => (
<ToggleButton
id={options.length > 0 ? id : undefined}
key={`option-${i}-${option.value}`}
text={option.title}
on_change={option.handleSelect}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,33 @@ describe('Selection', () => {
expect(buttons[1].getAttribute('aria-pressed')).toBe('true')
})

it('renders fieldset/legend if more than two options are given', () => {
const { rerender } = render(
<Field.Selection variant="button" label="Legend">
<Field.Option value="foo">Fooo</Field.Option>
<Field.Option value="bar">Baar</Field.Option>
</Field.Selection>
)

expect(document.querySelectorAll('fieldset')).toHaveLength(1)
expect(document.querySelectorAll('legend')).toHaveLength(1)
expect(document.querySelectorAll('label')).toHaveLength(0)

rerender(
<Field.Selection variant="button" label="Label">
<Field.Option value="foo">Fooo</Field.Option>
</Field.Selection>
)

expect(document.querySelectorAll('fieldset')).toHaveLength(0)
expect(document.querySelectorAll('legend')).toHaveLength(0)
expect(document.querySelectorAll('label')).toHaveLength(1)
expect(document.querySelector('label')).toHaveAttribute('for')
expect(document.querySelector('label').getAttribute('for')).toBe(
document.querySelector('button').getAttribute('id')
)
})

it('renders update selected option based on external value change', () => {
const { rerender } = render(
<Field.Selection variant="button" value="bar">
Expand Down

0 comments on commit 71ad30d

Please sign in to comment.