Skip to content

Commit

Permalink
fix(Token): suppress close button a11y issue(#3337)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackUait authored Feb 19, 2024
1 parent c11465b commit b7de35f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
11 changes: 3 additions & 8 deletions packages/react-ui/components/Token/Token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,10 @@ export class Token extends React.Component<TokenProps> {
const theme = this.theme;

const validation = getValidation(error, warning);
const removeButtonAriaLabel = this.locale.removeButtonAriaLabel + ' ' + children;

const icon = isTheme2022(theme) ? (
<CloseButtonIcon
aria-label={this.locale.removeButtonAriaLabel}
side={16}
color="inherit"
colorHover="inherit"
tabbable={false}
/>
<CloseButtonIcon side={16} color="inherit" colorHover="inherit" role="none" />
) : (
<CrossIcon />
);
Expand Down Expand Up @@ -149,7 +144,7 @@ export class Token extends React.Component<TokenProps> {
<span className={styles.text(this.theme)}>{children}</span>
<span
role={isTheme2022(theme) ? undefined : 'button'}
aria-label={isTheme2022(theme) ? undefined : this.locale.removeButtonAriaLabel}
aria-label={isTheme2022(theme) ? undefined : removeButtonAriaLabel}
className={cx(styles.removeIcon(this.theme), globalClasses.removeIcon)}
onClick={this.onRemoveClick}
data-tid={TokenDataTids.removeIcon}
Expand Down
21 changes: 15 additions & 6 deletions packages/react-ui/components/Token/__tests__/Token-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,39 @@ describe('Token', () => {
});

it('has correct value on close button aria-label attribute (ru)', () => {
render(<Token />);
const tokenName = 'name';
render(<Token>{tokenName}</Token>);

expect(screen.getByRole('button')).toHaveAttribute('aria-label', TokenLocalesRu.removeButtonAriaLabel);
expect(screen.getByRole('button')).toHaveAttribute(
'aria-label',
TokenLocalesRu.removeButtonAriaLabel + ' ' + tokenName,
);
});

it('has correct value on close button aria-label attribute (en)', () => {
const tokenName = 'name';
render(
<LocaleContext.Provider value={{ langCode: LangCodes.en_GB }}>
<Token />
<Token>{tokenName}</Token>
</LocaleContext.Provider>,
);

expect(screen.getByRole('button')).toHaveAttribute('aria-label', TokenLocalesEn.removeButtonAriaLabel);
expect(screen.getByRole('button')).toHaveAttribute(
'aria-label',
TokenLocalesEn.removeButtonAriaLabel + ' ' + tokenName,
);
});

it('sets custom value for `closeButtonAriaLabel` locale', () => {
const customAriaLabel = 'test';
const tokenName = 'name';
render(
<LocaleContext.Provider value={{ locale: { Token: { removeButtonAriaLabel: customAriaLabel } } }}>
<Token />
<Token>{tokenName}</Token>
</LocaleContext.Provider>,
);

expect(screen.getByRole('button')).toHaveAttribute('aria-label', customAriaLabel);
expect(screen.getByRole('button')).toHaveAttribute('aria-label', customAriaLabel + ' ' + tokenName);
});
});
});

0 comments on commit b7de35f

Please sign in to comment.