@dmitryvim/form-builder 0.2.23 → 0.2.25

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) {
@@ -2268,6 +2288,10 @@ async function renderFilePreview(container, resourceId, state, options = {}) {
2268
2288
  meta,
2269
2289
  state
2270
2290
  );
2291
+ const isVideo = meta?.type?.startsWith("video/");
2292
+ if (!isReadonly && !isVideo) {
2293
+ renderDeleteButton(container, resourceId, state);
2294
+ }
2271
2295
  }
2272
2296
  }
2273
2297
  async function renderFilePreviewReadonly(resourceId, state, fileName) {
@@ -4492,7 +4516,10 @@ function renderSingleContainerElement(element, ctx, wrapper, pathKey) {
4492
4516
  state: ctx.state
4493
4517
  };
4494
4518
  element.elements.forEach((child) => {
4495
- if (!child.hidden) {
4519
+ if (child.hidden || child.type === "hidden") {
4520
+ const prefillVal = containerPrefill[child.key] ?? child.default ?? null;
4521
+ itemsWrap.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), prefillVal));
4522
+ } else {
4496
4523
  itemsWrap.appendChild(renderElement(child, subCtx));
4497
4524
  }
4498
4525
  });
@@ -4559,7 +4586,9 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4559
4586
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4560
4587
  }
4561
4588
  element.elements.forEach((child) => {
4562
- if (!child.hidden) {
4589
+ if (child.hidden || child.type === "hidden") {
4590
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), child.default ?? null));
4591
+ } else {
4563
4592
  childWrapper.appendChild(renderElement(child, subCtx));
4564
4593
  }
4565
4594
  });
@@ -4628,7 +4657,10 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4628
4657
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4629
4658
  }
4630
4659
  element.elements.forEach((child) => {
4631
- if (!child.hidden) {
4660
+ if (child.hidden || child.type === "hidden") {
4661
+ const prefillVal = prefillObj?.[child.key] ?? child.default ?? null;
4662
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), prefillVal));
4663
+ } else {
4632
4664
  childWrapper.appendChild(renderElement(child, subCtx));
4633
4665
  }
4634
4666
  });
@@ -4678,7 +4710,9 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4678
4710
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4679
4711
  }
4680
4712
  element.elements.forEach((child) => {
4681
- if (!child.hidden) {
4713
+ if (child.hidden || child.type === "hidden") {
4714
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), child.default ?? null));
4715
+ } else {
4682
4716
  childWrapper.appendChild(renderElement(child, subCtx));
4683
4717
  }
4684
4718
  });
@@ -4789,20 +4823,16 @@ function validateContainerElement(element, key, context) {
4789
4823
  );
4790
4824
  }
4791
4825
  }
4792
- if (child.hidden || child.type === "hidden") {
4793
- itemData[child.key] = child.default !== void 0 ? child.default : null;
4826
+ const childKey = `${key}[${domIndex}].${child.key}`;
4827
+ const childResult = validateElement(
4828
+ { ...child, key: childKey },
4829
+ { path },
4830
+ itemContainer
4831
+ );
4832
+ if (childResult.spread && childResult.value !== null && typeof childResult.value === "object") {
4833
+ Object.assign(itemData, childResult.value);
4794
4834
  } 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
- }
4835
+ itemData[child.key] = childResult.value;
4806
4836
  }
4807
4837
  });
4808
4838
  items.push(itemData);
@@ -4833,9 +4863,7 @@ function validateContainerElement(element, key, context) {
4833
4863
  );
4834
4864
  }
4835
4865
  }
