@elementor/editor-controls 3.32.0-41 → 3.32.0-43

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/index.mjs CHANGED
@@ -1271,12 +1271,12 @@ var Repeater = ({
1271
1271
  persistWhen: () => true
1272
1272
  });
1273
1273
  const [uniqueKeys, setUniqueKeys] = useState5(items2.map((_, index) => index));
1274
- const generateNextKey2 = (source) => {
1274
+ const generateNextKey = (source) => {
1275
1275
  return 1 + Math.max(0, ...source);
1276
1276
  };
1277
1277
  const addRepeaterItem = () => {
1278
1278
  const newItem = structuredClone(itemSettings.initialValues);
1279
- const newKey = generateNextKey2(uniqueKeys);
1279
+ const newKey = generateNextKey(uniqueKeys);
1280
1280
  if (addToBottom) {
1281
1281
  setItems([...items2, newItem]);
1282
1282
  setUniqueKeys([...uniqueKeys, newKey]);
@@ -1290,7 +1290,7 @@ var Repeater = ({
1290
1290
  };
1291
1291
  const duplicateRepeaterItem = (index) => {
1292
1292
  const newItem = structuredClone(items2[index]);
1293
- const newKey = generateNextKey2(uniqueKeys);
1293
+ const newKey = generateNextKey(uniqueKeys);
1294
1294
  const atPosition = 1 + index;
1295
1295
  setItems([...items2.slice(0, atPosition), newItem, ...items2.slice(atPosition)]);
1296
1296
  setUniqueKeys([...uniqueKeys.slice(0, atPosition), newKey, ...uniqueKeys.slice(atPosition)]);
@@ -3350,13 +3350,20 @@ var RepeaterContextProvider = ({
3350
3350
  setExternal: setRepeaterValues,
3351
3351
  persistWhen: () => true
3352
3352
  });
3353
- const itemWithKeysState = useState12(
3354
- items2?.map((item, index) => ({ key: index, item })) ?? []
3355
- );
3356
- const itemsWithKeys = itemWithKeysState[0];
3357
- const setItemsWithKeys = (newItems) => {
3358
- itemWithKeysState[1](newItems);
3359
- setItems(newItems.map(({ item }) => item));
3353
+ const [itemsWithKeys, setItemsWithKeys] = useState12(() => {
3354
+ return items2?.map((item) => ({ key: generateUniqueKey(), item })) ?? [];
3355
+ });
3356
+ React49.useEffect(() => {
3357
+ setItemsWithKeys((prevItemsWithKeys) => {
3358
+ const newItemsWithKeys = items2?.map((item) => {
3359
+ const existingItem = prevItemsWithKeys.find((i) => i.item === item);
3360
+ return existingItem || { key: generateUniqueKey(), item };
3361
+ }) ?? [];
3362
+ return newItemsWithKeys;
3363
+ });
3364
+ }, [items2]);
3365
+ const handleSetItems = (newItemsWithKeys) => {
3366
+ setItems(newItemsWithKeys.map(({ item }) => item));
3360
3367
  };
3361
3368
  const [openItemIndex, setOpenItemIndex] = useState12(EMPTY_OPEN_ITEM2);
3362
3369
  const [rowRef, setRowRef] = useState12(null);
@@ -3365,19 +3372,18 @@ var RepeaterContextProvider = ({
3365
3372
  const addItem = (ev, config) => {
3366
3373
  const item = config?.item ?? initial;
3367
3374
  const newIndex = config?.index ?? items2.length;
3368
- const newKey = generateNextKey(itemsWithKeys.map(({ key }) => key));
3369
- const newItemsWithKeys = [...itemsWithKeys];
3370
- newItemsWithKeys.splice(newIndex, 0, { item, key: newKey });
3371
- setItemsWithKeys(newItemsWithKeys);
3375
+ const newItems = [...items2];
3376
+ newItems.splice(newIndex, 0, item);
3377
+ setItems(newItems);
3372
3378
  setOpenItemIndex(newIndex);
3373
3379
  popoverState.open(rowRef ?? ev);
3374
3380
  };
3375
3381
  const removeItem = (index) => {
3376
- setItemsWithKeys(itemsWithKeys.filter((_, pos) => pos !== index));
3382
+ setItems(items2.filter((_, pos) => pos !== index));
3377
3383
  };
3378
3384
  const updateItem = (updatedItem, index) => {
3379
- const item = itemsWithKeys[index];
3380
- setItemsWithKeys(itemsWithKeys.toSpliced(index, 1, { ...item, item: updatedItem }));
3385
+ const newItems = [...items2.slice(0, index), updatedItem, ...items2.slice(index + 1)];
3386
+ setItems(newItems);
3381
3387
  };
3382
3388
  return /* @__PURE__ */ React49.createElement(
3383
3389
  RepeaterContext.Provider,
@@ -3387,7 +3393,7 @@ var RepeaterContextProvider = ({
3387
3393
  openItemIndex,
3388
3394
  setOpenItemIndex,
3389
3395
  items: itemsWithKeys ?? [],
3390
- setItems: setItemsWithKeys,
3396
+ setItems: handleSetItems,
3391
3397
  popoverState,
3392
3398
  initial,
3393
3399
  updateItem,
@@ -3400,8 +3406,8 @@ var RepeaterContextProvider = ({
3400
3406
  children
3401
3407
  );
3402
3408
  };
3403
- var generateNextKey = (source) => {
3404
- return 1 + Math.max(0, ...source);
3409
+ var generateUniqueKey = () => {
3410
+ return Date.now() + Math.floor(Math.random() * 1e6);
3405
3411
  };
3406
3412
 
3407
3413
  // src/components/unstable-repeater/actions/tooltip-add-item-action.tsx