Skip to content

Commit dccf3a4

Browse files
committed
refactor useAnnounceEmptyCells
1 parent 900ca62 commit dccf3a4

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

packages/main/src/components/AnalyticalTable/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client';
22

33
import { useVirtualizer } from '@tanstack/react-virtual';
4+
import { ARIA_LABEL_EMPTY_CELL } from '@ui5/webcomponents/dist/generated/i18n/i18n-defaults.js';
45
import {
56
debounce,
67
enrichEventWithDetails,
@@ -204,13 +205,15 @@ const AnalyticalTable = forwardRef<AnalyticalTableDomRef, AnalyticalTablePropTyp
204205

205206
const uniqueId = useId();
206207
const i18nBundle = useI18nBundle('@ui5/webcomponents-react');
208+
const i18nBundleWc = useI18nBundle('@ui5/webcomponents');
207209
const titleBarId = `titlebar-${uniqueId}`;
208210
const invalidTableTextId = `invalidTableText-${uniqueId}`;
209211

210212
const cellSelectDescId = `cell-select-${uniqueId}`;
211213
const cellUnselectDescId = `cell-unselect-${uniqueId}`;
212214
const cellExpandDescId = `cell-expand-${uniqueId}`;
213215
const cellCollapseDescId = `cell-collapse-${uniqueId}`;
216+
const cellEmptyDescId = `cell-empty-${uniqueId}`;
214217

215218
const tableRef = useRef<DivWithCustomScrollProp>(null);
216219
const parentRef = useRef<DivWithCustomScrollProp>(null);
@@ -252,6 +255,7 @@ const AnalyticalTable = forwardRef<AnalyticalTableDomRef, AnalyticalTablePropTyp
252255
cellUnselectDescId,
253256
cellExpandDescId,
254257
cellCollapseDescId,
258+
cellEmptyDescId,
255259
},
256260
translatableTexts: {
257261
selectAllText: i18nBundle.getText(SELECT_ALL),
@@ -932,6 +936,10 @@ const AnalyticalTable = forwardRef<AnalyticalTableDomRef, AnalyticalTablePropTyp
932936
<span id={cellCollapseDescId} className={classNames.hiddenA11yText}>
933937
{i18nBundle.getText(COLLAPSE_PRESS_SPACE)}
934938
</span>
939+
{/* useAnnounceEmptyCells */}
940+
<span id={cellEmptyDescId} className={classNames.hiddenA11yText}>
941+
{i18nBundleWc.getText(ARIA_LABEL_EMPTY_CELL)}
942+
</span>
935943
</>
936944
);
937945
});

packages/main/src/components/AnalyticalTable/pluginHooks/useAnnounceEmptyCells.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
import { ARIA_LABEL_EMPTY_CELL } from '@ui5/webcomponents/dist/generated/i18n/i18n-defaults.js';
2-
import { useI18nBundle } from '@ui5/webcomponents-react-base';
31
import { useCallback } from 'react';
4-
import type { ReactTableHooks } from '../types/index.js';
2+
import type { CellType, ReactTableHooks, TableInstance } from '../types/index.js';
53

64
/**
75
* The `useAnnounceEmptyCells` plugin hook adds screen reader announcements for empty cells.
86
*
97
* **Note:** Some screen readers (depending on their configuration) automatically detect empty cells, potentially resulting in duplicate announcements of empty cells.
108
*/
119
export const useAnnounceEmptyCells = (hooks: ReactTableHooks) => {
12-
const i18nBundleWc = useI18nBundle('@ui5/webcomponents');
13-
const emptyCellLabel = i18nBundleWc.getText(ARIA_LABEL_EMPTY_CELL);
14-
1510
const setCellProps = useCallback(
16-
(cellProps, { cell: { value } }) => {
11+
(
12+
cellProps,
13+
{
14+
cell: { value },
15+
instance: {
16+
webComponentsReactProperties: {
17+
a11yElementIds: { cellEmptyDescId },
18+
},
19+
},
20+
}: { cell: CellType; instance: TableInstance },
21+
) => {
1722
if (typeof value !== 'number' && !value) {
18-
return [cellProps, { 'aria-label': `${cellProps['aria-label']} ${emptyCellLabel}` }];
23+
return [cellProps, { 'aria-labelledby': `${cellProps['aria-labelledby']} ${cellEmptyDescId}` }];
1924
}
2025
return cellProps;
2126
},
22-
[emptyCellLabel],
27+
[],
2328
);
2429

2530
hooks.getCellProps.push(setCellProps);

packages/main/src/components/AnalyticalTable/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export interface WCRPropertiesType {
222222
cellUnselectDescId: string;
223223
cellExpandDescId: string;
224224
cellCollapseDescId: string;
225+
cellEmptyDescId: string;
225226
};
226227
translatableTexts: {
227228
selectAllText: string;

0 commit comments

Comments
 (0)