shadcn-zod-formkit 1.20.0 → 1.21.0
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 +128 -77
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +9 -6
- package/dist/index.d.ts +9 -6
- package/dist/index.mjs +128 -78
- package/dist/index.mjs.map +1 -1
- package/dist/shadcn-zod-formkit-1.21.0.tgz +0 -0
- package/package.json +1 -1
- package/dist/shadcn-zod-formkit-1.20.0.tgz +0 -0
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
|
-
|
|
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
|
-
|
|
5497
|
-
|
|
5498
|
-
|
|
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
|
|
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
|
|
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
|
|
5552
|
-
|
|
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,6 +5589,9 @@ 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) => {
|
|
5593
|
+
return !Array.isArray(f);
|
|
5594
|
+
};
|
|
5574
5595
|
var FormFieldsGrid = ({
|
|
5575
5596
|
fields,
|
|
5576
5597
|
form,
|
|
@@ -5579,47 +5600,54 @@ var FormFieldsGrid = ({
|
|
|
5579
5600
|
className = "",
|
|
5580
5601
|
gap = "gap-2"
|
|
5581
5602
|
}) => {
|
|
5582
|
-
|
|
5583
|
-
|
|
5584
|
-
|
|
5585
|
-
|
|
5586
|
-
|
|
5587
|
-
|
|
5588
|
-
|
|
5589
|
-
|
|
5590
|
-
|
|
5591
|
-
|
|
5592
|
-
|
|
5593
|
-
|
|
5594
|
-
|
|
5595
|
-
|
|
5596
|
-
|
|
5597
|
-
|
|
5598
|
-
|
|
5599
|
-
|
|
5600
|
-
|
|
5601
|
-
|
|
5602
|
-
|
|
5603
|
-
|
|
5604
|
-
|
|
5605
|
-
|
|
5606
|
-
|
|
5607
|
-
|
|
5608
|
-
|
|
5609
|
-
|
|
5610
|
-
|
|
5611
|
-
|
|
5612
|
-
|
|
5613
|
-
|
|
5614
|
-
|
|
5615
|
-
|
|
5616
|
-
|
|
5617
|
-
|
|
5618
|
-
|
|
5619
|
-
|
|
5620
|
-
|
|
5621
|
-
|
|
5622
|
-
|
|
5603
|
+
const renderField = (field) => {
|
|
5604
|
+
const fieldCopy = {
|
|
5605
|
+
...field,
|
|
5606
|
+
disabled: readOnly ? true : field.disabled
|
|
5607
|
+
};
|
|
5608
|
+
const renderUp = fieldCopy.childrenPosition !== "down" && isRenderableChild(fieldCopy.children);
|
|
5609
|
+
const renderDown = fieldCopy.childrenPosition === "down" && isRenderableChild(fieldCopy.children);
|
|
5610
|
+
const dirClass = fieldCopy.direction === "row" ? "flex flex-row items-center gap-4 w-full px-2" : "flex flex-col gap-2 w-full px-2";
|
|
5611
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: dirClass, children: [
|
|
5612
|
+
renderUp && /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fieldCopy.children }),
|
|
5613
|
+
InputFactory.create(fieldCopy, form, isPending),
|
|
5614
|
+
renderDown && /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fieldCopy.children })
|
|
5615
|
+
] });
|
|
5616
|
+
};
|
|
5617
|
+
const renderGroup = (group, groupDirection = "row") => {
|
|
5618
|
+
const dirClass = groupDirection === "row" ? "flex flex-row items-start gap-4 py-3 w-full" : "flex flex-col gap-4 py-3 w-full";
|
|
5619
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { className: dirClass, children: group.map(
|
|
5620
|
+
(f, idx) => isFieldProps(f) ? renderField(f) : Array.isArray(f) ? (
|
|
5621
|
+
// ⚠ Solo soportamos [[FieldProps]] como subgrupo
|
|
5622
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex flex-row items-start gap-4", children: f.map(
|
|
5623
|
+
(subField) => isFieldProps(subField) ? renderField(subField) : null
|
|
5624
|
+
) }, idx)
|
|
5625
|
+
) : null
|
|
5626
|
+
) });
|
|
5627
|
+
};
|
|
5628
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { className: `w-full grid grid-cols-1 ${gap} ${className}`, children: fields.map((f, idx) => {
|
|
5629
|
+
if (isFieldProps(f)) {
|
|
5630
|
+
return renderField(f);
|
|
5631
|
+
} else if (Array.isArray(f)) {
|
|
5632
|
+
if (f.length > 0 && Array.isArray(f[0])) {
|
|
5633
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-4 py-3", children: f.map((subGroup, subIdx) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
5634
|
+
"div",
|
|
5635
|
+
{
|
|
5636
|
+
className: "w-full flex flex-row items-start gap-4",
|
|
5637
|
+
children: Array.isArray(subGroup) && subGroup.map(
|
|
5638
|
+
(subField) => isFieldProps(subField) ? renderField(subField) : null
|
|
5639
|
+
)
|
|
5640
|
+
},
|
|
5641
|
+
subIdx
|
|
5642
|
+
)) }, idx);
|
|
5643
|
+
} else {
|
|
5644
|
+
const firstField = f[0];
|
|
5645
|
+
const direction = firstField?.direction ?? "row";
|
|
5646
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { children: renderGroup(f, direction) }, idx);
|
|
5647
|
+
}
|
|
5648
|
+
}
|
|
5649
|
+
return null;
|
|
5650
|
+
}) });
|
|
5623
5651
|
};
|
|
5624
5652
|
var DynamicForm = ({
|
|
5625
5653
|
formTitle,
|
|
@@ -5642,7 +5670,10 @@ var DynamicForm = ({
|
|
|
5642
5670
|
submitBtnLabel = "Guardar"
|
|
5643
5671
|
}) => {
|
|
5644
5672
|
const [isPending, startTransition] = React3.useTransition();
|
|
5645
|
-
const schema = React3.useMemo(() =>
|
|
5673
|
+
const schema = React3.useMemo(() => {
|
|
5674
|
+
const allFields = flattenFields(fields);
|
|
5675
|
+
return getDynamicSchema(allFields, extraValidations);
|
|
5676
|
+
}, [fields, extraValidations]);
|
|
5646
5677
|
const resolver = zod.zodResolver(schema);
|
|
5647
5678
|
const initialValues = React3.useMemo(() => getDefaultValues(record), [record]);
|
|
5648
5679
|
const form = reactHookForm.useForm({
|
|
@@ -5678,7 +5709,13 @@ var DynamicForm = ({
|
|
|
5678
5709
|
] }),
|
|
5679
5710
|
childrenHeader && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-row items-center gap-2 w-full h-full", children: childrenHeader })
|
|
5680
5711
|
] }),
|
|
5681
|
-
withErrorsAlert && errorAlertPosition === "up" && /* @__PURE__ */ jsxRuntime.jsx(
|
|
5712
|
+
withErrorsAlert && errorAlertPosition === "up" && /* @__PURE__ */ jsxRuntime.jsx(
|
|
5713
|
+
FormErrorsAlert,
|
|
5714
|
+
{
|
|
5715
|
+
formState: form.formState,
|
|
5716
|
+
fields
|
|
5717
|
+
}
|
|
5718
|
+
),
|
|
5682
5719
|
/* @__PURE__ */ jsxRuntime.jsx(Form, { ...form, children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
5683
5720
|
"form",
|
|
5684
5721
|
{
|
|
@@ -5686,10 +5723,17 @@ var DynamicForm = ({
|
|
|
5686
5723
|
className: `flex flex-col gap-2 ${readOnly ? "opacity-70 pointer-events-none select-none" : ""}`,
|
|
5687
5724
|
children: [
|
|
5688
5725
|
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full grid grid-cols-1", children: [
|
|
5689
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
5726
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
5727
|
+
FormFieldsGrid,
|
|
5728
|
+
{
|
|
5729
|
+
fields,
|
|
5730
|
+
form,
|
|
5731
|
+
readOnly
|
|
5732
|
+
}
|
|
5733
|
+
),
|
|
5690
5734
|
children && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-row items-center gap-2 w-full h-full", children })
|
|
5691
5735
|
] }),
|
|
5692
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ButtonGroup, { className: "flex flex-row
|
|
5736
|
+
/* @__PURE__ */ jsxRuntime.jsxs(ButtonGroup, { className: "flex flex-row w-full", children: [
|
|
5693
5737
|
listBtnConfig.map((btn, key) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
5694
5738
|
Button,
|
|
5695
5739
|
{
|
|
@@ -5724,7 +5768,13 @@ var DynamicForm = ({
|
|
|
5724
5768
|
]
|
|
5725
5769
|
}
|
|
5726
5770
|
) }),
|
|
5727
|
-
withErrorsAlert && errorAlertPosition === "down" && /* @__PURE__ */ jsxRuntime.jsx(
|
|
5771
|
+
withErrorsAlert && errorAlertPosition === "down" && /* @__PURE__ */ jsxRuntime.jsx(
|
|
5772
|
+
FormErrorsAlert,
|
|
5773
|
+
{
|
|
5774
|
+
formState: form.formState,
|
|
5775
|
+
fields: fields.flatMap((f) => Array.isArray(f) ? f : [f])
|
|
5776
|
+
}
|
|
5777
|
+
)
|
|
5728
5778
|
] });
|
|
5729
5779
|
if (!withCard) return formContent;
|
|
5730
5780
|
return /* @__PURE__ */ jsxRuntime.jsx(Card, { children: /* @__PURE__ */ jsxRuntime.jsx(CardContent, { children: formContent }) });
|
|
@@ -6002,6 +6052,7 @@ exports.entitiesToGroupedOption = entitiesToGroupedOption;
|
|
|
6002
6052
|
exports.entitiesToInputOption = entitiesToInputOption;
|
|
6003
6053
|
exports.entityToGroupedOption = entityToGroupedOption;
|
|
6004
6054
|
exports.entityToInputOption = entityToInputOption;
|
|
6055
|
+
exports.flattenFields = flattenFields;
|
|
6005
6056
|
exports.getDefaultValues = getDefaultValues;
|
|
6006
6057
|
exports.getDynamicSchema = getDynamicSchema;
|
|
6007
6058
|
exports.getFieldLabel = getFieldLabel;
|