@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.
package/dist/esm/index.js CHANGED
@@ -268,6 +268,26 @@ function formatFileSize(bytes) {
268
268
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
269
269
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
270
270
  }
271
+ function serializeHiddenValue(value) {
272
+ if (value === null || value === void 0) return "";
273
+ return typeof value === "object" ? JSON.stringify(value) : String(value);
274
+ }
275
+ function deserializeHiddenValue(raw) {
276
+ if (raw === "") return null;
277
+ try {
278
+ return JSON.parse(raw);
279
+ } catch {
280
+ return raw;
281
+ }
282
+ }
283
+ function createHiddenInput(name, value) {
284
+ const input = document.createElement("input");
285
+ input.type = "hidden";
286
+ input.name = name;
287
+ input.setAttribute("data-hidden-field", "true");
288
+ input.value = serializeHiddenValue(value);
289
+ return input;
290
+ }
271
291
 
272
292
  // src/utils/enable-conditions.ts
273
293
  function getValueByPath(data, path) {
@@ -4492,7 +4512,10 @@ function renderSingleContainerElement(element, ctx, wrapper, pathKey) {
4492
4512
  state: ctx.state
4493
4513
  };
4494
4514
  element.elements.forEach((child) => {
4495
- if (!child.hidden) {
4515
+ if (child.hidden || child.type === "hidden") {
4516
+ const prefillVal = containerPrefill[child.key] ?? child.default ?? null;
4517
+ itemsWrap.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), prefillVal));
4518
+ } else {
4496
4519
  itemsWrap.appendChild(renderElement(child, subCtx));
4497
4520
  }
4498
4521
  });
@@ -4559,7 +4582,9 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4559
4582
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4560
4583
  }
4561
4584
  element.elements.forEach((child) => {
4562
- if (!child.hidden) {
4585
+ if (child.hidden || child.type === "hidden") {
4586
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), child.default ?? null));
4587
+ } else {
4563
4588
  childWrapper.appendChild(renderElement(child, subCtx));
4564
4589
  }
4565
4590
  });
@@ -4628,7 +4653,10 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4628
4653
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4629
4654
  }
4630
4655
  element.elements.forEach((child) => {
4631
- if (!child.hidden) {
4656
+ if (child.hidden || child.type === "hidden") {
4657
+ const prefillVal = prefillObj?.[child.key] ?? child.default ?? null;
4658
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), prefillVal));
4659
+ } else {
4632
4660
  childWrapper.appendChild(renderElement(child, subCtx));
4633
4661
  }
4634
4662
  });
@@ -4678,7 +4706,9 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4678
4706
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4679
4707
  }
4680
4708
  element.elements.forEach((child) => {
4681
- if (!child.hidden) {
4709
+ if (child.hidden || child.type === "hidden") {
4710
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), child.default ?? null));
4711
+ } else {
4682
4712
  childWrapper.appendChild(renderElement(child, subCtx));
4683
4713
  }
4684
4714
  });
@@ -4789,20 +4819,16 @@ function validateContainerElement(element, key, context) {
4789
4819
  );
4790
4820
  }
4791
4821
  }
4792
- if (child.hidden || child.type === "hidden") {
4793
- itemData[child.key] = child.default !== void 0 ? child.default : null;
4822
+ const childKey = `${key}[${domIndex}].${child.key}`;
4823
+ const childResult = validateElement(
4824
+ { ...child, key: childKey },
4825
+ { path },
4826
+ itemContainer
4827
+ );
4828
+ if (childResult.spread && childResult.value !== null && typeof childResult.value === "object") {
4829
+ Object.assign(itemData, childResult.value);
4794
4830
  } else {
4795
- const childKey = `${key}[${domIndex}].${child.key}`;
4796
- const childResult = validateElement(
4797
- { ...child, key: childKey },
4798
- { path },
4799
- itemContainer
4800
- );
4801
- if (childResult.spread && childResult.value !== null && typeof childResult.value === "object") {
4802
- Object.assign(itemData, childResult.value);
4803
- } else {
4804
- itemData[child.key] = childResult.value;
4805
- }
4831
+ itemData[child.key] = childResult.value;
4806
4832
  }
4807
4833
  });
4808
4834
  items.push(itemData);
@@ -4833,9 +4859,7 @@ function validateContainerElement(element, key, context) {
4833
4859
  );
4834
4860
  }
4835
4861
  }
