shadcn-zod-formkit 1.9.1 → 1.10.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 +114 -56
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +114 -56
- package/dist/index.mjs.map +1 -1
- package/dist/shadcn-zod-formkit-1.10.0.tgz +0 -0
- package/package.json +1 -1
- package/dist/shadcn-zod-formkit-1.9.1.tgz +0 -0
package/dist/index.cjs
CHANGED
|
@@ -4519,44 +4519,95 @@ var SelectInput = class extends BaseInput {
|
|
|
4519
4519
|
};
|
|
4520
4520
|
var FieldSelect = ({ form, input, isSubmitting }) => {
|
|
4521
4521
|
const mockInputOptions = [
|
|
4522
|
-
{ id: 1, name: "MOCK OPTION - PERMISO 1"
|
|
4523
|
-
{ id: 2, name: "MOCK OPTION - PERMISO 2"
|
|
4524
|
-
{ id: 3, name: "MOCK OPTION - PERMISO 3"
|
|
4525
|
-
{ id: 4, name: "MOCK OPTION - PERMISO 4"
|
|
4522
|
+
{ value: 1, id: 1, name: "MOCK OPTION - PERMISO 1" },
|
|
4523
|
+
{ value: 2, id: 2, name: "MOCK OPTION - PERMISO 2" },
|
|
4524
|
+
{ value: 3, id: 3, name: "MOCK OPTION - PERMISO 3" },
|
|
4525
|
+
{ value: 4, id: 4, name: "MOCK OPTION - PERMISO 4" }
|
|
4526
4526
|
];
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4527
|
+
const [lista, setLista] = React3.useState(
|
|
4528
|
+
input?.listConfig?.list?.every((item) => "name" in item) ? input.listConfig.list : mockInputOptions
|
|
4529
|
+
);
|
|
4530
|
+
const [loading, setLoading] = React3.useState(false);
|
|
4531
|
+
const [value, setValue] = React3.useState(input.value?.toString() ?? "");
|
|
4530
4532
|
const optionValue = input?.listConfig?.optionValue ?? input.optionValue ?? "id";
|
|
4533
|
+
React3.useEffect(() => {
|
|
4534
|
+
const currentValue = form.getValues(input.name);
|
|
4535
|
+
if (!currentValue && input.value) {
|
|
4536
|
+
form.setValue(input.name, input.value);
|
|
4537
|
+
setValue(input.value.toString());
|
|
4538
|
+
}
|
|
4539
|
+
}, [form, input.name, input.value]);
|
|
4540
|
+
React3.useEffect(() => {
|
|
4541
|
+
if (input.dependsOn && input.loadOptions) {
|
|
4542
|
+
const subscription = form.watch(async (values) => {
|
|
4543
|
+
const dependencyValue = values[input.dependsOn];
|
|
4544
|
+
if (dependencyValue) {
|
|
4545
|
+
try {
|
|
4546
|
+
setLoading(true);
|
|
4547
|
+
const newOptions = await input.loadOptions(dependencyValue);
|
|
4548
|
+
setLista(newOptions);
|
|
4549
|
+
} catch (err) {
|
|
4550
|
+
console.error(`Error loading options for ${input.name}:`, err);
|
|
4551
|
+
setLista([]);
|
|
4552
|
+
} finally {
|
|
4553
|
+
setLoading(false);
|
|
4554
|
+
}
|
|
4555
|
+
} else {
|
|
4556
|
+
setLista([]);
|
|
4557
|
+
form.setValue(input.name, "");
|
|
4558
|
+
}
|
|
4559
|
+
});
|
|
4560
|
+
return () => subscription.unsubscribe?.();
|
|
4561
|
+
}
|
|
4562
|
+
}, [form, input.dependsOn, input.loadOptions, input.name]);
|
|
4531
4563
|
const getValue = (item) => {
|
|
4532
|
-
|
|
4533
|
-
return
|
|
4564
|
+
const val = optionValue === "name" ? item.name : item.value?.toString?.() ?? item.id?.toString();
|
|
4565
|
+
return val?.toString() ?? "";
|
|
4534
4566
|
};
|
|
4535
4567
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
4536
4568
|
FormField,
|
|
4537
4569
|
{
|
|
4538
4570
|
control: form.control,
|
|
4539
4571
|
name: input.name,
|
|
4540
|
-
render: ({ field }) =>
|
|
4541
|
-
|
|
4542
|
-
|
|
4543
|
-
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
{
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4572
|
+
render: ({ field }) => {
|
|
4573
|
+
const currentValue = field.value?.toString() ?? value;
|
|
4574
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(FormItem, { className: "flex flex-row items-center justify-between rounded-lg border p-3 shadow-lg bg-blue-100/20", children: [
|
|
4575
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-0.5 pr-5", children: [
|
|
4576
|
+
/* @__PURE__ */ jsxRuntime.jsx(FormLabel, { children: /* @__PURE__ */ jsxRuntime.jsx("b", { children: input.label }) }),
|
|
4577
|
+
input.description && /* @__PURE__ */ jsxRuntime.jsx(FormDescription, { children: input.description }),
|
|
4578
|
+
/* @__PURE__ */ jsxRuntime.jsx(FormMessage, {})
|
|
4579
|
+
] }),
|
|
4580
|
+
/* @__PURE__ */ jsxRuntime.jsx(FormControl, { children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
4581
|
+
Select,
|
|
4582
|
+
{
|
|
4583
|
+
disabled: input.disabled || isSubmitting || loading,
|
|
4584
|
+
onValueChange: (val) => {
|
|
4585
|
+
field.onChange(val);
|
|
4586
|
+
setValue(val);
|
|
4587
|
+
if (input.listConfig?.onOptionChange) {
|
|
4588
|
+
const selectedItem = lista.find(
|
|
4589
|
+
(item) => getValue(item) === val
|
|
4590
|
+
);
|
|
4591
|
+
input.listConfig.onOptionChange(selectedItem);
|
|
4592
|
+
}
|
|
4593
|
+
},
|
|
4594
|
+
value: currentValue || void 0,
|
|
4595
|
+
children: [
|
|
4596
|
+
/* @__PURE__ */ jsxRuntime.jsx(FormControl, { children: /* @__PURE__ */ jsxRuntime.jsx(SelectTrigger, { className: "w-[60%] bg-black/10 dark:bg-white/25", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
4597
|
+
SelectValue,
|
|
4598
|
+
{
|
|
4599
|
+
placeholder: loading ? "Cargando..." : input.placeHolder ?? "Seleccionar"
|
|
4600
|
+
}
|
|
4601
|
+
) }) }),
|
|
4602
|
+
/* @__PURE__ */ jsxRuntime.jsxs(SelectContent, { children: [
|
|
4603
|
+
lista.map((item) => /* @__PURE__ */ jsxRuntime.jsx(SelectItem, { value: getValue(item), children: item.name }, item.id)),
|
|
4604
|
+
lista.length === 0 && !loading && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "p-2 text-sm text-muted-foreground", children: "No hay opciones disponibles" })
|
|
4605
|
+
] })
|
|
4606
|
+
]
|
|
4607
|
+
}
|
|
4608
|
+
) })
|
|
4609
|
+
] });
|
|
4610
|
+
}
|
|
4560
4611
|
},
|
|
4561
4612
|
input.name
|
|
4562
4613
|
);
|
|
@@ -5063,34 +5114,40 @@ var InputFactory = class {
|
|
|
5063
5114
|
return /* @__PURE__ */ jsxRuntime.jsx(Card, { className: "p-4 space-y-3", children: instance.render() });
|
|
5064
5115
|
}
|
|
5065
5116
|
};
|
|
5066
|
-
function getDefaultValues(entity) {
|
|
5117
|
+
function getDefaultValues(entity, fields) {
|
|
5067
5118
|
const defaults = {};
|
|
5068
|
-
if (
|
|
5069
|
-
|
|
5070
|
-
|
|
5071
|
-
|
|
5072
|
-
|
|
5073
|
-
|
|
5074
|
-
|
|
5075
|
-
|
|
5076
|
-
|
|
5077
|
-
|
|
5078
|
-
|
|
5079
|
-
|
|
5080
|
-
|
|
5081
|
-
|
|
5082
|
-
|
|
5083
|
-
|
|
5084
|
-
|
|
5085
|
-
|
|
5086
|
-
|
|
5087
|
-
|
|
5088
|
-
|
|
5089
|
-
defaults[key] =
|
|
5090
|
-
|
|
5091
|
-
|
|
5092
|
-
|
|
5093
|
-
|
|
5119
|
+
if (entity) {
|
|
5120
|
+
for (const key in entity) {
|
|
5121
|
+
const value = entity[key];
|
|
5122
|
+
if (value === null || value === void 0) {
|
|
5123
|
+
defaults[key] = "";
|
|
5124
|
+
continue;
|
|
5125
|
+
}
|
|
5126
|
+
switch (typeof value) {
|
|
5127
|
+
case "string":
|
|
5128
|
+
defaults[key] = value;
|
|
5129
|
+
break;
|
|
5130
|
+
case "number":
|
|
5131
|
+
defaults[key] = value;
|
|
5132
|
+
break;
|
|
5133
|
+
case "boolean":
|
|
5134
|
+
defaults[key] = value;
|
|
5135
|
+
break;
|
|
5136
|
+
case "object":
|
|
5137
|
+
defaults[key] = Array.isArray(value) ? [...value] : { ...value };
|
|
5138
|
+
break;
|
|
5139
|
+
default:
|
|
5140
|
+
defaults[key] = value;
|
|
5141
|
+
}
|
|
5142
|
+
}
|
|
5143
|
+
}
|
|
5144
|
+
if (fields) {
|
|
5145
|
+
const flatFields = fields.flat();
|
|
5146
|
+
for (const field of flatFields) {
|
|
5147
|
+
const key = field.name;
|
|
5148
|
+
if (defaults[key] === void 0) {
|
|
5149
|
+
defaults[key] = field.value ?? field.defaultValue ?? "";
|
|
5150
|
+
}
|
|
5094
5151
|
}
|
|
5095
5152
|
}
|
|
5096
5153
|
return defaults;
|
|
@@ -5184,6 +5241,7 @@ var DynamicForm = ({
|
|
|
5184
5241
|
const schema = React3.useMemo(() => getDynamicSchema(fields, extraValidations), [fields, extraValidations]);
|
|
5185
5242
|
const resolver = zod.zodResolver(schema);
|
|
5186
5243
|
const initialValues = React3.useMemo(() => getDefaultValues(record), [record]);
|
|
5244
|
+
console.log("initialValues", initialValues);
|
|
5187
5245
|
const form = reactHookForm.useForm({
|
|
5188
5246
|
resolver,
|
|
5189
5247
|
defaultValues: initialValues
|