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(a11y): add better aria labels for yes/no component #4876

Merged
merged 1 commit into from
Sep 16, 2022
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
22 changes: 10 additions & 12 deletions frontend/src/components/Field/YesNo/YesNo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ import { BiCheck, BiX } from 'react-icons/bi'
import {
forwardRef,
HStack,
Icon,
useFormControlProps,
useMultiStyleConfig,
useRadioGroup,
} from '@chakra-ui/react'
import pick from 'lodash/pick'

import { YESNO_THEME_KEY } from '~theme/components/Field/YesNo'
import { FieldColorScheme } from '~theme/foundations/colours'

import { YesNoOption } from './YesNoOption'
Expand Down Expand Up @@ -53,7 +50,6 @@ export interface YesNoProps {
*/
export const YesNo = forwardRef<YesNoProps, 'input'>(
({ colorScheme, ...props }, ref) => {
const styles = useMultiStyleConfig(YESNO_THEME_KEY, props)
const formControlProps = useFormControlProps(props)
const { getRootProps, getRadioProps } = useRadioGroup(props)

Expand Down Expand Up @@ -88,17 +84,19 @@ export const YesNo = forwardRef<YesNoProps, 'input'>(
side="left"
colorScheme={colorScheme}
{...noProps}
leftIcon={BiX}
label="No"
// Ref is set here for tracking current value, and also so any errors
// can focus this input.
ref={ref}
>
<Icon as={BiX} __css={styles.icon} />
No
</YesNoOption>
<YesNoOption side="right" colorScheme={colorScheme} {...yesProps}>
<Icon as={BiCheck} __css={styles.icon} />
Yes
</YesNoOption>
/>
<YesNoOption
side="right"
colorScheme={colorScheme}
{...yesProps}
leftIcon={BiCheck}
label="Yes"
/>
</HStack>
)
},
Expand Down
29 changes: 21 additions & 8 deletions frontend/src/components/Field/YesNo/YesNoOption.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { KeyboardEvent, useCallback } from 'react'
import { IconType } from 'react-icons/lib'
import {
Box,
forwardRef,
Icon,
useMultiStyleConfig,
useRadio,
UseRadioGroupReturn,
UseRadioProps,
VisuallyHidden,
} from '@chakra-ui/react'

import { YESNO_THEME_KEY } from '~theme/components/Field/YesNo'
import { FieldColorScheme } from '~theme/foundations/colours'

interface YesNoOptionProps extends UseRadioProps {
children: React.ReactNode
/**
* Icon to be displayed to the left of the option content.
*/
leftIcon?: IconType

/**
* Label to be displayed as the option content.
*/
label: string

/**
* Side of the option for styling to be used for styling.
Expand All @@ -37,7 +46,7 @@ interface YesNoOptionProps extends UseRadioProps {
* Option rendering for `YesNo` component.
*/
export const YesNoOption = forwardRef<YesNoOptionProps, 'input'>(
({ children, ...props }, ref) => {
({ leftIcon, label, ...props }, ref) => {
const styles = useMultiStyleConfig(YESNO_THEME_KEY, props)

const { getInputProps, getCheckboxProps } = useRadio(props)
Expand Down Expand Up @@ -76,17 +85,21 @@ export const YesNoOption = forwardRef<YesNoOptionProps, 'input'>(
as="label"
__css={styles.container}
data-testid={`${props.name}-${props.side}`}
role="button"
ref={ref}
aria-label={`${label} option, ${
props.isChecked ? 'selected' : 'unselected'
}`}
>
<input
{...inputProps}
onClick={handleSelect}
onKeyDown={handleSpacebar}
aria-hidden
/>
<VisuallyHidden>
"{children}" option {props.isChecked ? 'selected' : 'unselected'}
</VisuallyHidden>
<Box {...checkboxProps} __css={styles.option}>
{children}
<Box {...checkboxProps} __css={styles.option} aria-hidden>
{leftIcon ? <Icon as={leftIcon} __css={styles.icon} /> : null}
{label}
</Box>
</Box>
)
Expand Down