@dxos/react-ui-table 0.4.10-main.e2d2318 → 0.4.10-main.e3078ed
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +221 -131
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/ColumnMenu/ColumnMenu.d.ts.map +1 -1
- package/dist/types/src/components/ColumnMenu/ColumnMenu.stories.d.ts +5 -1
- package/dist/types/src/components/ColumnMenu/ColumnMenu.stories.d.ts.map +1 -1
- package/dist/types/src/components/ColumnMenu/ColumnSettingsForm.d.ts.map +1 -1
- package/dist/types/src/components/Table/Table.d.ts.map +1 -1
- package/dist/types/src/components/Table/Table.stories.d.ts +31 -0
- package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
- package/dist/types/src/helpers.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +2 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useColumnSizing.d.ts +12 -0
- package/dist/types/src/hooks/useColumnSizing.d.ts.map +1 -0
- package/dist/types/src/hooks/usePinLastRow.d.ts +1 -1
- package/dist/types/src/hooks/usePinLastRow.d.ts.map +1 -1
- package/dist/types/src/hooks/useRowSelection.d.ts +7 -0
- package/dist/types/src/hooks/useRowSelection.d.ts.map +1 -0
- package/package.json +17 -16
- package/src/components/ColumnMenu/ColumnMenu.stories.tsx +30 -0
- package/src/components/ColumnMenu/ColumnMenu.tsx +34 -32
- package/src/components/ColumnMenu/ColumnSettingsForm.tsx +69 -63
- package/src/components/Table/Table.stories.tsx +161 -2
- package/src/components/Table/Table.tsx +35 -60
- package/src/helpers.tsx +9 -7
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useColumnSizing.ts +45 -0
- package/src/hooks/usePinLastRow.ts +41 -20
- package/src/hooks/useRowSelection.ts +39 -0
|
@@ -2,42 +2,63 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type Table } from '@tanstack/react-table';
|
|
6
|
-
import { useEffect, useState } from 'react';
|
|
5
|
+
import { type Row, type Table } from '@tanstack/react-table';
|
|
6
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
7
7
|
|
|
8
8
|
export const usePinLastRow = (
|
|
9
9
|
pinLastRow: boolean | undefined,
|
|
10
10
|
table: Table<any>,
|
|
11
|
-
data: any,
|
|
11
|
+
data: any[],
|
|
12
12
|
getScrollElement?: () => Element | null,
|
|
13
13
|
) => {
|
|
14
14
|
const [pinnedRowKey, setPinnedRowKey] = useState<string>();
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
// Scrolls to the bottom of a scrollable element.
|
|
17
|
+
const scrollToBottom = useCallback(() => {
|
|
18
|
+
const scrollElement = getScrollElement?.();
|
|
19
|
+
if (scrollElement) {
|
|
20
|
+
requestAnimationFrame(() => scrollElement.scrollTo({ top: scrollElement.scrollHeight }));
|
|
19
21
|
}
|
|
22
|
+
}, [getScrollElement]);
|
|
23
|
+
|
|
24
|
+
const pinRow = useCallback(
|
|
25
|
+
(rowToPin: Row<any>) => {
|
|
26
|
+
table.resetRowPinning();
|
|
27
|
+
rowToPin.pin('bottom');
|
|
28
|
+
setPinnedRowKey(rowToPin.id);
|
|
29
|
+
},
|
|
30
|
+
[table],
|
|
31
|
+
);
|
|
20
32
|
|
|
33
|
+
const clearRowPinning = useCallback(() => {
|
|
34
|
+
table.resetRowPinning();
|
|
35
|
+
setPinnedRowKey(undefined);
|
|
36
|
+
}, [table]);
|
|
37
|
+
|
|
38
|
+
// Define the logic for pinning the last row in a callback.
|
|
39
|
+
const pinLastRowCallback = useCallback(() => {
|
|
21
40
|
const rows = table.getRowModel().rows;
|
|
22
|
-
const rowToPin = rows[rows.length - 1];
|
|
23
|
-
const rowId = rowToPin.id;
|
|
24
41
|
|
|
25
|
-
if (
|
|
26
|
-
return;
|
|
42
|
+
if (rows.length === 0) {
|
|
43
|
+
return clearRowPinning();
|
|
27
44
|
}
|
|
28
45
|
|
|
29
|
-
|
|
30
|
-
|
|
46
|
+
const lastRow = rows[rows.length - 1];
|
|
47
|
+
if (lastRow.id === pinnedRowKey) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
31
50
|
|
|
32
|
-
|
|
33
|
-
const isFirstRender = pinnedRowKey === undefined;
|
|
51
|
+
pinRow(lastRow);
|
|
34
52
|
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
scrollElement.scrollTo({ top: scrollElement.scrollHeight });
|
|
38
|
-
});
|
|
53
|
+
if (pinnedRowKey !== undefined) {
|
|
54
|
+
scrollToBottom();
|
|
39
55
|
}
|
|
56
|
+
}, [table, scrollToBottom, pinnedRowKey]);
|
|
40
57
|
|
|
41
|
-
|
|
42
|
-
|
|
58
|
+
// Effect to handle pinning logic when data changes.
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (pinLastRow) {
|
|
61
|
+
pinLastRowCallback();
|
|
62
|
+
}
|
|
63
|
+
}, [data, pinLastRow, pinLastRowCallback]);
|
|
43
64
|
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
import { useControllableState } from '@radix-ui/react-use-controllable-state';
|
|
5
|
+
import { type OnChangeFn, type RowSelectionState } from '@tanstack/react-table';
|
|
6
|
+
import { useCallback } from 'react';
|
|
7
|
+
|
|
8
|
+
import { type TableProps } from '../components/Table';
|
|
9
|
+
|
|
10
|
+
export const useRowSelection = (
|
|
11
|
+
props: Pick<
|
|
12
|
+
TableProps<any>,
|
|
13
|
+
'rowsSelectable' | 'defaultRowSelection' | 'rowSelection' | 'onRowSelectionChange' | 'rowsSelectable'
|
|
14
|
+
>,
|
|
15
|
+
) => {
|
|
16
|
+
const [rowSelection = {}, setRowSelection] = useControllableState({
|
|
17
|
+
prop: props.rowSelection,
|
|
18
|
+
onChange: props.onRowSelectionChange,
|
|
19
|
+
defaultProp: props.defaultRowSelection,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// TODO(thure): Does @tanstack/react-table really need this intervention? It did seem necessary to enforce single-selection...
|
|
23
|
+
const handleRowSelectionChange = useCallback<OnChangeFn<RowSelectionState>>(
|
|
24
|
+
(updaterOrValue) => {
|
|
25
|
+
const nextRowSelection = typeof updaterOrValue === 'function' ? updaterOrValue(rowSelection) : updaterOrValue;
|
|
26
|
+
if (props.rowsSelectable === 'multi') {
|
|
27
|
+
setRowSelection(nextRowSelection);
|
|
28
|
+
} else if (props.rowsSelectable) {
|
|
29
|
+
const nextRowSelectionKey = Object.keys(nextRowSelection).filter((id) => !rowSelection[id])[0];
|
|
30
|
+
setRowSelection(nextRowSelectionKey ? { [nextRowSelectionKey]: true } : {});
|
|
31
|
+
} else {
|
|
32
|
+
setRowSelection({});
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
[props.rowsSelectable, setRowSelection, rowSelection],
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return { rowSelection, handleRowSelectionChange };
|
|
39
|
+
};
|