shadcn-zod-formkit 1.8.0 → 1.9.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 +53 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +13 -12
- package/dist/index.d.ts +13 -12
- package/dist/index.mjs +53 -25
- package/dist/index.mjs.map +1 -1
- package/dist/shadcn-zod-formkit-1.9.1.tgz +0 -0
- package/package.json +1 -1
- package/dist/shadcn-zod-formkit-1.8.0.tgz +0 -0
package/dist/index.cjs
CHANGED
|
@@ -5065,6 +5065,7 @@ var InputFactory = class {
|
|
|
5065
5065
|
};
|
|
5066
5066
|
function getDefaultValues(entity) {
|
|
5067
5067
|
const defaults = {};
|
|
5068
|
+
if (!entity) return defaults;
|
|
5068
5069
|
for (const key in entity) {
|
|
5069
5070
|
const value = entity[key];
|
|
5070
5071
|
if (value === null || value === void 0) {
|
|
@@ -5095,26 +5096,31 @@ function getDefaultValues(entity) {
|
|
|
5095
5096
|
return defaults;
|
|
5096
5097
|
}
|
|
5097
5098
|
var getDynamicSchema = (fields, extraValidations) => {
|
|
5098
|
-
const flatFields = fields.flatMap(
|
|
5099
|
-
|
|
5100
|
-
|
|
5101
|
-
|
|
5102
|
-
|
|
5099
|
+
const flatFields = fields.flatMap(
|
|
5100
|
+
(f) => Array.isArray(f) ? f : [f]
|
|
5101
|
+
);
|
|
5102
|
+
const shape = flatFields.reduce((acc, f) => {
|
|
5103
|
+
acc[f.name] = f.zodType ?? z2__default.default.any();
|
|
5104
|
+
return acc;
|
|
5105
|
+
}, {});
|
|
5103
5106
|
let schema = z2__default.default.object(shape);
|
|
5104
|
-
if (extraValidations
|
|
5105
|
-
|
|
5107
|
+
if (extraValidations?.length) {
|
|
5108
|
+
for (const fn of extraValidations) {
|
|
5106
5109
|
schema = fn(schema);
|
|
5107
|
-
}
|
|
5110
|
+
}
|
|
5108
5111
|
}
|
|
5109
5112
|
return schema;
|
|
5110
5113
|
};
|
|
5111
|
-
var FormErrorsAlert = ({
|
|
5114
|
+
var FormErrorsAlert = ({
|
|
5115
|
+
formState,
|
|
5116
|
+
fields
|
|
5117
|
+
}) => {
|
|
5112
5118
|
const flatFields = fields.flatMap((f) => Array.isArray(f) ? f : [f]);
|
|
5113
5119
|
return /* @__PURE__ */ jsxRuntime.jsx("div", { style: { marginTop: 4 }, children: Object.entries(formState.errors).length > 0 && /* @__PURE__ */ jsxRuntime.jsx(
|
|
5114
5120
|
CustomAlert,
|
|
5115
5121
|
{
|
|
5116
5122
|
title: "Revisar los siguientes criterios",
|
|
5117
|
-
description: /* @__PURE__ */ jsxRuntime.jsx("ul", { children: Object.entries(formState
|
|
5123
|
+
description: /* @__PURE__ */ jsxRuntime.jsx("ul", { children: Object.entries(formState.errors).map(([key, value]) => /* @__PURE__ */ jsxRuntime.jsxs("li", { children: [
|
|
5118
5124
|
/* @__PURE__ */ jsxRuntime.jsxs("strong", { children: [
|
|
5119
5125
|
getFieldLabel(key, flatFields),
|
|
5120
5126
|
":"
|
|
@@ -5128,15 +5134,36 @@ var FormErrorsAlert = ({ formState, fields }) => {
|
|
|
5128
5134
|
) });
|
|
5129
5135
|
};
|
|
5130
5136
|
var getFieldLabel = (fieldErrorKey, fields) => {
|
|
5131
|
-
const
|
|
5132
|
-
return
|
|
5137
|
+
const foundField = fields.find((field) => field.name === fieldErrorKey);
|
|
5138
|
+
return foundField?.label ?? fieldErrorKey;
|
|
5133
5139
|
};
|
|
5134
|
-
var FormFieldsGrid = ({
|
|
5135
|
-
|
|
5136
|
-
|
|
5137
|
-
|
|
5138
|
-
|
|
5139
|
-
|
|
5140
|
+
var FormFieldsGrid = ({
|
|
5141
|
+
fields,
|
|
5142
|
+
form,
|
|
5143
|
+
isPending,
|
|
5144
|
+
readOnly,
|
|
5145
|
+
className = "",
|
|
5146
|
+
gap = "gap-2"
|
|
5147
|
+
}) => {
|
|
5148
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { className: `w-full grid grid-cols-1 ${gap} ${className}`, children: fields.map(
|
|
5149
|
+
(input, idx) => Array.isArray(input) ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
5150
|
+
"span",
|
|
5151
|
+
{
|
|
5152
|
+
className: "w-full flex flex-row justify-between py-3",
|
|
5153
|
+
children: input.map((field, subIdx) => {
|
|
5154
|
+
if (readOnly) field.disabled = true;
|
|
5155
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full px-2", children: InputFactory.create(field, form, isPending) }, subIdx);
|
|
5156
|
+
})
|
|
5157
|
+
},
|
|
5158
|
+
`field-group-${idx}`
|
|
5159
|
+
) : /* @__PURE__ */ jsxRuntime.jsx(
|
|
5160
|
+
"span",
|
|
5161
|
+
{
|
|
5162
|
+
className: "flex flex-col justify-between py-3 w-full px-2",
|
|
5163
|
+
children: InputFactory.create(input, form, isPending)
|
|
5164
|
+
},
|
|
5165
|
+
`field-group-${idx}`
|
|
5166
|
+
)
|
|
5140
5167
|
) });
|
|
5141
5168
|
};
|
|
5142
5169
|
var DynamicForm = ({
|
|
@@ -5155,14 +5182,15 @@ var DynamicForm = ({
|
|
|
5155
5182
|
}) => {
|
|
5156
5183
|
const [isPending, startTransition] = React3.useTransition();
|
|
5157
5184
|
const schema = React3.useMemo(() => getDynamicSchema(fields, extraValidations), [fields, extraValidations]);
|
|
5158
|
-
const
|
|
5185
|
+
const resolver = zod.zodResolver(schema);
|
|
5186
|
+
const initialValues = React3.useMemo(() => getDefaultValues(record), [record]);
|
|
5159
5187
|
const form = reactHookForm.useForm({
|
|
5160
|
-
resolver
|
|
5161
|
-
defaultValues
|
|
5188
|
+
resolver,
|
|
5189
|
+
defaultValues: initialValues
|
|
5162
5190
|
});
|
|
5163
5191
|
React3.useEffect(() => {
|
|
5164
|
-
form.reset(
|
|
5165
|
-
}, [
|
|
5192
|
+
form.reset(initialValues);
|
|
5193
|
+
}, [initialValues, form]);
|
|
5166
5194
|
const handleSubmit = (data) => {
|
|
5167
5195
|
if (readOnly) return;
|
|
5168
5196
|
try {
|
|
@@ -5171,7 +5199,7 @@ var DynamicForm = ({
|
|
|
5171
5199
|
onSubmit?.(resp);
|
|
5172
5200
|
});
|
|
5173
5201
|
} catch (error) {
|
|
5174
|
-
console.error("Ocurri\xF3 un error al enviar el formulario.");
|
|
5202
|
+
console.error("Ocurri\xF3 un error al enviar el formulario.", error);
|
|
5175
5203
|
}
|
|
5176
5204
|
};
|
|
5177
5205
|
const formContent = /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
@@ -5480,6 +5508,7 @@ exports.entityToGroupedOption = entityToGroupedOption;
|
|
|
5480
5508
|
exports.entityToInputOption = entityToInputOption;
|
|
5481
5509
|
exports.getDefaultValues = getDefaultValues;
|
|
5482
5510
|
exports.getDynamicSchema = getDynamicSchema;
|
|
5511
|
+
exports.getFieldLabel = getFieldLabel;
|
|
5483
5512
|
exports.inputFieldComp = inputFieldComp;
|
|
5484
5513
|
exports.mockFields = mockFields;
|
|
5485
5514
|
exports.useFormField = useFormField;
|