@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.js CHANGED
@@ -1329,12 +1329,12 @@ var Repeater = ({
1329
1329
  persistWhen: () => true
1330
1330
  });
1331
1331
  const [uniqueKeys, setUniqueKeys] = (0, import_react16.useState)(items2.map((_, index) => index));
1332
- const generateNextKey2 = (source) => {
1332
+ const generateNextKey = (source) => {
1333
1333
  return 1 + Math.max(0, ...source);
1334
1334
  };
1335
1335
  const addRepeaterItem = () => {
1336
1336
  const newItem = structuredClone(itemSettings.initialValues);
1337
- const newKey = generateNextKey2(uniqueKeys);
1337
+ const newKey = generateNextKey(uniqueKeys);
1338
1338
  if (addToBottom) {
1339
1339
  setItems([...items2, newItem]);
1340
1340
  setUniqueKeys([...uniqueKeys, newKey]);
@@ -1348,7 +1348,7 @@ var Repeater = ({
1348
1348
  };
1349
1349
  const duplicateRepeaterItem = (index) => {
1350
1350
  const newItem = structuredClone(items2[index]);
1351
- const newKey = generateNextKey2(uniqueKeys);
1351
+ const newKey = generateNextKey(uniqueKeys);
1352
1352
  const atPosition = 1 + index;
1353
1353
  setItems([...items2.slice(0, atPosition), newItem, ...items2.slice(atPosition)]);
1354
1354
  setUniqueKeys([...uniqueKeys.slice(0, atPosition), newKey, ...uniqueKeys.slice(atPosition)]);
@@ -3370,13 +3370,20 @@ var RepeaterContextProvider = ({
3370
3370
  setExternal: setRepeaterValues,
3371
3371
  persistWhen: () => true
3372
3372
  });
3373
- const itemWithKeysState = (0, import_react30.useState)(
3374
- items2?.map((item, index) => ({ key: index, item })) ?? []
3375
- );
3376
- const itemsWithKeys = itemWithKeysState[0];
3377
- const setItemsWithKeys = (newItems) => {
3378
- itemWithKeysState[1](newItems);
3379
- setItems(newItems.map(({ item }) => item));
3373
+ const [itemsWithKeys, setItemsWithKeys] = (0, import_react30.useState)(() => {
3374
+ return items2?.map((item) => ({ key: generateUniqueKey(), item })) ?? [];
3375
+ });
3376
+ React49.useEffect(() => {
3377
+ setItemsWithKeys((prevItemsWithKeys) => {
3378
+ const newItemsWithKeys = items2?.map((item) => {
3379
+ const existingItem = prevItemsWithKeys.find((i) => i.item === item);
3380
+ return existingItem || { key: generateUniqueKey(), item };
3381
+ }) ?? [];
3382
+ return newItemsWithKeys;
3383
+ });
3384
+ }, [items2]);
3385
+ const handleSetItems = (newItemsWithKeys) => {
3386
+ setItems(newItemsWithKeys.map(({ item }) => item));
3380
3387
  };
3381
3388
  const [openItemIndex, setOpenItemIndex] = (0, import_react30.useState)(EMPTY_OPEN_ITEM2);
3382
3389
  const [rowRef, setRowRef] = (0, import_react30.useState)(null);
@@ -3385,19 +3392,18 @@ var RepeaterContextProvider = ({
3385
3392
  const addItem = (ev, config) => {
3386
3393
  const item = config?.item ?? initial;
3387
3394
  const newIndex = config?.index ?? items2.length;
3388
- const newKey = generateNextKey(itemsWithKeys.map(({ key }) => key));
3389
- const newItemsWithKeys = [...itemsWithKeys];
3390
- newItemsWithKeys.splice(newIndex, 0, { item, key: newKey });
3391
- setItemsWithKeys(newItemsWithKeys);
3395
+ const newItems = [...items2];
3396
+ newItems.splice(newIndex, 0, item);
3397
+ setItems(newItems);
3392
3398
  setOpenItemIndex(newIndex);
3393
3399
  popoverState.open(rowRef ?? ev);
3394
3400
  };
3395
3401
  const removeItem = (index) => {
3396
- setItemsWithKeys(itemsWithKeys.filter((_, pos) => pos !== index));
3402
+ setItems(items2.filter((_, pos) => pos !== index));
3397
3403
  };
3398
3404
  const updateItem = (updatedItem, index) => {
3399
- const item = itemsWithKeys[index];
3400
- setItemsWithKeys(itemsWithKeys.toSpliced(index, 1, { ...item, item: updatedItem }));
3405
+ const newItems = [...items2.slice(0, index), updatedItem, ...items2.slice(index + 1)];
3406
+ setItems(newItems);
3401
3407
  };
3402
3408
  return /* @__PURE__ */ React49.createElement(
3403
3409
  RepeaterContext.Provider,
@@ -3407,7 +3413,7 @@ var RepeaterContextProvider = ({
3407
3413
  openItemIndex,
3408
3414
  setOpenItemIndex,
3409
3415
  items: itemsWithKeys ?? [],
3410
- setItems: setItemsWithKeys,
3416
+ setItems: handleSetItems,
3411
3417
  popoverState,
3412
3418
  initial,
3413
3419
  updateItem,
@@ -3420,8 +3426,8 @@ var RepeaterContextProvider = ({
3420
3426
  children
3421
3427
  );
3422
3428
  };
3423
- var generateNextKey = (source) => {
3424
- return 1 + Math.max(0, ...source);
3429
+ var generateUniqueKey = () => {
3430
+ return Date.now() + Math.floor(Math.random() * 1e6);
3425
3431
  };
3426
3432
 
3427
3433
  // src/components/unstable-repeater/actions/tooltip-add-item-action.tsx