@dmitryvim/form-builder 0.2.23 → 0.2.24

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.
@@ -274,6 +274,26 @@ function formatFileSize(bytes) {
274
274
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
275
275
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
276
276
  }
277
+ function serializeHiddenValue(value) {
278
+ if (value === null || value === void 0) return "";
279
+ return typeof value === "object" ? JSON.stringify(value) : String(value);
280
+ }
281
+ function deserializeHiddenValue(raw) {
282
+ if (raw === "") return null;
283
+ try {
284
+ return JSON.parse(raw);
285
+ } catch {
286
+ return raw;
287
+ }
288
+ }
289
+ function createHiddenInput(name, value) {
290
+ const input = document.createElement("input");
291
+ input.type = "hidden";
292
+ input.name = name;
293
+ input.setAttribute("data-hidden-field", "true");
294
+ input.value = serializeHiddenValue(value);
295
+ return input;
296
+ }
277
297
 
278
298
  // src/utils/enable-conditions.ts
279
299
  function getValueByPath(data, path) {
@@ -4556,7 +4576,11 @@ function renderSingleContainerElement(element, ctx, wrapper, pathKey) {
4556
4576
  state: ctx.state
4557
4577
  };
4558
4578
  element.elements.forEach((child) => {
4559
- if (!child.hidden) {
4579
+ var _a2, _b2;
4580
+ if (child.hidden || child.type === "hidden") {
4581
+ const prefillVal = (_b2 = (_a2 = containerPrefill[child.key]) != null ? _a2 : child.default) != null ? _b2 : null;
4582
+ itemsWrap.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), prefillVal));
4583
+ } else {
4560
4584
  itemsWrap.appendChild(renderElement(child, subCtx));
4561
4585
  }
4562
4586
  });
@@ -4624,7 +4648,10 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4624
4648
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4625
4649
  }
4626
4650
  element.elements.forEach((child) => {
4627
- if (!child.hidden) {
4651
+ var _a2;
4652
+ if (child.hidden || child.type === "hidden") {
4653
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), (_a2 = child.default) != null ? _a2 : null));
4654
+ } else {
4628
4655
  childWrapper.appendChild(renderElement(child, subCtx));
4629
4656
  }
4630
4657
  });
@@ -4694,7 +4721,11 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4694
4721
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4695
4722
  }
4696
4723
  element.elements.forEach((child) => {
4697
- if (!child.hidden) {
4724
+ var _a3, _b2;
4725
+ if (child.hidden || child.type === "hidden") {
4726
+ const prefillVal = (_b2 = (_a3 = prefillObj == null ? void 0 : prefillObj[child.key]) != null ? _a3 : child.default) != null ? _b2 : null;
4727
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), prefillVal));
4728
+ } else {
4698
4729
  childWrapper.appendChild(renderElement(child, subCtx));
4699
4730
  }
4700
4731
  });
@@ -4744,7 +4775,10 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4744
4775
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4745
4776
  }
4746
4777
  element.elements.forEach((child) => {
4747
- if (!child.hidden) {
4778
+ var _a2;
4779
+ if (child.hidden || child.type === "hidden") {
4780
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), (_a2 = child.default) != null ? _a2 : null));
4781
+ } else {
4748
4782
  childWrapper.appendChild(renderElement(child, subCtx));
4749
4783
  }
4750
4784
  });
@@ -4857,20 +4891,16 @@ function validateContainerElement(element, key, context) {
4857
4891
  );
4858
4892
  }
4859
4893
  }
4860
- if (child.hidden || child.type === "hidden") {
4861
- itemData[child.key] = child.default !== void 0 ? child.default : null;
4894
+ const childKey = `${key}[${domIndex}].${child.key}`;
4895
+ const childResult = validateElement(
4896
+ { ...child, key: childKey },
4897
+ { path },
4898
+ itemContainer
4899
+ );
4900
+ if (childResult.spread && childResult.value !== null && typeof childResult.value === "object") {
4901
+ Object.assign(itemData, childResult.value);
4862
4902
  } else {
4863
- const childKey = `${key}[${domIndex}].${child.key}`;
4864
- const childResult = validateElement(
4865
- { ...child, key: childKey },
4866
- { path },
4867
- itemContainer
4868
- );
4869
- if (childResult.spread && childResult.value !== null && typeof childResult.value === "object") {
4870
- Object.assign(itemData, childResult.value);
4871
- } else {
4872
- itemData[child.key] = childResult.value;
4873
- }
4903
+ itemData[child.key] = childResult.value;
4874
4904
  }
4875
4905
  });
4876
4906
  items.push(itemData);
@@ -4902,9 +4932,7 @@ function validateContainerElement(element, key, context) {
4902
4932
  );
4903
4933
  }
4904
4934
  }
