Skip to content

Commit 796f7c1

Browse files
authored
feat(AnalyticalTable - TypeScript): improve instance & prop types (#6256)
BREAKING CHANGE: The internal table instance types were updated, leading to stricter types, so depending on your implementation, you might encounter ts-errors.
1 parent 3f3b465 commit 796f7c1

21 files changed

+347
-85
lines changed

docs/MigrationGuide.mdx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,14 @@ function MyComponent() {
531531

532532
### AnalyticalTable
533533

534+
**TypeScript Changes:**
535+
536+
The internal table instance types were updated, leading to stricter types, so depending on your implementation, you might encounter ts-errors.
537+
534538
**Renamed or Changed Props:**
535539

536540
- `alwaysShowSubComponent` has been removed, please use `subComponentsBehavior` with `AnalyticalTableSubComponentsBehavior.Visibe` instead.
537-
- The default value of `sortable` (`true`) has been removed. To allow sorting in your table please set `sorting` to `true` yourself.
541+
- The default value of `sortable` (`true`) has been removed. To allow sorting in your table please set `sortable` to `true` yourself.
538542

539543
**Removed Props:**
540544

@@ -547,13 +551,13 @@ function MyComponent() {
547551
```js
548552
const handleOnRowSelect = (event) => {
549553
const { selectedRowIds, rowsById } = event.detail;
550-
const selectedRowIdsArrayMapped = Object.keys(selectedRowIds).reduce((acc, key) => {
554+
const selectedFlatRows = Object.keys(selectedRowIds).reduce((acc, key) => {
551555
if (selectedRowIds[key]) {
552556
acc.push(rowsById[key]);
553557
}
554558
return acc;
555559
}, []);
556-
console.log(selectedRowIdsArrayMapped);
560+
console.log(selectedFlatRows);
557561
};
558562
```
559563

packages/main/src/components/AnalyticalTable/TableBody/VirtualTableBody.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { Virtualizer } from '@tanstack/react-virtual';
22
import { clsx } from 'clsx';
3-
import type { MutableRefObject, ReactNode } from 'react';
3+
import type { MutableRefObject } from 'react';
44
import { useEffect, useMemo, useRef } from 'react';
55
import { AnalyticalTableSubComponentsBehavior } from '../../../enums/index.js';
66
import type {
77
AnalyticalTablePropTypes,
88
DivWithCustomScrollProp,
99
ScrollToRefType,
10+
TableInstance,
1011
TriggerScrollState
1112
} from '../types/index.js';
1213
import { getSubRowsByString } from '../util/index.js';
@@ -16,12 +17,12 @@ import { RowSubComponent as SubComponent } from './RowSubComponent.js';
1617
interface VirtualTableBodyProps {
1718
classes: Record<string, string>;
1819
prepareRow: (row: unknown) => void;
19-
rows: Record<string, any>[];
20+
rows: TableInstance['rows'];
2021
isTreeTable?: AnalyticalTablePropTypes['isTreeTable'];
2122
internalRowHeight: number;
2223
alternateRowColor?: AnalyticalTablePropTypes['alternateRowColor'];
2324
visibleColumns: Record<string, unknown>[];
24-
renderRowSubComponent: (row?: Record<string, unknown>) => ReactNode;
25+
renderRowSubComponent: AnalyticalTablePropTypes['renderRowSubComponent'];
2526
popInRowHeight: number;
2627
isRtl: boolean;
2728
markNavigatedRow?: AnalyticalTablePropTypes['markNavigatedRow'];

packages/main/src/components/AnalyticalTable/hooks/useA11y.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { KeyboardEventHandler } from 'react';
22
import { AnalyticalTableSelectionBehavior, AnalyticalTableSelectionMode } from '../../../enums/index.js';
3-
import type { ReactTableHooks } from '../types/index.js';
3+
import type { ReactTableHooks, TableInstance } from '../types/index.js';
44

55
interface UpdatedCellProptypes {
66
onKeyDown?: KeyboardEventHandler<HTMLDivElement>;
@@ -10,7 +10,7 @@ interface UpdatedCellProptypes {
1010
role?: string;
1111
}
1212

13-
const setCellProps = (cellProps, { cell, instance }) => {
13+
const setCellProps = (cellProps, { cell, instance }: { cell: TableInstance['cell']; instance: TableInstance }) => {
1414
const { column, row, value } = cell;
1515
const columnIndex = instance.visibleColumns.findIndex(({ id }) => id === column.id);
1616
const { alwaysShowSubComponent, renderRowSubComponent, translatableTexts, selectionMode, selectionBehavior } =
@@ -69,7 +69,10 @@ const setCellProps = (cellProps, { cell, instance }) => {
6969
return [cellProps, updatedCellProps];
7070
};
7171

72-
const setHeaderProps = (headerProps, { column, instance }) => {
72+
const setHeaderProps = (
73+
headerProps,
74+
{ column, instance }: { column: TableInstance['column']; instance: TableInstance }
75+
) => {
7376
const { translatableTexts } = instance.webComponentsReactProperties;
7477

7578
if (!column) {

packages/main/src/components/AnalyticalTable/hooks/useAutoResize.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { enrichEventWithDetails } from '@ui5/webcomponents-react-base';
22
import { DEFAULT_COLUMN_WIDTH } from '../defaults/Column/index.js';
3-
import type { ReactTableHooks } from '../types/index.js';
3+
import type { ReactTableHooks, TableInstance } from '../types/index.js';
44
import { CELL_PADDING_PX } from './useDynamicColumnWidths.js';
55

6-
function setResizerProps(props, { instance, header }) {
6+
function setResizerProps(props, { instance, header }: { instance: TableInstance; header: TableInstance['header'] }) {
77
const { dispatch, virtualRowsRange, rows, webComponentsReactProperties } = instance;
88
const { onAutoResize, tableRef, isTreeTable } = webComponentsReactProperties;
99
const { autoResizable, id: accessor } = header;
@@ -109,7 +109,7 @@ const setCellProps = (
109109
cell: {
110110
column: { id }
111111
}
112-
}
112+
}: { cell: TableInstance['cell'] }
113113
) => {
114114
return [props, { ['data-column-id-cell']: id }];
115115
};

packages/main/src/components/AnalyticalTable/hooks/useDragAndDrop.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { enrichEventWithDetails } from '@ui5/webcomponents-react-base';
2-
import type { ReactTableHooks } from '../types/index.js';
2+
import type { ReactTableHooks, TableInstance } from '../types/index.js';
33

44
const getColumnId = (column) => {
55
return typeof column.accessor === 'string' ? column.accessor : column.id;
66
};
77

88
function getHeaderProps(
99
props: Record<string, unknown>,
10-
{ instance: { dispatch, state, columns, setColumnOrder, webComponentsReactProperties } }
10+
{ instance: { dispatch, state, columns, setColumnOrder, webComponentsReactProperties } }: { instance: TableInstance }
1111
) {
1212
const { columnOrder, columnResizing, isRtl, dndColumn } = state;
1313
const { onColumnsReorder } = webComponentsReactProperties;

packages/main/src/components/AnalyticalTable/hooks/useDynamicColumnWidths.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useMemo } from 'react';
22
import { AnalyticalTableScaleWidthMode } from '../../../enums/index.js';
33
import { DEFAULT_COLUMN_WIDTH } from '../defaults/Column/index.js';
4-
import type { AnalyticalTableColumnDefinition, ReactTableHooks } from '../types/index.js';
4+
import type { AnalyticalTableColumnDefinition, ReactTableHooks, TableInstance } from '../types/index.js';
55

66
const ROW_SAMPLE_SIZE = 20;
77
const MAX_WIDTH = 700;
@@ -206,7 +206,7 @@ const smartColumns = (columns: AnalyticalTableColumnDefinition[], instance, hidd
206206
});
207207
};
208208

209-
const columns = (columns: AnalyticalTableColumnDefinition[], { instance }) => {
209+
const columns = (columns: TableInstance['columns'], { instance }: { instance: TableInstance }) => {
210210
if (!instance.state || !instance.rows) {
211211
return columns;
212212
}
@@ -231,7 +231,7 @@ const columns = (columns: AnalyticalTableColumnDefinition[], { instance }) => {
231231
}
232232
return column ?? false;
233233
})
234-
.filter(Boolean);
234+
.filter(Boolean) as TableInstance['columns'];
235235
if (scaleWidthMode === AnalyticalTableScaleWidthMode.Smart) {
236236
return smartColumns(columns, instance, hiddenColumns);
237237
}
@@ -296,7 +296,7 @@ const columns = (columns: AnalyticalTableColumnDefinition[], { instance }) => {
296296
}
297297
return false;
298298
})
299-
.filter(Boolean);
299+
.filter(Boolean) as number[];
300300

301301
const fixedWidth = columnsWithFixedWidth.reduce((acc, val) => acc + val, 0);
302302
// check if columns are visible and table has width

packages/main/src/components/AnalyticalTable/hooks/useKeyboardNavigation.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useCallback, useEffect, useRef } from 'react';
22
import { actions } from 'react-table';
3-
import type { ReactTableHooks } from '../types/index.js';
3+
import type { ColumnType, ReactTableHooks, TableInstance } from '../types/index.js';
44
import { getLeafHeaders } from '../util/index.js';
55

66
const CELL_DATA_ATTRIBUTES = ['visibleColumnIndex', 'columnIndex', 'rowIndex', 'visibleRowIndex'];
@@ -62,7 +62,10 @@ const navigateFromActiveSubCompItem = (currentlyFocusedCell, e) => {
6262
setFocus(currentlyFocusedCell, recursiveSubComponentElementSearch(e.target));
6363
};
6464

65-
const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties, data, columns, state } }) => {
65+
const useGetTableProps = (
66+
tableProps,
67+
{ instance: { webComponentsReactProperties, data, columns, state } }: { instance: TableInstance }
68+
) => {
6669
const { showOverlay, tableRef } = webComponentsReactProperties;
6770
const currentlyFocusedCell = useRef<HTMLDivElement>(null);
6871
const noData = data.length === 0;
@@ -171,7 +174,7 @@ const useGetTableProps = (tableProps, { instance: { webComponentsReactProperties
171174
switch (e.key) {
172175
case 'End': {
173176
e.preventDefault();
174-
const visibleColumns: HTMLDivElement[] = tableRef.current.querySelector(
177+
const visibleColumns = tableRef.current.querySelector(
175178
`div[data-component-name="AnalyticalTableHeaderRow"]`
176179
).children;
177180

@@ -346,7 +349,10 @@ function getPayload(e, column) {
346349
return { clientX, columnId, columnWidth, headerIdWidths };
347350
}
348351

349-
const setHeaderProps = (headerProps, { instance: { dispatch }, column }) => {
352+
const setHeaderProps = (
353+
headerProps,
354+
{ instance: { dispatch }, column }: { instance: TableInstance; column: ColumnType }
355+
) => {
350356
// resize col with keyboard
351357
const handleKeyDown = (e) => {
352358
if (typeof headerProps.onKeyDown === 'function') {

packages/main/src/components/AnalyticalTable/hooks/usePopIn.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import type { ReactTableHooks } from '../types/index.js';
1+
import type { ReactTableHooks, TableInstance } from '../types/index.js';
22

3-
const popInVisibleColumnsDeps = (deps, { instance: { state } }) => [...deps, state.tableClientWidth];
3+
const popInVisibleColumnsDeps = (deps, { instance: { state } }: { instance: TableInstance }) => [
4+
...deps,
5+
state.tableClientWidth
6+
];
47

5-
const popInVisibleColumns = (cols, { instance }) => {
8+
const popInVisibleColumns = (cols, { instance }: { instance: TableInstance }) => {
69
const { state, dispatch } = instance;
710

811
const tableClientWidth = state.isScrollable

packages/main/src/components/AnalyticalTable/hooks/useRowHighlight.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ValueState from '@ui5/webcomponents-base/dist/types/ValueState.js';
22
import { IndicationColor } from '../../../enums/index.js';
3-
import type { ReactTableHooks } from '../types/index.js';
3+
import type { ReactTableHooks, TableInstance } from '../types/index.js';
44

55
const baseStyles = {
66
width: '100%',
@@ -18,7 +18,7 @@ const HighlightColors = {
1818
*/
1919
const Header = () => <div style={{ width: '6px' }} />;
2020

21-
const Cell = (instance) => {
21+
const Cell = (instance: TableInstance) => {
2222
const { cell, webComponentsReactProperties } = instance;
2323
const styleClass = HighlightColors[cell?.value]
2424
? webComponentsReactProperties.classes[HighlightColors[cell.value].toLowerCase()]
@@ -29,11 +29,17 @@ const Cell = (instance) => {
2929
/*
3030
* TABLE HOOKS
3131
*/
32-
const columnsDeps = (deps, { instance: { webComponentsReactProperties } }) => {
32+
const columnsDeps = (deps, { instance: { webComponentsReactProperties } }: { instance: TableInstance }) => {
3333
return [...deps, webComponentsReactProperties.withRowHighlight, webComponentsReactProperties.highlightField];
3434
};
35-
const visibleColumnsDeps = (deps, { instance }) => [...deps, instance.webComponentsReactProperties.withRowHighlight];
36-
const visibleColumns = (currentVisibleColumns, { instance: { webComponentsReactProperties } }) => {
35+
const visibleColumnsDeps = (deps, { instance }: { instance: TableInstance }) => [
36+
...deps,
37+
instance.webComponentsReactProperties.withRowHighlight
38+
];
39+
const visibleColumns = (
40+
currentVisibleColumns,
41+
{ instance: { webComponentsReactProperties } }: { instance: TableInstance }
42+
) => {
3743
if (!webComponentsReactProperties.withRowHighlight) {
3844
return currentVisibleColumns.filter(({ id }) => id !== '__ui5wcr__internal_highlight_column');
3945
}
@@ -42,7 +48,7 @@ const visibleColumns = (currentVisibleColumns, { instance: { webComponentsReactP
4248
return [highlightColumn, ...currentVisibleColumns.filter(({ id }) => id !== '__ui5wcr__internal_highlight_column')];
4349
};
4450

45-
const columns = (currentColumns, { instance }) => {
51+
const columns = (currentColumns, { instance }: { instance: TableInstance }) => {
4652
const { withRowHighlight, highlightField } = instance.webComponentsReactProperties;
4753

4854
if (!withRowHighlight) {

packages/main/src/components/AnalyticalTable/hooks/useRowNavigationIndicator.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ReactTableHooks } from '../types/index.js';
1+
import type { ReactTableHooks, TableInstance } from '../types/index.js';
22

33
const baseStyles = {
44
width: '100%',
@@ -27,14 +27,17 @@ const Cell = (instance) => {
2727
/*
2828
* TABLE HOOKS
2929
*/
30-
const columnsDeps = (deps, { instance: { webComponentsReactProperties } }) => {
30+
const columnsDeps = (deps, { instance: { webComponentsReactProperties } }: { instance: TableInstance }) => {
3131
return [...deps, webComponentsReactProperties.withNavigationHighlight];
3232
};
33-
const visibleColumnsDeps = (deps, { instance }) => [
33+
const visibleColumnsDeps = (deps, { instance }: { instance: TableInstance }) => [
3434
...deps,
3535
instance.webComponentsReactProperties.withNavigationHighlight
3636
];
37-
const visibleColumns = (currentVisibleColumns, { instance: { webComponentsReactProperties } }) => {
37+
const visibleColumns = (
38+
currentVisibleColumns,
39+
{ instance: { webComponentsReactProperties } }: { instance: TableInstance }
40+
) => {
3841
if (!webComponentsReactProperties.withNavigationHighlight) {
3942
return currentVisibleColumns.filter(({ id }) => id !== '__ui5wcr__internal_navigation_column');
4043
}
@@ -43,7 +46,7 @@ const visibleColumns = (currentVisibleColumns, { instance: { webComponentsReactP
4346
return [...currentVisibleColumns.filter(({ id }) => id !== '__ui5wcr__internal_navigation_column'), highlightColumn];
4447
};
4548

46-
const columns = (currentColumns, { instance }) => {
49+
const columns = (currentColumns, { instance }: { instance: TableInstance }) => {
4750
const { withNavigationHighlight } = instance.webComponentsReactProperties;
4851

4952
if (!withNavigationHighlight) {

packages/main/src/components/AnalyticalTable/hooks/useRowSelectionColumn.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CssSizeVariablesNames, enrichEventWithDetails } from '@ui5/webcomponent
22
import type { CSSProperties } from 'react';
33
import { AnalyticalTableSelectionBehavior, AnalyticalTableSelectionMode } from '../../../enums/index.js';
44
import { CheckBox } from '../../../webComponents/CheckBox/index.js';
5-
import type { ReactTableHooks } from '../types/index.js';
5+
import type { ReactTableHooks, TableInstance } from '../types/index.js';
66

77
const customCheckBoxStyling = {
88
verticalAlign: 'middle',
@@ -14,7 +14,7 @@ const customCheckBoxStyling = {
1414
* COMPONENTS
1515
*/
1616

17-
const Header = (instance) => {
17+
const Header = (instance: TableInstance) => {
1818
const {
1919
getToggleAllRowsSelectedProps,
2020
webComponentsReactProperties: { selectionMode }
@@ -59,7 +59,7 @@ function getNextSelectedRowIds(rowsById) {
5959
}, {});
6060
}
6161

62-
const headerProps = (props, { instance }) => {
62+
const headerProps = (props, { instance }: { instance: TableInstance }) => {
6363
const {
6464
webComponentsReactProperties: {
6565
onRowSelect,
@@ -128,17 +128,20 @@ const headerProps = (props, { instance }) => {
128128
return props;
129129
};
130130

131-
const columnDeps = (deps, { instance: { webComponentsReactProperties } }) => {
131+
const columnDeps = (deps, { instance: { webComponentsReactProperties } }: { instance: TableInstance }) => {
132132
return [...deps, webComponentsReactProperties.selectionMode, webComponentsReactProperties.selectionBehavior];
133133
};
134134

135-
const visibleColumnsDeps = (deps, { instance }) => [
135+
const visibleColumnsDeps = (deps, { instance }: { instance: TableInstance }) => [
136136
...deps,
137137
instance.webComponentsReactProperties.selectionMode,
138138
instance.webComponentsReactProperties.selectionBehavior
139139
];
140140

141-
const visibleColumns = (currentVisibleColumns, { instance: { webComponentsReactProperties } }) => {
141+
const visibleColumns = (
142+
currentVisibleColumns,
143+
{ instance: { webComponentsReactProperties } }: { instance: TableInstance }
144+
) => {
142145
if (
143146
webComponentsReactProperties.selectionMode === AnalyticalTableSelectionMode.None ||
144147
webComponentsReactProperties.selectionBehavior === AnalyticalTableSelectionBehavior.RowOnly
@@ -149,7 +152,7 @@ const visibleColumns = (currentVisibleColumns, { instance: { webComponentsReactP
149152
const selectionColumn = currentVisibleColumns.find(({ id }) => id === '__ui5wcr__internal_selection_column');
150153
return [selectionColumn, ...currentVisibleColumns.filter(({ id }) => id !== '__ui5wcr__internal_selection_column')];
151154
};
152-
const columns = (currentColumns, { instance }) => {
155+
const columns = (currentColumns, { instance }: { instance: TableInstance }) => {
153156
const { webComponentsReactProperties } = instance;
154157
const { selectionMode, selectionBehavior, tableRef } = webComponentsReactProperties;
155158

@@ -187,15 +190,18 @@ const columns = (currentColumns, { instance }) => {
187190
];
188191
};
189192

190-
const getCellProps = (props, { cell }) => {
193+
const getCellProps = (props, { cell }: { cell: TableInstance['cell'] }) => {
191194
if (cell.column.id === '__ui5wcr__internal_selection_column') {
192195
const style = { ...props.style, cursor: 'pointer', justifyContent: 'center' };
193196
return [props, { style }];
194197
}
195198
return props;
196199
};
197200

198-
const setToggleAllRowsSelectedProps = (props, { instance: { webComponentsReactProperties } }) => {
201+
const setToggleAllRowsSelectedProps = (
202+
props,
203+
{ instance: { webComponentsReactProperties } }: { instance: TableInstance }
204+
) => {
199205
const { classes } = webComponentsReactProperties;
200206
return [props, { className: classes.checkBox, title: undefined }];
201207
};

packages/main/src/components/AnalyticalTable/hooks/useSelectionChangeCallback.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { enrichEventWithDetails } from '@ui5/webcomponents-react-base';
22
import { useEffect } from 'react';
33
import { AnalyticalTableSelectionMode } from '../../../enums/index.js';
4-
import type { ReactTableHooks } from '../types/index.js';
4+
import type { ReactTableHooks, TableInstance } from '../types/index.js';
55

66
export const useSelectionChangeCallback = (hooks: ReactTableHooks) => {
7-
hooks.useControlledState.push((state, { instance }) => {
7+
hooks.useControlledState.push((state, { instance }: { instance: TableInstance }) => {
88
const { selectedRowPayload, selectedRowIds, filters, globalFilter } = state;
99
const { rowsById, preFilteredRowsById, webComponentsReactProperties, dispatch } = instance;
1010
const isFiltered = filters?.length > 0 || !!globalFilter;

0 commit comments

Comments
 (0)