@etsoo/materialui 1.6.10 → 1.6.12
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/lib/cjs/ButtonPopupCheckbox.d.ts +3 -2
- package/lib/cjs/ButtonPopupCheckbox.js +6 -4
- package/lib/cjs/DnDSortableList.d.ts +105 -0
- package/lib/cjs/DnDSortableList.js +130 -0
- package/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +1 -1
- package/lib/mjs/ButtonPopupCheckbox.d.ts +3 -2
- package/lib/mjs/ButtonPopupCheckbox.js +6 -4
- package/lib/mjs/DnDSortableList.d.ts +105 -0
- package/lib/mjs/DnDSortableList.js +122 -0
- package/lib/mjs/index.d.ts +1 -1
- package/lib/mjs/index.js +1 -1
- package/package.json +13 -15
- package/src/ButtonPopupCheckbox.tsx +20 -10
- package/src/DnDSortableList.tsx +318 -0
- package/src/index.ts +1 -1
- package/lib/cjs/DnDList.d.ts +0 -111
- package/lib/cjs/DnDList.js +0 -238
- package/lib/mjs/DnDList.d.ts +0 -111
- package/lib/mjs/DnDList.js +0 -230
- package/src/DnDList.tsx +0 -528
package/lib/cjs/DnDList.js
DELETED
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.DnDItemStyle = void 0;
|
|
7
|
-
exports.DnDList = DnDList;
|
|
8
|
-
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
9
|
-
const Skeleton_1 = __importDefault(require("@mui/material/Skeleton"));
|
|
10
|
-
const styles_1 = require("@mui/material/styles");
|
|
11
|
-
const react_1 = __importDefault(require("react"));
|
|
12
|
-
function SortableItem(props) {
|
|
13
|
-
// Destruct
|
|
14
|
-
const { id, useSortableType, CSSType, itemRenderer, style = {} } = props;
|
|
15
|
-
// Use sortable
|
|
16
|
-
const { attributes, listeners, setNodeRef, transform, transition, setActivatorNodeRef } = useSortableType({ id });
|
|
17
|
-
const allStyle = {
|
|
18
|
-
...style,
|
|
19
|
-
transform: CSSType.Transform.toString(transform),
|
|
20
|
-
transition
|
|
21
|
-
};
|
|
22
|
-
const nodeRef = {
|
|
23
|
-
style: allStyle,
|
|
24
|
-
ref: setNodeRef,
|
|
25
|
-
...attributes
|
|
26
|
-
};
|
|
27
|
-
const actionNodeRef = {
|
|
28
|
-
...listeners,
|
|
29
|
-
ref: setActivatorNodeRef
|
|
30
|
-
};
|
|
31
|
-
return itemRenderer(nodeRef, actionNodeRef);
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* DnD item default style
|
|
35
|
-
* @param index Item index
|
|
36
|
-
* @param isDragging Is dragging
|
|
37
|
-
* @param theme Theme
|
|
38
|
-
* @returns Style
|
|
39
|
-
*/
|
|
40
|
-
const DnDItemStyle = (index, isDragging, theme) => ({
|
|
41
|
-
padding: theme.spacing(1),
|
|
42
|
-
zIndex: isDragging ? 1 : "auto",
|
|
43
|
-
background: isDragging
|
|
44
|
-
? theme.palette.primary.light
|
|
45
|
-
: index % 2 === 0
|
|
46
|
-
? theme.palette.grey[100]
|
|
47
|
-
: theme.palette.grey[50]
|
|
48
|
-
});
|
|
49
|
-
exports.DnDItemStyle = DnDItemStyle;
|
|
50
|
-
/**
|
|
51
|
-
* DnD (Drag and Drop) sortable list
|
|
52
|
-
* @param props Props
|
|
53
|
-
* @returns Component
|
|
54
|
-
*/
|
|
55
|
-
function DnDList(props) {
|
|
56
|
-
// Destruct
|
|
57
|
-
const { componentProps, height = 360, itemRenderer, labelField, mRef, sortingStrategy, onChange, onFormChange, onDragEnd } = props;
|
|
58
|
-
const Component = props.component || react_1.default.Fragment;
|
|
59
|
-
// Theme
|
|
60
|
-
const theme = (0, styles_1.useTheme)();
|
|
61
|
-
// States
|
|
62
|
-
const [items, setItems] = react_1.default.useState([]);
|
|
63
|
-
const [activeId, setActiveId] = react_1.default.useState();
|
|
64
|
-
react_1.default.useEffect(() => {
|
|
65
|
-
setItems(props.items);
|
|
66
|
-
}, [props.items]);
|
|
67
|
-
const doFormChange = react_1.default.useCallback((newItems) => {
|
|
68
|
-
if (onFormChange) {
|
|
69
|
-
const locals = Array.isArray(newItems) ? newItems : items;
|
|
70
|
-
onFormChange(locals);
|
|
71
|
-
}
|
|
72
|
-
}, [items, onFormChange]);
|
|
73
|
-
const changeItems = react_1.default.useCallback((newItems) => {
|
|
74
|
-
// Possible to alter items with the handler
|
|
75
|
-
if (onChange)
|
|
76
|
-
onChange(newItems);
|
|
77
|
-
doFormChange(newItems);
|
|
78
|
-
// Update state
|
|
79
|
-
setItems(newItems);
|
|
80
|
-
}, [onChange, doFormChange]);
|
|
81
|
-
// Methods
|
|
82
|
-
react_1.default.useImperativeHandle(mRef, () => {
|
|
83
|
-
return {
|
|
84
|
-
addItem(newItem) {
|
|
85
|
-
// Existence check
|
|
86
|
-
if (items.some((item) => item[labelField] === newItem[labelField])) {
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
// Clone
|
|
90
|
-
const newItems = [newItem, ...items];
|
|
91
|
-
// Update the state
|
|
92
|
-
changeItems(newItems);
|
|
93
|
-
return true;
|
|
94
|
-
},
|
|
95
|
-
addItems(inputItems) {
|
|
96
|
-
// Clone
|
|
97
|
-
const newItems = [...items];
|
|
98
|
-
// Insert items
|
|
99
|
-
inputItems.forEach((newItem) => {
|
|
100
|
-
// Existence check
|
|
101
|
-
if (newItems.some((item) => item[labelField] === newItem[labelField])) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
newItems.push(newItem);
|
|
105
|
-
});
|
|
106
|
-
// Update the state
|
|
107
|
-
changeItems(newItems);
|
|
108
|
-
return newItems.length - items.length;
|
|
109
|
-
},
|
|
110
|
-
editItem(newItem, index) {
|
|
111
|
-
// Existence check
|
|
112
|
-
const newIndex = items.findIndex((item) => item[labelField] === newItem[labelField]);
|
|
113
|
-
if (newIndex >= 0 && newIndex !== index) {
|
|
114
|
-
// Label field is the same with a different item
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
// Clone
|
|
118
|
-
const newItems = [...items];
|
|
119
|
-
// Remove the item
|
|
120
|
-
newItems.splice(index, 1, newItem);
|
|
121
|
-
// Update the state
|
|
122
|
-
changeItems(newItems);
|
|
123
|
-
return true;
|
|
124
|
-
},
|
|
125
|
-
deleteItem(index) {
|
|
126
|
-
// Clone
|
|
127
|
-
const newItems = [...items];
|
|
128
|
-
// Remove the item
|
|
129
|
-
newItems.splice(index, 1);
|
|
130
|
-
// Update the state
|
|
131
|
-
changeItems(newItems);
|
|
132
|
-
},
|
|
133
|
-
getItems() {
|
|
134
|
-
return items;
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
}, [items, labelField, changeItems]);
|
|
138
|
-
// Dynamic import library
|
|
139
|
-
const [dnd, setDnd] = react_1.default.useState();
|
|
140
|
-
react_1.default.useEffect(() => {
|
|
141
|
-
Promise.all([
|
|
142
|
-
import("@dnd-kit/core"),
|
|
143
|
-
import("@dnd-kit/sortable"),
|
|
144
|
-
import("@dnd-kit/utilities")
|
|
145
|
-
]).then(([{ DndContext }, { SortableContext, useSortable, rectSortingStrategy, rectSwappingStrategy, horizontalListSortingStrategy, verticalListSortingStrategy }, { CSS }]) => {
|
|
146
|
-
setDnd([
|
|
147
|
-
DndContext,
|
|
148
|
-
SortableContext,
|
|
149
|
-
useSortable,
|
|
150
|
-
rectSortingStrategy,
|
|
151
|
-
rectSwappingStrategy,
|
|
152
|
-
horizontalListSortingStrategy,
|
|
153
|
-
verticalListSortingStrategy,
|
|
154
|
-
CSS
|
|
155
|
-
]);
|
|
156
|
-
});
|
|
157
|
-
}, []);
|
|
158
|
-
const setupDiv = (div, clearup = false) => {
|
|
159
|
-
// Inputs
|
|
160
|
-
div
|
|
161
|
-
.querySelectorAll("input")
|
|
162
|
-
.forEach((input) => clearup
|
|
163
|
-
? input.removeEventListener("change", doFormChange)
|
|
164
|
-
: input.addEventListener("change", doFormChange));
|
|
165
|
-
// Textareas
|
|
166
|
-
div
|
|
167
|
-
.querySelectorAll("textarea")
|
|
168
|
-
.forEach((input) => clearup
|
|
169
|
-
? input.removeEventListener("change", doFormChange)
|
|
170
|
-
: input.addEventListener("change", doFormChange));
|
|
171
|
-
// Select
|
|
172
|
-
div
|
|
173
|
-
.querySelectorAll("select")
|
|
174
|
-
.forEach((input) => clearup
|
|
175
|
-
? input.removeEventListener("change", doFormChange)
|
|
176
|
-
: input.addEventListener("change", doFormChange));
|
|
177
|
-
};
|
|
178
|
-
const divRef = react_1.default.useRef(null);
|
|
179
|
-
if (dnd == null) {
|
|
180
|
-
return (0, jsx_runtime_1.jsx)(Skeleton_1.default, { variant: "rectangular", width: "100%", height: height });
|
|
181
|
-
}
|
|
182
|
-
const [DndContextType, SortableContextType, useSortableType, rectSortingStrategyType, rectSwappingStrategyType, horizontalListSortingStrategyType, verticalListSortingStrategyType, CSSType] = dnd;
|
|
183
|
-
const strategy = typeof sortingStrategy === "function"
|
|
184
|
-
? sortingStrategy()
|
|
185
|
-
: sortingStrategy === "rect"
|
|
186
|
-
? rectSortingStrategyType
|
|
187
|
-
: sortingStrategy === "rectSwapping"
|
|
188
|
-
? rectSwappingStrategyType
|
|
189
|
-
: sortingStrategy === "horizontal"
|
|
190
|
-
? horizontalListSortingStrategyType
|
|
191
|
-
: sortingStrategy === "vertical"
|
|
192
|
-
? verticalListSortingStrategyType
|
|
193
|
-
: undefined;
|
|
194
|
-
let getItemStyle = props.getItemStyle;
|
|
195
|
-
if (getItemStyle == null) {
|
|
196
|
-
getItemStyle = (index, isDragging) => (0, exports.DnDItemStyle)(index, isDragging, theme);
|
|
197
|
-
}
|
|
198
|
-
// Drag event handlers
|
|
199
|
-
function handleDragStart(event) {
|
|
200
|
-
const { active } = event;
|
|
201
|
-
setActiveId(active.id);
|
|
202
|
-
}
|
|
203
|
-
function handleDragEnd(event) {
|
|
204
|
-
const { active, over } = event;
|
|
205
|
-
if (over && active.id !== over.id) {
|
|
206
|
-
// Indices
|
|
207
|
-
const oldIndex = items.findIndex((item) => item.id === active.id);
|
|
208
|
-
const newIndex = items.findIndex((item) => item.id === over.id);
|
|
209
|
-
// Clone
|
|
210
|
-
const newItems = [...items];
|
|
211
|
-
// Removed item
|
|
212
|
-
const [removed] = newItems.splice(oldIndex, 1);
|
|
213
|
-
// Insert to the destination index
|
|
214
|
-
newItems.splice(newIndex, 0, removed);
|
|
215
|
-
changeItems(newItems);
|
|
216
|
-
// Drag end handler
|
|
217
|
-
if (onDragEnd)
|
|
218
|
-
onDragEnd(newItems);
|
|
219
|
-
}
|
|
220
|
-
setActiveId(undefined);
|
|
221
|
-
}
|
|
222
|
-
const children = ((0, jsx_runtime_1.jsx)(DndContextType, { onDragStart: handleDragStart, onDragEnd: handleDragEnd, children: (0, jsx_runtime_1.jsx)(SortableContextType, { items: items, strategy: strategy, children: (0, jsx_runtime_1.jsx)(Component, { ...componentProps, children: items.map((item, index) => {
|
|
223
|
-
const id = item.id;
|
|
224
|
-
return ((0, jsx_runtime_1.jsx)(SortableItem, { id: id, useSortableType: useSortableType, CSSType: CSSType, style: getItemStyle(index, id === activeId), itemRenderer: (nodeRef, actionNodeRef) => itemRenderer(item, index, nodeRef, actionNodeRef) }, id));
|
|
225
|
-
}) }) }) }));
|
|
226
|
-
if (onFormChange) {
|
|
227
|
-
return ((0, jsx_runtime_1.jsx)("div", { style: { width: "100%" }, ref: (div) => {
|
|
228
|
-
if (div && divRef.current != div) {
|
|
229
|
-
if (divRef.current) {
|
|
230
|
-
setupDiv(divRef.current, true);
|
|
231
|
-
}
|
|
232
|
-
divRef.current = div;
|
|
233
|
-
setupDiv(div);
|
|
234
|
-
}
|
|
235
|
-
}, children: children }));
|
|
236
|
-
}
|
|
237
|
-
return children;
|
|
238
|
-
}
|
package/lib/mjs/DnDList.d.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import type { UniqueIdentifier } from "@dnd-kit/core";
|
|
2
|
-
import { SortingStrategy } from "@dnd-kit/sortable/dist/types/strategies";
|
|
3
|
-
import { DataTypes } from "@etsoo/shared";
|
|
4
|
-
import { Theme } from "@mui/material/styles";
|
|
5
|
-
import React, { CSSProperties } from "react";
|
|
6
|
-
/**
|
|
7
|
-
* DnD item default style
|
|
8
|
-
* @param index Item index
|
|
9
|
-
* @param isDragging Is dragging
|
|
10
|
-
* @param theme Theme
|
|
11
|
-
* @returns Style
|
|
12
|
-
*/
|
|
13
|
-
export declare const DnDItemStyle: (index: number, isDragging: boolean, theme: Theme) => {
|
|
14
|
-
padding: string;
|
|
15
|
-
zIndex: string | number;
|
|
16
|
-
background: string;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* DnD list forward ref
|
|
20
|
-
*/
|
|
21
|
-
export interface DnDListRef<D extends object> {
|
|
22
|
-
/**
|
|
23
|
-
* Add item
|
|
24
|
-
* @param item New item
|
|
25
|
-
*/
|
|
26
|
-
addItem(item: D): void;
|
|
27
|
-
/**
|
|
28
|
-
* Add items
|
|
29
|
-
* @param items items
|
|
30
|
-
*/
|
|
31
|
-
addItems(items: D[]): void;
|
|
32
|
-
/**
|
|
33
|
-
* Delete item
|
|
34
|
-
* @param index Item index
|
|
35
|
-
*/
|
|
36
|
-
deleteItem(index: number): void;
|
|
37
|
-
/**
|
|
38
|
-
* Edit item
|
|
39
|
-
* @param newItem New item
|
|
40
|
-
* @param index Index
|
|
41
|
-
*/
|
|
42
|
-
editItem(newItem: D, index: number): boolean;
|
|
43
|
-
/**
|
|
44
|
-
* Get all items
|
|
45
|
-
*/
|
|
46
|
-
getItems(): D[];
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* DnD sortable list properties
|
|
50
|
-
*/
|
|
51
|
-
export interface DnDListPros<D extends {
|
|
52
|
-
id: UniqueIdentifier;
|
|
53
|
-
}, E extends React.ElementType = React.ElementType> {
|
|
54
|
-
/**
|
|
55
|
-
* Component type to render the list into
|
|
56
|
-
* Default is React.Fragment
|
|
57
|
-
*/
|
|
58
|
-
component?: E;
|
|
59
|
-
/**
|
|
60
|
-
* Component props
|
|
61
|
-
*/
|
|
62
|
-
componentProps?: React.ComponentProps<E>;
|
|
63
|
-
/**
|
|
64
|
-
* Get list item style callback
|
|
65
|
-
*/
|
|
66
|
-
getItemStyle?: (index: number, isDragging: boolean) => CSSProperties;
|
|
67
|
-
/**
|
|
68
|
-
* Item renderer
|
|
69
|
-
*/
|
|
70
|
-
itemRenderer: (item: D, index: number, nodeRef: React.ComponentProps<any>, actionNodeRef: React.ComponentProps<any>) => React.ReactElement;
|
|
71
|
-
/**
|
|
72
|
-
* Height
|
|
73
|
-
*/
|
|
74
|
-
height?: string | number;
|
|
75
|
-
/**
|
|
76
|
-
* List items
|
|
77
|
-
*/
|
|
78
|
-
items: D[];
|
|
79
|
-
/**
|
|
80
|
-
* Label field
|
|
81
|
-
*/
|
|
82
|
-
labelField: DataTypes.Keys<D>;
|
|
83
|
-
/**
|
|
84
|
-
* Methods ref
|
|
85
|
-
*/
|
|
86
|
-
mRef?: React.Ref<DnDListRef<D>>;
|
|
87
|
-
/**
|
|
88
|
-
* Sorting strategy
|
|
89
|
-
*/
|
|
90
|
-
sortingStrategy?: "rect" | "vertical" | "horizontal" | "rectSwapping" | (() => SortingStrategy);
|
|
91
|
-
/**
|
|
92
|
-
* Data change handler
|
|
93
|
-
*/
|
|
94
|
-
onChange?: (items: D[]) => void;
|
|
95
|
-
/**
|
|
96
|
-
* Form data change handler
|
|
97
|
-
*/
|
|
98
|
-
onFormChange?: (items: D[]) => void;
|
|
99
|
-
/**
|
|
100
|
-
* Drag end handler
|
|
101
|
-
*/
|
|
102
|
-
onDragEnd?: (items: D[]) => void;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* DnD (Drag and Drop) sortable list
|
|
106
|
-
* @param props Props
|
|
107
|
-
* @returns Component
|
|
108
|
-
*/
|
|
109
|
-
export declare function DnDList<D extends {
|
|
110
|
-
id: UniqueIdentifier;
|
|
111
|
-
}, E extends React.ElementType = React.ElementType>(props: DnDListPros<D, E>): import("react/jsx-runtime").JSX.Element;
|
package/lib/mjs/DnDList.js
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import Skeleton from "@mui/material/Skeleton";
|
|
3
|
-
import { useTheme } from "@mui/material/styles";
|
|
4
|
-
import React from "react";
|
|
5
|
-
function SortableItem(props) {
|
|
6
|
-
// Destruct
|
|
7
|
-
const { id, useSortableType, CSSType, itemRenderer, style = {} } = props;
|
|
8
|
-
// Use sortable
|
|
9
|
-
const { attributes, listeners, setNodeRef, transform, transition, setActivatorNodeRef } = useSortableType({ id });
|
|
10
|
-
const allStyle = {
|
|
11
|
-
...style,
|
|
12
|
-
transform: CSSType.Transform.toString(transform),
|
|
13
|
-
transition
|
|
14
|
-
};
|
|
15
|
-
const nodeRef = {
|
|
16
|
-
style: allStyle,
|
|
17
|
-
ref: setNodeRef,
|
|
18
|
-
...attributes
|
|
19
|
-
};
|
|
20
|
-
const actionNodeRef = {
|
|
21
|
-
...listeners,
|
|
22
|
-
ref: setActivatorNodeRef
|
|
23
|
-
};
|
|
24
|
-
return itemRenderer(nodeRef, actionNodeRef);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* DnD item default style
|
|
28
|
-
* @param index Item index
|
|
29
|
-
* @param isDragging Is dragging
|
|
30
|
-
* @param theme Theme
|
|
31
|
-
* @returns Style
|
|
32
|
-
*/
|
|
33
|
-
export const DnDItemStyle = (index, isDragging, theme) => ({
|
|
34
|
-
padding: theme.spacing(1),
|
|
35
|
-
zIndex: isDragging ? 1 : "auto",
|
|
36
|
-
background: isDragging
|
|
37
|
-
? theme.palette.primary.light
|
|
38
|
-
: index % 2 === 0
|
|
39
|
-
? theme.palette.grey[100]
|
|
40
|
-
: theme.palette.grey[50]
|
|
41
|
-
});
|
|
42
|
-
/**
|
|
43
|
-
* DnD (Drag and Drop) sortable list
|
|
44
|
-
* @param props Props
|
|
45
|
-
* @returns Component
|
|
46
|
-
*/
|
|
47
|
-
export function DnDList(props) {
|
|
48
|
-
// Destruct
|
|
49
|
-
const { componentProps, height = 360, itemRenderer, labelField, mRef, sortingStrategy, onChange, onFormChange, onDragEnd } = props;
|
|
50
|
-
const Component = props.component || React.Fragment;
|
|
51
|
-
// Theme
|
|
52
|
-
const theme = useTheme();
|
|
53
|
-
// States
|
|
54
|
-
const [items, setItems] = React.useState([]);
|
|
55
|
-
const [activeId, setActiveId] = React.useState();
|
|
56
|
-
React.useEffect(() => {
|
|
57
|
-
setItems(props.items);
|
|
58
|
-
}, [props.items]);
|
|
59
|
-
const doFormChange = React.useCallback((newItems) => {
|
|
60
|
-
if (onFormChange) {
|
|
61
|
-
const locals = Array.isArray(newItems) ? newItems : items;
|
|
62
|
-
onFormChange(locals);
|
|
63
|
-
}
|
|
64
|
-
}, [items, onFormChange]);
|
|
65
|
-
const changeItems = React.useCallback((newItems) => {
|
|
66
|
-
// Possible to alter items with the handler
|
|
67
|
-
if (onChange)
|
|
68
|
-
onChange(newItems);
|
|
69
|
-
doFormChange(newItems);
|
|
70
|
-
// Update state
|
|
71
|
-
setItems(newItems);
|
|
72
|
-
}, [onChange, doFormChange]);
|
|
73
|
-
// Methods
|
|
74
|
-
React.useImperativeHandle(mRef, () => {
|
|
75
|
-
return {
|
|
76
|
-
addItem(newItem) {
|
|
77
|
-
// Existence check
|
|
78
|
-
if (items.some((item) => item[labelField] === newItem[labelField])) {
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
// Clone
|
|
82
|
-
const newItems = [newItem, ...items];
|
|
83
|
-
// Update the state
|
|
84
|
-
changeItems(newItems);
|
|
85
|
-
return true;
|
|
86
|
-
},
|
|
87
|
-
addItems(inputItems) {
|
|
88
|
-
// Clone
|
|
89
|
-
const newItems = [...items];
|
|
90
|
-
// Insert items
|
|
91
|
-
inputItems.forEach((newItem) => {
|
|
92
|
-
// Existence check
|
|
93
|
-
if (newItems.some((item) => item[labelField] === newItem[labelField])) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
newItems.push(newItem);
|
|
97
|
-
});
|
|
98
|
-
// Update the state
|
|
99
|
-
changeItems(newItems);
|
|
100
|
-
return newItems.length - items.length;
|
|
101
|
-
},
|
|
102
|
-
editItem(newItem, index) {
|
|
103
|
-
// Existence check
|
|
104
|
-
const newIndex = items.findIndex((item) => item[labelField] === newItem[labelField]);
|
|
105
|
-
if (newIndex >= 0 && newIndex !== index) {
|
|
106
|
-
// Label field is the same with a different item
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
// Clone
|
|
110
|
-
const newItems = [...items];
|
|
111
|
-
// Remove the item
|
|
112
|
-
newItems.splice(index, 1, newItem);
|
|
113
|
-
// Update the state
|
|
114
|
-
changeItems(newItems);
|
|
115
|
-
return true;
|
|
116
|
-
},
|
|
117
|
-
deleteItem(index) {
|
|
118
|
-
// Clone
|
|
119
|
-
const newItems = [...items];
|
|
120
|
-
// Remove the item
|
|
121
|
-
newItems.splice(index, 1);
|
|
122
|
-
// Update the state
|
|
123
|
-
changeItems(newItems);
|
|
124
|
-
},
|
|
125
|
-
getItems() {
|
|
126
|
-
return items;
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
}, [items, labelField, changeItems]);
|
|
130
|
-
// Dynamic import library
|
|
131
|
-
const [dnd, setDnd] = React.useState();
|
|
132
|
-
React.useEffect(() => {
|
|
133
|
-
Promise.all([
|
|
134
|
-
import("@dnd-kit/core"),
|
|
135
|
-
import("@dnd-kit/sortable"),
|
|
136
|
-
import("@dnd-kit/utilities")
|
|
137
|
-
]).then(([{ DndContext }, { SortableContext, useSortable, rectSortingStrategy, rectSwappingStrategy, horizontalListSortingStrategy, verticalListSortingStrategy }, { CSS }]) => {
|
|
138
|
-
setDnd([
|
|
139
|
-
DndContext,
|
|
140
|
-
SortableContext,
|
|
141
|
-
useSortable,
|
|
142
|
-
rectSortingStrategy,
|
|
143
|
-
rectSwappingStrategy,
|
|
144
|
-
horizontalListSortingStrategy,
|
|
145
|
-
verticalListSortingStrategy,
|
|
146
|
-
CSS
|
|
147
|
-
]);
|
|
148
|
-
});
|
|
149
|
-
}, []);
|
|
150
|
-
const setupDiv = (div, clearup = false) => {
|
|
151
|
-
// Inputs
|
|
152
|
-
div
|
|
153
|
-
.querySelectorAll("input")
|
|
154
|
-
.forEach((input) => clearup
|
|
155
|
-
? input.removeEventListener("change", doFormChange)
|
|
156
|
-
: input.addEventListener("change", doFormChange));
|
|
157
|
-
// Textareas
|
|
158
|
-
div
|
|
159
|
-
.querySelectorAll("textarea")
|
|
160
|
-
.forEach((input) => clearup
|
|
161
|
-
? input.removeEventListener("change", doFormChange)
|
|
162
|
-
: input.addEventListener("change", doFormChange));
|
|
163
|
-
// Select
|
|
164
|
-
div
|
|
165
|
-
.querySelectorAll("select")
|
|
166
|
-
.forEach((input) => clearup
|
|
167
|
-
? input.removeEventListener("change", doFormChange)
|
|
168
|
-
: input.addEventListener("change", doFormChange));
|
|
169
|
-
};
|
|
170
|
-
const divRef = React.useRef(null);
|
|
171
|
-
if (dnd == null) {
|
|
172
|
-
return _jsx(Skeleton, { variant: "rectangular", width: "100%", height: height });
|
|
173
|
-
}
|
|
174
|
-
const [DndContextType, SortableContextType, useSortableType, rectSortingStrategyType, rectSwappingStrategyType, horizontalListSortingStrategyType, verticalListSortingStrategyType, CSSType] = dnd;
|
|
175
|
-
const strategy = typeof sortingStrategy === "function"
|
|
176
|
-
? sortingStrategy()
|
|
177
|
-
: sortingStrategy === "rect"
|
|
178
|
-
? rectSortingStrategyType
|
|
179
|
-
: sortingStrategy === "rectSwapping"
|
|
180
|
-
? rectSwappingStrategyType
|
|
181
|
-
: sortingStrategy === "horizontal"
|
|
182
|
-
? horizontalListSortingStrategyType
|
|
183
|
-
: sortingStrategy === "vertical"
|
|
184
|
-
? verticalListSortingStrategyType
|
|
185
|
-
: undefined;
|
|
186
|
-
let getItemStyle = props.getItemStyle;
|
|
187
|
-
if (getItemStyle == null) {
|
|
188
|
-
getItemStyle = (index, isDragging) => DnDItemStyle(index, isDragging, theme);
|
|
189
|
-
}
|
|
190
|
-
// Drag event handlers
|
|
191
|
-
function handleDragStart(event) {
|
|
192
|
-
const { active } = event;
|
|
193
|
-
setActiveId(active.id);
|
|
194
|
-
}
|
|
195
|
-
function handleDragEnd(event) {
|
|
196
|
-
const { active, over } = event;
|
|
197
|
-
if (over && active.id !== over.id) {
|
|
198
|
-
// Indices
|
|
199
|
-
const oldIndex = items.findIndex((item) => item.id === active.id);
|
|
200
|
-
const newIndex = items.findIndex((item) => item.id === over.id);
|
|
201
|
-
// Clone
|
|
202
|
-
const newItems = [...items];
|
|
203
|
-
// Removed item
|
|
204
|
-
const [removed] = newItems.splice(oldIndex, 1);
|
|
205
|
-
// Insert to the destination index
|
|
206
|
-
newItems.splice(newIndex, 0, removed);
|
|
207
|
-
changeItems(newItems);
|
|
208
|
-
// Drag end handler
|
|
209
|
-
if (onDragEnd)
|
|
210
|
-
onDragEnd(newItems);
|
|
211
|
-
}
|
|
212
|
-
setActiveId(undefined);
|
|
213
|
-
}
|
|
214
|
-
const children = (_jsx(DndContextType, { onDragStart: handleDragStart, onDragEnd: handleDragEnd, children: _jsx(SortableContextType, { items: items, strategy: strategy, children: _jsx(Component, { ...componentProps, children: items.map((item, index) => {
|
|
215
|
-
const id = item.id;
|
|
216
|
-
return (_jsx(SortableItem, { id: id, useSortableType: useSortableType, CSSType: CSSType, style: getItemStyle(index, id === activeId), itemRenderer: (nodeRef, actionNodeRef) => itemRenderer(item, index, nodeRef, actionNodeRef) }, id));
|
|
217
|
-
}) }) }) }));
|
|
218
|
-
if (onFormChange) {
|
|
219
|
-
return (_jsx("div", { style: { width: "100%" }, ref: (div) => {
|
|
220
|
-
if (div && divRef.current != div) {
|
|
221
|
-
if (divRef.current) {
|
|
222
|
-
setupDiv(divRef.current, true);
|
|
223
|
-
}
|
|
224
|
-
divRef.current = div;
|
|
225
|
-
setupDiv(div);
|
|
226
|
-
}
|
|
227
|
-
}, children: children }));
|
|
228
|
-
}
|
|
229
|
-
return children;
|
|
230
|
-
}
|