4836
- if (child.hidden || child.type === "hidden") {
4837
- containerData[child.key] = child.default !== void 0 ? child.default : null;
4838
- } else {
4862
+ {
4839
4863
  const childKey = `${key}.${child.key}`;
4840
4864
  const childResult = validateElement(
4841
4865
  { ...child, key: childKey },
@@ -8124,7 +8148,8 @@ function createInstanceState(config) {
8124
8148
  ...config,
8125
8149
  translations: mergedTranslations
8126
8150
  },
8127
- debounceTimer: null
8151
+ debounceTimer: null,
8152
+ prefill: {}
8128
8153
  };
8129
8154
  }
8130
8155
  function generateInstanceId() {
@@ -8322,6 +8347,26 @@ function applyActionButtonStyles(button, isFormLevel = false) {
8322
8347
  }
8323
8348
 
8324
8349
  // src/components/registry.ts
8350
+ function validateHiddenElement(element, key, context) {
8351
+ const { scopeRoot } = context;
8352
+ const input = scopeRoot.querySelector(
8353
+ `input[type="hidden"][data-hidden-field="true"][name="${key}"]`
8354
+ );
8355
+ const raw = input?.value ?? "";
8356
+ if (raw === "") {
8357
+ const defaultVal = "default" in element ? element.default : null;
8358
+ return { value: defaultVal !== void 0 ? defaultVal : null, errors: [] };
8359
+ }
8360
+ return { value: deserializeHiddenValue(raw), errors: [] };
8361
+ }
8362
+ function updateHiddenField(_element, fieldPath, value, context) {
8363
+ const { scopeRoot } = context;
8364
+ const input = scopeRoot.querySelector(
8365
+ `input[type="hidden"][data-hidden-field="true"][name="${fieldPath}"]`
8366
+ );
8367
+ if (!input) return;
8368
+ input.value = serializeHiddenValue(value);
8369
+ }
8325
8370
  var componentRegistry = {
8326
8371
  text: {
8327
8372
  validate: validateTextElement,
@@ -8376,6 +8421,11 @@ var componentRegistry = {
8376
8421
  richinput: {
8377
8422
  validate: validateRichInputElement,
8378
8423
  update: updateRichInputField
8424
+ },
8425
+ hidden: {
8426
+ // Legacy type: `type: "hidden"` — reads/writes DOM <input type="hidden"> element
8427
+ validate: validateHiddenElement,
8428
+ update: updateHiddenField
8379
8429
  }
8380
8430
  };
8381
8431
  function getComponentOperations(elementType) {
@@ -8785,6 +8835,7 @@ var FormBuilderInstance = class {
8785
8835
  this.state.formRoot = root;
8786
8836
  this.state.schema = schema;
8787
8837
  this.state.externalActions = actions || null;
8838
+ this.state.prefill = prefill || {};
8788
8839
  clear(root);
8789
8840
  root.setAttribute("data-fb-root", "true");
8790
8841
  injectThemeVariables(root, this.state.config.theme);
@@ -8802,7 +8853,9 @@ var FormBuilderInstance = class {
8802
8853
  fieldsWrapper.className = `grid grid-cols-${columns} gap-4`;
8803
8854
  }
8804
8855
  schema.elements.forEach((element) => {
8805
- if (element.hidden) {
8856
+ if (element.hidden || element.type === "hidden") {
8857
+ const val = prefill?.[element.key] ?? element.default ?? null;
8858
+ fieldsWrapper.appendChild(createHiddenInput(element.key, val));
8806
8859
  return;
8807
8860
  }
8808
8861
  const block = renderElement2(element, {
@@ -8874,8 +8927,16 @@ var FormBuilderInstance = class {
8874
8927
  );
8875
8928
  }
8876
8929
  }
8877
- if (element.hidden) {
8878
- data[element.key] = element.default !== void 0 ? element.default : null;
8930
+ if (element.hidden || element.type === "hidden") {
8931
+ const hiddenInput = this.state.formRoot.querySelector(
8932
+ `input[type="hidden"][data-hidden-field="true"][name="${element.key}"]`
8933
+ );
8934
+ const raw = hiddenInput?.value ?? "";
8935
+ if (raw !== "") {
8936
+ data[element.key] = deserializeHiddenValue(raw);
8937
+ } else {
8938
+ data[element.key] = element.default !== void 0 ? element.default : null;
8939
+ }
8879
8940
  } else {
8880
8941
  const result = validateElement2(element, { path: "" });
8881
8942
  if (result.spread && result.value !== null && typeof result.value === "object") {