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

fix(forms): Selection accessibility enhancement by using toggle buttons #2795

Merged
merged 1 commit into from
Oct 26, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface ToggleButtonProps
/**
* <em>(required)</em> the text shown in the ToggleButton.
*/
text?: string;
text?: React.ReactNode;
/**
* Use either the `label` property or provide a custom one.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useMemo, useContext, useCallback } from 'react'
import {
Button,
ToggleButton,
Dropdown,
Radio,
HelpButton,
Expand All @@ -14,6 +14,7 @@ import Option from '../Option'
import { useDataValue } from '../../hooks'
import { FormError, FieldProps, FieldHelpProps } from '../../types'
import { pickSpacingProps } from '../../../../components/flex/utils'
import ToggleButtonGroupContext from '../../../../components/toggle-button/ToggleButtonGroupContext'

interface IOption {
title: string | React.ReactNode
Expand Down Expand Up @@ -153,17 +154,21 @@ function Selection(props: Props) {
return (
<FieldBlock {...fieldBlockProps}>
<ButtonRow>
{options.map((option, i) => (
<Button
key={`option-${i}-${option.value}`}
id={id}
text={option.title}
on_click={option.handleSelect}
variant={option.value === value ? undefined : 'secondary'}
status={error ? 'error' : undefined}
disabled={disabled}
/>
))}
<ToggleButtonGroupContext.Provider
value={{
status: error ? 'error' : undefined,
disabled,
}}
>
{options.map((option, i) => (
<ToggleButton
key={`option-${i}-${option.value}`}
text={option.title}
on_change={option.handleSelect}
checked={option.value === value}
/>
))}
</ToggleButtonGroupContext.Provider>
</ButtonRow>
</FieldBlock>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,57 @@ describe('Selection', () => {
expect(radioButtons[1]).not.toBeChecked()
})
})

describe('button', () => {
it('has no selected value by defualt', () => {
render(
<Field.Selection variant="button">
<Field.Option value="foo">Fooo</Field.Option>
<Field.Option value="bar">Baar</Field.Option>
</Field.Selection>
)

const buttons = document.querySelectorAll('button')
expect(buttons.length).toEqual(2)
expect(buttons[0].getAttribute('aria-pressed')).toBe('false')
expect(buttons[1].getAttribute('aria-pressed')).toBe('false')
})

it('renders selected option', () => {
render(
<Field.Selection variant="button" value="bar">
<Field.Option value="foo">Fooo</Field.Option>
<Field.Option value="bar">Baar</Field.Option>
</Field.Selection>
)

const buttons = document.querySelectorAll('button')
expect(buttons.length).toEqual(2)
expect(buttons[0].getAttribute('aria-pressed')).toBe('false')
expect(buttons[1].getAttribute('aria-pressed')).toBe('true')
})

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

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

const buttons = document.querySelectorAll('button')
expect(buttons.length).toEqual(2)
expect(buttons[0].getAttribute('aria-pressed')).toBe('true')
expect(buttons[1].getAttribute('aria-pressed')).toBe('false')
})
})
})

describe('event handlers', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function Toggle(props: Props) {
)
const handleToggleChange = useCallback(
({ value }) => {
console.log('value', value)
handleChange?.(value === 'on' ? valueOn : valueOff)
},
[handleChange, valueOn, valueOff]
Expand Down
Loading