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 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", checked: false },
4523
- { id: 2, name: "MOCK OPTION - PERMISO 2", checked: true },
4524
- { id: 3, name: "MOCK OPTION - PERMISO 3", checked: false },
4525
- { id: 4, name: "MOCK OPTION - PERMISO 4", checked: false }
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
- let lista = input?.listConfig?.list ?? mockInputOptions;
4528
- if (lista == void 0) lista = [];
4529
- const value = input.value ?? "";
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
- if (optionValue == "name") return item[optionValue];
4533
- return item.value ?? item.id;
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 }) => /* @__PURE__ */ jsxRuntime.jsxs(FormItem, { className: "flex flex-row items-center justify-between rounded-lg border p-3 shadow-lg bg-blue-100/20", children: [
4541
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-0.5 pr-5", children: [
4542
- /* @__PURE__ */ jsxRuntime.jsx(FormLabel, { children: /* @__PURE__ */ jsxRuntime.jsx("b", { children: input.label }) }),
4543
- input.description && /* @__PURE__ */ jsxRuntime.jsx(FormDescription, { children: input.description }),
4544
- /* @__PURE__ */ jsxRuntime.jsx(FormMessage, {})
4545
- ] }),
4546
- /* @__PURE__ */ jsxRuntime.jsx(FormControl, { children: /* @__PURE__ */ jsxRuntime.jsxs(
4547
- Select,
4548
- {
4549
- disabled: input.disabled || isSubmitting,
4550
- onValueChange: field.onChange,
4551
- defaultValue: value.toString(),
4552
- value: field.value?.toString() ?? value.toString(),
4553
- children: [
4554
- /* @__PURE__ */ jsxRuntime.jsx(FormControl, { children: /* @__PURE__ */ jsxRuntime.jsx(SelectTrigger, { className: "w-[60%] bg-black/10 dark:bg-white/25", children: /* @__PURE__ */ jsxRuntime.jsx(SelectValue, { placeholder: input.placeHolder }) }) }),
4555
- /* @__PURE__ */ jsxRuntime.jsx(SelectContent, { children: lista.filter((item) => item.name !== void 0).map((item) => /* @__PURE__ */ jsxRuntime.jsx(SelectItem, { value: getValue(item).toString(), children: item.name }, item.id)) })
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 (!entity) return defaults;
5069
- for (const key in entity) {
5070
- const value = entity[key];
5071
- if (value === null || value === void 0) {
5072
- defaults[key] = "";
5073
- continue;
5074
- }
5075
- switch (typeof value) {
5076
- case "string":
5077
- defaults[key] = value ?? "";
5078
- break;
5079
- case "number":
5080
- defaults[key] = value ?? 0;
5081
- break;
5082
- case "boolean":
5083
- defaults[key] = value ?? false;
5084
- break;
5085
- case "object":
5086
- if (Array.isArray(value)) {
5087
- defaults[key] = [...value];
5088
- } else {
5089
- defaults[key] = { ...value };
5090
- }
5091
- break;
5092
- default:
5093
- defaults[key] = value;
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