shadcn-zod-formkit 1.20.0 → 1.21.1

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/index.cjs CHANGED
@@ -234,6 +234,19 @@ var entitiesToGroupedOption = (data, optionValue = "name") => {
234
234
  };
235
235
 
236
236
  // src/components/custom/form/inputs/base/definitions.ts
237
+ var flattenFields = (fields) => {
238
+ const result = [];
239
+ for (const field of fields) {
240
+ if (Array.isArray(field)) {
241
+ result.push(...flattenFields(field));
242
+ } else if (field.fields) {
243
+ result.push(...flattenFields(field.fields));
244
+ } else {
245
+ result.push(field);
246
+ }
247
+ }
248
+ return result;
249
+ };
237
250
  var TextInputType = /* @__PURE__ */ ((TextInputType2) => {
238
251
  TextInputType2["DEFAULT"] = "default";
239
252
  TextInputType2["NUMBER"] = "number";
@@ -1817,7 +1830,7 @@ var FieldButtonGroup = ({ input, form, isSubmitting, className = "w-full flex-1"
1817
1830
  if (input.listConfig?.onOptionChange) input.listConfig.onOptionChange(value);
1818
1831
  };
1819
1832
  const selectedValue = form.watch(input.name);
1820
- return /* @__PURE__ */ jsxRuntime.jsx(ButtonGroup, { className: "flex flex-row w-full", children: options.map((option) => /* @__PURE__ */ jsxRuntime.jsx(
1833
+ return /* @__PURE__ */ jsxRuntime.jsx(ButtonGroup, { className: "flex flex-row w-full", children: options.map((option, key) => /* @__PURE__ */ jsxRuntime.jsx(
1821
1834
  Button,
1822
1835
  {
1823
1836
  type: "button",
@@ -1827,7 +1840,7 @@ var FieldButtonGroup = ({ input, form, isSubmitting, className = "w-full flex-1"
1827
1840
  disabled: isSubmitting,
1828
1841
  children: option.label ?? option.name
1829
1842
  },
1830
- option.value
1843
+ key
1831
1844
  )) });
1832
1845
  };
1833
1846
  var CheckListInput = class extends BaseInput {
@@ -5493,32 +5506,12 @@ var InputFactory = class {
5493
5506
  function getDefaultValues(entity, fields) {
5494
5507
  const defaults = {};
5495
5508
  if (entity) {
5496
- for (const key in entity) {
5497
- const value = entity[key];
5498
- if (value === null || value === void 0) {
5499
- defaults[key] = "";
5500
- continue;
5501
- }
5502
- switch (typeof value) {
5503
- case "string":
5504
- defaults[key] = value;
5505
- break;
5506
- case "number":
5507
- defaults[key] = value;
5508
- break;
5509
- case "boolean":
5510
- defaults[key] = value;
5511
- break;
5512
- case "object":
5513
- defaults[key] = Array.isArray(value) ? [...value] : { ...value };
5514
- break;
5515
- default:
5516
- defaults[key] = value;
5517
- }
5518
- }
5509
+ Object.entries(entity).forEach(([key, value]) => {
5510
+ defaults[key] = value ?? "";
5511
+ });
5519
5512
  }
5520
5513
  if (fields) {
5521
- const flatFields = fields.flat();
5514
+ const flatFields = flattenFields2(fields);
5522
5515
  for (const field of flatFields) {
5523
5516
  const key = field.name;
5524
5517
  if (defaults[key] === void 0) {
@@ -5528,10 +5521,21 @@ function getDefaultValues(entity, fields) {
5528
5521
  }
5529
5522
  return defaults;
5530
5523
  }
5524
+ var flattenFields2 = (fields) => {
5525
+ const result = [];
5526
+ for (const field of fields) {
5527
+ if (Array.isArray(field)) {
5528
+ result.push(...flattenFields2(field));
5529
+ } else if (field.fields) {
5530
+ result.push(...flattenFields2(field.fields));
5531
+ } else {
5532
+ result.push(field);
5533
+ }
5534
+ }
5535
+ return result;
5536
+ };
5531
5537
  var getDynamicSchema = (fields, extraValidations) => {
5532
- const flatFields = fields.flatMap(
5533
- (f) => Array.isArray(f) ? f : [f]
5534
- );
5538
+ const flatFields = flattenFields2(fields);
5535
5539
  const shape = flatFields.reduce((acc, f) => {
5536
5540
  acc[f.name] = f.zodType ?? z2__default.default.any();
5537
5541
  return acc;
@@ -5544,12 +5548,26 @@ var getDynamicSchema = (fields, extraValidations) => {
5544
5548
  }
5545
5549
  return schema;
5546
5550
  };
5551
+ var flattenFields3 = (fields) => {
5552
+ const result = [];
5553
+ for (const field of fields) {
5554
+ if (Array.isArray(field)) {
5555
+ result.push(...flattenFields3(field));
5556
+ } else if (field.fields) {
5557
+ result.push(...flattenFields3(field.fields));
5558
+ } else {
5559
+ result.push(field);
5560
+ }
5561
+ }
5562
+ return result;
5563
+ };
5547
5564
  var FormErrorsAlert = ({
5548
5565
  formState,
5549
5566
  fields
5550
5567
  }) => {
5551
- const flatFields = fields.flatMap((f) => Array.isArray(f) ? f : [f]);
5552
- return /* @__PURE__ */ jsxRuntime.jsx("div", { style: { marginTop: 4 }, children: Object.entries(formState.errors).length > 0 && /* @__PURE__ */ jsxRuntime.jsx(
5568
+ const flatFields = flattenFields3(fields);
5569
+ const hasErrors = Object.keys(formState.errors).length > 0;
5570
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { style: { marginTop: 4 }, children: hasErrors && /* @__PURE__ */ jsxRuntime.jsx(
5553
5571
  CustomAlert,
5554
5572
  {
5555
5573
  title: "Revisar los siguientes criterios",
@@ -5571,55 +5589,48 @@ var getFieldLabel = (fieldErrorKey, fields) => {
5571
5589
  return foundField?.label ?? fieldErrorKey;
5572
5590
  };
5573
5591
  var isRenderableChild = (c) => c !== void 0 && c !== null && typeof c !== "function";
5592
+ var isFieldProps = (f) => !Array.isArray(f);
5574
5593
  var FormFieldsGrid = ({
5575
5594
  fields,
5576
5595
  form,
5577
5596
  isPending,
5578
5597
  readOnly,
5579
5598
  className = "",
5580
- gap = "gap-2"
5599
+ gap = "gap-4"
5581
5600
  }) => {
5582
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: `w-full grid grid-cols-1 ${gap} ${className}`, children: fields.map(
5583
- (inputOrGroup, idx) => Array.isArray(inputOrGroup) ? /* @__PURE__ */ jsxRuntime.jsx(
5584
- "div",
5585
- {
5586
- className: "w-full flex flex-row items-start gap-4 py-3",
5587
- children: inputOrGroup.map((field, subIdx) => {
5588
- const fieldCopy = {
5589
- ...field,
5590
- disabled: readOnly ? true : field.disabled
5591
- };
5592
- const renderInlineChild = fieldCopy.childrenPosition !== "down" && isRenderableChild(fieldCopy.children);
5593
- const renderInlineChildDown = fieldCopy.childrenPosition === "down" && isRenderableChild(fieldCopy.children);
5594
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full px-2", children: [
5595
- renderInlineChild && /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fieldCopy.children }),
5596
- InputFactory.create(fieldCopy, form, isPending),
5597
- renderInlineChildDown && /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fieldCopy.children })
5598
- ] }, `field-${idx}-${subIdx}`);
5599
- })
5600
- },
5601
- `field-group-${idx}`
5602
- ) : /* @__PURE__ */ jsxRuntime.jsx(
5603
- "div",
5604
- {
5605
- className: "flex flex-col justify-between py-3 w-full px-2",
5606
- children: (() => {
5607
- const fieldCopy = {
5608
- ...inputOrGroup,
5609
- disabled: readOnly ? true : inputOrGroup.disabled
5610
- };
5611
- const renderUp = fieldCopy.childrenPosition !== "down" && isRenderableChild(fieldCopy.children);
5612
- const renderDown = fieldCopy.childrenPosition === "down" && isRenderableChild(fieldCopy.children);
5613
- return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
5614
- renderUp && /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fieldCopy.children }),
5615
- InputFactory.create(fieldCopy, form, isPending),
5616
- renderDown && /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fieldCopy.children })
5617
- ] });
5618
- })()
5619
- },
5620
- `field-single-${idx}`
5621
- )
5622
- ) });
5601
+ const renderField = (field) => {
5602
+ const fieldCopy = { ...field, disabled: readOnly ? true : field.disabled };
5603
+ const renderUp = fieldCopy.childrenPosition !== "down" && isRenderableChild(fieldCopy.children);
5604
+ const renderDown = fieldCopy.childrenPosition === "down" && isRenderableChild(fieldCopy.children);
5605
+ const dirClass = fieldCopy.direction === "row" ? "flex flex-row items-center gap-4" : "flex flex-col gap-2";
5606
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: `${dirClass} w-full`, children: [
5607
+ renderUp && /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fieldCopy.children }),
5608
+ InputFactory.create(fieldCopy, form, isPending),
5609
+ renderDown && /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fieldCopy.children })
5610
+ ] });
5611
+ };
5612
+ const renderColumn = (col) => {
5613
+ if (col.length === 0) return null;
5614
+ const colDirection = isFieldProps(col[0]) && col[0].direction === "row" ? "flex flex-row gap-4" : "flex flex-col gap-2";
5615
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: `${colDirection} flex-1`, children: col.map((item, idx) => {
5616
+ if (isFieldProps(item)) return renderField(item);
5617
+ if (Array.isArray(item)) return renderColumn(item);
5618
+ return null;
5619
+ }) });
5620
+ };
5621
+ const renderRow = (row) => {
5622
+ if (row.length === 0) return null;
5623
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex flex-row gap-4 py-2", children: row.map((col, idx) => {
5624
+ if (isFieldProps(col)) return renderColumn([col]);
5625
+ if (Array.isArray(col)) return renderColumn(col);
5626
+ return null;
5627
+ }) });
5628
+ };
5629
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: `w-full flex flex-col gap-4 ${className}`, children: fields.map((f, idx) => {
5630
+ if (isFieldProps(f)) return /* @__PURE__ */ jsxRuntime.jsx("div", { children: renderField(f) }, idx);
5631
+ if (Array.isArray(f)) return /* @__PURE__ */ jsxRuntime.jsx("div", { children: renderRow(f) }, idx);
5632
+ return null;
5633
+ }) });
5623
5634
  };
