@uxf/core-react 11.128.0 → 12.0.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/README.md +6 -179
- package/package.json +3 -7
- package/dnd/hooks/use-sortable-core.d.ts +0 -9
- package/dnd/hooks/use-sortable-core.js +0 -32
- package/dnd/hooks/use-sortable-items.d.ts +0 -15
- package/dnd/hooks/use-sortable-items.js +0 -57
- package/dnd/hooks/use-sortable-multi.d.ts +0 -14
- package/dnd/hooks/use-sortable-multi.js +0 -93
- package/dnd/hooks/use-sortable-single.d.ts +0 -13
- package/dnd/hooks/use-sortable-single.js +0 -37
package/README.md
CHANGED
|
@@ -525,187 +525,14 @@ const isDesktopClientOnly = useMediaQuery(`(min-width: ${twScreens.sm})`, undefi
|
|
|
525
525
|
- `callback` _(optional)_ – called with `(isMatching, mediaQueryString)` on every match change
|
|
526
526
|
- `isSSR` _(optional, default: `true`)_ – when `true`, initial state is `false` to avoid SSR/hydration mismatch; when `false`, initial state is read directly from `window.matchMedia` (use only in client-only components)
|
|
527
527
|
|
|
528
|
-
## Drag and Drop
|
|
528
|
+
## Drag and Drop
|
|
529
529
|
|
|
530
|
-
|
|
530
|
+
Moved to its own package. The sortable hooks and a facade over [@dnd-kit](https://dndkit.com/) now live in
|
|
531
|
+
`@uxf/dnd`, so that `@dnd-kit` is a dependency only of the projects that actually render drag and drop —
|
|
532
|
+
it used to be a peer of this package, and therefore of everything that depends on it.
|
|
531
533
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
Low-level hook that provides the core drag-and-drop functionality. Used as a building block for other sortable hooks.
|
|
535
|
-
|
|
536
|
-
```tsx
|
|
537
|
-
import { useSortableCore } from "@uxf/core-react/dnd/hooks/use-sortable-core";
|
|
538
|
-
|
|
539
|
-
const {
|
|
540
|
-
activeElement, // Currently dragged element
|
|
541
|
-
setActiveElement, // Set active element manually
|
|
542
|
-
onDragStart, // Drag start handler
|
|
543
|
-
onDragCancel, // Drag cancel handler
|
|
544
|
-
sensors, // DND sensors (pointer, keyboard)
|
|
545
|
-
dropAnimationConfig, // Animation configuration for drop
|
|
546
|
-
} = useSortableCore();
|
|
547
|
-
```
|
|
548
|
-
|
|
549
|
-
---
|
|
550
|
-
|
|
551
|
-
### useSortableSingle
|
|
552
|
-
|
|
553
|
-
Hook for implementing drag-and-drop sorting within a single list. Manages item reordering and state internally.
|
|
554
|
-
|
|
555
|
-
```tsx
|
|
556
|
-
import { DndContext, DragOverlay } from "@dnd-kit/core";
|
|
557
|
-
import { SortableContext, verticalListSortingStrategy } from "@dnd-kit/sortable";
|
|
558
|
-
import { useSortableSingle } from "@uxf/core-react/dnd/hooks/use-sortable-single";
|
|
559
|
-
import { SortableItem } from "./sortable-item"; // Your sortable item component
|
|
560
|
-
|
|
561
|
-
interface Task {
|
|
562
|
-
id: string;
|
|
563
|
-
title: string;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
function TaskList() {
|
|
567
|
-
const initialTasks: Task[] = [
|
|
568
|
-
{ id: "1", title: "Write documentation" },
|
|
569
|
-
{ id: "2", title: "Review PR" },
|
|
570
|
-
{ id: "3", title: "Deploy to production" },
|
|
571
|
-
];
|
|
572
|
-
|
|
573
|
-
const {
|
|
574
|
-
items, // Current items state
|
|
575
|
-
setItems, // Update items manually
|
|
576
|
-
activeItem, // Currently dragged item
|
|
577
|
-
onDragStart,
|
|
578
|
-
onDragEnd,
|
|
579
|
-
onDragCancel,
|
|
580
|
-
sensors,
|
|
581
|
-
dropAnimationConfig,
|
|
582
|
-
} = useSortableSingle(initialTasks, (fromIndex, toIndex, updatedList) => {
|
|
583
|
-
// Optional callback when items are reordered
|
|
584
|
-
console.log(`Moved from ${fromIndex} to ${toIndex}`, updatedList);
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
return (
|
|
588
|
-
<DndContext sensors={sensors} onDragStart={onDragStart} onDragEnd={onDragEnd} onDragCancel={onDragCancel}>
|
|
589
|
-
<SortableContext items={items.map((item) => item.id)} strategy={verticalListSortingStrategy}>
|
|
590
|
-
{items.map((task) => (
|
|
591
|
-
<SortableItem key={task.id} id={task.id}>
|
|
592
|
-
{task.title}
|
|
593
|
-
</SortableItem>
|
|
594
|
-
))}
|
|
595
|
-
</SortableContext>
|
|
596
|
-
|
|
597
|
-
<DragOverlay dropAnimation={dropAnimationConfig}>
|
|
598
|
-
{activeItem ? <div>{activeItem.title}</div> : null}
|
|
599
|
-
</DragOverlay>
|
|
600
|
-
</DndContext>
|
|
601
|
-
);
|
|
602
|
-
}
|
|
603
|
-
```
|
|
604
|
-
|
|
605
|
-
---
|
|
606
|
-
|
|
607
|
-
### useSortableMulti
|
|
608
|
-
|
|
609
|
-
Hook for implementing drag-and-drop between multiple sections/lists. Ideal for kanban boards, categorized lists, or visible/hidden column management.
|
|
610
|
-
|
|
611
|
-
```tsx
|
|
612
|
-
import { DndContext, DragOverlay, rectIntersection } from "@dnd-kit/core";
|
|
613
|
-
import { SortableContext, verticalListSortingStrategy } from "@dnd-kit/sortable";
|
|
614
|
-
import { useSortableMulti } from "@uxf/core-react/dnd/hooks/use-sortable-multi";
|
|
615
|
-
import { SortableItem } from "./sortable-item"; // Your sortable item component
|
|
616
|
-
|
|
617
|
-
interface Task {
|
|
618
|
-
id: string;
|
|
619
|
-
title: string;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
interface Sections {
|
|
623
|
-
todo: Task[];
|
|
624
|
-
inProgress: Task[];
|
|
625
|
-
done: Task[];
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
function KanbanBoard() {
|
|
629
|
-
const [sections, setSections] = useState<Sections>({
|
|
630
|
-
todo: [
|
|
631
|
-
{ id: "1", title: "Design UI" },
|
|
632
|
-
{ id: "2", title: "Write tests" },
|
|
633
|
-
],
|
|
634
|
-
inProgress: [{ id: "3", title: "Implement feature" }],
|
|
635
|
-
done: [{ id: "4", title: "Setup project" }],
|
|
636
|
-
});
|
|
637
|
-
|
|
638
|
-
const {
|
|
639
|
-
getSectionItemsIds, // Get IDs for a section (handles empty sections)
|
|
640
|
-
activeItem, // Currently dragged item
|
|
641
|
-
onDragStart,
|
|
642
|
-
onDragEnd,
|
|
643
|
-
onDragCancel,
|
|
644
|
-
sensors,
|
|
645
|
-
dropAnimationConfig,
|
|
646
|
-
} = useSortableMulti<Task, Sections>(sections, setSections, (sourceKey, destKey, fromIndex, toIndex, nextState) => {
|
|
647
|
-
// Optional callback when items are moved between sections
|
|
648
|
-
console.log(`Moved from ${String(sourceKey)} to ${String(destKey)}`);
|
|
649
|
-
// Save to backend, analytics, etc.
|
|
650
|
-
});
|
|
651
|
-
|
|
652
|
-
return (
|
|
653
|
-
<DndContext
|
|
654
|
-
sensors={sensors}
|
|
655
|
-
collisionDetection={rectIntersection}
|
|
656
|
-
onDragStart={onDragStart}
|
|
657
|
-
onDragEnd={onDragEnd}
|
|
658
|
-
onDragCancel={onDragCancel}
|
|
659
|
-
>
|
|
660
|
-
<div className="kanban-board">
|
|
661
|
-
{(Object.keys(sections) as Array<keyof Sections>).map((sectionKey) => (
|
|
662
|
-
<div key={sectionKey} className="kanban-column">
|
|
663
|
-
<h3>{sectionKey}</h3>
|
|
664
|
-
<SortableContext items={getSectionItemsIds(sectionKey)} strategy={verticalListSortingStrategy}>
|
|
665
|
-
{sections[sectionKey].map((task) => (
|
|
666
|
-
<SortableItem key={task.id} id={task.id}>
|
|
667
|
-
{task.title}
|
|
668
|
-
</SortableItem>
|
|
669
|
-
))}
|
|
670
|
-
</SortableContext>
|
|
671
|
-
</div>
|
|
672
|
-
))}
|
|
673
|
-
</div>
|
|
674
|
-
|
|
675
|
-
<DragOverlay dropAnimation={dropAnimationConfig}>
|
|
676
|
-
{activeItem ? <div>{activeItem.title}</div> : null}
|
|
677
|
-
</DragOverlay>
|
|
678
|
-
</DndContext>
|
|
679
|
-
);
|
|
680
|
-
}
|
|
681
|
-
```
|
|
682
|
-
|
|
683
|
-
**Notes:**
|
|
684
|
-
|
|
685
|
-
- Items must have an `id: string` property
|
|
686
|
-
- `getSectionItemsIds` automatically handles empty sections by returning placeholder IDs
|
|
687
|
-
- Use `rectIntersection` collision detection for better multi-section behavior
|
|
688
|
-
- The hook manages item movement between sections automatically
|
|
689
|
-
|
|
690
|
-
---
|
|
691
|
-
|
|
692
|
-
### useSortableItems
|
|
693
|
-
|
|
694
|
-
Legacy hook for drag-and-drop sorting. Consider using `useSortableSingle` for new implementations.
|
|
695
|
-
|
|
696
|
-
```tsx
|
|
697
|
-
import { useSortableItems } from "@uxf/core-react/dnd/hooks/use-sortable-items";
|
|
698
|
-
|
|
699
|
-
const { memoizedSensors, dropAnimationConfig, itemsIds, activeItem, onDragStart, onDragEnd, onDragCancel } =
|
|
700
|
-
useSortableItems({
|
|
701
|
-
items: myItems,
|
|
702
|
-
handleDragEnd: (activeIndex, overIndex) => {
|
|
703
|
-
// Handle reorder
|
|
704
|
-
},
|
|
705
|
-
});
|
|
706
|
-
```
|
|
707
|
-
|
|
708
|
-
---
|
|
534
|
+
See the `@uxf/dnd` README, and [the migration note](https://gitlab.uxf.cz/uxf/fe-monorepo/-/blob/master/docs/migration/dnd-package-extraction.md)
|
|
535
|
+
for the import rewrites.
|
|
709
536
|
|
|
710
537
|
## Global Context
|
|
711
538
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uxf/core-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.0.0",
|
|
4
4
|
"description": "UXF Core",
|
|
5
5
|
"author": "UX Fans s.r.o",
|
|
6
6
|
"license": "MIT",
|
|
@@ -12,18 +12,14 @@
|
|
|
12
12
|
"typecheck": "tsc --noEmit"
|
|
13
13
|
},
|
|
14
14
|
"peerDependencies": {
|
|
15
|
-
"@
|
|
16
|
-
"@dnd-kit/sortable": "^10.0.0",
|
|
17
|
-
"@uxf/core": "11.128.0",
|
|
15
|
+
"@uxf/core": "11.129.0",
|
|
18
16
|
"react": ">=18.2.0",
|
|
19
17
|
"react-dom": ">=18.2.0"
|
|
20
18
|
},
|
|
21
19
|
"devDependencies": {
|
|
22
|
-
"@dnd-kit/core": "^6.3.1",
|
|
23
|
-
"@dnd-kit/sortable": "^10.0.0",
|
|
24
20
|
"@types/react": "18.3.31",
|
|
25
21
|
"@types/react-dom": "18.3.7",
|
|
26
|
-
"@uxf/core": "11.
|
|
22
|
+
"@uxf/core": "11.129.0",
|
|
27
23
|
"react": "18.3.1",
|
|
28
24
|
"react-dom": "18.3.1"
|
|
29
25
|
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Active, DragStartEvent } from "@dnd-kit/core";
|
|
2
|
-
export declare const useSortableCore: () => {
|
|
3
|
-
activeElement: Active | null;
|
|
4
|
-
setActiveElement: import("react").Dispatch<import("react").SetStateAction<Active | null>>;
|
|
5
|
-
onDragStart: ({ active }: DragStartEvent) => void;
|
|
6
|
-
onDragCancel: () => void;
|
|
7
|
-
sensors: import("@dnd-kit/core").SensorDescriptor<import("@dnd-kit/core").SensorOptions>[];
|
|
8
|
-
dropAnimationConfig: import("@dnd-kit/core/dist/components/DragOverlay/hooks/useDropAnimation").DropAnimationOptions;
|
|
9
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useSortableCore = void 0;
|
|
4
|
-
const core_1 = require("@dnd-kit/core");
|
|
5
|
-
const sortable_1 = require("@dnd-kit/sortable");
|
|
6
|
-
const react_1 = require("react");
|
|
7
|
-
const useSortableCore = () => {
|
|
8
|
-
const [activeElement, setActiveElement] = (0, react_1.useState)(null);
|
|
9
|
-
const sensors = (0, core_1.useSensors)((0, core_1.useSensor)(core_1.PointerSensor), (0, core_1.useSensor)(core_1.KeyboardSensor, {
|
|
10
|
-
coordinateGetter: sortable_1.sortableKeyboardCoordinates,
|
|
11
|
-
}));
|
|
12
|
-
const dropAnimationConfig = {
|
|
13
|
-
sideEffects: (0, core_1.defaultDropAnimationSideEffects)({
|
|
14
|
-
styles: { active: { opacity: "0.4" } },
|
|
15
|
-
}),
|
|
16
|
-
};
|
|
17
|
-
const onDragStart = (0, react_1.useCallback)(({ active }) => {
|
|
18
|
-
setActiveElement(active);
|
|
19
|
-
}, []);
|
|
20
|
-
const onDragCancel = (0, react_1.useCallback)(() => {
|
|
21
|
-
setActiveElement(null);
|
|
22
|
-
}, []);
|
|
23
|
-
return {
|
|
24
|
-
activeElement,
|
|
25
|
-
setActiveElement,
|
|
26
|
-
onDragStart,
|
|
27
|
-
onDragCancel,
|
|
28
|
-
sensors,
|
|
29
|
-
dropAnimationConfig,
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
exports.useSortableCore = useSortableCore;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { DragEndEvent, DragStartEvent } from "@dnd-kit/core";
|
|
2
|
-
type Props = {
|
|
3
|
-
items: any[];
|
|
4
|
-
handleDragEnd: (activeIndex: number, overIndex: number) => void;
|
|
5
|
-
};
|
|
6
|
-
export declare const useSortableItems: ({ items, handleDragEnd }: Props) => {
|
|
7
|
-
memoizedSensors: import("@dnd-kit/core").SensorDescriptor<import("@dnd-kit/core").SensorOptions>[];
|
|
8
|
-
dropAnimationConfig: import("@dnd-kit/core/dist/components/DragOverlay/hooks/useDropAnimation").DropAnimationOptions;
|
|
9
|
-
itemsIds: any[];
|
|
10
|
-
activeItem: any;
|
|
11
|
-
onDragStart: ({ active }: DragStartEvent) => void;
|
|
12
|
-
onDragEnd: ({ active, over }: DragEndEvent) => void;
|
|
13
|
-
onDragCancel: () => void;
|
|
14
|
-
};
|
|
15
|
-
export {};
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useSortableItems = void 0;
|
|
4
|
-
const core_1 = require("@dnd-kit/core");
|
|
5
|
-
const sortable_1 = require("@dnd-kit/sortable");
|
|
6
|
-
const react_1 = require("react");
|
|
7
|
-
const useSortableItems = ({ items, handleDragEnd }) => {
|
|
8
|
-
const [sortableItems, setSortableItems] = (0, react_1.useState)(items);
|
|
9
|
-
const [activeElement, setActiveElement] = (0, react_1.useState)(null);
|
|
10
|
-
const activeItem = (0, react_1.useMemo)(() => sortableItems.find((item) => item.id === (activeElement === null || activeElement === void 0 ? void 0 : activeElement.id)), [activeElement, sortableItems]);
|
|
11
|
-
const sensors = (0, core_1.useSensors)((0, core_1.useSensor)(core_1.PointerSensor), (0, core_1.useSensor)(core_1.KeyboardSensor, {
|
|
12
|
-
coordinateGetter: sortable_1.sortableKeyboardCoordinates,
|
|
13
|
-
}));
|
|
14
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
15
|
-
const memoizedSensors = (0, react_1.useMemo)(() => sensors, []);
|
|
16
|
-
const idsStringified = JSON.stringify(sortableItems.map((i) => i.id));
|
|
17
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
18
|
-
const itemsIds = (0, react_1.useMemo)(() => sortableItems.map((i) => i.id), [idsStringified]);
|
|
19
|
-
(0, react_1.useEffect)(() => {
|
|
20
|
-
setSortableItems(items);
|
|
21
|
-
}, [items]);
|
|
22
|
-
const dropAnimationConfig = {
|
|
23
|
-
sideEffects: (0, core_1.defaultDropAnimationSideEffects)({
|
|
24
|
-
styles: {
|
|
25
|
-
active: {
|
|
26
|
-
opacity: "0.4",
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
}),
|
|
30
|
-
};
|
|
31
|
-
const onDragStart = (0, react_1.useCallback)(({ active }) => {
|
|
32
|
-
setActiveElement(active);
|
|
33
|
-
}, [setActiveElement]);
|
|
34
|
-
const onDragEnd = (0, react_1.useCallback)(({ active, over }) => {
|
|
35
|
-
if (over && active.id !== over.id) {
|
|
36
|
-
const activeIndex = sortableItems.findIndex(({ id }) => id === active.id);
|
|
37
|
-
const overIndex = sortableItems.findIndex(({ id }) => id === over.id);
|
|
38
|
-
const resorted = (0, sortable_1.arrayMove)(sortableItems, activeIndex, overIndex);
|
|
39
|
-
handleDragEnd(activeIndex, overIndex);
|
|
40
|
-
setSortableItems(resorted);
|
|
41
|
-
}
|
|
42
|
-
setActiveElement(null);
|
|
43
|
-
}, [setActiveElement, setSortableItems, handleDragEnd, sortableItems]);
|
|
44
|
-
const onDragCancel = (0, react_1.useCallback)(() => {
|
|
45
|
-
setActiveElement(null);
|
|
46
|
-
}, [setActiveElement]);
|
|
47
|
-
return {
|
|
48
|
-
memoizedSensors,
|
|
49
|
-
dropAnimationConfig,
|
|
50
|
-
itemsIds,
|
|
51
|
-
activeItem,
|
|
52
|
-
onDragStart,
|
|
53
|
-
onDragEnd,
|
|
54
|
-
onDragCancel,
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
exports.useSortableItems = useSortableItems;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { DragEndEvent } from "@dnd-kit/core";
|
|
2
|
-
export declare const EMPTY_VISIBLE_COLUMNS_PLACEHOLDER_ID = "__empty-visible-placeholder__";
|
|
3
|
-
export declare const EMPTY_HIDDEN_COLUMNS_PLACEHOLDER_ID = "__empty-hidden-placeholder__";
|
|
4
|
-
export declare function useSortableMulti<T extends {
|
|
5
|
-
id: string;
|
|
6
|
-
}, SectionMap extends Record<string, T[]>>(sections: SectionMap, setSections: (next: SectionMap) => void, onMove?: (source: keyof SectionMap, destination: keyof SectionMap, fromIndex: number, toIndex: number, nextState: SectionMap) => void): {
|
|
7
|
-
getSectionItemsIds: (section: keyof SectionMap) => string[];
|
|
8
|
-
activeItem: T | null;
|
|
9
|
-
onDragStart: ({ active }: import("@dnd-kit/core").DragStartEvent) => void;
|
|
10
|
-
onDragEnd: ({ active, over }: DragEndEvent) => void;
|
|
11
|
-
onDragCancel: () => void;
|
|
12
|
-
sensors: import("@dnd-kit/core").SensorDescriptor<import("@dnd-kit/core").SensorOptions>[];
|
|
13
|
-
dropAnimationConfig: import("@dnd-kit/core/dist/components/DragOverlay/hooks/useDropAnimation").DropAnimationOptions;
|
|
14
|
-
};
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EMPTY_HIDDEN_COLUMNS_PLACEHOLDER_ID = exports.EMPTY_VISIBLE_COLUMNS_PLACEHOLDER_ID = void 0;
|
|
4
|
-
exports.useSortableMulti = useSortableMulti;
|
|
5
|
-
const sortable_1 = require("@dnd-kit/sortable");
|
|
6
|
-
const react_1 = require("react");
|
|
7
|
-
const use_sortable_core_1 = require("./use-sortable-core");
|
|
8
|
-
exports.EMPTY_VISIBLE_COLUMNS_PLACEHOLDER_ID = "__empty-visible-placeholder__";
|
|
9
|
-
exports.EMPTY_HIDDEN_COLUMNS_PLACEHOLDER_ID = "__empty-hidden-placeholder__";
|
|
10
|
-
function useSortableMulti(sections, setSections, onMove) {
|
|
11
|
-
const { activeElement, setActiveElement, onDragStart, onDragCancel, sensors, dropAnimationConfig } = (0, use_sortable_core_1.useSortableCore)();
|
|
12
|
-
const activeItem = (0, react_1.useMemo)(() => {
|
|
13
|
-
if (!(activeElement === null || activeElement === void 0 ? void 0 : activeElement.id)) {
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
16
|
-
for (const items of Object.values(sections)) {
|
|
17
|
-
const item = items.find((i) => i.id === activeElement.id);
|
|
18
|
-
if (item) {
|
|
19
|
-
return item;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return null;
|
|
23
|
-
}, [activeElement, sections]);
|
|
24
|
-
const onDragEnd = (0, react_1.useCallback)(({ active, over }) => {
|
|
25
|
-
if (!over || active.id === over.id) {
|
|
26
|
-
setActiveElement(null);
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const isDroppingToEmptyPlaceholder = over.id === exports.EMPTY_HIDDEN_COLUMNS_PLACEHOLDER_ID || over.id === exports.EMPTY_VISIBLE_COLUMNS_PLACEHOLDER_ID;
|
|
30
|
-
let sourceKey = null;
|
|
31
|
-
let destKey = null;
|
|
32
|
-
// Determine source key by checking where the active item currently is
|
|
33
|
-
for (const key in sections) {
|
|
34
|
-
if (sections[key].find((item) => item.id === active.id)) {
|
|
35
|
-
sourceKey = key;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
// Determine destination key based on over.id
|
|
39
|
-
if (isDroppingToEmptyPlaceholder) {
|
|
40
|
-
destKey = over.id === exports.EMPTY_VISIBLE_COLUMNS_PLACEHOLDER_ID ? "visible" : "hidden";
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
for (const key in sections) {
|
|
44
|
-
if (sections[key].find((item) => item.id === over.id)) {
|
|
45
|
-
destKey = key;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
if (!sourceKey || !destKey) {
|
|
50
|
-
setActiveElement(null);
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
const sourceList = [...sections[sourceKey]];
|
|
54
|
-
const destList = [...sections[destKey]];
|
|
55
|
-
const fromIndex = sourceList.findIndex((item) => item.id === active.id);
|
|
56
|
-
let toIndex = destList.findIndex((item) => item.id === over.id);
|
|
57
|
-
// If dropping onto a placeholder or not found in list, append at end
|
|
58
|
-
if (toIndex === -1 || isDroppingToEmptyPlaceholder) {
|
|
59
|
-
toIndex = destList.length;
|
|
60
|
-
}
|
|
61
|
-
const nextState = { ...sections };
|
|
62
|
-
if (sourceKey === destKey) {
|
|
63
|
-
nextState[sourceKey] = (0, sortable_1.arrayMove)(sourceList, fromIndex, toIndex);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
const [moved] = sourceList.splice(fromIndex, 1);
|
|
67
|
-
destList.splice(toIndex, 0, moved);
|
|
68
|
-
nextState[sourceKey] = sourceList;
|
|
69
|
-
nextState[destKey] = destList;
|
|
70
|
-
}
|
|
71
|
-
setSections(nextState);
|
|
72
|
-
onMove === null || onMove === void 0 ? void 0 : onMove(sourceKey, destKey, fromIndex, toIndex, nextState);
|
|
73
|
-
setActiveElement(null);
|
|
74
|
-
}, [sections, setSections, onMove, setActiveElement]);
|
|
75
|
-
const getSectionItemsIds = (0, react_1.useCallback)((section) => {
|
|
76
|
-
const ids = sections[section].map((i) => i.id);
|
|
77
|
-
if (ids.length === 0) {
|
|
78
|
-
return [
|
|
79
|
-
section === "visible" ? exports.EMPTY_VISIBLE_COLUMNS_PLACEHOLDER_ID : exports.EMPTY_HIDDEN_COLUMNS_PLACEHOLDER_ID,
|
|
80
|
-
];
|
|
81
|
-
}
|
|
82
|
-
return ids;
|
|
83
|
-
}, [sections]);
|
|
84
|
-
return {
|
|
85
|
-
getSectionItemsIds,
|
|
86
|
-
activeItem,
|
|
87
|
-
onDragStart,
|
|
88
|
-
onDragEnd,
|
|
89
|
-
onDragCancel,
|
|
90
|
-
sensors,
|
|
91
|
-
dropAnimationConfig,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { DragEndEvent } from "@dnd-kit/core";
|
|
2
|
-
export declare const useSortableSingle: <T extends {
|
|
3
|
-
id: string;
|
|
4
|
-
}>(initialItems: T[], onReorder?: (fromIndex: number, toIndex: number, updatedList: T[]) => void) => {
|
|
5
|
-
items: T[];
|
|
6
|
-
setItems: import("react").Dispatch<import("react").SetStateAction<T[]>>;
|
|
7
|
-
activeItem: T | null;
|
|
8
|
-
onDragStart: ({ active }: import("@dnd-kit/core").DragStartEvent) => void;
|
|
9
|
-
onDragEnd: ({ active, over }: DragEndEvent) => void;
|
|
10
|
-
onDragCancel: () => void;
|
|
11
|
-
sensors: import("@dnd-kit/core").SensorDescriptor<import("@dnd-kit/core").SensorOptions>[];
|
|
12
|
-
dropAnimationConfig: import("@dnd-kit/core/dist/components/DragOverlay/hooks/useDropAnimation").DropAnimationOptions;
|
|
13
|
-
};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useSortableSingle = void 0;
|
|
4
|
-
const sortable_1 = require("@dnd-kit/sortable");
|
|
5
|
-
const react_1 = require("react");
|
|
6
|
-
const use_sortable_core_1 = require("./use-sortable-core");
|
|
7
|
-
const useSortableSingle = (initialItems, onReorder) => {
|
|
8
|
-
const [items, setItems] = (0, react_1.useState)(initialItems);
|
|
9
|
-
const { activeElement, setActiveElement, onDragStart, onDragCancel, sensors, dropAnimationConfig } = (0, use_sortable_core_1.useSortableCore)();
|
|
10
|
-
(0, react_1.useEffect)(() => {
|
|
11
|
-
setItems(initialItems);
|
|
12
|
-
}, [initialItems]);
|
|
13
|
-
const activeItem = (0, react_1.useMemo)(() => { var _a; return (_a = items.find((item) => item.id === (activeElement === null || activeElement === void 0 ? void 0 : activeElement.id))) !== null && _a !== void 0 ? _a : null; }, [activeElement, items]);
|
|
14
|
-
const onDragEnd = (0, react_1.useCallback)(({ active, over }) => {
|
|
15
|
-
if (!over || active.id === over.id) {
|
|
16
|
-
setActiveElement(null);
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
const fromIndex = items.findIndex((i) => i.id === active.id);
|
|
20
|
-
const toIndex = items.findIndex((i) => i.id === over.id);
|
|
21
|
-
const reordered = (0, sortable_1.arrayMove)(items, fromIndex, toIndex);
|
|
22
|
-
setItems(reordered);
|
|
23
|
-
onReorder === null || onReorder === void 0 ? void 0 : onReorder(fromIndex, toIndex, reordered);
|
|
24
|
-
setActiveElement(null);
|
|
25
|
-
}, [items, setActiveElement, onReorder]);
|
|
26
|
-
return {
|
|
27
|
-
items,
|
|
28
|
-
setItems,
|
|
29
|
-
activeItem,
|
|
30
|
-
onDragStart,
|
|
31
|
-
onDragEnd,
|
|
32
|
-
onDragCancel,
|
|
33
|
-
sensors,
|
|
34
|
-
dropAnimationConfig,
|
|
35
|
-
};
|
|
36
|
-
};
|
|
37
|
-
exports.useSortableSingle = useSortableSingle;
|