@cloud-ru/ds-table 1.0.2-preview-cd0cd6fb.0 → 1.0.2-preview-ad4a761f.0
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/cjs/helperComponents/Cells/BodyCell/BodyCell.d.ts +3 -1
- package/dist/cjs/helperComponents/Cells/BodyCell/BodyCell.js +8 -9
- package/dist/cjs/helperComponents/Rows/BodyRow.js +1 -2
- package/dist/cjs/hooks/index.d.ts +1 -0
- package/dist/cjs/hooks/index.js +1 -0
- package/dist/cjs/hooks/useColumnDragTransform.d.ts +15 -0
- package/dist/cjs/hooks/useColumnDragTransform.js +38 -0
- package/dist/esm/helperComponents/Cells/BodyCell/BodyCell.d.ts +3 -1
- package/dist/esm/helperComponents/Cells/BodyCell/BodyCell.js +9 -10
- package/dist/esm/helperComponents/Rows/BodyRow.js +1 -2
- package/dist/esm/hooks/index.d.ts +1 -0
- package/dist/esm/hooks/index.js +1 -0
- package/dist/esm/hooks/useColumnDragTransform.d.ts +15 -0
- package/dist/esm/hooks/useColumnDragTransform.js +35 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +8 -8
- package/src/helperComponents/Cells/BodyCell/BodyCell.tsx +14 -14
- package/src/helperComponents/Rows/BodyRow.tsx +9 -10
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useColumnDragTransform.ts +52 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloud-ru/ds-table",
|
|
3
|
-
"version": "1.0.2-preview-
|
|
3
|
+
"version": "1.0.2-preview-ad4a761f.0",
|
|
4
4
|
"description": "Таблица данных: клиентская Table, серверная ServerTable и preset-обёртки с упрощённым API.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -50,22 +50,22 @@
|
|
|
50
50
|
"classnames": "^2.5.1",
|
|
51
51
|
"@cloud-ru/ds-adaptive": "1.0.0",
|
|
52
52
|
"@cloud-ru/ds-button": "1.0.0",
|
|
53
|
-
"@cloud-ru/ds-card": "1.0.1-preview-cd0cd6fb.0",
|
|
54
|
-
"@cloud-ru/ds-chips": "1.0.2-preview-cd0cd6fb.0",
|
|
55
53
|
"@cloud-ru/ds-drag-and-drop": "1.1.0",
|
|
56
|
-
"@cloud-ru/ds-
|
|
54
|
+
"@cloud-ru/ds-card": "1.0.1-preview-ad4a761f.0",
|
|
57
55
|
"@cloud-ru/ds-icons": "1.0.0",
|
|
58
|
-
"@cloud-ru/ds-
|
|
56
|
+
"@cloud-ru/ds-info-block": "1.0.0",
|
|
57
|
+
"@cloud-ru/ds-list": "1.1.1-preview-ad4a761f.0",
|
|
59
58
|
"@cloud-ru/ds-locale": "1.0.0",
|
|
60
59
|
"@cloud-ru/ds-materials": "1.0.0",
|
|
60
|
+
"@cloud-ru/ds-chips": "1.0.2-preview-ad4a761f.0",
|
|
61
61
|
"@cloud-ru/ds-pagination": "1.0.0",
|
|
62
62
|
"@cloud-ru/ds-scroll": "1.0.0",
|
|
63
63
|
"@cloud-ru/ds-skeleton": "1.0.0",
|
|
64
|
-
"@cloud-ru/ds-theme": "1.0.0",
|
|
65
64
|
"@cloud-ru/ds-status": "1.0.0",
|
|
66
|
-
"@cloud-ru/ds-
|
|
67
|
-
"@cloud-ru/ds-
|
|
65
|
+
"@cloud-ru/ds-theme": "1.0.0",
|
|
66
|
+
"@cloud-ru/ds-toggles": "1.0.1-preview-ad4a761f.0",
|
|
68
67
|
"@cloud-ru/ds-tooltip": "1.0.0",
|
|
68
|
+
"@cloud-ru/ds-toolbar": "1.0.2-preview-ad4a761f.0",
|
|
69
69
|
"@cloud-ru/ds-truncate-string": "1.0.0",
|
|
70
70
|
"@cloud-ru/ds-utils": "1.0.0"
|
|
71
71
|
},
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { useSortable } from '@dnd-kit/sortable';
|
|
2
1
|
import { Cell as TableCell, flexRender } from '@tanstack/react-table';
|
|
3
2
|
import cn from 'classnames';
|
|
4
3
|
import { CSSProperties } from 'react';
|
|
5
4
|
|
|
6
5
|
import { TEST_IDS } from '../../../constants';
|
|
7
|
-
import { useCellSizes } from '../../../hooks';
|
|
8
|
-
import { ColumnDefinition } from '../../../types';
|
|
6
|
+
import { useCellSizes, useColumnDragTransform } from '../../../hooks';
|
|
7
|
+
import { ColumnDefinition, ColumnOrder } from '../../../types';
|
|
9
8
|
import { Cell, CellProps } from '../Cell';
|
|
10
9
|
import styles from './styles.module.scss';
|
|
11
10
|
|
|
@@ -13,11 +12,11 @@ type BodyCellProps<TData> = Omit<CellProps, 'style' | 'children'> & {
|
|
|
13
12
|
cell: TableCell<TData, unknown>;
|
|
14
13
|
rowAutoHeight?: boolean;
|
|
15
14
|
isDraggable?: boolean;
|
|
15
|
+
columnOrder?: ColumnOrder;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
type BodyCellViewProps<TData> = Omit<BodyCellProps<TData>, 'isDraggable'> & {
|
|
18
|
+
type BodyCellViewProps<TData> = Omit<BodyCellProps<TData>, 'isDraggable' | 'columnOrder'> & {
|
|
19
19
|
style: CSSProperties;
|
|
20
|
-
setNodeRef?: (node: HTMLDivElement | null) => void;
|
|
21
20
|
isDragging?: boolean;
|
|
22
21
|
};
|
|
23
22
|
function BodyCellView<TData>({
|
|
@@ -25,7 +24,6 @@ function BodyCellView<TData>({
|
|
|
25
24
|
className,
|
|
26
25
|
rowAutoHeight,
|
|
27
26
|
style,
|
|
28
|
-
setNodeRef,
|
|
29
27
|
isDragging,
|
|
30
28
|
...props
|
|
31
29
|
}: BodyCellViewProps<TData>) {
|
|
@@ -34,7 +32,6 @@ function BodyCellView<TData>({
|
|
|
34
32
|
return (
|
|
35
33
|
<Cell
|
|
36
34
|
{...props}
|
|
37
|
-
ref={setNodeRef}
|
|
38
35
|
style={style}
|
|
39
36
|
className={cn(styles.tableBodyCell, className, columnDef.cellClassName)}
|
|
40
37
|
data-row-auto-height={rowAutoHeight || undefined}
|
|
@@ -49,22 +46,25 @@ function BodyCellView<TData>({
|
|
|
49
46
|
);
|
|
50
47
|
}
|
|
51
48
|
|
|
52
|
-
function StaticBodyCell<TData>(props: Omit<BodyCellProps<TData>, 'isDraggable'>) {
|
|
49
|
+
function StaticBodyCell<TData>(props: Omit<BodyCellProps<TData>, 'isDraggable' | 'columnOrder'>) {
|
|
53
50
|
const style = useCellSizes(props.cell);
|
|
54
51
|
|
|
55
52
|
return <BodyCellView {...props} style={style} />;
|
|
56
53
|
}
|
|
57
54
|
|
|
58
|
-
function DraggableBodyCell<TData>(
|
|
59
|
-
|
|
55
|
+
function DraggableBodyCell<TData>({
|
|
56
|
+
columnOrder,
|
|
57
|
+
...props
|
|
58
|
+
}: Omit<BodyCellProps<TData>, 'isDraggable'> & { columnOrder: ColumnOrder }) {
|
|
59
|
+
const { transform, isDragging } = useColumnDragTransform(props.cell.column.id, columnOrder);
|
|
60
60
|
const style = useCellSizes(props.cell, { isDraggable: true, transform });
|
|
61
61
|
|
|
62
|
-
return <BodyCellView {...props} style={style}
|
|
62
|
+
return <BodyCellView {...props} style={style} isDragging={isDragging} />;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
export function BodyCell<TData>({ isDraggable, ...props }: BodyCellProps<TData>) {
|
|
66
|
-
if (isDraggable) {
|
|
67
|
-
return <DraggableBodyCell {...props} />;
|
|
65
|
+
export function BodyCell<TData>({ isDraggable, columnOrder, ...props }: BodyCellProps<TData>) {
|
|
66
|
+
if (isDraggable && columnOrder) {
|
|
67
|
+
return <DraggableBodyCell {...props} columnOrder={columnOrder} />;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
return <StaticBodyCell {...props} />;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { horizontalListSortingStrategy, SortableContext } from '@dnd-kit/sortable';
|
|
2
1
|
import { Row as TableRow } from '@tanstack/react-table';
|
|
3
2
|
import { CSSProperties, MouseEvent, useState } from 'react';
|
|
4
3
|
|
|
@@ -86,15 +85,15 @@ export function BodyRow<TData>({
|
|
|
86
85
|
{columnVirtualPadding && columnVirtualPadding.left > 0 && (
|
|
87
86
|
<div aria-hidden style={{ width: columnVirtualPadding.left, flexShrink: 0 } as CSSProperties} />
|
|
88
87
|
)}
|
|
89
|
-
{
|
|
90
|
-
<
|
|
91
|
-
{
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
)}
|
|
88
|
+
{unpinned.map(cell => (
|
|
89
|
+
<BodyCell
|
|
90
|
+
key={cell.id}
|
|
91
|
+
cell={cell}
|
|
92
|
+
rowAutoHeight={rowAutoHeight}
|
|
93
|
+
isDraggable={enableColumnsOrderSortByDrag}
|
|
94
|
+
columnOrder={columnOrder}
|
|
95
|
+
/>
|
|
96
|
+
))}
|
|
98
97
|
{columnVirtualPadding && columnVirtualPadding.right > 0 && (
|
|
99
98
|
<div aria-hidden style={{ width: columnVirtualPadding.right, flexShrink: 0 } as CSSProperties} />
|
|
100
99
|
)}
|
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ClientRect, useDndContext } from '@dnd-kit/core';
|
|
2
|
+
import { horizontalListSortingStrategy } from '@dnd-kit/sortable';
|
|
3
|
+
import { Transform } from '@dnd-kit/utilities';
|
|
4
|
+
import { useMemo } from 'react';
|
|
5
|
+
|
|
6
|
+
import { ColumnOrder } from '../types';
|
|
7
|
+
|
|
8
|
+
type ColumnDragState = {
|
|
9
|
+
transform: Transform | null;
|
|
10
|
+
isDragging: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Сдвиг ячейки тела за колонкой, которую тянут за шапку.
|
|
15
|
+
*
|
|
16
|
+
* Не `useSortable`: он зарегистрировал бы ячейку в реестре dnd-kit под id колонки — тем же, что у
|
|
17
|
+
* шапки. Реестр ключуется по id, поэтому размонтирование строк удаляло запись колонки целиком и
|
|
18
|
+
* драг переставал работать.
|
|
19
|
+
*/
|
|
20
|
+
export function useColumnDragTransform(columnId: string, columnOrder: ColumnOrder): ColumnDragState {
|
|
21
|
+
const { active, over, activeNodeRect, droppableRects } = useDndContext();
|
|
22
|
+
|
|
23
|
+
const activeId = active ? String(active.id) : undefined;
|
|
24
|
+
const overId = over ? String(over.id) : undefined;
|
|
25
|
+
|
|
26
|
+
const transform = useMemo(() => {
|
|
27
|
+
if (!activeId) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const measured = columnOrder.filter(id => droppableRects.get(id));
|
|
32
|
+
|
|
33
|
+
const index = measured.indexOf(columnId);
|
|
34
|
+
const activeIndex = measured.indexOf(activeId);
|
|
35
|
+
|
|
36
|
+
if (index < 0 || activeIndex < 0) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const overIndex = overId ? measured.indexOf(overId) : -1;
|
|
41
|
+
|
|
42
|
+
return horizontalListSortingStrategy({
|
|
43
|
+
rects: measured.map(id => droppableRects.get(id) as ClientRect),
|
|
44
|
+
activeNodeRect,
|
|
45
|
+
activeIndex,
|
|
46
|
+
overIndex: overIndex < 0 ? activeIndex : overIndex,
|
|
47
|
+
index,
|
|
48
|
+
});
|
|
49
|
+
}, [activeId, overId, columnId, columnOrder, droppableRects, activeNodeRect]);
|
|
50
|
+
|
|
51
|
+
return { transform, isDragging: activeId === columnId };
|
|
52
|
+
}
|