|
1 | | -import { enrichEventWithDetails } from '@ui5/webcomponents-react-base/internal/utils'; |
| 1 | +import announce from '@ui5/webcomponents-base/dist/util/InvisibleMessage.js'; |
| 2 | +import { useI18nBundle } from '@ui5/webcomponents-react-base/hooks'; |
| 3 | +import { debounce, enrichEventWithDetails } from '@ui5/webcomponents-react-base/internal/utils'; |
2 | 4 | import { useEffect, useRef } from 'react'; |
3 | 5 | import { AnalyticalTableSelectionMode } from '../../../enums/AnalyticalTableSelectionMode.js'; |
| 6 | +import { |
| 7 | + ALL_ROWS_DESELECTED, |
| 8 | + ALL_ROWS_DESELECTED_FILTERED, |
| 9 | + ALL_ROWS_SELECTED, |
| 10 | + ALL_ROWS_SELECTED_FILTERED, |
| 11 | + ROW_DESELECTED, |
| 12 | + ROW_DESELECTED_MULTI, |
| 13 | + ROW_DESELECTED_MULTI_FILTERED, |
| 14 | + ROW_SELECTED, |
| 15 | + ROW_SELECTED_MULTI, |
| 16 | + ROW_SELECTED_MULTI_FILTERED, |
| 17 | +} from '../../../i18n/i18n-defaults.js'; |
4 | 18 | import { ensurePluginOrder } from '../react-table/index.js'; |
5 | 19 | import type { AnalyticalTablePropTypes, ReactTableHooks, TableInstance } from '../types/index.js'; |
6 | 20 |
|
7 | 21 | type OnRowSelectEvent = Parameters<NonNullable<AnalyticalTablePropTypes['onRowSelect']>>[0]; |
8 | 22 | type OnRowSelectDetail = OnRowSelectEvent['detail']; |
9 | 23 |
|
| 24 | +// debounce announce to prevent excessive successive announcements |
| 25 | +const debouncedAnnounce = debounce((announcement: string) => { |
| 26 | + announce(announcement, 'Polite'); |
| 27 | +}, 200); |
| 28 | + |
10 | 29 | const useInstance = (instance: TableInstance) => { |
11 | 30 | const { webComponentsReactProperties, rowsById, preFilteredRowsById, state, plugins } = instance; |
12 | 31 | const { selectedRowIds, filters, globalFilter } = state; |
13 | 32 | const { onRowSelect, selectionMode } = webComponentsReactProperties; |
14 | 33 |
|
15 | 34 | ensurePluginOrder(plugins, ['useRowSelect'], 'useSelectionChangeCallback'); |
16 | 35 |
|
| 36 | + const i18nBundle = useI18nBundle('@ui5/webcomponents-react'); |
| 37 | + |
17 | 38 | const prevSelectedRowIdsRef = useRef(selectedRowIds); |
18 | 39 |
|
19 | 40 | // react-table instance is intentionally mutable |
@@ -58,13 +79,46 @@ const useInstance = (instance: TableInstance) => { |
58 | 79 | selectedRowIds: payload.selectedRowIds, |
59 | 80 | }) as OnRowSelectEvent, |
60 | 81 | ); |
| 82 | + let allRowsMsgKey; |
| 83 | + if (instance.isAllRowsSelected) { |
| 84 | + allRowsMsgKey = isFiltered ? ALL_ROWS_SELECTED_FILTERED : ALL_ROWS_SELECTED; |
| 85 | + } else { |
| 86 | + allRowsMsgKey = isFiltered ? ALL_ROWS_DESELECTED_FILTERED : ALL_ROWS_DESELECTED; |
| 87 | + } |
| 88 | + debouncedAnnounce(i18nBundle.getText(allRowsMsgKey)); |
61 | 89 | } else { |
62 | 90 | onRowSelect?.(enrichEventWithDetails(e as Event & { detail?: OnRowSelectDetail }, payload) as OnRowSelectEvent); |
| 91 | + |
| 92 | + if (row) { |
| 93 | + const isSelected = row.isSelected; |
| 94 | + if (selectionMode === AnalyticalTableSelectionMode.Multiple) { |
| 95 | + const count = instance.selectedFlatRows.length; |
| 96 | + let rowMsgKey; |
| 97 | + if (isSelected) { |
| 98 | + rowMsgKey = isFiltered ? ROW_SELECTED_MULTI_FILTERED : ROW_SELECTED_MULTI; |
| 99 | + } else { |
| 100 | + rowMsgKey = isFiltered ? ROW_DESELECTED_MULTI_FILTERED : ROW_DESELECTED_MULTI; |
| 101 | + } |
| 102 | + debouncedAnnounce(i18nBundle.getText(rowMsgKey, count)); |
| 103 | + } else if (selectionMode === AnalyticalTableSelectionMode.Single) { |
| 104 | + debouncedAnnounce(i18nBundle.getText(isSelected ? ROW_SELECTED : ROW_DESELECTED)); |
| 105 | + } |
| 106 | + } |
63 | 107 | } |
64 | 108 | } |
65 | 109 |
|
66 | 110 | prevSelectedRowIdsRef.current = selectedRowIds; |
67 | | - }, [selectedRowIds, rowsById, preFilteredRowsById, filters, globalFilter, selectionMode, instance, onRowSelect]); |
| 111 | + }, [ |
| 112 | + selectedRowIds, |
| 113 | + rowsById, |
| 114 | + preFilteredRowsById, |
| 115 | + filters, |
| 116 | + globalFilter, |
| 117 | + selectionMode, |
| 118 | + instance, |
| 119 | + onRowSelect, |
| 120 | + i18nBundle, |
| 121 | + ]); |
68 | 122 | }; |
69 | 123 |
|
70 | 124 | export const useSelectionChangeCallback = (hooks: ReactTableHooks) => { |
|
0 commit comments