-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathResizeableDataTable.tsx
More file actions
68 lines (57 loc) · 1.99 KB
/
ResizeableDataTable.tsx
File metadata and controls
68 lines (57 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from 'react';
import type {Column, DataTableProps, Settings} from '@gravity-ui/react-data-table';
import DataTable, {updateColumnsWidth} from '@gravity-ui/react-data-table';
import {Skeleton} from '@gravity-ui/uikit';
import {cn} from '../../utils/cn';
import {useTableResize} from '../../utils/hooks/useTableResize';
import './ResizeableDataTable.scss';
const b = cn('ydb-resizeable-data-table');
export interface ResizeableDataTableProps<T> extends Omit<DataTableProps<T>, 'theme' | 'onResize'> {
columnsWidthLSKey?: string;
wrapperClassName?: string;
loading?: boolean;
onSortChange?: (params: any) => void;
}
export function ResizeableDataTable<T>({
columnsWidthLSKey,
columns,
settings,
wrapperClassName,
loading,
onSort,
onSortChange,
...props
}: ResizeableDataTableProps<T>) {
const [tableColumnsWidth, setTableColumnsWidth] = useTableResize(columnsWidthLSKey);
const handleSort = React.useCallback(
(params: any) => {
onSort?.(params); // Original onSort if provided
onSortChange?.(params); // Expose sort params to parent
},
[onSort, onSortChange],
);
// If loading is true, override the render method of each column to display a Skeleton
const processedColumns = loading
? columns.map((column: Column<T>) => ({
...column,
render: () => <Skeleton className={b('row-skeleton')} />,
}))
: columns;
const updatedColumns = updateColumnsWidth(processedColumns, tableColumnsWidth);
const newSettings: Settings = {
...settings,
defaultResizeable: true,
};
return (
<div className={b(null, wrapperClassName)}>
<DataTable
theme="yandex-cloud"
columns={updatedColumns}
onResize={setTableColumnsWidth}
onSort={handleSort}
settings={newSettings}
{...props}
/>
</div>
);
}