shadcn-zod-formkit 1.9.2 → 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.mjs CHANGED
@@ -4487,11 +4487,12 @@ var FieldSelect = ({ form, input, isSubmitting }) => {
4487
4487
  { value: 3, id: 3, name: "MOCK OPTION - PERMISO 3" },
4488
4488
  { value: 4, id: 4, name: "MOCK OPTION - PERMISO 4" }
4489
4489
  ];
4490
- const lista = input?.listConfig?.list?.every((item) => "name" in item) ? input.listConfig.list : mockInputOptions;
4491
- const optionValue = input?.listConfig?.optionValue ?? input.optionValue ?? "id";
4492
- const [value, setValue] = useState(
4493
- input.value?.toString() ?? ""
4490
+ const [lista, setLista] = useState(
4491
+ input?.listConfig?.list?.every((item) => "name" in item) ? input.listConfig.list : mockInputOptions
4494
4492
  );
4493
+ const [loading, setLoading] = useState(false);
4494
+ const [value, setValue] = useState(input.value?.toString() ?? "");
4495
+ const optionValue = input?.listConfig?.optionValue ?? input.optionValue ?? "id";
4495
4496
  useEffect(() => {
4496
4497
  const currentValue = form.getValues(input.name);
4497
4498
  if (!currentValue && input.value) {
@@ -4499,8 +4500,32 @@ var FieldSelect = ({ form, input, isSubmitting }) => {
4499
4500
  setValue(input.value.toString());
4500
4501
  }
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]);
4502
4526
  const getValue = (item) => {
4503
- return (optionValue === "name" ? item.name.toString() : item.value.toString() ?? item.id).toString();
4527
+ const val = optionValue === "name" ? item.name : item.value?.toString?.() ?? item.id?.toString();
4528
+ return val?.toString() ?? "";
4504
4529
  };
4505
4530
  return /* @__PURE__ */ jsx(
4506
4531
  FormField,
@@ -4518,15 +4543,29 @@ var FieldSelect = ({ form, input, isSubmitting }) => {
4518
4543
  /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsxs(
4519
4544
  Select,
4520
4545
  {
4521
- disabled: input.disabled || isSubmitting,
4546
+ disabled: input.disabled || isSubmitting || loading,
4522
4547
  onValueChange: (val) => {
4523
4548
  field.onChange(val);
4524
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
+ }
4525
4556
  },
4526
4557
  value: currentValue || void 0,
4527
4558
  children: [
4528
- /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsx(SelectTrigger, { className: "w-[60%] bg-black/10 dark:bg-white/25", children: /* @__PURE__ */ jsx(SelectValue, { placeholder: input.placeHolder }) }) }),
4529
- /* @__PURE__ */ jsx(SelectContent, { children: lista.map((item) => /* @__PURE__ */ jsx(SelectItem, { value: getValue(item), children: item.name }, item.id)) })
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
+ ] })
4530
4569
  ]
4531
4570
  }
4532
4571
  ) })