Skip to content

Commit 0b88f6d

Browse files
authored
Merge pull request #10204 from marmelab/fix-filter-button-accessibility
Fix filter button accessibility
2 parents 1686e21 + 947fd6d commit 0b88f6d

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

packages/ra-ui-materialui/src/list/filter/FilterButton.spec.tsx

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,27 @@ describe('<FilterButton />', () => {
4949

5050
fireEvent.click(await screen.findByLabelText('Add filter'));
5151

52-
let checkboxs: HTMLInputElement[] = screen.getAllByRole('checkbox');
53-
expect(checkboxs).toHaveLength(3);
54-
expect(checkboxs[0].checked).toBe(false);
55-
expect(checkboxs[1].checked).toBe(false);
56-
expect(checkboxs[2].checked).toBe(false);
52+
let checkboxes: HTMLInputElement[] =
53+
screen.getAllByRole('menuitemcheckbox');
54+
expect(checkboxes).toHaveLength(3);
55+
expect(checkboxes[0].getAttribute('aria-checked')).toBe('false');
56+
expect(checkboxes[1].getAttribute('aria-checked')).toBe('false');
57+
expect(checkboxes[2].getAttribute('aria-checked')).toBe('false');
5758

58-
fireEvent.click(checkboxs[0]);
59+
fireEvent.click(checkboxes[0]);
5960

6061
await screen.findByRole('textbox', {
6162
name: 'Title',
6263
});
6364
fireEvent.click(screen.getByLabelText('Add filter'));
6465

65-
checkboxs = screen.getAllByRole('checkbox');
66-
expect(checkboxs).toHaveLength(3);
67-
expect(checkboxs[0].checked).toBe(true);
68-
expect(checkboxs[1].checked).toBe(false);
69-
expect(checkboxs[2].checked).toBe(false);
66+
checkboxes = screen.getAllByRole('menuitemcheckbox');
67+
expect(checkboxes).toHaveLength(3);
68+
expect(checkboxes[0].getAttribute('aria-checked')).toBe('true');
69+
expect(checkboxes[1].getAttribute('aria-checked')).toBe('false');
70+
expect(checkboxes[2].getAttribute('aria-checked')).toBe('false');
7071

71-
fireEvent.click(checkboxs[0]);
72+
fireEvent.click(checkboxes[0]);
7273

7374
await waitFor(
7475
() => {
@@ -82,20 +83,21 @@ describe('<FilterButton />', () => {
8283
);
8384

8485
fireEvent.click(screen.getByLabelText('Add filter'));
85-
checkboxs = screen.getAllByRole('checkbox');
86-
expect(checkboxs).toHaveLength(3);
87-
expect(checkboxs[0].checked).toBe(false);
88-
expect(checkboxs[1].checked).toBe(false);
89-
expect(checkboxs[2].checked).toBe(false);
86+
checkboxes = screen.getAllByRole('menuitemcheckbox');
87+
expect(checkboxes).toHaveLength(3);
88+
expect(checkboxes[0].getAttribute('aria-checked')).toBe('false');
89+
expect(checkboxes[1].getAttribute('aria-checked')).toBe('false');
90+
expect(checkboxes[2].getAttribute('aria-checked')).toBe('false');
9091
}, 7000);
9192

9293
it('should remove the checked state of the menu item when removing its matching filter', async () => {
9394
render(<Basic />);
9495

9596
fireEvent.click(await screen.findByLabelText('Add filter'));
9697

97-
let checkboxs: HTMLInputElement[] = screen.getAllByRole('checkbox');
98-
fireEvent.click(checkboxs[0]);
98+
let checkboxes: HTMLInputElement[] =
99+
screen.getAllByRole('menuitemcheckbox');
100+
fireEvent.click(checkboxes[0]);
99101

100102
await screen.findByRole('textbox', {
101103
name: 'Title',
@@ -115,11 +117,11 @@ describe('<FilterButton />', () => {
115117
);
116118

117119
fireEvent.click(screen.getByLabelText('Add filter'));
118-
checkboxs = screen.getAllByRole('checkbox');
119-
expect(checkboxs).toHaveLength(3);
120-
expect(checkboxs[0].checked).toBe(false);
121-
expect(checkboxs[1].checked).toBe(false);
122-
expect(checkboxs[2].checked).toBe(false);
120+
checkboxes = screen.getAllByRole('menuitemcheckbox');
121+
expect(checkboxes).toHaveLength(3);
122+
expect(checkboxes[0].getAttribute('aria-checked')).toBe('false');
123+
expect(checkboxes[1].getAttribute('aria-checked')).toBe('false');
124+
expect(checkboxes[2].getAttribute('aria-checked')).toBe('false');
123125
});
124126

125127
it('should display the filter button if all filters are shown and there is a filter value', () => {
@@ -191,13 +193,11 @@ describe('<FilterButton />', () => {
191193
await screen.findByText('Add filter');
192194
fireEvent.click(screen.getByText('Add filter'));
193195

194-
await screen.findByText('Title', { selector: 'li > span' });
196+
await screen.findByText('Title', { selector: 'li span' });
195197
expect(screen.queryByDisplayValue('Remove all filters')).toBeNull();
196198

197199
// Then we apply a filter
198-
fireEvent.click(
199-
screen.getByText('Title', { selector: 'li > span' })
200-
);
200+
fireEvent.click(screen.getByText('Title', { selector: 'li span' }));
201201
await screen.findByDisplayValue(
202202
'Accusantium qui nihil voluptatum quia voluptas maxime ab similique'
203203
);
@@ -224,7 +224,7 @@ describe('<FilterButton />', () => {
224224
await screen.findByText('Add filter');
225225
fireEvent.click(screen.getByText('Add filter'));
226226

227-
await screen.findByText('Title', { selector: 'li > span' });
227+
await screen.findByText('Title', { selector: 'li span' });
228228
expect(screen.queryByDisplayValue('Remove all filters')).toBeNull();
229229

230230
// Then we apply a filter to an alwaysOn filter

packages/ra-ui-materialui/src/list/filter/FilterButtonMenuItem.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export const FilterButtonMenuItem = forwardRef<any, FilterButtonMenuItemProps>(
3131
autoFocus={autoFocus}
3232
ref={ref}
3333
disabled={filter.props.disabled}
34+
role="menuitemcheckbox"
35+
aria-checked={displayed}
3436
>
3537
<ListItemIcon>
3638
{displayed ? (

packages/ra-ui-materialui/src/list/filter/FilterForm.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ describe('<FilterForm />', () => {
376376
// Set nested filter value to 'bar'
377377
fireEvent.click(await screen.findByLabelText('Add filter'));
378378
fireEvent.click(
379-
await screen.findByRole('menuitem', { name: 'Nested' })
379+
await screen.findByRole('menuitemcheckbox', { name: 'Nested' })
380380
);
381381
fireEvent.click(await screen.findByText('bar'));
382382
fireEvent.blur(await screen.findByLabelText('Nested'));

0 commit comments

Comments
 (0)