@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.
@@ -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) {
@@ -2274,6 +2294,7 @@ async function renderUploadedFilePreview(container, resourceId, fileName, meta,
2274
2294
  }
2275
2295
  }
2276
2296
  async function renderFilePreview(container, resourceId, state, options = {}) {
2297
+ var _a;
2277
2298
  const { fileName = "", isReadonly = false, deps = null } = options;
2278
2299
  if (!isReadonly && deps && (!deps.picker || !deps.fileUploadHandler || !deps.dragHandler)) {
2279
2300
  throw new Error(
@@ -2303,6 +2324,10 @@ async function renderFilePreview(container, resourceId, state, options = {}) {
2303
2324
  meta,
2304
2325
  state
2305
2326
  );
2327
+ const isVideo = (_a = meta == null ? void 0 : meta.type) == null ? void 0 : _a.startsWith("video/");
2328
+ if (!isReadonly && !isVideo) {
2329
+ renderDeleteButton(container, resourceId, state);
2330
+ }
2306
2331
  }
2307
2332
  }
2308
2333
  async function renderFilePreviewReadonly(resourceId, state, fileName) {
@@ -4556,7 +4581,11 @@ function renderSingleContainerElement(element, ctx, wrapper, pathKey) {
4556
4581
  state: ctx.state
4557
4582
  };
4558
4583
  element.elements.forEach((child) => {
4559
- if (!child.hidden) {
4584
+ var _a2, _b2;
4585
+ if (child.hidden || child.type === "hidden") {
4586
+ const prefillVal = (_b2 = (_a2 = containerPrefill[child.key]) != null ? _a2 : child.default) != null ? _b2 : null;
4587
+ itemsWrap.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), prefillVal));
4588
+ } else {
4560
4589
  itemsWrap.appendChild(renderElement(child, subCtx));
4561
4590
  }
4562
4591
  });
@@ -4624,7 +4653,10 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4624
4653
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4625
4654
  }
4626
4655
  element.elements.forEach((child) => {
4627
- if (!child.hidden) {
4656
+ var _a2;
4657
+ if (child.hidden || child.type === "hidden") {
4658
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), (_a2 = child.default) != null ? _a2 : null));
4659
+ } else {
4628
4660
  childWrapper.appendChild(renderElement(child, subCtx));
4629
4661
  }
4630
4662
  });
@@ -4694,7 +4726,11 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4694
4726
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4695
4727
  }
4696
4728
  element.elements.forEach((child) => {
4697
- if (!child.hidden) {
4729
+ var _a3, _b2;
4730
+ if (child.hidden || child.type === "hidden") {
4731
+ const prefillVal = (_b2 = (_a3 = prefillObj == null ? void 0 : prefillObj[child.key]) != null ? _a3 : child.default) != null ? _b2 : null;
4732
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), prefillVal));
4733
+ } else {
4698
4734
  childWrapper.appendChild(renderElement(child, subCtx));
4699
4735
  }
4700
4736
  });
@@ -4744,7 +4780,10 @@ function renderMultipleContainerElement(element, ctx, wrapper, _pathKey) {
4744
4780
  childWrapper.className = `grid grid-cols-${columns} gap-4`;
4745
4781
  }
4746
4782
  element.elements.forEach((child) => {
4747
- if (!child.hidden) {
4783
+ var _a2;
4784
+ if (child.hidden || child.type === "hidden") {
4785
+ childWrapper.appendChild(createHiddenInput(pathJoin(subCtx.path, child.key), (_a2 = child.default) != null ? _a2 : null));
4786
+ } else {
4748
4787
  childWrapper.appendChild(renderElement(child, subCtx));
4749
4788
  }
4750
4789
  });
@@ -4857,20 +4896,16 @@ function validateContainerElement(element, key, context) {
4857
4896
  );
4858
4897
  }
4859
4898
  }
4860
- if (child.hidden || child.type === "hidden") {
4861
- itemData[child.key] = child.default !== void 0 ? child.default : null;
4899
+ const childKey = `${key}[${domIndex}].${child.key}`;
4900
+ const childResult = validateElement(
4901
+ { ...child, key: childKey },
4902
+ { path },
4903
+ itemContainer
4904
+ );
4905
+ if (childResult.spread && childResult.value !== null && typeof childResult.value === "object") {
4906
+ Object.assign(itemData, childResult.value);
4862
4907
  } 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
- }
4908
+ itemData[child.key] = childResult.value;
4874
4909
  }
4875
4910
  });
4876
4911
  items.push(itemData);
@@ -4902,9 +4937,7 @@ function validateContainerElement(element, key, context) {
4902
4937
  );
4903
4938
  }
4904
4939
  }
