@mbao01/common 0.0.44 → 0.0.46
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/types/components/Chart/Chart.d.ts +2 -0
- package/dist/types/components/Chart/ChartContext.d.ts +2 -0
- package/dist/types/components/Chart/components/ChartLegend.d.ts +4 -0
- package/dist/types/components/Chart/components/ChartStyle.d.ts +2 -0
- package/dist/types/components/Chart/components/ChartTooltip.d.ts +4 -0
- package/dist/types/components/Chart/components/index.d.ts +3 -0
- package/dist/types/components/Chart/constants.d.ts +22 -0
- package/dist/types/components/Chart/helpers.d.ts +11 -0
- package/dist/types/components/Chart/hooks/index.d.ts +1 -0
- package/dist/types/components/Chart/hooks/useChart/index.d.ts +1 -0
- package/dist/types/components/Chart/hooks/useChart/useChart.d.ts +1 -0
- package/dist/types/components/Chart/index.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/activeShapeArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/animationArgs.d.ts +12 -0
- package/dist/types/components/Chart/stories/args/areaArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/areaChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/barArgs.d.ts +5 -0
- package/dist/types/components/Chart/stories/args/barChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/cartesianChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/cartesianSharedArgs.d.ts +11 -0
- package/dist/types/components/Chart/stories/args/chartArgs.d.ts +6 -0
- package/dist/types/components/Chart/stories/args/dotArgs.d.ts +10 -0
- package/dist/types/components/Chart/stories/args/events.d.ts +804 -0
- package/dist/types/components/Chart/stories/args/labelListArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/legendArgs.d.ts +3 -0
- package/dist/types/components/Chart/stories/args/lineArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/lineChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/pieArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/pieChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/polarChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/polarSharedArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radarArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radarChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radialBarArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/radialBarChartArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/rectangleArgs.d.ts +9 -0
- package/dist/types/components/Chart/stories/args/referenceSharedArgs.d.ts +4 -0
- package/dist/types/components/Chart/stories/args/sectorArgs.d.ts +7 -0
- package/dist/types/components/Chart/stories/args/sharedAxisArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/stylesArgs.d.ts +6 -0
- package/dist/types/components/Chart/stories/args/textArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/tooltipArgs.d.ts +4 -0
- package/dist/types/components/Chart/stories/args/types.d.ts +13 -0
- package/dist/types/components/Chart/stories/args/xAxisArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/args/yAxisArgs.d.ts +2 -0
- package/dist/types/components/Chart/stories/examples/AreaChart.d.ts +16 -0
- package/dist/types/components/Chart/stories/examples/BarChart.d.ts +20 -0
- package/dist/types/components/Chart/stories/examples/LineChart.d.ts +16 -0
- package/dist/types/components/Chart/stories/examples/PieChart.d.ts +19 -0
- package/dist/types/components/Chart/stories/examples/RadarChart.d.ts +24 -0
- package/dist/types/components/Chart/stories/examples/RadialChart.d.ts +17 -0
- package/dist/types/components/Chart/stories/examples/Tooltip.d.ts +15 -0
- package/dist/types/components/Chart/stories/helpers/index.d.ts +9 -0
- package/dist/types/components/Chart/types.d.ts +37 -0
- package/dist/types/components/DragAndDrop/Draggable/Draggable.d.ts +29 -0
- package/dist/types/components/DragAndDrop/Draggable/Draggable.example.d.ts +24 -0
- package/dist/types/components/DragAndDrop/Draggable/constants.d.ts +9 -0
- package/dist/types/components/DragAndDrop/Draggable/index.d.ts +1 -0
- package/dist/types/components/DragAndDrop/Draggable/types.d.ts +41 -0
- package/dist/types/components/DragAndDrop/Droppable/Droppable.d.ts +2 -0
- package/dist/types/components/DragAndDrop/Droppable/Droppable.example.d.ts +10 -0
- package/dist/types/components/DragAndDrop/Droppable/constants.d.ts +5 -0
- package/dist/types/components/DragAndDrop/Droppable/index.d.ts +1 -0
- package/dist/types/components/DragAndDrop/Droppable/types.d.ts +4 -0
- package/dist/types/components/DragAndDrop/Sortable/Sortable.d.ts +2 -0
- package/dist/types/components/DragAndDrop/Sortable/Sortable.example.d.ts +6 -0
- package/dist/types/components/DragAndDrop/Sortable/constants.d.ts +4 -0
- package/dist/types/components/DragAndDrop/Sortable/index.d.ts +1 -0
- package/dist/types/components/DragAndDrop/Sortable/types.d.ts +7 -0
- package/dist/types/components/DragAndDrop/index.d.ts +3 -0
- package/dist/types/components/Widget/InternalWidgetsContext.d.ts +2 -0
- package/dist/types/components/Widget/Widget.d.ts +2 -0
- package/dist/types/components/Widget/Widgets.example.d.ts +1 -0
- package/dist/types/components/Widget/WidgetsContext.d.ts +2 -0
- package/dist/types/components/Widget/hooks/index.d.ts +2 -0
- package/dist/types/components/Widget/hooks/useWidget/index.d.ts +1 -0
- package/dist/types/components/Widget/hooks/useWidget/useWidget.d.ts +7 -0
- package/dist/types/components/Widget/hooks/useWidgets/index.d.ts +1 -0
- package/dist/types/components/Widget/hooks/useWidgets/useWidgets.d.ts +1 -0
- package/dist/types/components/Widget/index.d.ts +3 -0
- package/dist/types/components/Widget/modifiers/index.d.ts +1 -0
- package/dist/types/components/Widget/modifiers/restrictToElement.d.ts +2 -0
- package/dist/types/components/Widget/types.d.ts +28 -0
- package/package.json +9 -3
- package/src/components/Chart/Chart.tsx +26 -0
- package/src/components/Chart/ChartContext.tsx +4 -0
- package/src/components/Chart/components/ChartLegend.tsx +52 -0
- package/src/components/Chart/components/ChartStyle.tsx +32 -0
- package/src/components/Chart/components/ChartTooltip.tsx +126 -0
- package/src/components/Chart/components/index.ts +3 -0
- package/src/components/Chart/constants.ts +78 -0
- package/src/components/Chart/helpers.ts +27 -0
- package/src/components/Chart/hooks/index.ts +1 -0
- package/src/components/Chart/hooks/useChart/index.ts +1 -0
- package/src/components/Chart/hooks/useChart/useChart.ts +12 -0
- package/src/components/Chart/index.ts +2 -0
- package/src/components/Chart/stories/args/activeShapeArgs.ts +34 -0
- package/src/components/Chart/stories/args/animationArgs.ts +67 -0
- package/src/components/Chart/stories/args/areaArgs.ts +16 -0
- package/src/components/Chart/stories/args/areaChartArgs.ts +19 -0
- package/src/components/Chart/stories/args/barArgs.ts +123 -0
- package/src/components/Chart/stories/args/barChartArgs.ts +65 -0
- package/src/components/Chart/stories/args/cartesianChartArgs.ts +42 -0
- package/src/components/Chart/stories/args/cartesianSharedArgs.ts +136 -0
- package/src/components/Chart/stories/args/chartArgs.ts +244 -0
- package/src/components/Chart/stories/args/dotArgs.ts +46 -0
- package/src/components/Chart/stories/args/events.ts +343 -0
- package/src/components/Chart/stories/args/index.ts +178 -0
- package/src/components/Chart/stories/args/labelListArgs.ts +124 -0
- package/src/components/Chart/stories/args/legendArgs.ts +205 -0
- package/src/components/Chart/stories/args/lineArgs.ts +87 -0
- package/src/components/Chart/stories/args/lineChartArgs.ts +17 -0
- package/src/components/Chart/stories/args/pieArgs.ts +18 -0
- package/src/components/Chart/stories/args/pieChartArgs.ts +7 -0
- package/src/components/Chart/stories/args/polarChartArgs.ts +86 -0
- package/src/components/Chart/stories/args/polarSharedArgs.ts +42 -0
- package/src/components/Chart/stories/args/radarArgs.ts +26 -0
- package/src/components/Chart/stories/args/radarChartArgs.ts +26 -0
- package/src/components/Chart/stories/args/radialBarArgs.ts +36 -0
- package/src/components/Chart/stories/args/radialBarChartArgs.ts +16 -0
- package/src/components/Chart/stories/args/rectangleArgs.ts +35 -0
- package/src/components/Chart/stories/args/referenceSharedArgs.ts +75 -0
- package/src/components/Chart/stories/args/sectorArgs.ts +106 -0
- package/src/components/Chart/stories/args/sharedAxisArgs.ts +332 -0
- package/src/components/Chart/stories/args/stylesArgs.ts +258 -0
- package/src/components/Chart/stories/args/textArgs.ts +97 -0
- package/src/components/Chart/stories/args/tooltipArgs.ts +41 -0
- package/src/components/Chart/stories/args/types.ts +23 -0
- package/src/components/Chart/stories/args/xAxisArgs.ts +109 -0
- package/src/components/Chart/stories/args/yAxisArgs.ts +73 -0
- package/src/components/Chart/stories/examples/AreaChart.tsx +156 -0
- package/src/components/Chart/stories/examples/BarChart.tsx +425 -0
- package/src/components/Chart/stories/examples/LineChart.tsx +144 -0
- package/src/components/Chart/stories/examples/PieChart.tsx +238 -0
- package/src/components/Chart/stories/examples/RadarChart.tsx +261 -0
- package/src/components/Chart/stories/examples/RadialChart.tsx +239 -0
- package/src/components/Chart/stories/examples/Tooltip.tsx +68 -0
- package/src/components/Chart/stories/helpers/index.tsx +75 -0
- package/src/components/Chart/types.ts +39 -0
- package/src/components/DragAndDrop/Draggable/Draggable.example.tsx +147 -0
- package/src/components/DragAndDrop/Draggable/Draggable.tsx +161 -0
- package/src/components/DragAndDrop/Draggable/constants.ts +47 -0
- package/src/components/DragAndDrop/Draggable/index.ts +1 -0
- package/src/components/DragAndDrop/Draggable/types.ts +56 -0
- package/src/components/DragAndDrop/Droppable/Droppable.example.tsx +86 -0
- package/src/components/DragAndDrop/Droppable/Droppable.tsx +38 -0
- package/src/components/DragAndDrop/Droppable/constants.ts +15 -0
- package/src/components/DragAndDrop/Droppable/index.ts +1 -0
- package/src/components/DragAndDrop/Droppable/types.ts +7 -0
- package/src/components/DragAndDrop/Sortable/Sortable.example.tsx +61 -0
- package/src/components/DragAndDrop/Sortable/Sortable.tsx +65 -0
- package/src/components/DragAndDrop/Sortable/constants.ts +12 -0
- package/src/components/DragAndDrop/Sortable/index.ts +1 -0
- package/src/components/DragAndDrop/Sortable/types.ts +11 -0
- package/src/components/DragAndDrop/index.ts +3 -0
- package/src/components/Widget/InternalWidgetsContext.tsx +4 -0
- package/src/components/Widget/Widget.tsx +17 -0
- package/src/components/Widget/Widgets.example.tsx +118 -0
- package/src/components/Widget/WidgetsContext.tsx +97 -0
- package/src/components/Widget/hooks/index.ts +2 -0
- package/src/components/Widget/hooks/useWidget/index.ts +1 -0
- package/src/components/Widget/hooks/useWidget/useWidget.ts +21 -0
- package/src/components/Widget/hooks/useWidgets/index.ts +1 -0
- package/src/components/Widget/hooks/useWidgets/useWidgets.ts +12 -0
- package/src/components/Widget/index.ts +3 -0
- package/src/components/Widget/modifiers/index.ts +1 -0
- package/src/components/Widget/modifiers/restrictToElement.ts +8 -0
- package/src/components/Widget/types.ts +30 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { MoveIcon, TrashIcon } from "@radix-ui/react-icons";
|
|
3
|
+
import { Draggable } from "../DragAndDrop";
|
|
4
|
+
import { useWidgets } from "./hooks/useWidgets/useWidgets";
|
|
5
|
+
import { Widget } from "./Widget";
|
|
6
|
+
import { WidgetsContext } from "./WidgetsContext";
|
|
7
|
+
|
|
8
|
+
export const WidgetsExample = () => {
|
|
9
|
+
return (
|
|
10
|
+
<WidgetsContext
|
|
11
|
+
initialWidgets={[
|
|
12
|
+
{ id: "A" },
|
|
13
|
+
{ id: "B" },
|
|
14
|
+
{ id: "C" },
|
|
15
|
+
{ id: "D" },
|
|
16
|
+
{ id: "E" },
|
|
17
|
+
{ id: "F" },
|
|
18
|
+
{ id: "G" },
|
|
19
|
+
]}
|
|
20
|
+
>
|
|
21
|
+
<Widgets />
|
|
22
|
+
</WidgetsContext>
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const Widgets = () => {
|
|
27
|
+
const [widgetId, setWidgetId] = useState<string>("");
|
|
28
|
+
const [insertionIndex, setInsertionIndex] = useState<number>();
|
|
29
|
+
const { widgets, addWidget, addWidgets, deleteWidget, deleteWidgets, resetWidgets } =
|
|
30
|
+
useWidgets();
|
|
31
|
+
|
|
32
|
+
const widgetIds = widgetId.split(",").map((id) => ({
|
|
33
|
+
id,
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div className="relative h-[calc(100dvh-50px)] border border-base-200 rounded-md p-6 overflow-y-auto">
|
|
38
|
+
<div className="flex flex-col gap-2 mb-6">
|
|
39
|
+
<span className="text-sm">Controls:</span>
|
|
40
|
+
<div className="flex gap-2">
|
|
41
|
+
<input
|
|
42
|
+
type="text"
|
|
43
|
+
placeholder="Enter Widget ID"
|
|
44
|
+
className="input input-xs input-bordered rounded-md"
|
|
45
|
+
value={widgetId}
|
|
46
|
+
onChange={(e) => setWidgetId(e.target.value)}
|
|
47
|
+
/>
|
|
48
|
+
<input
|
|
49
|
+
type="number"
|
|
50
|
+
placeholder="Enter Insertion Index"
|
|
51
|
+
className="input input-xs input-bordered rounded-md"
|
|
52
|
+
value={insertionIndex}
|
|
53
|
+
onChange={(e) => setInsertionIndex(Number(e.target.value))}
|
|
54
|
+
/>
|
|
55
|
+
</div>
|
|
56
|
+
<div className="flex gap-2">
|
|
57
|
+
<button
|
|
58
|
+
className="btn btn-outline btn-xs btn-success"
|
|
59
|
+
onClick={() => addWidget(widgetIds[0], insertionIndex)}
|
|
60
|
+
>
|
|
61
|
+
Add widget
|
|
62
|
+
</button>
|
|
63
|
+
<button
|
|
64
|
+
className="btn btn-outline btn-xs btn-info"
|
|
65
|
+
onClick={() => addWidgets(widgetIds, insertionIndex)}
|
|
66
|
+
>
|
|
67
|
+
Add widgets
|
|
68
|
+
</button>
|
|
69
|
+
<button
|
|
70
|
+
className="btn btn-outline btn-xs btn-warning"
|
|
71
|
+
onClick={() => deleteWidget(widgetIds[0])}
|
|
72
|
+
>
|
|
73
|
+
Delete widget
|
|
74
|
+
</button>
|
|
75
|
+
<button
|
|
76
|
+
className="btn btn-outline btn-xs btn-error"
|
|
77
|
+
onClick={() => deleteWidgets(widgetIds)}
|
|
78
|
+
>
|
|
79
|
+
Delete widgets
|
|
80
|
+
</button>
|
|
81
|
+
<button className="btn btn-outline btn-xs" onClick={() => resetWidgets()}>
|
|
82
|
+
Reset widgets
|
|
83
|
+
</button>
|
|
84
|
+
</div>
|
|
85
|
+
</div>
|
|
86
|
+
<div className="grid gap-4 grid-cols-3 w-fit h-fit">
|
|
87
|
+
{widgets.map((widget) => (
|
|
88
|
+
<Widget
|
|
89
|
+
key={widget.id}
|
|
90
|
+
id={widget.id}
|
|
91
|
+
actions={({ draggable, deleteWidget }) => (
|
|
92
|
+
<div className="absolute top-1 right-1 opacity-0 flex gap-1 [&>*]:cursor-pointer [&_svg]:size-4 pointer-events-none transition-opacity duration-300 hover:opacity-100 group-hover:pointer-events-auto">
|
|
93
|
+
<Draggable.Action
|
|
94
|
+
ref={draggable?.ref}
|
|
95
|
+
{...draggable?.listeners}
|
|
96
|
+
aria-label={`Drag widget ${widget.id}`}
|
|
97
|
+
className="transition-all hover:text-primary active:cursor-grabbing"
|
|
98
|
+
>
|
|
99
|
+
<MoveIcon />
|
|
100
|
+
</Draggable.Action>
|
|
101
|
+
<Draggable.Action
|
|
102
|
+
onClick={deleteWidget}
|
|
103
|
+
aria-label={`Delete widget ${widget.id}`}
|
|
104
|
+
className="transition-all hover:text-error"
|
|
105
|
+
>
|
|
106
|
+
<TrashIcon />
|
|
107
|
+
</Draggable.Action>
|
|
108
|
+
</div>
|
|
109
|
+
)}
|
|
110
|
+
className="group flex items-center justify-center w-32 h-32 bg-base-100 border border-primary-content/30 rounded-md text-sm p-2 cursor-default data-[draggable]:shadow data-[draggable-active]:z-20"
|
|
111
|
+
>
|
|
112
|
+
{widget.id}
|
|
113
|
+
</Widget>
|
|
114
|
+
))}
|
|
115
|
+
</div>
|
|
116
|
+
</div>
|
|
117
|
+
);
|
|
118
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { useRef, useState } from "react";
|
|
2
|
+
import { closestCenter, DndContext } from "@dnd-kit/core";
|
|
3
|
+
import { arrayMove, SortableContext } from "@dnd-kit/sortable";
|
|
4
|
+
import { InternalWidgetsContext } from "./InternalWidgetsContext";
|
|
5
|
+
import { restrictToElement } from "./modifiers";
|
|
6
|
+
import { Widget, WidgetsContextProps } from "./types";
|
|
7
|
+
|
|
8
|
+
export const WidgetsContext = ({ children, initialWidgets }: WidgetsContextProps) => {
|
|
9
|
+
const [activeId, setActiveId] = useState<Widget["id"] | null>(null);
|
|
10
|
+
const [widgets, setWidgets] = useState<Widget[]>(initialWidgets ?? []);
|
|
11
|
+
const boundingRef = useRef<HTMLDivElement>(null);
|
|
12
|
+
const boundingRect = boundingRef.current?.getBoundingClientRect();
|
|
13
|
+
|
|
14
|
+
const activeIndex = activeId ? widgets.findIndex((item) => item.id === activeId) : -1;
|
|
15
|
+
|
|
16
|
+
const addWidget = (widget: Widget, insertIndex?: number) => {
|
|
17
|
+
const isValid = Boolean(widget.id);
|
|
18
|
+
const isExist = widgets.some((w) => w.id === widget.id);
|
|
19
|
+
|
|
20
|
+
if (!isExist && isValid) {
|
|
21
|
+
// if there's no existing widget in the list, then add it at the given index or at the end of the list
|
|
22
|
+
setWidgets((widgets) => widgets.toSpliced(insertIndex ?? Infinity, 0, widget));
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const addWidgets = (_widgets: Widget[], startIndex?: number) => {
|
|
27
|
+
setWidgets((widgets) => {
|
|
28
|
+
// filter widgets that do NOT already exist do avoid duplicate IDs
|
|
29
|
+
const newWidgets = _widgets.filter(
|
|
30
|
+
(widget) => widget.id && !widgets.some((w) => w.id === widget.id)
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
// insert new widgets at the given start index or at the end of the list
|
|
34
|
+
return widgets.toSpliced(startIndex ?? Infinity, 0, ...newWidgets);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const deleteWidget = (widget: Widget) => {
|
|
39
|
+
const widgetIndex = widgets.findIndex((w) => w.id === widget.id);
|
|
40
|
+
|
|
41
|
+
if (widgetIndex >= 0) {
|
|
42
|
+
// if the widget to be deleted exists in the list, then delete it.
|
|
43
|
+
setWidgets((widgets) => widgets.toSpliced(widgetIndex, 1));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const deleteWidgets = (_widgets: Widget[]) => {
|
|
48
|
+
setWidgets((widgets) => {
|
|
49
|
+
// filter widgets that do NOT exist in the deletion list
|
|
50
|
+
return widgets.filter((widget) => !_widgets.some((w) => w.id === widget.id));
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const resetWidgets = () => {
|
|
55
|
+
setWidgets(initialWidgets ?? []);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<DndContext
|
|
60
|
+
modifiers={[restrictToElement(boundingRect)]}
|
|
61
|
+
collisionDetection={closestCenter}
|
|
62
|
+
onDragStart={({ active }) => {
|
|
63
|
+
if (!active) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
setActiveId(active.id);
|
|
68
|
+
}}
|
|
69
|
+
onDragEnd={({ over }) => {
|
|
70
|
+
setActiveId(null);
|
|
71
|
+
|
|
72
|
+
if (over) {
|
|
73
|
+
const overIndex = widgets.findIndex((item) => item.id === over.id);
|
|
74
|
+
if (activeIndex !== overIndex) {
|
|
75
|
+
setWidgets((w) => arrayMove(w, activeIndex, overIndex));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}}
|
|
79
|
+
onDragCancel={() => setActiveId(null)}
|
|
80
|
+
>
|
|
81
|
+
<SortableContext items={widgets}>
|
|
82
|
+
<InternalWidgetsContext.Provider
|
|
83
|
+
value={{
|
|
84
|
+
widgets,
|
|
85
|
+
addWidget,
|
|
86
|
+
addWidgets,
|
|
87
|
+
deleteWidget,
|
|
88
|
+
deleteWidgets,
|
|
89
|
+
resetWidgets,
|
|
90
|
+
}}
|
|
91
|
+
>
|
|
92
|
+
<div ref={boundingRef}>{children}</div>
|
|
93
|
+
</InternalWidgetsContext.Provider>
|
|
94
|
+
</SortableContext>
|
|
95
|
+
</DndContext>
|
|
96
|
+
);
|
|
97
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useWidget } from "./useWidget";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { UniqueIdentifier } from "@dnd-kit/core";
|
|
3
|
+
import { InternalWidgetsContext } from "../../InternalWidgetsContext";
|
|
4
|
+
|
|
5
|
+
export const useWidget = (id: UniqueIdentifier) => {
|
|
6
|
+
const context = useContext(InternalWidgetsContext);
|
|
7
|
+
|
|
8
|
+
if (!context) {
|
|
9
|
+
throw new Error("useWidget must be used within a <WidgetsContext />");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const widget = context.widgets.find((widget) => widget.id === id);
|
|
13
|
+
|
|
14
|
+
const deleteWidget = () => {
|
|
15
|
+
if (widget) {
|
|
16
|
+
context.deleteWidget(widget);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return { widget, deleteWidget };
|
|
21
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useWidgets } from "./useWidgets";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { InternalWidgetsContext } from "../../InternalWidgetsContext";
|
|
3
|
+
|
|
4
|
+
export const useWidgets = () => {
|
|
5
|
+
const context = useContext(InternalWidgetsContext);
|
|
6
|
+
|
|
7
|
+
if (!context) {
|
|
8
|
+
throw new Error("useWidgets must be used within a <WidgetsContext />");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return context;
|
|
12
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { restrictToElement } from "./restrictToElement";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Modifier } from "@dnd-kit/core";
|
|
2
|
+
import { restrictToParentElement } from "@dnd-kit/modifiers";
|
|
3
|
+
|
|
4
|
+
export const restrictToElement = (rect: DOMRect | null | undefined) => {
|
|
5
|
+
const modifier: Modifier = (args) =>
|
|
6
|
+
restrictToParentElement({ ...args, containerNodeRect: rect ?? null });
|
|
7
|
+
return modifier;
|
|
8
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Data, UniqueIdentifier } from "@dnd-kit/core";
|
|
2
|
+
import { type ReactNode } from "react";
|
|
3
|
+
import { type DraggableActionsArgs } from "../DragAndDrop/Draggable/types";
|
|
4
|
+
import { type SortableProps } from "../DragAndDrop/Sortable/types";
|
|
5
|
+
|
|
6
|
+
export type Widget = Data<{ id: UniqueIdentifier }>;
|
|
7
|
+
|
|
8
|
+
type WidgetActions = (
|
|
9
|
+
args: Partial<DraggableActionsArgs> & {
|
|
10
|
+
deleteWidget: () => void;
|
|
11
|
+
}
|
|
12
|
+
) => ReactNode;
|
|
13
|
+
|
|
14
|
+
export type WidgetProps = Omit<SortableProps, "actions"> & {
|
|
15
|
+
actions?: WidgetActions;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type WidgetsContextProps = {
|
|
19
|
+
children?: ReactNode;
|
|
20
|
+
initialWidgets?: Widget[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type InternalWidgetsContextProps = {
|
|
24
|
+
widgets: Data<{ id: UniqueIdentifier }>[];
|
|
25
|
+
addWidget: (widget: Widget, insertIndex?: number) => void;
|
|
26
|
+
addWidgets: (widgets: Widget[], startIndex?: number) => void;
|
|
27
|
+
deleteWidget: (widget: Widget) => void;
|
|
28
|
+
deleteWidgets: (widgets: Widget[]) => void;
|
|
29
|
+
resetWidgets: () => void;
|
|
30
|
+
};
|