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.d.mts CHANGED
@@ -78,6 +78,7 @@ declare const inputFieldComp: InputTypes[];
78
78
  interface FieldProps<T = Record<string, any>> {
79
79
  name: keyof T;
80
80
  label: string;
81
+ defaultValue?: any;
81
82
  repeaterFields?: Array<FieldProps | FieldProps[]>;
82
83
  minItems?: number;
83
84
  maxItems?: number;
@@ -214,7 +215,7 @@ declare const getFieldLabel: <T extends FieldValues>(fieldErrorKey: string, fiel
214
215
  declare class InputFactory {
215
216
  static create<T extends Record<string, any> = Record<string, any>>(input: FieldProps<T>, form: UseFormReturn<T>, isSubmitting?: boolean): JSX.Element;
216
217
  }
217
- declare function getDefaultValues<T extends Record<string, any>>(entity?: Partial<T>): Record<string, any>;
218
+ declare function getDefaultValues<T extends Record<string, any>>(entity?: Partial<T>, fields?: Array<FieldProps<T> | FieldProps<T>[]>): Record<string, any>;
218
219
  declare const getDynamicSchema: <T extends Record<string, any>>(fields: Array<FieldProps<T> | FieldProps<T>[]>, extraValidations?: ((schema: ZodObject<any>) => ZodObject<any>)[]) => ZodObject<Record<keyof T, ZodTypeAny>>;
219
220
 
220
221
  declare class CheckListInput extends BaseInput {
package/dist/index.d.ts CHANGED
@@ -78,6 +78,7 @@ declare const inputFieldComp: InputTypes[];
78
78
  interface FieldProps<T = Record<string, any>> {
79
79
  name: keyof T;
80
80
  label: string;
81
+ defaultValue?: any;
81
82
  repeaterFields?: Array<FieldProps | FieldProps[]>;
82
83
  minItems?: number;
83
84
  maxItems?: number;
@@ -214,7 +215,7 @@ declare const getFieldLabel: <T extends FieldValues>(fieldErrorKey: string, fiel
214
215
  declare class InputFactory {
215
216
  static create<T extends Record<string, any> = Record<string, any>>(input: FieldProps<T>, form: UseFormReturn<T>, isSubmitting?: boolean): JSX.Element;
216
217
  }
217
- declare function getDefaultValues<T extends Record<string, any>>(entity?: Partial<T>): Record<string, any>;
218
+ declare function getDefaultValues<T extends Record<string, any>>(entity?: Partial<T>, fields?: Array<FieldProps<T> | FieldProps<T>[]>): Record<string, any>;
218
219
  declare const getDynamicSchema: <T extends Record<string, any>>(fields: Array<FieldProps<T> | FieldProps<T>[]>, extraValidations?: ((schema: ZodObject<any>) => ZodObject<any>)[]) => ZodObject<Record<keyof T, ZodTypeAny>>;
219
220
 
220
221
  declare class CheckListInput extends BaseInput {
package/dist/index.mjs CHANGED
@@ -4482,44 +4482,95 @@ var SelectInput = class extends BaseInput {
4482
4482
  };
4483
4483
  var FieldSelect = ({ form, input, isSubmitting }) => {
4484
4484
  const mockInputOptions = [
4485
- { id: 1, name: "MOCK OPTION - PERMISO 1", checked: false },
4486
- { id: 2, name: "MOCK OPTION - PERMISO 2", checked: true },
4487
- { id: 3, name: "MOCK OPTION - PERMISO 3", checked: false },
4488
- { id: 4, name: "MOCK OPTION - PERMISO 4", checked: false }
4485
+ { value: 1, id: 1, name: "MOCK OPTION - PERMISO 1" },
4486
+ { value: 2, id: 2, name: "MOCK OPTION - PERMISO 2" },
4487
+ { value: 3, id: 3, name: "MOCK OPTION - PERMISO 3" },
4488
+ { value: 4, id: 4, name: "MOCK OPTION - PERMISO 4" }
4489
4489
  ];
4490
- let lista = input?.listConfig?.list ?? mockInputOptions;
4491
- if (lista == void 0) lista = [];
4492
- const value = input.value ?? "";
4490
+ const [lista, setLista] = useState(
4491
+ input?.listConfig?.list?.every((item) => "name" in item) ? input.listConfig.list : mockInputOptions
4492
+ );
4493
+ const [loading, setLoading] = useState(false);
4494
+ const [value, setValue] = useState(input.value?.toString() ?? "");
4493
4495
  const optionValue = input?.listConfig?.optionValue ?? input.optionValue ?? "id";
4496
+ useEffect(() => {
4497
+ const currentValue = form.getValues(input.name);
4498
+ if (!currentValue && input.value) {
4499
+ form.setValue(input.name, input.value);
4500
+ setValue(input.value.toString());
4501
+ }
4502
+ }, [form, input.name, input.value]);
4503
+ useEffect(() => {
4504
+ if (input.dependsOn && input.loadOptions) {
4505
+ const subscription = form.watch(async (values) => {
4506
+ const dependencyValue = values[input.dependsOn];
4507
+ if (dependencyValue) {
4508
+ try {
4509
+ setLoading(true);
4510
+ const newOptions = await input.loadOptions(dependencyValue);
4511
+ setLista(newOptions);
4512
+ } catch (err) {
4513
+ console.error(`Error loading options for ${input.name}:`, err);
4514
+ setLista([]);
4515
+ } finally {
4516
+ setLoading(false);
4517
+ }
4518
+ } else {
4519
+ setLista([]);
4520
+ form.setValue(input.name, "");
4521
+ }
4522
+ });
4523
+ return () => subscription.unsubscribe?.();
4524
+ }
4525
+ }, [form, input.dependsOn, input.loadOptions, input.name]);
4494
4526
  const getValue = (item) => {
4495
- if (optionValue == "name") return item[optionValue];
4496
- return item.value ?? item.id;
4527
+ const val = optionValue === "name" ? item.name : item.value?.toString?.() ?? item.id?.toString();
4528
+ return val?.toString() ?? "";
4497
4529
  };
4498
4530
  return /* @__PURE__ */ jsx(
4499
4531
  FormField,
4500
4532
  {
4501
4533
  control: form.control,
4502
4534
  name: input.name,
4503
- render: ({ field }) => /* @__PURE__ */ jsxs(FormItem, { className: "flex flex-row items-center justify-between rounded-lg border p-3 shadow-lg bg-blue-100/20", children: [
4504
- /* @__PURE__ */ jsxs("div", { className: "space-y-0.5 pr-5", children: [
4505
- /* @__PURE__ */ jsx(FormLabel, { children: /* @__PURE__ */ jsx("b", { children: input.label }) }),
4506
- input.description && /* @__PURE__ */ jsx(FormDescription, { children: input.description }),
4507
- /* @__PURE__ */ jsx(FormMessage, {})
4508
- ] }),
4509
- /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsxs(
4510
- Select,
4511
- {
4512
- disabled: input.disabled || isSubmitting,
4513
- onValueChange: field.onChange,
4514
- defaultValue: value.toString(),
4515
- value: field.value?.toString() ?? value.toString(),
4516
- children: [
4517
- /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsx(SelectTrigger, { className: "w-[60%] bg-black/10 dark:bg-white/25", children: /* @__PURE__ */ jsx(SelectValue, { placeholder: input.placeHolder }) }) }),
4518
- /* @__PURE__ */ jsx(SelectContent, { children: lista.filter((item) => item.name !== void 0).map((item) => /* @__PURE__ */ jsx(SelectItem, { value: getValue(item).toString(), children: item.name }, item.id)) })
4519
- ]
4520
- }
4521
- ) })
4522
- ] })
4535
+ render: ({ field }) => {
4536
+ const currentValue = field.value?.toString() ?? value;
4537
+ return /* @__PURE__ */ jsxs(FormItem, { className: "flex flex-row items-center justify-between rounded-lg border p-3 shadow-lg bg-blue-100/20", children: [
4538
+ /* @__PURE__ */ jsxs("div", { className: "space-y-0.5 pr-5", children: [
4539
+ /* @__PURE__ */ jsx(FormLabel, { children: /* @__PURE__ */ jsx("b", { children: input.label }) }),
4540
+ input.description && /* @__PURE__ */ jsx(FormDescription, { children: input.description }),
4541
+ /* @__PURE__ */ jsx(FormMessage, {})
4542
+ ] }),
4543
+ /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsxs(
4544
+ Select,
4545
+ {
4546
+ disabled: input.disabled || isSubmitting || loading,
4547
+ onValueChange: (val) => {
4548
+ field.onChange(val);
4549
+ setValue(val);
4550
+ if (input.listConfig?.onOptionChange) {
4551
+ const selectedItem = lista.find(
4552
+ (item) => getValue(item) === val
4553
+ );
4554
+ input.listConfig.onOptionChange(selectedItem);
4555
+ }
4556
+ },
4557
+ value: currentValue || void 0,
4558
+ children: [
4559
+ /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsx(SelectTrigger, { className: "w-[60%] bg-black/10 dark:bg-white/25", children: /* @__PURE__ */ jsx(
4560
+ SelectValue,
4561
+ {
4562
+ placeholder: loading ? "Cargando..." : input.placeHolder ?? "Seleccionar"
4563
+ }
4564
+ ) }) }),
4565
+ /* @__PURE__ */ jsxs(SelectContent, { children: [
4566
+ lista.map((item) => /* @__PURE__ */ jsx(SelectItem, { value: getValue(item), children: item.name }, item.id)),
4567
+ lista.length === 0 && !loading && /* @__PURE__ */ jsx("div", { className: "p-2 text-sm text-muted-foreground", children: "No hay opciones disponibles" })
4568
+ ] })
4569
+ ]
4570
+ }
4571
+ ) })
4572
+ ] });
4573
+ }
4523
4574
  },
