@dmitryvim/form-builder 0.2.16 → 0.2.17

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/esm/index.js CHANGED
@@ -3877,10 +3877,7 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
3877
3877
  rem.addEventListener("mouseleave", () => {
3878
3878
  rem.style.backgroundColor = "transparent";
3879
3879
  });
3880
- rem.onclick = () => {
3881
- item.remove();
3882
- updateAddButton();
3883
- };
3880
+ rem.onclick = () => handleRemoveItem(item);
3884
3881
  item.style.position = "relative";
3885
3882
  item.appendChild(rem);
3886
3883
  }
@@ -3902,6 +3899,10 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
3902
3899
  }
3903
3900
  countDisplay.textContent = `${currentCount}/${max === Infinity ? "\u221E" : max}`;
3904
3901
  };
3902
+ const handleRemoveItem = (item) => {
3903
+ item.remove();
3904
+ updateAddButton();
3905
+ };
3905
3906
  if (pre && Array.isArray(pre)) {
3906
3907
  pre.forEach((prefillObj, idx) => {
3907
3908
  const mergedPrefill = mergeWithDefaults(prefillObj || {}, childDefaults);
@@ -3945,10 +3946,7 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
3945
3946
  rem.addEventListener("mouseleave", () => {
3946
3947
  rem.style.backgroundColor = "transparent";
3947
3948
  });
3948
- rem.onclick = () => {
3949
- item.remove();
3950
- updateAddButton();
3951
- };
3949
+ rem.onclick = () => handleRemoveItem(item);
3952
3950
  item.style.position = "relative";
3953
3951
  item.appendChild(rem);
3954
3952
  }
@@ -3999,8 +3997,7 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
3999
3997
  });
4000
3998
  rem.onclick = () => {
4001
3999
  if (countItems() > min) {
4002
- item.remove();
4003
- updateAddButton();
4000
+ handleRemoveItem(item);
4004
4001
  }
4005
4002
  };
4006
4003
  item.style.position = "relative";
@@ -4058,15 +4055,16 @@ function validateContainerElement(element, key, context) {
4058
4055
  "[data-container-item]"
4059
4056
  );
4060
4057
  const containerWrappers = Array.from(allContainerWrappers).filter((el) => {
4061
- const attr = el.getAttribute("data-container-item");
4062
- return attr?.startsWith(`${key}[`);
4058
+ const attr = el.getAttribute("data-container-item") || "";
4059
+ if (!attr.startsWith(`${key}[`)) return false;
4060
+ const suffix = attr.slice(key.length);
4061
+ return /^\[\d+\]$/.test(suffix);
4063
4062
  });
4064
- const itemCount = containerWrappers.length;
4065
- for (let i = 0; i < itemCount; i++) {
4063
+ containerWrappers.forEach((itemContainer) => {
4066
4064
  const itemData = {};
4067
- const itemContainer = scopeRoot.querySelector(
4068
- `[data-container-item="${key}[${i}]"]`
4069
- ) || scopeRoot;
4065
+ const containerAttr = itemContainer.getAttribute("data-container-item") || "";
4066
+ const indexMatch = containerAttr.match(/\[(\d+)\]$/);
4067
+ const domIndex = indexMatch ? parseInt(indexMatch[1], 10) : 0;
4070
4068
  element.elements.forEach((child) => {
4071
4069
  if (child.enableIf) {
4072
4070
  try {
@@ -4083,7 +4081,7 @@ function validateContainerElement(element, key, context) {
4083
4081
  }
4084
4082
  } catch (error) {
4085
4083
  console.error(
4086
- `Error evaluating enableIf for field "${child.key}" in container "${key}[${i}]":`,
4084
+ `Error evaluating enableIf for field "${child.key}" in container "${key}[${domIndex}]":`,
4087
4085
  error
4088
4086
  );
4089
4087
  }
@@ -4091,7 +4089,7 @@ function validateContainerElement(element, key, context) {
4091
4089
  if (child.hidden || child.type === "hidden") {
4092
4090
  itemData[child.key] = child.default !== void 0 ? child.default : null;
4093
4091
  } else {
4094
- const childKey = `${key}[${i}].${child.key}`;
4092
+ const childKey = `${key}[${domIndex}].${child.key}`;
4095
4093
  itemData[child.key] = validateElement(
4096
4094
  { ...child, key: childKey },
4097
4095
  { path },
@@ -4100,7 +4098,7 @@ function validateContainerElement(element, key, context) {
4100
4098
  }
4101
4099
  });
4102
4100
  items.push(itemData);
4103
- }
4101
+ });
4104
4102
  validateContainerCount(key, items, element);
4105
4103
  return { value: items, errors };
4106
4104
  } else {
@@ -5751,8 +5749,18 @@ var FormBuilderInstance = class {
5751
5749
  if ((element.type === "container" || element.type === "group") && "elements" in element && element.elements) {
5752
5750
  const containerData = formData?.[element.key];
5753
5751
  if (Array.isArray(containerData)) {
5754
- containerData.forEach((_, index) => {
5755
- checkElements(element.elements, `${fullPath}[${index}]`);
5752
+ const containerItems = this.state.formRoot.querySelectorAll(
5753
+ `[data-container-item]`
5754
+ );
5755
+ const directItems = Array.from(containerItems).filter((el) => {
5756
+ const attr = el.getAttribute("data-container-item") || "";
5757
+ if (!attr.startsWith(`${fullPath}[`)) return false;
5758
+ const suffix = attr.slice(fullPath.length);
5759
+ return /^\[\d+\]$/.test(suffix);
5760
+ });
5761
+ directItems.forEach((el) => {
5762
+ const attr = el.getAttribute("data-container-item") || "";
5763
+ checkElements(element.elements, attr);
5756
5764
  });
5757
5765
  } else {
5758
5766
  checkElements(element.elements, fullPath);