4905
- if (child.hidden || child.type === "hidden") {
4906
- containerData[child.key] = child.default !== void 0 ? child.default : null;
4907
- } else {
4935
+ {
4908
4936
  const childKey = `${key}.${child.key}`;
4909
4937
  const childResult = validateElement(
4910
4938
  { ...child, key: childKey },
@@ -8263,7 +8291,8 @@ function createInstanceState(config) {
8263
8291
  ...config,
8264
8292
  translations: mergedTranslations
8265
8293
  },
8266
- debounceTimer: null
8294
+ debounceTimer: null,
8295
+ prefill: {}
8267
8296
  };
8268
8297
  }
8269
8298
  function generateInstanceId() {
@@ -8461,6 +8490,27 @@ function applyActionButtonStyles(button, isFormLevel = false) {
8461
8490
  }
8462
8491
 
8463
8492
  // src/components/registry.ts
8493
+ function validateHiddenElement(element, key, context) {
8494
+ var _a;
8495
+ const { scopeRoot } = context;
8496
+ const input = scopeRoot.querySelector(
8497
+ `input[type="hidden"][data-hidden-field="true"][name="${key}"]`
8498
+ );
8499
+ const raw = (_a = input == null ? void 0 : input.value) != null ? _a : "";
8500
+ if (raw === "") {
8501
+ const defaultVal = "default" in element ? element.default : null;
8502
+ return { value: defaultVal !== void 0 ? defaultVal : null, errors: [] };
8503
+ }
8504
+ return { value: deserializeHiddenValue(raw), errors: [] };
8505
+ }
8506
+ function updateHiddenField(_element, fieldPath, value, context) {
8507
+ const { scopeRoot } = context;
8508
+ const input = scopeRoot.querySelector(
8509
+ `input[type="hidden"][data-hidden-field="true"][name="${fieldPath}"]`
8510
+ );
8511
+ if (!input) return;
8512
+ input.value = serializeHiddenValue(value);
8513
+ }
8464
8514
  var componentRegistry = {
8465
8515
  text: {
8466
8516
  validate: validateTextElement,
@@ -8515,6 +8565,11 @@ var componentRegistry = {
8515
8565
  richinput: {
8516
8566
  validate: validateRichInputElement,
8517
8567
  update: updateRichInputField
8568
+ },
8569
+ hidden: {
8570
+ // Legacy type: `type: "hidden"` — reads/writes DOM <input type="hidden"> element
8571
+ validate: validateHiddenElement,
8572
+ update: updateHiddenField
8518
8573
  }
8519
8574
  };
8520
8575
  function getComponentOperations(elementType) {
@@ -8924,6 +8979,7 @@ var FormBuilderInstance = class {
8924
8979
  this.state.formRoot = root;
8925
8980
  this.state.schema = schema;
8926
8981
  this.state.externalActions = actions || null;
8982
+ this.state.prefill = prefill || {};
8927
8983
  clear(root);
8928
8984
  root.setAttribute("data-fb-root", "true");
8929
8985
  injectThemeVariables(root, this.state.config.theme);
@@ -8941,7 +8997,10 @@ var FormBuilderInstance = class {
8941
8997
  fieldsWrapper.className = `grid grid-cols-${columns} gap-4`;
8942
8998
  }
8943
8999
  schema.elements.forEach((element) => {
8944
- if (element.hidden) {
9000
+ var _a, _b;
9001
+ if (element.hidden || element.type === "hidden") {
9002
+ const val = (_b = (_a = prefill == null ? void 0 : prefill[element.key]) != null ? _a : element.default) != null ? _b : null;
9003
+ fieldsWrapper.appendChild(createHiddenInput(element.key, val));
8945
9004
  return;
8946
9005
  }
8947
9006
  const block = renderElement2(element, {
@@ -8997,6 +9056,7 @@ var FormBuilderInstance = class {
8997
9056
  };
8998
9057
  setValidateElement(validateElement2);
8999
9058
  this.state.schema.elements.forEach((element) => {
9059
+ var _a;
9000
9060
  if (element.enableIf) {
9001
9061
  try {
9002
9062
  const shouldEnable = evaluateEnableCondition(
@@ -9013,8 +9073,16 @@ var FormBuilderInstance = class {
9013
9073
  );
9014
9074
  }
9015
9075
  }
9016
- if (element.hidden) {
9017
- data[element.key] = element.default !== void 0 ? element.default : null;
9076
+ if (element.hidden || element.type === "hidden") {
9077
+ const hiddenInput = this.state.formRoot.querySelector(
9078
+ `input[type="hidden"][data-hidden-field="true"][name="${element.key}"]`
9079
+ );
9080
+ const raw = (_a = hiddenInput == null ? void 0 : hiddenInput.value) != null ? _a : "";
9081
+ if (raw !== "") {
9082
+ data[element.key] = deserializeHiddenValue(raw);
9083
+ } else {
9084
+ data[element.key] = element.default !== void 0 ? element.default : null;
9085
+ }
9018
9086
  } else {
9019
9087
  const result = validateElement2(element, { path: "" });
9020
9088
  if (result.spread && result.value !== null && typeof result.value === "object") {