Skip to content

Commit 00786f8

Browse files
dakahnjoshblack
andcommitted
fix(tag): adds various fixes for screen reader accessibility; adds AAT (#4863)
* fix(filter): add role button and update story for clarity * fix(tag): change span to button, remove role and add button-reset helper * fix(tag): add button-reset to tag styling * test(tag): add Axe test * fix(tag): remove unused aria, title, tab-index; add DAP * test(tag): add busted unit tests for screenreader HALP * test(tag): add aria-label test * Update packages/react/src/components/Tag/Tag-test.js Co-Authored-By: Josh Black <josh@josh.black> * Update packages/react/src/components/Tag/Tag-test.js Co-Authored-By: Josh Black <josh@josh.black> * Update packages/react/src/components/Tag/Tag-test.js Co-Authored-By: Josh Black <josh@josh.black> * Update packages/react/src/components/Tag/Tag-test.js Co-Authored-By: Josh Black <josh@josh.black> * test(tags): add less specific check for children for aria-label * test(tag): add <main> wrapper for DAP test element Co-authored-by: Josh Black <josh@josh.black>
1 parent 21025ae commit 00786f8

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

packages/components/src/components/tag/_tag.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@mixin tags {
1818
.#{$prefix}--tag {
1919
@include type-style('label-01');
20+
@include button-reset($width: false);
2021

2122
display: inline-flex;
2223
align-items: center;

packages/react/src/components/Tag/Tag-story.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const props = {
2626
'red'
2727
),
2828
disabled: boolean('Disabled (disabled)', false),
29-
title: 'Clear Selection',
29+
title: 'Clear Filter',
3030
}),
3131
filter() {
3232
return { ...this.regular(), onClick: action('onClick') };

packages/react/src/components/Tag/Tag-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,43 @@ import Tag from '../Tag';
1010
import TagSkeleton from '../Tag/Tag.Skeleton';
1111
import { shallow } from 'enzyme';
1212
import { settings } from 'carbon-components';
13+
import { render, cleanup } from '@carbon/test-utils/react';
1314

1415
const { prefix } = settings;
1516

1617
describe('Tag', () => {
18+
afterEach(cleanup);
19+
20+
describe('automated accessibility testing', () => {
21+
it('should have no Axe violations', async () => {
22+
const { container } = render(<Tag>This is not a tag</Tag>);
23+
await expect(container).toHaveNoAxeViolations();
24+
});
25+
26+
it('should have no DAP violations', async () => {
27+
const { container } = render(
28+
<main>
29+
<Tag>This is not a tag</Tag>
30+
</main>
31+
);
32+
await expect(container).toHaveNoDAPViolations('Tag');
33+
});
34+
});
35+
36+
describe('with a screenreader', () => {
37+
it('filtered variant should have appropriate aria-label', () => {
38+
const children = 'tag content';
39+
const { container } = render(<Tag filter>{children}</Tag>);
40+
const button = container.querySelector('[aria-label], [aria-labelledby]');
41+
const accessibilityLabel =
42+
button.getAttribute('aria-label') ||
43+
button.getAttribute('aria-labelledby');
44+
// This check would mirror our "Accessibility label must contain at least all of visible label"
45+
// requirement
46+
expect(accessibilityLabel).toEqual(expect.stringContaining(children));
47+
});
48+
});
49+
1750
describe('Renders as expected', () => {
1851
it('should render with the appropriate type', () => {
1952
const tag = shallow(<Tag type="beta" />);

packages/react/src/components/Tag/Tag.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,21 @@ const Tag = ({
4141
[`${prefix}--tag--filter`]: filter,
4242
});
4343
return filter ? (
44-
<span
45-
role="button"
44+
<button
4645
className={tagClasses}
47-
title={title || 'Clear filter'}
48-
tabIndex="0" // eslint-disable-line jsx-a11y/no-noninteractive-tabindex
46+
aria-label={
47+
title !== undefined
48+
? `${title} ${children}`
49+
: `Clear filter ${children}`
50+
}
4951
{...other}>
5052
{children !== null && children !== undefined ? children : TYPES[type]}
51-
<Close16 aria-label={title || 'Clear filter'} />
52-
</span>
53+
<Close16 />
54+
</button>
5355
) : (
54-
<span className={tagClasses} {...other}>
56+
<button className={tagClasses} {...other}>
5557
{children !== null && children !== undefined ? children : TYPES[type]}
56-
</span>
58+
</button>
5759
);
5860
};
5961

0 commit comments

Comments
 (0)