4905
- if (child.hidden || child.type === "hidden") {
4906
- containerData[child.key] = child.default !== void 0 ? child.default : null;
4907
- } else {
4940
+ {
4908
4941
  const childKey = `${key}.${child.key}`;
4909
4942
  const childResult = validateElement(
4910
4943
  { ...child, key: childKey },
@@ -8263,7 +8296,8 @@ function createInstanceState(config) {
8263
8296
  ...config,
8264
8297
  translations: mergedTranslations
8265
8298
  },
8266
- debounceTimer: null
8299
+ debounceTimer: null,
8300
+ prefill: {}
8267
8301
  };
8268
8302
  }
8269
8303
  function generateInstanceId() {
@@ -8461,6 +8495,27 @@ function applyActionButtonStyles(button, isFormLevel = false) {
8461
8495
  }
8462
8496
 
8463
8497
  // src/components/registry.ts
8498
+ function validateHiddenElement(element, key, context) {
8499
+ var _a;
8500
+ const { scopeRoot } = context;
8501
+ const input = scopeRoot.querySelector(
8502
+ `input[type="hidden"][data-hidden-field="true"][name="${key}"]`
8503
+ );
8504
+ const raw = (_a = input == null ? void 0 : input.value) != null ? _a : "";
8505
+ if (raw === "") {
8506
+ const defaultVal = "default" in element ? element.default : null;
8507
+ return { value: defaultVal !== void 0 ? defaultVal : null, errors: [] };
8508
+ }
8509
+ return { value: deserializeHiddenValue(raw), errors: [] };
8510
+ }
8511
+ function updateHiddenField(_element, fieldPath, value, context) {
8512
+ const { scopeRoot } = context;
8513
+ const input = scopeRoot.querySelector(
8514
+ `input[type="hidden"][data-hidden-field="true"][name="${fieldPath}"]`
8515
+ );
8516
+ if (!input) return;
8517
+ input.value = serializeHiddenValue(value);
8518
+ }
8464
8519
  var componentRegistry = {
8465
8520
  text: {
8466
8521
  validate: validateTextElement,
@@ -8515,6 +8570,11 @@ var componentRegistry = {
8515
8570
  richinput: {
8516
8571
  validate: validateRichInputElement,
8517
8572
  update: updateRichInputField
8573
+ },
8574
+ hidden: {
8575
+ // Legacy type: `type: "hidden"` — reads/writes DOM <input type="hidden"> element
8576
+ validate: validateHiddenElement,
8577
+ update: updateHiddenField
8518
8578
  }
8519
8579
  };
8520
8580
  function getComponentOperations(elementType) {
@@ -8924,6 +8984,7 @@ var FormBuilderInstance = class {
8924
8984
  this.state.formRoot = root;
8925
8985
  this.state.schema = schema;
8926
8986
  this.state.externalActions = actions || null;
8987
+ this.state.prefill = prefill || {};
8927
8988
  clear(root);
8928
8989
  root.setAttribute("data-fb-root", "true");
8929
8990
  injectThemeVariables(root, this.state.config.theme);
@@ -8941,7 +9002,10 @@ var FormBuilderInstance = class {
8941
9002
  fieldsWrapper.className = `grid grid-cols-${columns} gap-4`;
8942
9003
  }
8943
9004
  schema.elements.forEach((element) => {
8944
- if (element.hidden) {
9005
+ var _a, _b;
9006
+ if (element.hidden || element.type === "hidden") {
9007
+ const val = (_b = (_a = prefill == null ? void 0 : prefill[element.key]) != null ? _a : element.default) != null ? _b : null;
9008
+ fieldsWrapper.appendChild(createHiddenInput(element.key, val));
8945
9009
  return;
8946
9010
  }
8947
9011
  const block = renderElement2(element, {
@@ -8997,6 +9061,7 @@ var FormBuilderInstance = class {
8997
9061
  };
8998
9062
  setValidateElement(validateElement2);
8999
9063
  this.state.schema.elements.forEach((element) => {
9064
+ var _a;
9000
9065
  if (element.enableIf) {
9001
9066
  try {
9002
9067
  const shouldEnable = evaluateEnableCondition(
@@ -9013,8 +9078,16 @@ var FormBuilderInstance = class {
9013
9078
  );
9014
9079
  }
9015
9080
  }
9016
- if (element.hidden) {
9017
- data[element.key] = element.default !== void 0 ? element.default : null;
9081
+ if (element.hidden || element.type === "hidden") {
9082
+ const hiddenInput = this.state.formRoot.querySelector(
9083
+ `input[type="hidden"][data-hidden-field="true"][name="${element.key}"]`
9084
+ );
9085
+ const raw = (_a = hiddenInput == null ? void 0 : hiddenInput.value) != null ? _a : "";
9086
+ if (raw !== "") {
9087
+ data[element.key] = deserializeHiddenValue(raw);
9088
+ } else {
9089
+ data[element.key] = element.default !== void 0 ? element.default : null;
9090
+ }
9018
9091
  } else {
9019
9092
  const result = validateElement2(element, { path: "" });
9020
9093
  if (result.spread && result.value !== null && typeof result.value === "object") {