@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
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { Cell as TableCell } from '@tanstack/react-table';
|
|
2
|
+
import { ColumnOrder } from '../../../types';
|
|
2
3
|
import { CellProps } from '../Cell';
|
|
3
4
|
type BodyCellProps<TData> = Omit<CellProps, 'style' | 'children'> & {
|
|
4
5
|
cell: TableCell<TData, unknown>;
|
|
5
6
|
rowAutoHeight?: boolean;
|
|
6
7
|
isDraggable?: boolean;
|
|
8
|
+
columnOrder?: ColumnOrder;
|
|
7
9
|
};
|
|
8
|
-
export declare function BodyCell<TData>({ isDraggable, ...props }: BodyCellProps<TData>): import("react/jsx-runtime").JSX.Element;
|
|
10
|
+
export declare function BodyCell<TData>({ isDraggable, columnOrder, ...props }: BodyCellProps<TData>): import("react/jsx-runtime").JSX.Element;
|
|
9
11
|
export {};
|
|
@@ -5,29 +5,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.BodyCell = BodyCell;
|
|
7
7
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
-
const sortable_1 = require("@dnd-kit/sortable");
|
|
9
8
|
const react_table_1 = require("@tanstack/react-table");
|
|
10
9
|
const classnames_1 = __importDefault(require("classnames"));
|
|
11
10
|
const constants_1 = require("../../../constants");
|
|
12
11
|
const hooks_1 = require("../../../hooks");
|
|
13
12
|
const Cell_1 = require("../Cell");
|
|
14
13
|
const styles_module_scss_1 = __importDefault(require('./styles.module.css'));
|
|
15
|
-
function BodyCellView({ cell, className, rowAutoHeight, style,
|
|
14
|
+
function BodyCellView({ cell, className, rowAutoHeight, style, isDragging, ...props }) {
|
|
16
15
|
const columnDef = cell.column.columnDef;
|
|
17
|
-
return ((0, jsx_runtime_1.jsx)(Cell_1.Cell, { ...props,
|
|
16
|
+
return ((0, jsx_runtime_1.jsx)(Cell_1.Cell, { ...props, style: style, className: (0, classnames_1.default)(styles_module_scss_1.default.tableBodyCell, className, columnDef.cellClassName), "data-row-auto-height": rowAutoHeight || undefined, "data-align": columnDef.align, "data-no-padding": columnDef.noBodyCellPadding || undefined, "data-column-id": cell.column.id, "data-dragging": isDragging || undefined, "data-test-id": constants_1.TEST_IDS.bodyCell, children: (0, react_table_1.flexRender)(columnDef.cell, cell.getContext()) }));
|
|
18
17
|
}
|
|
19
18
|
function StaticBodyCell(props) {
|
|
20
19
|
const style = (0, hooks_1.useCellSizes)(props.cell);
|
|
21
20
|
return (0, jsx_runtime_1.jsx)(BodyCellView, { ...props, style: style });
|
|
22
21
|
}
|
|
23
|
-
function DraggableBodyCell(props) {
|
|
24
|
-
const {
|
|
22
|
+
function DraggableBodyCell({ columnOrder, ...props }) {
|
|
23
|
+
const { transform, isDragging } = (0, hooks_1.useColumnDragTransform)(props.cell.column.id, columnOrder);
|
|
25
24
|
const style = (0, hooks_1.useCellSizes)(props.cell, { isDraggable: true, transform });
|
|
26
|
-
return (0, jsx_runtime_1.jsx)(BodyCellView, { ...props, style: style,
|
|
25
|
+
return (0, jsx_runtime_1.jsx)(BodyCellView, { ...props, style: style, isDragging: isDragging });
|
|
27
26
|
}
|
|
28
|
-
function BodyCell({ isDraggable, ...props }) {
|
|
29
|
-
if (isDraggable) {
|
|
30
|
-
return (0, jsx_runtime_1.jsx)(DraggableBodyCell, { ...props });
|
|
27
|
+
function BodyCell({ isDraggable, columnOrder, ...props }) {
|
|
28
|
+
if (isDraggable && columnOrder) {
|
|
29
|
+
return (0, jsx_runtime_1.jsx)(DraggableBodyCell, { ...props, columnOrder: columnOrder });
|
|
31
30
|
}
|
|
32
31
|
return (0, jsx_runtime_1.jsx)(StaticBodyCell, { ...props });
|
|
33
32
|
}
|
|
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.BodyRow = BodyRow;
|
|
7
7
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
-
const sortable_1 = require("@dnd-kit/sortable");
|
|
9
8
|
const react_1 = require("react");
|
|
10
9
|
const components_1 = require("../../components");
|
|
11
10
|
const constants_1 = require("../../constants");
|
|
@@ -33,5 +32,5 @@ function BodyRow({ row, onRowClick, rowAutoHeight, columnOrder, enableColumnsOrd
|
|
|
33
32
|
};
|
|
34
33
|
const isSelected = row.getIsSelected() || (row.getIsSomeSelected() && !row.getCanMultiSelect() && !row.getIsExpanded());
|
|
35
34
|
const rowBackgroundColor = getRowBackgroundColor?.(row.original);
|
|
36
|
-
return ((0, jsx_runtime_1.jsx)(contexts_1.RowContext.Provider, { value: { dropListOpened, setDropListOpen, disabledRowAppearance }, children: (0, jsx_runtime_1.jsxs)(Row_1.Row, { onClick: handleRowClick, clickable: Boolean(onRowClick) && !(disabled && disabledRowAppearance === components_1.RowAppearance.Disabled), "data-clickable": Boolean(onRowClick) || undefined, "data-disabled": disabled || undefined, "data-row-appearance": disabled ? disabledRowAppearance : undefined, "data-selected": isSelected || undefined, "data-actions-opened": dropListOpened || undefined, "data-test-id": constants_1.TEST_IDS.bodyRow, "data-row-id": row.id, "data-row-bg-appearance": rowBackgroundColor && !disabled && !isSelected ? rowBackgroundColor : undefined, className: styles_module_scss_1.default.bodyRow, rowAutoHeight: rowAutoHeight, children: [leftPinned && ((0, jsx_runtime_1.jsx)(PinnedCells_1.PinnedCells, { position: constants_1.COLUMN_PIN_POSITION.Left, children: leftPinned.map(cell => ((0, jsx_runtime_1.jsx)(Cells_1.BodyCell, { cell: cell, rowAutoHeight: rowAutoHeight }, cell.id))) })), columnVirtualPadding && columnVirtualPadding.left > 0 && ((0, jsx_runtime_1.jsx)("div", { "aria-hidden": true, style: { width: columnVirtualPadding.left, flexShrink: 0 } })),
|
|
35
|
+
return ((0, jsx_runtime_1.jsx)(contexts_1.RowContext.Provider, { value: { dropListOpened, setDropListOpen, disabledRowAppearance }, children: (0, jsx_runtime_1.jsxs)(Row_1.Row, { onClick: handleRowClick, clickable: Boolean(onRowClick) && !(disabled && disabledRowAppearance === components_1.RowAppearance.Disabled), "data-clickable": Boolean(onRowClick) || undefined, "data-disabled": disabled || undefined, "data-row-appearance": disabled ? disabledRowAppearance : undefined, "data-selected": isSelected || undefined, "data-actions-opened": dropListOpened || undefined, "data-test-id": constants_1.TEST_IDS.bodyRow, "data-row-id": row.id, "data-row-bg-appearance": rowBackgroundColor && !disabled && !isSelected ? rowBackgroundColor : undefined, className: styles_module_scss_1.default.bodyRow, rowAutoHeight: rowAutoHeight, children: [leftPinned && ((0, jsx_runtime_1.jsx)(PinnedCells_1.PinnedCells, { position: constants_1.COLUMN_PIN_POSITION.Left, children: leftPinned.map(cell => ((0, jsx_runtime_1.jsx)(Cells_1.BodyCell, { cell: cell, rowAutoHeight: rowAutoHeight }, cell.id))) })), columnVirtualPadding && columnVirtualPadding.left > 0 && ((0, jsx_runtime_1.jsx)("div", { "aria-hidden": true, style: { width: columnVirtualPadding.left, flexShrink: 0 } })), unpinned.map(cell => ((0, jsx_runtime_1.jsx)(Cells_1.BodyCell, { cell: cell, rowAutoHeight: rowAutoHeight, isDraggable: enableColumnsOrderSortByDrag, columnOrder: columnOrder }, cell.id))), columnVirtualPadding && columnVirtualPadding.right > 0 && ((0, jsx_runtime_1.jsx)("div", { "aria-hidden": true, style: { width: columnVirtualPadding.right, flexShrink: 0 } })), rightPinned && ((0, jsx_runtime_1.jsx)(PinnedCells_1.PinnedCells, { position: constants_1.COLUMN_PIN_POSITION.Right, children: rightPinned.map(cell => ((0, jsx_runtime_1.jsx)(Cells_1.BodyCell, { cell: cell, rowAutoHeight: rowAutoHeight, isDraggable: enableColumnsOrderSortByDrag }, cell.id))) }))] }) }));
|
|
37
36
|
}
|
package/dist/cjs/hooks/index.js
CHANGED
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./useCellSizes"), exports);
|
|
18
|
+
__exportStar(require("./useColumnDragTransform"), exports);
|
|
18
19
|
__exportStar(require("./useColumnsSettingsToolbarSlot"), exports);
|
|
19
20
|
__exportStar(require("./useExportToolbarSlot"), exports);
|
|
20
21
|
__exportStar(require("./useHeaderGroups"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Transform } from '@dnd-kit/utilities';
|
|
2
|
+
import { ColumnOrder } from '../types';
|
|
3
|
+
type ColumnDragState = {
|
|
4
|
+
transform: Transform | null;
|
|
5
|
+
isDragging: boolean;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Сдвиг ячейки тела за колонкой, которую тянут за шапку.
|
|
9
|
+
*
|
|
10
|
+
* Не `useSortable`: он зарегистрировал бы ячейку в реестре dnd-kit под id колонки — тем же, что у
|
|
11
|
+
* шапки. Реестр ключуется по id, поэтому размонтирование строк удаляло запись колонки целиком и
|
|
12
|
+
* драг переставал работать.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useColumnDragTransform(columnId: string, columnOrder: ColumnOrder): ColumnDragState;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useColumnDragTransform = useColumnDragTransform;
|
|
4
|
+
const core_1 = require("@dnd-kit/core");
|
|
5
|
+
const sortable_1 = require("@dnd-kit/sortable");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
/**
|
|
8
|
+
* Сдвиг ячейки тела за колонкой, которую тянут за шапку.
|
|
9
|
+
*
|
|
10
|
+
* Не `useSortable`: он зарегистрировал бы ячейку в реестре dnd-kit под id колонки — тем же, что у
|
|
11
|
+
* шапки. Реестр ключуется по id, поэтому размонтирование строк удаляло запись колонки целиком и
|
|
12
|
+
* драг переставал работать.
|
|
13
|
+
*/
|
|
14
|
+
function useColumnDragTransform(columnId, columnOrder) {
|
|
15
|
+
const { active, over, activeNodeRect, droppableRects } = (0, core_1.useDndContext)();
|
|
16
|
+
const activeId = active ? String(active.id) : undefined;
|
|
17
|
+
const overId = over ? String(over.id) : undefined;
|
|
18
|
+
const transform = (0, react_1.useMemo)(() => {
|
|
19
|
+
if (!activeId) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
const measured = columnOrder.filter(id => droppableRects.get(id));
|
|
23
|
+
const index = measured.indexOf(columnId);
|
|
24
|
+
const activeIndex = measured.indexOf(activeId);
|
|
25
|
+
if (index < 0 || activeIndex < 0) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const overIndex = overId ? measured.indexOf(overId) : -1;
|
|
29
|
+
return (0, sortable_1.horizontalListSortingStrategy)({
|
|
30
|
+
rects: measured.map(id => droppableRects.get(id)),
|
|
31
|
+
activeNodeRect,
|
|
32
|
+
activeIndex,
|
|
33
|
+
overIndex: overIndex < 0 ? activeIndex : overIndex,
|
|
34
|
+
index,
|
|
35
|
+
});
|
|
36
|
+
}, [activeId, overId, columnId, columnOrder, droppableRects, activeNodeRect]);
|
|
37
|
+
return { transform, isDragging: activeId === columnId };
|
|
38
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { Cell as TableCell } from '@tanstack/react-table';
|
|
2
|
+
import { ColumnOrder } from '../../../types.js';
|
|
2
3
|
import { CellProps } from '../Cell.js';
|
|
3
4
|
type BodyCellProps<TData> = Omit<CellProps, 'style' | 'children'> & {
|
|
4
5
|
cell: TableCell<TData, unknown>;
|
|
5
6
|
rowAutoHeight?: boolean;
|
|
6
7
|
isDraggable?: boolean;
|
|
8
|
+
columnOrder?: ColumnOrder;
|
|
7
9
|
};
|
|
8
|
-
export declare function BodyCell<TData>({ isDraggable, ...props }: BodyCellProps<TData>): import("react/jsx-runtime").JSX.Element;
|
|
10
|
+
export declare function BodyCell<TData>({ isDraggable, columnOrder, ...props }: BodyCellProps<TData>): import("react/jsx-runtime").JSX.Element;
|
|
9
11
|
export {};
|
|
@@ -1,27 +1,26 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useSortable } from '@dnd-kit/sortable';
|
|
3
2
|
import { flexRender } from '@tanstack/react-table';
|
|
4
3
|
import cn from 'classnames';
|
|
5
4
|
import { TEST_IDS } from '../../../constants.js';
|
|
6
|
-
import { useCellSizes } from '../../../hooks/index.js';
|
|
5
|
+
import { useCellSizes, useColumnDragTransform } from '../../../hooks/index.js';
|
|
7
6
|
import { Cell } from '../Cell.js';
|
|
8
7
|
import styles from './styles.module.css';
|
|
9
|
-
function BodyCellView({ cell, className, rowAutoHeight, style,
|
|
8
|
+
function BodyCellView({ cell, className, rowAutoHeight, style, isDragging, ...props }) {
|
|
10
9
|
const columnDef = cell.column.columnDef;
|
|
11
|
-
return (_jsx(Cell, { ...props,
|
|
10
|
+
return (_jsx(Cell, { ...props, style: style, className: cn(styles.tableBodyCell, className, columnDef.cellClassName), "data-row-auto-height": rowAutoHeight || undefined, "data-align": columnDef.align, "data-no-padding": columnDef.noBodyCellPadding || undefined, "data-column-id": cell.column.id, "data-dragging": isDragging || undefined, "data-test-id": TEST_IDS.bodyCell, children: flexRender(columnDef.cell, cell.getContext()) }));
|
|
12
11
|
}
|
|
13
12
|
function StaticBodyCell(props) {
|
|
14
13
|
const style = useCellSizes(props.cell);
|
|
15
14
|
return _jsx(BodyCellView, { ...props, style: style });
|
|
16
15
|
}
|
|
17
|
-
function DraggableBodyCell(props) {
|
|
18
|
-
const {
|
|
16
|
+
function DraggableBodyCell({ columnOrder, ...props }) {
|
|
17
|
+
const { transform, isDragging } = useColumnDragTransform(props.cell.column.id, columnOrder);
|
|
19
18
|
const style = useCellSizes(props.cell, { isDraggable: true, transform });
|
|
20
|
-
return _jsx(BodyCellView, { ...props, style: style,
|
|
19
|
+
return _jsx(BodyCellView, { ...props, style: style, isDragging: isDragging });
|
|
21
20
|
}
|
|
22
|
-
export function BodyCell({ isDraggable, ...props }) {
|
|
23
|
-
if (isDraggable) {
|
|
24
|
-
return _jsx(DraggableBodyCell, { ...props });
|
|
21
|
+
export function BodyCell({ isDraggable, columnOrder, ...props }) {
|
|
22
|
+
if (isDraggable && columnOrder) {
|
|
23
|
+
return _jsx(DraggableBodyCell, { ...props, columnOrder: columnOrder });
|
|
25
24
|
}
|
|
26
25
|
return _jsx(StaticBodyCell, { ...props });
|
|
27
26
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { horizontalListSortingStrategy, SortableContext } from '@dnd-kit/sortable';
|
|
3
2
|
import { useState } from 'react';
|
|
4
3
|
import { RowAppearance } from '../../components/index.js';
|
|
5
4
|
import { COLUMN_PIN_POSITION, TEST_IDS } from '../../constants.js';
|
|
@@ -27,5 +26,5 @@ export function BodyRow({ row, onRowClick, rowAutoHeight, columnOrder, enableCol
|
|
|
27
26
|
};
|
|
28
27
|
const isSelected = row.getIsSelected() || (row.getIsSomeSelected() && !row.getCanMultiSelect() && !row.getIsExpanded());
|
|
29
28
|
const rowBackgroundColor = getRowBackgroundColor?.(row.original);
|
|
30
|
-
return (_jsx(RowContext.Provider, { value: { dropListOpened, setDropListOpen, disabledRowAppearance }, children: _jsxs(Row, { onClick: handleRowClick, clickable: Boolean(onRowClick) && !(disabled && disabledRowAppearance === RowAppearance.Disabled), "data-clickable": Boolean(onRowClick) || undefined, "data-disabled": disabled || undefined, "data-row-appearance": disabled ? disabledRowAppearance : undefined, "data-selected": isSelected || undefined, "data-actions-opened": dropListOpened || undefined, "data-test-id": TEST_IDS.bodyRow, "data-row-id": row.id, "data-row-bg-appearance": rowBackgroundColor && !disabled && !isSelected ? rowBackgroundColor : undefined, className: styles.bodyRow, rowAutoHeight: rowAutoHeight, children: [leftPinned && (_jsx(PinnedCells, { position: COLUMN_PIN_POSITION.Left, children: leftPinned.map(cell => (_jsx(BodyCell, { cell: cell, rowAutoHeight: rowAutoHeight }, cell.id))) })), columnVirtualPadding && columnVirtualPadding.left > 0 && (_jsx("div", { "aria-hidden": true, style: { width: columnVirtualPadding.left, flexShrink: 0 } })),
|
|
29
|
+
return (_jsx(RowContext.Provider, { value: { dropListOpened, setDropListOpen, disabledRowAppearance }, children: _jsxs(Row, { onClick: handleRowClick, clickable: Boolean(onRowClick) && !(disabled && disabledRowAppearance === RowAppearance.Disabled), "data-clickable": Boolean(onRowClick) || undefined, "data-disabled": disabled || undefined, "data-row-appearance": disabled ? disabledRowAppearance : undefined, "data-selected": isSelected || undefined, "data-actions-opened": dropListOpened || undefined, "data-test-id": TEST_IDS.bodyRow, "data-row-id": row.id, "data-row-bg-appearance": rowBackgroundColor && !disabled && !isSelected ? rowBackgroundColor : undefined, className: styles.bodyRow, rowAutoHeight: rowAutoHeight, children: [leftPinned && (_jsx(PinnedCells, { position: COLUMN_PIN_POSITION.Left, children: leftPinned.map(cell => (_jsx(BodyCell, { cell: cell, rowAutoHeight: rowAutoHeight }, cell.id))) })), columnVirtualPadding && columnVirtualPadding.left > 0 && (_jsx("div", { "aria-hidden": true, style: { width: columnVirtualPadding.left, flexShrink: 0 } })), unpinned.map(cell => (_jsx(BodyCell, { cell: cell, rowAutoHeight: rowAutoHeight, isDraggable: enableColumnsOrderSortByDrag, columnOrder: columnOrder }, cell.id))), columnVirtualPadding && columnVirtualPadding.right > 0 && (_jsx("div", { "aria-hidden": true, style: { width: columnVirtualPadding.right, flexShrink: 0 } })), rightPinned && (_jsx(PinnedCells, { position: COLUMN_PIN_POSITION.Right, children: rightPinned.map(cell => (_jsx(BodyCell, { cell: cell, rowAutoHeight: rowAutoHeight, isDraggable: enableColumnsOrderSortByDrag }, cell.id))) }))] }) }));
|
|
31
30
|
}
|
package/dist/esm/hooks/index.js
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Transform } from '@dnd-kit/utilities';
|
|
2
|
+
import { ColumnOrder } from '../types.js';
|
|
3
|
+
type ColumnDragState = {
|
|
4
|
+
transform: Transform | null;
|
|
5
|
+
isDragging: boolean;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Сдвиг ячейки тела за колонкой, которую тянут за шапку.
|
|
9
|
+
*
|
|
10
|
+
* Не `useSortable`: он зарегистрировал бы ячейку в реестре dnd-kit под id колонки — тем же, что у
|
|
11
|
+
* шапки. Реестр ключуется по id, поэтому размонтирование строк удаляло запись колонки целиком и
|
|
12
|
+
* драг переставал работать.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useColumnDragTransform(columnId: string, columnOrder: ColumnOrder): ColumnDragState;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useDndContext } from '@dnd-kit/core';
|
|
2
|
+
import { horizontalListSortingStrategy } from '@dnd-kit/sortable';
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
/**
|
|
5
|
+
* Сдвиг ячейки тела за колонкой, которую тянут за шапку.
|
|
6
|
+
*
|
|
7
|
+
* Не `useSortable`: он зарегистрировал бы ячейку в реестре dnd-kit под id колонки — тем же, что у
|
|
8
|
+
* шапки. Реестр ключуется по id, поэтому размонтирование строк удаляло запись колонки целиком и
|
|
9
|
+
* драг переставал работать.
|
|
10
|
+
*/
|
|
11
|
+
export function useColumnDragTransform(columnId, columnOrder) {
|
|
12
|
+
const { active, over, activeNodeRect, droppableRects } = useDndContext();
|
|
13
|
+
const activeId = active ? String(active.id) : undefined;
|
|
14
|
+
const overId = over ? String(over.id) : undefined;
|
|
15
|
+
const transform = useMemo(() => {
|
|
16
|
+
if (!activeId) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
const measured = columnOrder.filter(id => droppableRects.get(id));
|
|
20
|
+
const index = measured.indexOf(columnId);
|
|
21
|
+
const activeIndex = measured.indexOf(activeId);
|
|
22
|
+
if (index < 0 || activeIndex < 0) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const overIndex = overId ? measured.indexOf(overId) : -1;
|
|
26
|
+
return horizontalListSortingStrategy({
|
|
27
|
+
rects: measured.map(id => droppableRects.get(id)),
|
|
28
|
+
activeNodeRect,
|
|
29
|
+
activeIndex,
|
|
30
|
+
overIndex: overIndex < 0 ? activeIndex : overIndex,
|
|
31
|
+
index,
|
|
32
|
+
});
|
|
33
|
+
}, [activeId, overId, columnId, columnOrder, droppableRects, activeNodeRect]);
|
|
34
|
+
return { transform, isDragging: activeId === columnId };
|
|
35
|
+
}
|