4524
4575
  input.name
4525
4576
  );
@@ -5026,34 +5077,40 @@ var InputFactory = class {
5026
5077
  return /* @__PURE__ */ jsx(Card, { className: "p-4 space-y-3", children: instance.render() });
5027
5078
  }
5028
5079
  };
5029
- function getDefaultValues(entity) {
5080
+ function getDefaultValues(entity, fields) {
5030
5081
  const defaults = {};
5031
- if (!entity) return defaults;
5032
- for (const key in entity) {
5033
- const value = entity[key];
5034
- if (value === null || value === void 0) {
5035
- defaults[key] = "";
5036
- continue;
5037
- }
5038
- switch (typeof value) {
5039
- case "string":
5040
- defaults[key] = value ?? "";
5041
- break;
5042
- case "number":
5043
- defaults[key] = value ?? 0;
5044
- break;
5045
- case "boolean":
5046
- defaults[key] = value ?? false;
5047
- break;
5048
- case "object":
5049
- if (Array.isArray(value)) {
5050
- defaults[key] = [...value];
5051
- } else {
5052
- defaults[key] = { ...value };
5053
- }
5054
- break;
5055
- default:
5056
- defaults[key] = value;
5082
+ if (entity) {
5083
+ for (const key in entity) {
5084
+ const value = entity[key];
5085
+ if (value === null || value === void 0) {
5086
+ defaults[key] = "";
5087
+ continue;
5088
+ }
5089
+ switch (typeof value) {
5090
+ case "string":
5091
+ defaults[key] = value;
5092
+ break;
5093
+ case "number":
5094
+ defaults[key] = value;
5095
+ break;
5096
+ case "boolean":
5097
+ defaults[key] = value;
5098
+ break;
5099
+ case "object":
5100
+ defaults[key] = Array.isArray(value) ? [...value] : { ...value };
5101
+ break;
5102
+ default:
5103
+ defaults[key] = value;
5104
+ }
5105
+ }
5106
+ }
5107
+ if (fields) {
5108
+ const flatFields = fields.flat();
5109
+ for (const field of flatFields) {
5110
+ const key = field.name;
5111
+ if (defaults[key] === void 0) {
5112
+ defaults[key] = field.value ?? field.defaultValue ?? "";
5113
+ }
5057
5114
  }
5058
5115
  }
5059
5116
  return defaults;
@@ -5147,6 +5204,7 @@ var DynamicForm = ({
5147
5204
  const schema = useMemo(() => getDynamicSchema(fields, extraValidations), [fields, extraValidations]);
5148
5205
  const resolver = zodResolver(schema);
5149
5206
  const initialValues = useMemo(() => getDefaultValues(record), [record]);
5207
+ console.log("initialValues", initialValues);
5150
5208
  const form = useForm({
5151
5209
  resolver,
5152
5210
  defaultValues: initialValues