Skip to content

Commit 950d95e

Browse files
authored
docs(AnalyticalTable): memoize tableHooks (#7370)
Closes #7179
1 parent e2b36fd commit 950d95e

File tree

8 files changed

+78
-36
lines changed

8 files changed

+78
-36
lines changed

packages/main/src/components/AnalyticalTable/AnalyticalTableHooks.stories.tsx

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import dataManualSelect from '@sb/mockData/FriendsManualSelect25.json';
44
import dataTree from '@sb/mockData/FriendsTree.json';
55
import type { Meta, StoryObj } from '@storybook/react';
66
import InputType from '@ui5/webcomponents/dist/types/InputType.js';
7-
import { useReducer, useState } from 'react';
7+
import { useCallback, useMemo, useReducer, useState } from 'react';
88
import { AnalyticalTableSelectionMode, FlexBoxAlignItems, FlexBoxDirection } from '../../enums';
99
import { Button, CheckBox, Input, Label, ToggleButton, Text } from '../../webComponents';
1010
import { FlexBox } from '../FlexBox';
@@ -20,6 +20,7 @@ const pluginsMeta = {
2020
export default pluginsMeta;
2121
type Story = StoryObj<typeof pluginsMeta>;
2222

23+
const tableHooksEmptyCells = [AnalyticalTableHooks.useAnnounceEmptyCells];
2324
export const PluginAnnounceEmptyCells: Story = {
2425
args: {
2526
data: [
@@ -41,19 +42,21 @@ export const PluginAnnounceEmptyCells: Story = {
4142
columns={args.columns}
4243
data={args.data}
4344
visibleRows={args.visibleRows}
44-
tableHooks={[AnalyticalTableHooks.useAnnounceEmptyCells]}
45+
tableHooks={tableHooksEmptyCells}
4546
/>
4647
);
4748
},
4849
};
4950

51+
const disableRowFunc = (row) => row.original.age < 40;
52+
const tableHooksDisableRowSel = [AnalyticalTableHooks.useRowDisableSelection(disableRowFunc)];
53+
const tableHooksDisableRowSel1 = [AnalyticalTableHooks.useRowDisableSelection('disableSelection')];
5054
export const PluginDisableRowSelection: Story = {
5155
args: {
5256
data: dataLarge.map((item) => ({ ...item, disableSelection: Math.random() < 0.5 })),
5357
selectionMode: AnalyticalTableSelectionMode.Multiple,
5458
},
5559
render: (args) => {
56-
const disableRowFunc = (row) => row.original.age < 40;
5760
const [isFunc, setIsFunc] = useState(true);
5861
return (
5962
<>
@@ -78,7 +81,7 @@ export const PluginDisableRowSelection: Story = {
7881
data={args.data}
7982
columns={args.columns}
8083
selectionMode={args.selectionMode}
81-
tableHooks={[AnalyticalTableHooks.useRowDisableSelection(disableRowFunc)]}
84+
tableHooks={tableHooksDisableRowSel}
8285
visibleRows={10}
8386
header="All under 40 are not selectable"
8487
/>
@@ -88,7 +91,7 @@ export const PluginDisableRowSelection: Story = {
8891
columns={args.columns}
8992
selectionMode={args.selectionMode}
9093
selectionBehavior={args.selectionBehavior}
91-
tableHooks={[AnalyticalTableHooks.useRowDisableSelection('disableSelection')]}
94+
tableHooks={tableHooksDisableRowSel1}
9295
visibleRows={10}
9396
header={`All with "disableSelection: true" are not selectable`}
9497
/>
@@ -98,6 +101,7 @@ export const PluginDisableRowSelection: Story = {
98101
},
99102
};
100103

104+
const tableHooksIndeterminateRowSel = [AnalyticalTableHooks.useIndeterminateRowSelection()];
101105
export const PluginIndeterminateRowSelection: Story = {
102106
render: (args) => {
103107
const [selectSubRows, setSelectSubRows] = useReducer((prev) => !prev, true);
@@ -111,14 +115,15 @@ export const PluginIndeterminateRowSelection: Story = {
111115
data={dataTree}
112116
columns={args.columns}
113117
isTreeTable
114-
tableHooks={[AnalyticalTableHooks.useIndeterminateRowSelection()]}
118+
tableHooks={tableHooksIndeterminateRowSel}
115119
reactTableOptions={{ selectSubRows: selectSubRows }}
116120
/>
117121
</>
118122
);
119123
},
120124
};
121125

126+
const tableHooksManualRowSel = [AnalyticalTableHooks.useManualRowSelect('isSelected')];
122127
export const PluginManualRowSelect: Story = {
123128
args: {
124129
data: dataManualSelect,
@@ -146,7 +151,7 @@ export const PluginManualRowSelect: Story = {
146151
selectionMode={AnalyticalTableSelectionMode.Multiple}
147152
data={data}
148153
columns={args.columns}
149-
tableHooks={[AnalyticalTableHooks.useManualRowSelect('isSelected')]}
154+
tableHooks={tableHooksManualRowSel}
150155
/>
151156
<Button onClick={setCollapsedCode}>Show first entries in data array</Button>
152157
{!collapsedCode && (
@@ -175,9 +180,17 @@ export const PluginOnColumnResize: Story = {
175180
const handleWaitChange = (e) => {
176181
setWait(parseInt(e.target.value));
177182
};
178-
const handleColWidthUpdate = (e) => {
179-
setUseColResizeEvent(e);
180-
};
183+
const handleColWidthUpdate = useCallback(
184+
(e) => {
185+
setUseColResizeEvent(e);
186+
},
187+
[setUseColResizeEvent],
188+
);
189+
190+
const tableHooksColResize = useMemo(
191+
() => [AnalyticalTableHooks.useOnColumnResize(handleColWidthUpdate, { liveUpdate, wait })],
192+
[handleColWidthUpdate, liveUpdate, wait],
193+
);
181194
return (
182195
<>
183196
<AnalyticalTable
@@ -196,7 +209,7 @@ export const PluginOnColumnResize: Story = {
196209
}
197210
data={args.data}
198211
columns={args.columns}
199-
tableHooks={[AnalyticalTableHooks.useOnColumnResize(handleColWidthUpdate, { liveUpdate, wait })]}
212+
tableHooks={tableHooksColResize}
200213
/>
201214
{!!Object.keys(useColResizeEvent).length && (
202215
<FlexBox direction={FlexBoxDirection.Column}>
@@ -266,12 +279,16 @@ export const PluginOrderedMultiSort = {
266279
},
267280
},
268281
render(args) {
282+
const tableHooksOrderedMultiSort = useMemo(
283+
() => [AnalyticalTableHooks.useOrderedMultiSort(args.orderedIds)],
284+
[args.orderedIds],
285+
);
269286
return (
270287
<AnalyticalTable
271288
columns={orderedMultiSortColumns}
272289
data={orderedMultiSortData}
273290
sortable
274-
tableHooks={[AnalyticalTableHooks.useOrderedMultiSort(args.orderedIds)]}
291+
tableHooks={tableHooksOrderedMultiSort}
275292
/>
276293
);
277294
},

packages/main/src/components/AnalyticalTable/PluginAnnounceEmptyCells.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ The `useAnnounceEmptyCells` plugin hook adds screen reader announcements for emp
1818
<Canvas sourceState="none" of={ComponentStories.PluginAnnounceEmptyCells} />
1919

2020
```jsx
21+
const tableHooks = [AnalyticalTableHooks.useAnnounceEmptyCells] // this array should be memoized
22+
2123
<AnalyticalTable
2224
data={data}
2325
columns={columns}
2426
visibleRows={5}
25-
tableHooks={[AnalyticalTableHooks.useAnnounceEmptyCells]}
27+
tableHooks={tableHooks}
2628
/>
2729
```
2830

packages/main/src/components/AnalyticalTable/PluginDisableRowSelection.mdx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,44 @@ With the `useRowDisableSelection` it's possible to disable the selection of spec
3232
## Example
3333

3434
```jsx
35+
const disableRowFunc = (row) => row.original.age < 40;
36+
const tableHooks = [
37+
AnalyticalTableHooks.useRowDisableSelection(disableRowFunc),
38+
];
3539
const WithFunctionParameter = () => {
36-
const disableRowFunc = (row) => row.original.age < 40;
40+
3741
return (
3842
<AnalyticalTable
3943
data={data}
4044
columns={columns}
4145
selectionMode={AnalyticalTableSelectionMode.Multiple}
42-
tableHooks={[AnalyticalTableHooks.useRowDisableSelection(disableRowFunc)]}
46+
tableHooks={tableHooks}
4347
/>
4448
);
4549
};
4650

47-
const WithStringParameter = () => {
51+
```
52+
53+
```jsx
4854
const data = [
49-
{
50-
"name": "Peter Franco",
51-
"age": 22,
52-
...
53-
"disableSelection": Math.random() < 0.5
54-
},
55-
...
55+
{
56+
name: "Peter Franco",
57+
age: 22,
58+
//...
59+
disableSelection: Math.random() < 0.5,
60+
},
61+
//...
5662
];
63+
const tableHooks = [
64+
AnalyticalTableHooks.useRowDisableSelection("disableSelection"),
65+
];
66+
const WithStringParameter = () => {
5767
return (
5868
<AnalyticalTable
5969
data={data}
6070
columns={columns}
6171
selectionMode={AnalyticalTableSelectionMode.Multiple}
62-
tableHooks={[AnalyticalTableHooks.useRowDisableSelection('disableSelection')]}
72+
tableHooks={tableHooks}
6373
/>
6474
);
6575
};

packages/main/src/components/AnalyticalTable/PluginIndeterminateRowSelection.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ When using this hook, it is recommended to also select all sub-rows when selecti
3333
<Canvas sourceState="none" of={ComponentStories.PluginIndeterminateRowSelection} />
3434

3535
```jsx
36+
const tableHooks = [AnalyticalTableHooks.useIndeterminateRowSelection()]; // should be memoized
37+
3638
<AnalyticalTable
3739
selectionMode={AnalyticalTableSelectionMode.Multiple}
3840
data={data}
3941
columns={columns}
4042
isTreeTable
41-
tableHooks={[AnalyticalTableHooks.useIndeterminateRowSelection()]}
43+
tableHooks={tableHooks}
4244
reactTableOptions={{ selectSubRows: true }}
4345
/>
4446
```

packages/main/src/components/AnalyticalTable/PluginManualRowSelect.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ If this key is found on the original data row, and it is `true`, this row will b
1717
<Canvas sourceState="none" of={ComponentStories.PluginManualRowSelect} />
1818

1919
```jsx
20+
// you could also omit the `'isSelected'`, as this is the default value
21+
const tableHooks = [AnalyticalTableHooks.useManualRowSelect('isSelected')]; // should be memoized
22+
2023
<AnalyticalTable
2124
selectionMode={AnalyticalTableSelectionMode.Multiple}
2225
data={data}
2326
columns={columns}
24-
// you could also omit the `'isSelected'`, as this is the default value
25-
tableHooks={[AnalyticalTableHooks.useManualRowSelect('isSelected')]}
27+
tableHooks={tableHooks}
2628
/>
2729
```
2830

packages/main/src/components/AnalyticalTable/PluginOnColumnResize.mdx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,25 @@ The first parameter is the callback itself (`e: { columnWidth: number; header: R
2323

2424
```jsx
2525
const TableComponent = (props) => {
26-
const handleColumResize = (e) => {
26+
const handleColumResize = useCallback((e) => {
2727
console.log(e.columnWidth, e.header);
28-
};
28+
}, []);
29+
30+
const tableHooks = useMemo(
31+
() => [
32+
AnalyticalTableHooks.useOnColumnResize(handleColumResize, {
33+
liveUpdate: false,
34+
wait: 100,
35+
}),
36+
],
37+
[handleColumResize],
38+
);
39+
2940
return (
3041
<AnalyticalTable
3142
data={props.data}
3243
columns={props.columns}
33-
tableHooks={[
34-
AnalyticalTableHooks.useOnColumnResize(handleColumResize, {
35-
liveUpdate: false,
36-
wait: 100
37-
})
38-
]}
44+
tableHooks={tableHooks}
3945
/>
4046
);
4147
};

packages/main/src/components/AnalyticalTable/PluginOrderedMultiSort.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ const data = [
6464
{ name: 'Graham', age: 62, name2: 'Willis', age2: 26 }
6565
];
6666
const orderedIds = ['name', 'name2', 'age', 'age2'];
67+
const tableHooks = [AnalyticalTableHooks.useOrderedMultiSort(orderedIds)]; // should be memoized
6768
const TableComponent = () => {
6869
return (
6970
<AnalyticalTable
7071
columns={columns}
7172
data={data}
7273
sortable
73-
tableHooks={[AnalyticalTableHooks.useOrderedMultiSort(orderedIds)]}
74+
tableHooks={tableHooks}
7475
/>
7576
);
7677
};

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,8 @@ export interface AnalyticalTablePropTypes extends Omit<CommonProps, 'title'> {
853853
/**
854854
* You can use this prop to add custom hooks to the table.
855855
*
856+
* __Note:__ Should be memoized!
857+
*
856858
* @default []
857859
*/
858860
tableHooks?: ((hooks: ReactTableHooks) => void)[];

0 commit comments

Comments
 (0)