5624
5635
  var DynamicForm = ({
5625
5636
  formTitle,
@@ -5642,7 +5653,10 @@ var DynamicForm = ({
5642
5653
  submitBtnLabel = "Guardar"
5643
5654
  }) => {
5644
5655
  const [isPending, startTransition] = React3.useTransition();
5645
- const schema = React3.useMemo(() => getDynamicSchema(fields, extraValidations), [fields, extraValidations]);
5656
+ const schema = React3.useMemo(() => {
5657
+ const allFields = flattenFields(fields);
5658
+ return getDynamicSchema(allFields, extraValidations);
5659
+ }, [fields, extraValidations]);
5646
5660
  const resolver = zod.zodResolver(schema);
5647
5661
  const initialValues = React3.useMemo(() => getDefaultValues(record), [record]);
5648
5662
  const form = reactHookForm.useForm({
@@ -5678,7 +5692,13 @@ var DynamicForm = ({
5678
5692
  ] }),
5679
5693
  childrenHeader && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-row items-center gap-2 w-full h-full", children: childrenHeader })
5680
5694
  ] }),
5681
- withErrorsAlert && errorAlertPosition === "up" && /* @__PURE__ */ jsxRuntime.jsx(FormErrorsAlert, { formState: form.formState, fields }),
5695
+ withErrorsAlert && errorAlertPosition === "up" && /* @__PURE__ */ jsxRuntime.jsx(
5696
+ FormErrorsAlert,
5697
+ {
5698
+ formState: form.formState,
5699
+ fields
5700
+ }
5701
+ ),
5682
5702
  /* @__PURE__ */ jsxRuntime.jsx(Form, { ...form, children: /* @__PURE__ */ jsxRuntime.jsxs(
5683
5703
  "form",
5684
5704
  {
@@ -5686,10 +5706,17 @@ var DynamicForm = ({
5686
5706
  className: `flex flex-col gap-2 ${readOnly ? "opacity-70 pointer-events-none select-none" : ""}`,
5687
5707
  children: [
5688
5708
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full grid grid-cols-1", children: [
5689
- /* @__PURE__ */ jsxRuntime.jsx(FormFieldsGrid, { fields, form, readOnly }),
5709
+ /* @__PURE__ */ jsxRuntime.jsx(
5710
+ FormFieldsGrid,
5711
+ {
5712
+ fields,
5713
+ form,
5714
+ readOnly
5715
+ }
5716
+ ),
5690
5717
  children && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-row items-center gap-2 w-full h-full", children })
5691
5718
  ] }),
5692
- /* @__PURE__ */ jsxRuntime.jsxs(ButtonGroup, { className: "flex flex-row w-full", children: [
5719
+ /* @__PURE__ */ jsxRuntime.jsxs(ButtonGroup, { className: "flex flex-row w-full", children: [
5693
5720
  listBtnConfig.map((btn, key) => /* @__PURE__ */ jsxRuntime.jsx(
5694
5721
  Button,
5695
5722
  {
@@ -5724,7 +5751,13 @@ var DynamicForm = ({
5724
5751
  ]
5725
5752
  }
5726
5753
  ) }),
5727
- withErrorsAlert && errorAlertPosition === "down" && /* @__PURE__ */ jsxRuntime.jsx(FormErrorsAlert, { formState: form.formState, fields })
5754
+ withErrorsAlert && errorAlertPosition === "down" && /* @__PURE__ */ jsxRuntime.jsx(
5755
+ FormErrorsAlert,
5756
+ {
5757
+ formState: form.formState,
5758
+ fields: fields.flatMap((f) => Array.isArray(f) ? f : [f])
5759
+ }
5760
+ )
5728
5761
  ] });
5729
5762
  if (!withCard) return formContent;
5730
5763
  return /* @__PURE__ */ jsxRuntime.jsx(Card, { children: /* @__PURE__ */ jsxRuntime.jsx(CardContent, { children: formContent }) });
@@ -6002,6 +6035,7 @@ exports.entitiesToGroupedOption = entitiesToGroupedOption;
6002
6035
  exports.entitiesToInputOption = entitiesToInputOption;
6003
6036
  exports.entityToGroupedOption = entityToGroupedOption;
6004
6037
  exports.entityToInputOption = entityToInputOption;
6038
+ exports.flattenFields = flattenFields;
6005
6039
  exports.getDefaultValues = getDefaultValues;
6006
6040
  exports.getDynamicSchema = getDynamicSchema;
6007
6041
  exports.getFieldLabel = getFieldLabel;