shadcn-zod-formkit 1.9.1 → 1.9.2

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,56 @@ 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 = input?.listConfig?.list?.every((item) => "name" in item) ? input.listConfig.list : mockInputOptions;
4530
4528
  const optionValue = input?.listConfig?.optionValue ?? input.optionValue ?? "id";
4529
+ const [value, setValue] = React3.useState(
4530
+ input.value?.toString() ?? ""
4531
+ );
4532
+ React3.useEffect(() => {
4533
+ const currentValue = form.getValues(input.name);
4534
+ if (!currentValue && input.value) {
4535
+ form.setValue(input.name, input.value);
4536
+ setValue(input.value.toString());
4537
+ }
4538
+ }, [form, input.name, input.value]);
4531
4539
  const getValue = (item) => {
4532
- if (optionValue == "name") return item[optionValue];
4533
- return item.value ?? item.id;
4540
+ return (optionValue === "name" ? item.name.toString() : item.value.toString() ?? item.id).toString();
4534
4541
  };
4535
4542
  return /* @__PURE__ */ jsxRuntime.jsx(
4536
4543
  FormField,
4537
4544
  {
4538
4545
  control: form.control,
4539
4546
  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
- ] })
4547
+ render: ({ field }) => {
4548
+ const currentValue = field.value?.toString() ?? value;
4549
+ 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: [
4550
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-0.5 pr-5", children: [
4551
+ /* @__PURE__ */ jsxRuntime.jsx(FormLabel, { children: /* @__PURE__ */ jsxRuntime.jsx("b", { children: input.label }) }),
4552
+ input.description && /* @__PURE__ */ jsxRuntime.jsx(FormDescription, { children: input.description }),
4553
+ /* @__PURE__ */ jsxRuntime.jsx(FormMessage, {})
4554
+ ] }),
4555
+ /* @__PURE__ */ jsxRuntime.jsx(FormControl, { children: /* @__PURE__ */ jsxRuntime.jsxs(
4556
+ Select,
4557
+ {
4558
+ disabled: input.disabled || isSubmitting,
4559
+ onValueChange: (val) => {
4560
+ field.onChange(val);
4561
+ setValue(val);
4562
+ },
4563
+ value: currentValue || void 0,
4564
+ children: [
4565
+ /* @__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 }) }) }),
4566
+ /* @__PURE__ */ jsxRuntime.jsx(SelectContent, { children: lista.map((item) => /* @__PURE__ */ jsxRuntime.jsx(SelectItem, { value: getValue(item), children: item.name }, item.id)) })
4567
+ ]
4568
+ }
4569
+ ) })
4570
+ ] });
4571
+ }
4560
4572
  },
4561
4573
  input.name
4562
4574
  );
@@ -5063,34 +5075,40 @@ var InputFactory = class {
5063
5075
  return /* @__PURE__ */ jsxRuntime.jsx(Card, { className: "p-4 space-y-3", children: instance.render() });
5064
5076
  }
5065
5077
  };
5066
- function getDefaultValues(entity) {
5078
+ function getDefaultValues(entity, fields) {
5067
5079
  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;
5080
+ if (entity) {
5081
+ for (const key in entity) {
5082
+ const value = entity[key];
5083
+ if (value === null || value === void 0) {
5084
+ defaults[key] = "";
5085
+ continue;
5086
+ }
5087
+ switch (typeof value) {
5088
+ case "string":
5089
+ defaults[key] = value;
5090
+ break;
5091
+ case "number":
5092
+ defaults[key] = value;
5093
+ break;
5094
+ case "boolean":
5095
+ defaults[key] = value;
5096
+ break;
5097
+ case "object":
5098
+ defaults[key] = Array.isArray(value) ? [...value] : { ...value };
5099
+ break;
5100
+ default:
5101
+ defaults[key] = value;
5102
+ }
5103
+ }
5104
+ }
5105
+ if (fields) {
5106
+ const flatFields = fields.flat();
5107
+ for (const field of flatFields) {
5108
+ const key = field.name;
5109
+ if (defaults[key] === void 0) {
5110
+ defaults[key] = field.value ?? field.defaultValue ?? "";
5111
+ }
5094
5112
  }
5095
5113
  }
5096
5114
  return defaults;
@@ -5184,6 +5202,7 @@ var DynamicForm = ({
5184
5202
  const schema = React3.useMemo(() => getDynamicSchema(fields, extraValidations), [fields, extraValidations]);
5185
5203
  const resolver = zod.zodResolver(schema);
5186
5204
  const initialValues = React3.useMemo(() => getDefaultValues(record), [record]);
5205
+ console.log("initialValues", initialValues);
5187
5206
  const form = reactHookForm.useForm({
5188
5207
  resolver,
5189
5208
  defaultValues: initialValues