Skip to content

Commit f2ce463

Browse files
fix(eui): remove hasColumnActions from sorting helper text in EuiDataGrid (#8598)
1 parent 76a0585 commit f2ce463

File tree

4 files changed

+32
-53
lines changed

4 files changed

+32
-53
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**Accessibility**
2+
3+
- Fixed duplicate screen reader output on `EuiDataGrid` for single sorted header cells with actions

packages/eui/src/components/datagrid/body/header/column_sorting.test.tsx

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,42 +56,19 @@ describe('useColumnSorting', () => {
5656
});
5757

5858
describe('when only the current column is being sorted', () => {
59-
describe('when the header cell has no actions', () => {
60-
it('renders aria-sort but not sortingScreenReaderText', () => {
61-
const { ariaSort, sortingScreenReaderText } = renderHook(() =>
62-
useColumnSorting({
63-
...mockSortingArgs,
64-
sorting: {
65-
onSort,
66-
columns: [{ id: columnId, direction: 'asc' }],
67-
},
68-
hasColumnActions: false,
69-
})
70-
).result.current;
71-
72-
expect(ariaSort).toEqual('ascending');
73-
expect(getRender(sortingScreenReaderText)).toHaveTextContent('');
74-
});
75-
});
76-
77-
describe('when the header cell has actions', () => {
78-
it('renders aria-sort and sortingScreenReaderText', () => {
79-
const { ariaSort, sortingScreenReaderText } = renderHook(() =>
80-
useColumnSorting({
81-
...mockSortingArgs,
82-
sorting: {
83-
onSort,
84-
columns: [{ id: columnId, direction: 'desc' }],
85-
},
86-
hasColumnActions: true,
87-
})
88-
).result.current;
89-
90-
expect(ariaSort).toEqual('descending');
91-
expect(getRender(sortingScreenReaderText)).toHaveTextContent(
92-
'Sorted descending.'
93-
);
94-
});
59+
it('renders aria-sort but not sortingScreenReaderText', () => {
60+
const { ariaSort, sortingScreenReaderText } = renderHook(() =>
61+
useColumnSorting({
62+
...mockSortingArgs,
63+
sorting: {
64+
onSort,
65+
columns: [{ id: columnId, direction: 'asc' }],
66+
},
67+
})
68+
).result.current;
69+
70+
expect(ariaSort).toEqual('ascending');
71+
expect(getRender(sortingScreenReaderText)).toHaveTextContent('');
9572
});
9673
});
9774
});

packages/eui/src/components/datagrid/body/header/column_sorting.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ import { EuiDataGridSorting } from '../../data_grid_types';
2121
export const useColumnSorting = ({
2222
sorting,
2323
id,
24-
hasColumnActions,
2524
}: {
2625
sorting?: EuiDataGridSorting;
2726
id: string;
28-
hasColumnActions: boolean;
2927
}) => {
3028
const sortedColumn = useMemo(
3129
() => sorting?.columns.find((col) => col.id === id),
@@ -49,30 +47,32 @@ export const useColumnSorting = ({
4947
}, [id, isColumnSorted, sortedColumn]);
5048

5149
/**
52-
* aria-sort attribute - should only be used when a single column is being sorted
50+
* `aria-sort` attribute - should only be used when a single column is being sorted
5351
* @see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-sort
5452
* @see https://www.w3.org/WAI/ARIA/apg/example-index/table/sortable-table.html
5553
* @see https://github.com/w3c/aria/issues/283 for potential future multi-column usage
5654
*/
57-
const ariaSort: AriaAttributes['aria-sort'] =
58-
isColumnSorted && hasOnlyOneSort
59-
? sorting.columns[0].direction === 'asc'
60-
? 'ascending'
61-
: 'descending'
62-
: undefined;
55+
const hasAriaSortOnly = isColumnSorted && hasOnlyOneSort;
6356

64-
// aria-describedby ID for when aria-sort isn't sufficient
57+
const ariaSort: AriaAttributes['aria-sort'] = hasAriaSortOnly
58+
? sorting.columns[0].direction === 'asc'
59+
? 'ascending'
60+
: 'descending'
61+
: undefined;
62+
63+
/**
64+
* `aria-describedby` ID for when `aria-sort` cannot be used
65+
*/
6566
const sortingAriaId = useGeneratedHtmlId({
6667
prefix: 'euiDataGridCellHeader',
6768
suffix: 'sorting',
6869
});
6970

7071
/**
71-
* Sorting status - screen reader text
72+
* Screen-reader-only sorting status, an alternative to `aria-sort` for multi-column sorting
7273
*/
7374
const sortingScreenReaderText = useMemo(() => {
74-
if (!isColumnSorted) return null;
75-
if (!hasColumnActions && hasOnlyOneSort) return null; // in this scenario, the `aria-sort` attribute will be used by screen readers
75+
if (!isColumnSorted || hasAriaSortOnly) return null;
7676
return (
7777
<p id={sortingAriaId} hidden>
7878
{sorting?.columns?.map(({ id: columnId, direction }, index) => {
@@ -141,10 +141,10 @@ export const useColumnSorting = ({
141141
);
142142
}, [
143143
isColumnSorted,
144-
hasColumnActions,
145-
hasOnlyOneSort,
146-
sorting,
144+
hasAriaSortOnly,
147145
sortingAriaId,
146+
sorting?.columns,
147+
hasOnlyOneSort,
148148
]);
149149

150150
return { sortingArrow, ariaSort, sortingAriaId, sortingScreenReaderText };

packages/eui/src/components/datagrid/body/header/data_grid_header_cell.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export const EuiDataGridHeaderCell: FunctionComponent<EuiDataGridHeaderCellProps
8080
useColumnSorting({
8181
sorting,
8282
id,
83-
hasColumnActions,
8483
});
8584

8685
const columnResizer = useMemo(() => {

0 commit comments

Comments
 (0)