4836
- if (child.hidden || child.type === "hidden") {
4837
- containerData[child.key] = child.default !== void 0 ? child.default : null;
4838
- } else {
4866
+ {
4839
4867
  const childKey = `${key}.${child.key}`;
4840
4868
  const childResult = validateElement(
4841
4869
  { ...child, key: childKey },
@@ -8124,7 +8152,8 @@ function createInstanceState(config) {
8124
8152
  ...config,
8125
8153
  translations: mergedTranslations
8126
8154
  },
8127
- debounceTimer: null
8155
+ debounceTimer: null,
8156
+ prefill: {}
8128
8157
  };
8129
8158
  }
8130
8159
  function generateInstanceId() {
@@ -8322,6 +8351,26 @@ function applyActionButtonStyles(button, isFormLevel = false) {
8322
8351
  }
8323
8352
 
8324
8353
  // src/components/registry.ts
8354
+ function validateHiddenElement(element, key, context) {
8355
+ const { scopeRoot } = context;
8356
+ const input = scopeRoot.querySelector(
8357
+ `input[type="hidden"][data-hidden-field="true"][name="${key}"]`
8358
+ );
8359
+ const raw = input?.value ?? "";
8360
+ if (raw === "") {
8361
+ const defaultVal = "default" in element ? element.default : null;
8362
+ return { value: defaultVal !== void 0 ? defaultVal : null, errors: [] };
8363
+ }
8364
+ return { value: deserializeHiddenValue(raw), errors: [] };
8365
+ }
8366
+ function updateHiddenField(_element, fieldPath, value, context) {
8367
+ const { scopeRoot } = context;
8368
+ const input = scopeRoot.querySelector(
8369
+ `input[type="hidden"][data-hidden-field="true"][name="${fieldPath}"]`
8370
+ );
8371
+ if (!input) return;
8372
+ input.value = serializeHiddenValue(value);
8373
+ }
8325
8374
  var componentRegistry = {
8326
8375
  text: {
8327
8376
  validate: validateTextElement,
@@ -8376,6 +8425,11 @@ var componentRegistry = {
8376
8425
  richinput: {
8377
8426
  validate: validateRichInputElement,
8378
8427
  update: updateRichInputField
8428
+ },
8429
+ hidden: {
8430
+ // Legacy type: `type: "hidden"` — reads/writes DOM <input type="hidden"> element
8431
+ validate: validateHiddenElement,
8432
+ update: updateHiddenField
8379
8433
  }
8380
8434
  };
8381
8435
  function getComponentOperations(elementType) {
@@ -8785,6 +8839,7 @@ var FormBuilderInstance = class {
8785
8839
  this.state.formRoot = root;
8786
8840
  this.state.schema = schema;
8787
8841
  this.state.externalActions = actions || null;
8842
+ this.state.prefill = prefill || {};
8788
8843
  clear(root);
8789
8844
  root.setAttribute("data-fb-root", "true");
8790
8845
  injectThemeVariables(root, this.state.config.theme);
@@ -8802,7 +8857,9 @@ var FormBuilderInstance = class {
8802
8857
  fieldsWrapper.className = `grid grid-cols-${columns} gap-4`;
8803
8858
  }
8804
8859
  schema.elements.forEach((element) => {
8805
- if (element.hidden) {
8860
+ if (element.hidden || element.type === "hidden") {
8861
+ const val = prefill?.[element.key] ?? element.default ?? null;
8862
+ fieldsWrapper.appendChild(createHiddenInput(element.key, val));
8806
8863
  return;
8807
8864
  }
8808
8865
  const block = renderElement2(element, {
@@ -8874,8 +8931,16 @@ var FormBuilderInstance = class {
8874
8931
  );
8875
8932
  }
8876
8933
  }
8877
- if (element.hidden) {
8878
- data[element.key] = element.default !== void 0 ? element.default : null;
8934
+ if (element.hidden || element.type === "hidden") {
8935
+ const hiddenInput = this.state.formRoot.querySelector(
8936
+ `input[type="hidden"][data-hidden-field="true"][name="${element.key}"]`
8937
+ );
8938
+ const raw = hiddenInput?.value ?? "";
8939
+ if (raw !== "") {
8940
+ data[element.key] = deserializeHiddenValue(raw);
8941
+ } else {
8942
+ data[element.key] = element.default !== void 0 ? element.default : null;
8943
+ }
8879
8944
  } else {
8880
8945
  const result = validateElement2(element, { path: "" });
8881
8946
  if (result.spread && result.value !== null && typeof result.value === "object") {