@etsoo/materialui 1.6.11 → 1.6.13

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.
@@ -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;
@@ -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
- }