@eightshift/ui-components 1.0.2 → 1.0.3
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.
|
@@ -8,7 +8,7 @@ import { useId, useState } from "react";
|
|
|
8
8
|
import { BaseControl } from "../base-control/base-control.js";
|
|
9
9
|
import { AnimatedVisibility } from "../animated-visibility/animated-visibility.js";
|
|
10
10
|
import { ToggleButton } from "../toggle-button/toggle-button.js";
|
|
11
|
-
import {
|
|
11
|
+
import { arrayMoveMultiple } from "../../utilities/array-helpers.js";
|
|
12
12
|
import { c as clsx } from "../../lite-pbIeT7tm.js";
|
|
13
13
|
import { $ as $e72dd72e1c76a225$export$2f645645f7bca764 } from "../../useListState-BRsq7O1C.js";
|
|
14
14
|
/**
|
|
@@ -67,7 +67,8 @@ const Repeater = (props) => {
|
|
|
67
67
|
const itemIdBase = useId("repeater-item-");
|
|
68
68
|
const {
|
|
69
69
|
children,
|
|
70
|
-
|
|
70
|
+
onChange: rawOnChange,
|
|
71
|
+
items: rawItems,
|
|
71
72
|
"aria-label": ariaLabel,
|
|
72
73
|
icon,
|
|
73
74
|
label,
|
|
@@ -77,7 +78,6 @@ const Repeater = (props) => {
|
|
|
77
78
|
hideEmptyState,
|
|
78
79
|
addDefaultItem = {},
|
|
79
80
|
addDisabled,
|
|
80
|
-
onChange,
|
|
81
81
|
onAfterItemAdd,
|
|
82
82
|
onAfterItemRemove,
|
|
83
83
|
minItems,
|
|
@@ -90,7 +90,15 @@ const Repeater = (props) => {
|
|
|
90
90
|
const [selectable, setSelectable] = useState(false);
|
|
91
91
|
const [canDelete, setCanDelete] = useState(false);
|
|
92
92
|
const [canReorder, setCanReorder] = useState(true);
|
|
93
|
-
|
|
93
|
+
const onChange = (items2) => {
|
|
94
|
+
const currentItems = [...items2];
|
|
95
|
+
currentItems.forEach((item) => delete item.id);
|
|
96
|
+
rawOnChange(currentItems);
|
|
97
|
+
};
|
|
98
|
+
const items = rawItems.map((item, i) => ({
|
|
99
|
+
id: item.id ?? `${itemIdBase}-${i}`,
|
|
100
|
+
...item
|
|
101
|
+
}));
|
|
94
102
|
const rawList = $e72dd72e1c76a225$export$2f645645f7bca764({
|
|
95
103
|
items,
|
|
96
104
|
selectionMode: selectable ? "multiple" : "none"
|
|
@@ -98,27 +106,27 @@ const Repeater = (props) => {
|
|
|
98
106
|
const list = {
|
|
99
107
|
items,
|
|
100
108
|
selectedKeys: rawList.selectionManager.selectedKeys,
|
|
101
|
-
setSelectedKeys: (
|
|
102
|
-
getKey: ({
|
|
103
|
-
getItem: (
|
|
104
|
-
update: (
|
|
105
|
-
const index = [...items].findIndex((item) => item.
|
|
109
|
+
setSelectedKeys: (ids) => rawList.selectionManager.setSelectedKeys(ids),
|
|
110
|
+
getKey: ({ id }) => items.find((item) => item.id === id),
|
|
111
|
+
getItem: (id) => items.find((item) => item.id === id),
|
|
112
|
+
update: (id, newValue) => {
|
|
113
|
+
const index = [...items].findIndex((item) => item.id === id);
|
|
106
114
|
items[index] = { ...items[index], ...newValue };
|
|
107
115
|
onChange(items);
|
|
108
116
|
},
|
|
109
117
|
move: (sourceKey, targetKeys, direction = "before") => {
|
|
110
|
-
const sourceIndex = items.findIndex((item) => item.
|
|
111
|
-
const targetIndices = [...targetKeys].map((
|
|
118
|
+
const sourceIndex = items.findIndex((item) => item.id === sourceKey);
|
|
119
|
+
const targetIndices = [...targetKeys].map((id) => items.findIndex((item) => item.id === id));
|
|
112
120
|
onChange(arrayMoveMultiple(items, targetIndices, sourceIndex, direction));
|
|
113
121
|
},
|
|
114
122
|
insert: (targetKey, ...newItems) => {
|
|
115
|
-
const targetIndex = items.findIndex((item) => item.
|
|
123
|
+
const targetIndex = items.findIndex((item) => item.id === targetKey);
|
|
116
124
|
const newItemsWithKeys = newItems.map((item) => ({ ...item, id: `${itemIdBase}${items.length + 1}` }));
|
|
117
125
|
onChange([...items.slice(0, targetIndex), ...newItemsWithKeys, ...items.slice(targetIndex)]);
|
|
118
126
|
},
|
|
119
127
|
removeSelectedItems: () => {
|
|
120
|
-
const
|
|
121
|
-
const newItems = items.filter((item) => !
|
|
128
|
+
const ids = rawList.selectionManager.selectedKeys;
|
|
129
|
+
const newItems = items.filter((item) => !ids.has(item.id));
|
|
122
130
|
onChange(newItems);
|
|
123
131
|
},
|
|
124
132
|
append: (item) => {
|
|
@@ -127,8 +135,9 @@ const Repeater = (props) => {
|
|
|
127
135
|
};
|
|
128
136
|
let { dragAndDropHooks } = $d8f176866e6dc039$export$2cfc5be7a55829f6({
|
|
129
137
|
isDisabled: selectable || !canReorder,
|
|
130
|
-
getItems: (
|
|
138
|
+
getItems: (ids) => [...ids].map((id) => ({ "text/plain": list.getItem(id).id })),
|
|
131
139
|
onReorder(e) {
|
|
140
|
+
console.log(e);
|
|
132
141
|
if (e.target.dropPosition === "before") {
|
|
133
142
|
list.move(e.target.key, e.keys);
|
|
134
143
|
} else if (e.target.dropPosition === "after") {
|
|
@@ -170,7 +179,7 @@ const Repeater = (props) => {
|
|
|
170
179
|
{
|
|
171
180
|
onPress: () => {
|
|
172
181
|
var _a;
|
|
173
|
-
const removedItems = [...((_a = list == null ? void 0 : list.selectedKeys.keys()) == null ? void 0 : _a.map((
|
|
182
|
+
const removedItems = [...((_a = list == null ? void 0 : list.selectedKeys.keys()) == null ? void 0 : _a.map((id) => list.getItem(id))) ?? []];
|
|
174
183
|
list.removeSelectedItems();
|
|
175
184
|
setSelectable(false);
|
|
176
185
|
setCanDelete(false);
|
|
@@ -246,10 +255,10 @@ const Repeater = (props) => {
|
|
|
246
255
|
items: list.items.map((item, index) => ({
|
|
247
256
|
...item,
|
|
248
257
|
updateData: (newValue) => {
|
|
249
|
-
list.update(item.
|
|
258
|
+
list.update(item.id, { ...list.getItem(item.id), ...newValue });
|
|
250
259
|
},
|
|
251
260
|
itemIndex: index,
|
|
252
|
-
deleteItem: () => list.remove(item.
|
|
261
|
+
deleteItem: () => list.remove(item.id),
|
|
253
262
|
canReorder,
|
|
254
263
|
setCanReorder
|
|
255
264
|
})),
|