@dxos/react-ui-table 0.4.10-main.4c7b3fa → 0.4.10-main.4c8c73c
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 +119 -76
- 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/Table/Table.d.ts.map +1 -1
- package/dist/types/src/components/Table/Table.stories.d.ts +28 -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/useRowSelection.d.ts +7 -0
- package/dist/types/src/hooks/useRowSelection.d.ts.map +1 -0
- package/package.json +15 -14
- package/src/components/ColumnMenu/ColumnMenu.tsx +34 -32
- package/src/components/Table/Table.stories.tsx +104 -0
- package/src/components/Table/Table.tsx +12 -52
- package/src/helpers.tsx +9 -7
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useColumnSizing.ts +45 -0
- package/src/hooks/useRowSelection.ts +39 -0
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
import { type ColumnSizingInfoState, type ColumnSizingState } from '@tanstack/react-table';
|
|
5
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { useOnTransition } from '@dxos/react-ui';
|
|
8
|
+
|
|
9
|
+
import { type TableColumnDef } from '../components/Table';
|
|
10
|
+
|
|
11
|
+
export const useColumnResizing = ({
|
|
12
|
+
columns,
|
|
13
|
+
onColumnResize,
|
|
14
|
+
}: {
|
|
15
|
+
columns: TableColumnDef<any>[];
|
|
16
|
+
onColumnResize: ((state: Record<string, number>) => void) | undefined;
|
|
17
|
+
}) => {
|
|
18
|
+
const [columnsInitialised, setColumnsInitialised] = useState(false);
|
|
19
|
+
const [columnSizing, setColumnSizing] = useState<ColumnSizingState>({});
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (columnsInitialised) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
setColumnSizing(
|
|
27
|
+
columns
|
|
28
|
+
.filter((column) => !!column.size && (column as any).prop !== undefined)
|
|
29
|
+
.reduce<ColumnSizingState>((state, column) => {
|
|
30
|
+
state[(column as any).prop] = column.size!;
|
|
31
|
+
return state;
|
|
32
|
+
}, {}),
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
setColumnsInitialised(true);
|
|
36
|
+
}, [columns, setColumnSizing]);
|
|
37
|
+
|
|
38
|
+
const [columnSizingInfo, setColumnSizingInfo] = useState<ColumnSizingInfoState>({} as ColumnSizingInfoState);
|
|
39
|
+
|
|
40
|
+
// Notify on column resize.
|
|
41
|
+
const notifyColumnResize = useCallback(() => onColumnResize?.(columnSizing), [onColumnResize, columnSizing]);
|
|
42
|
+
useOnTransition(columnSizingInfo.isResizingColumn, (v) => typeof v === 'string', false, notifyColumnResize);
|
|
43
|
+
|
|
44
|
+
return { columnSizing, columnSizingInfo, setColumnSizing, setColumnSizingInfo };
|
|
45
|
+
};
|
|
@@ -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
|
+
};
|