@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.
@@ -3927,10 +3927,7 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
3927
3927
  rem.addEventListener("mouseleave", () => {
3928
3928
  rem.style.backgroundColor = "transparent";
3929
3929
  });
3930
- rem.onclick = () => {
3931
- item.remove();
3932
- updateAddButton();
3933
- };
3930
+ rem.onclick = () => handleRemoveItem(item);
3934
3931
  item.style.position = "relative";
3935
3932
  item.appendChild(rem);
3936
3933
  }
@@ -3952,6 +3949,10 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
3952
3949
  }
3953
3950
  countDisplay.textContent = `${currentCount}/${max === Infinity ? "\u221E" : max}`;
3954
3951
  };
3952
+ const handleRemoveItem = (item) => {
3953
+ item.remove();
3954
+ updateAddButton();
3955
+ };
3955
3956
  if (pre && Array.isArray(pre)) {
3956
3957
  pre.forEach((prefillObj, idx) => {
3957
3958
  var _a2;
@@ -3996,10 +3997,7 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
3996
3997
  rem.addEventListener("mouseleave", () => {
3997
3998
  rem.style.backgroundColor = "transparent";
3998
3999
  });
3999
- rem.onclick = () => {
4000
- item.remove();
4001
- updateAddButton();
4002
- };
4000
+ rem.onclick = () => handleRemoveItem(item);
4003
4001
  item.style.position = "relative";
4004
4002
  item.appendChild(rem);
4005
4003
  }
@@ -4050,8 +4048,7 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4050
4048
  });
4051
4049
  rem.onclick = () => {
4052
4050
  if (countItems() > min) {
4053
- item.remove();
4054
- updateAddButton();
4051
+ handleRemoveItem(item);
4055
4052
  }
4056
4053
  };
4057
4054
  item.style.position = "relative";
@@ -4110,15 +4107,16 @@ function validateContainerElement(element, key, context) {
4110
4107
  "[data-container-item]"
4111
4108
  );
4112
4109
  const containerWrappers = Array.from(allContainerWrappers).filter((el) => {
4113
- const attr = el.getAttribute("data-container-item");
4114
- return attr == null ? void 0 : attr.startsWith(`${key}[`);
4110
+ const attr = el.getAttribute("data-container-item") || "";
4111
+ if (!attr.startsWith(`${key}[`)) return false;
4112
+ const suffix = attr.slice(key.length);
4113
+ return /^\[\d+\]$/.test(suffix);
4115
4114
  });
4116
- const itemCount = containerWrappers.length;
4117
- for (let i = 0; i < itemCount; i++) {
4115
+ containerWrappers.forEach((itemContainer) => {
4118
4116
  const itemData = {};
4119
- const itemContainer = scopeRoot.querySelector(
4120
- `[data-container-item="${key}[${i}]"]`
4121
- ) || scopeRoot;
4117
+ const containerAttr = itemContainer.getAttribute("data-container-item") || "";
4118
+ const indexMatch = containerAttr.match(/\[(\d+)\]$/);
4119
+ const domIndex = indexMatch ? parseInt(indexMatch[1], 10) : 0;
4122
4120
  element.elements.forEach((child) => {
4123
4121
  var _a;
4124
4122
  if (child.enableIf) {
@@ -4136,7 +4134,7 @@ function validateContainerElement(element, key, context) {
4136
4134
  }
4137
4135
  } catch (error) {
4138
4136
  console.error(
4139
- `Error evaluating enableIf for field "${child.key}" in container "${key}[${i}]":`,
4137
+ `Error evaluating enableIf for field "${child.key}" in container "${key}[${domIndex}]":`,
4140
4138
  error
4141
4139
  );
4142
4140
  }
@@ -4144,7 +4142,7 @@ function validateContainerElement(element, key, context) {
4144
4142
  if (child.hidden || child.type === "hidden") {
4145
4143
  itemData[child.key] = child.default !== void 0 ? child.default : null;
4146
4144
  } else {
4147
- const childKey = `${key}[${i}].${child.key}`;
4145
+ const childKey = `${key}[${domIndex}].${child.key}`;
4148
4146
  itemData[child.key] = validateElement(
4149
4147
  { ...child, key: childKey },
4150
4148
  { path },
@@ -4153,7 +4151,7 @@ function validateContainerElement(element, key, context) {
4153
4151
  }
4154
4152
  });
4155
4153
  items.push(itemData);
4156
- }
4154
+ });
4157
4155
  validateContainerCount(key, items, element);
4158
4156
  return { value: items, errors };
4159
4157
  } else {
@@ -5811,8 +5809,18 @@ var FormBuilderInstance = class {
5811
5809
  if ((element.type === "container" || element.type === "group") && "elements" in element && element.elements) {
5812
5810
  const containerData = formData == null ? void 0 : formData[element.key];
5813
5811
  if (Array.isArray(containerData)) {
5814
- containerData.forEach((_, index) => {
5815
- checkElements(element.elements, `${fullPath}[${index}]`);
5812
+ const containerItems = this.state.formRoot.querySelectorAll(
5813
+ `[data-container-item]`
5814
+ );
5815
+ const directItems = Array.from(containerItems).filter((el) => {
5816
+ const attr = el.getAttribute("data-container-item") || "";
5817
+ if (!attr.startsWith(`${fullPath}[`)) return false;
5818
+ const suffix = attr.slice(fullPath.length);
5819
+ return /^\[\d+\]$/.test(suffix);
5820
+ });
5821
+ directItems.forEach((el) => {
5822
+ const attr = el.getAttribute("data-container-item") || "";
5823
+ checkElements(element.elements, attr);
5816
5824
  });
5817
5825
  } else {
5818
5826
  checkElements(element.elements, fullPath);