shadcn-zod-formkit 1.4.0 → 1.6.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
@@ -265,9 +265,11 @@ var InputTypes = /* @__PURE__ */ ((InputTypes2) => {
265
265
  InputTypes2["BUTTON_GROUP"] = "button_group";
266
266
  InputTypes2["CURRENCY"] = "currency";
267
267
  InputTypes2["KEY_VALUE"] = "key_value";
268
+ InputTypes2["REPEATER"] = "repeater";
268
269
  return InputTypes2;
269
270
  })(InputTypes || {});
270
271
  var inputFieldComp = [
272
+ "repeater" /* REPEATER */,
271
273
  "key_value" /* KEY_VALUE */,
272
274
  "currency" /* CURRENCY */,
273
275
  "button_group" /* BUTTON_GROUP */,
@@ -4217,6 +4219,100 @@ var FieldRadioGroup = ({ input, form, isSubmitting }) => {
4217
4219
  );
4218
4220
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: formField });
4219
4221
  };
4222
+ var RepeaterInput = class extends BaseInput {
4223
+ render() {
4224
+ const { input, form, isSubmitting } = this;
4225
+ return /* @__PURE__ */ jsxRuntime.jsx(FieldRepeater, { form, input, isSubmitting });
4226
+ }
4227
+ };
4228
+ var FieldRepeater = ({ form, input, isSubmitting }) => {
4229
+ const { control } = form;
4230
+ const { fields, append, remove } = reactHookForm.useFieldArray({
4231
+ control,
4232
+ name: input.name
4233
+ });
4234
+ const canAdd = !input.maxItems || fields.length < input.maxItems;
4235
+ const canRemove = fields.length > (input.minItems ?? 0);
4236
+ return /* @__PURE__ */ jsxRuntime.jsx(
4237
+ FormField,
4238
+ {
4239
+ control,
4240
+ name: input.name,
4241
+ render: () => /* @__PURE__ */ jsxRuntime.jsxs(FormItem, { className: input.className, children: [
4242
+ /* @__PURE__ */ jsxRuntime.jsx(FormLabel, { children: /* @__PURE__ */ jsxRuntime.jsx("b", { children: input.label }) }),
4243
+ /* @__PURE__ */ jsxRuntime.jsx(FormControl, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-3", children: [
4244
+ fields.map((item, index) => /* @__PURE__ */ jsxRuntime.jsxs(
4245
+ "div",
4246
+ {
4247
+ className: "border p-3 rounded-md flex flex-col gap-3",
4248
+ children: [
4249
+ input.repeaterFields?.map((fieldGroup, groupIndex) => {
4250
+ const group = Array.isArray(fieldGroup) ? fieldGroup : [fieldGroup];
4251
+ const cols = group.length;
4252
+ return /* @__PURE__ */ jsxRuntime.jsx(
4253
+ "div",
4254
+ {
4255
+ className: `grid gap-3 grid-cols-${cols}`,
4256
+ children: group.map((subField) => /* @__PURE__ */ jsxRuntime.jsx(
4257
+ FormField,
4258
+ {
4259
+ control,
4260
+ name: `${input.name}.${index}.${subField.name}`,
4261
+ render: ({ field, fieldState }) => /* @__PURE__ */ jsxRuntime.jsxs(FormItem, { className: "flex-1", children: [
4262
+ /* @__PURE__ */ jsxRuntime.jsx(FormLabel, { children: subField.label }),
4263
+ /* @__PURE__ */ jsxRuntime.jsx(FormControl, { children: /* @__PURE__ */ jsxRuntime.jsx(
4264
+ Input,
4265
+ {
4266
+ placeholder: subField.placeHolder,
4267
+ disabled: subField.disabled || isSubmitting,
4268
+ ...field
4269
+ }
4270
+ ) }),
4271
+ /* @__PURE__ */ jsxRuntime.jsx(FormMessage, { children: fieldState.error?.message })
4272
+ ] })
4273
+ },
4274
+ `${input.name}.${index}.${subField.name}`
4275
+ ))
4276
+ },
4277
+ groupIndex
4278
+ );
4279
+ }),
4280
+ canRemove && /* @__PURE__ */ jsxRuntime.jsx(
4281
+ Button,
4282
+ {
4283
+ type: "button",
4284
+ variant: "destructive",
4285
+ size: "icon",
4286
+ onClick: () => remove(index),
4287
+ className: "self-end",
4288
+ children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Trash2, { size: 18 })
4289
+ }
4290
+ )
4291
+ ]
4292
+ },
4293
+ item.id
4294
+ )),
4295
+ canAdd && /* @__PURE__ */ jsxRuntime.jsxs(
4296
+ Button,
4297
+ {
4298
+ type: "button",
4299
+ variant: "secondary",
4300
+ onClick: () => append({}),
4301
+ disabled: isSubmitting,
4302
+ className: "w-full",
4303
+ children: [
4304
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Plus, { size: 16, className: "mr-2" }),
4305
+ "Agregar"
4306
+ ]
4307
+ }
4308
+ )
4309
+ ] }) }),
4310
+ input.description && /* @__PURE__ */ jsxRuntime.jsx(FormDescription, { children: input.description }),
4311
+ /* @__PURE__ */ jsxRuntime.jsx(FormMessage, {})
4312
+ ] })
4313
+ }
4314
+ );
4315
+ };
4220
4316
  var SelectInput = class extends BaseInput {
4221
4317
  render() {
4222
4318
  const { input, form, isSubmitting } = this;
@@ -4744,6 +4840,7 @@ var inputMap = {
4744
4840
  ["button_group" /* BUTTON_GROUP */]: ButtonGroupInput,
4745
4841
  ["currency" /* CURRENCY */]: CurrencyInput,
4746
4842
  ["key_value" /* KEY_VALUE */]: KeyValueListInput,
4843
+ ["repeater" /* REPEATER */]: RepeaterInput,
4747
4844
  //ToDos: ============================================================
4748
4845
  ["slider" /* SLIDER */]: SliderInput,
4749
4846
  //ToDo: // PENDIENTE ... VISUALMENTE NO SE VE BIEN.!!!
@@ -4835,6 +4932,11 @@ var getFieldLabel = (fieldErrorKey, fields) => {
4835
4932
  const findedField = fields.find((field) => field.name == fieldErrorKey);
4836
4933
  return findedField?.label ?? fieldErrorKey;
4837
4934
  };
4935
+ var FormFieldsGrid = ({ fields, form, isPending, className = "", gap = "gap-2" }) => {
4936
+ return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fields.map(
4937
+ (input, idx) => Array.isArray(input) ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "w-full flex flex-row justify-between py-3", children: input.map((field, subIdx) => /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full px-2", children: InputFactory.create(field, form, isPending) }, subIdx)) }, `field-group-${idx}`) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "flex flex-col justify-between py-3 w-full px-2", children: InputFactory.create(input, form, isPending) }, `field-group-${idx}`)
4938
+ ) });
4939
+ };
4838
4940
  var DynamicForm = ({
4839
4941
  formTitle,
4840
4942
  formSubTitle,
@@ -4842,9 +4944,9 @@ var DynamicForm = ({
4842
4944
  record = {},
4843
4945
  onSubmit,
4844
4946
  extraValidations,
4845
- withCard = false,
4846
4947
  withErrorsAlert = true,
4847
4948
  errorAlertPosition = "up",
4949
+ withCard = false,
4848
4950
  submitBtnClass = "",
4849
4951
  submitBtnLabel = "Submit"
4850
4952
  }) => {
@@ -4856,14 +4958,12 @@ var DynamicForm = ({
4856
4958
  defaultValues
4857
4959
  });
4858
4960
  React3.useEffect(() => {
4859
- console.log("\u{1F501} Redibuja cuando cambian los fields o los valores por defecto");
4860
4961
  form.reset(defaultValues);
4861
- }, []);
4962
+ }, [defaultValues, form]);
4862
4963
  const handleSubmit = (data) => {
4863
4964
  try {
4864
4965
  startTransition(async () => {
4865
4966
  const resp = { data, form };
4866
- await new Promise((resolve) => setTimeout(resolve, 3e3));
4867
4967
  onSubmit?.(resp);
4868
4968
  });
4869
4969
  } catch (error) {
@@ -4871,16 +4971,14 @@ var DynamicForm = ({
4871
4971
  }
4872
4972
  };
4873
4973
  const formContent = /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
4874
- /* @__PURE__ */ jsxRuntime.jsxs(CardTitle, { className: "flex items-center gap-2 p-2 border-b", children: [
4974
+ /* @__PURE__ */ jsxRuntime.jsxs(CardTitle, { className: "flex items-center gap-2 p-2 border-b", children: [
4875
4975
  /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Settings, { className: "h-5 w-5" }),
4876
4976
  formTitle
4877
4977
  ] }),
4878
- withErrorsAlert && errorAlertPosition == "up" && /* @__PURE__ */ jsxRuntime.jsx(FormErrorsAlert, { formState: form.formState, fields }),
4978
+ withErrorsAlert && errorAlertPosition === "up" && /* @__PURE__ */ jsxRuntime.jsx(FormErrorsAlert, { formState: form.formState, fields }),
4879
4979
  /* @__PURE__ */ jsxRuntime.jsx(Form, { ...form, children: /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: form.handleSubmit(handleSubmit), className: "flex flex-col gap-2", children: [
4880
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full grid grid-cols-1", children: fields.map(
4881
- (input, idx) => Array.isArray(input) ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "w-full flex flex-row justify-between py-3", children: input.map((field, subIdx) => /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full px-2", children: InputFactory.create(field, form, isPending) }, subIdx)) }, `field-group-${idx}`) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "flex flex-col justify-between py-3 w-full px-2", children: InputFactory.create(input, form, isPending) }, `field-group-${idx}`)
4882
- ) }),
4883
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-row gap-2 justify-end items-end justify-items-end", children: /* @__PURE__ */ jsxRuntime.jsx(Button, { type: "submit", size: "lg", className: submitBtnClass, disabled: isPending, children: isPending ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
4980
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full grid grid-cols-1", children: /* @__PURE__ */ jsxRuntime.jsx(FormFieldsGrid, { fields, form }) }),
4981
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-row gap-2 justify-end items-end", children: /* @__PURE__ */ jsxRuntime.jsx(Button, { type: "submit", size: "lg", className: submitBtnClass, disabled: isPending, children: isPending ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
4884
4982
  /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Loader2, { className: "h-4 w-4 mr-2 animate-spin" }),
4885
4983
  submitBtnLabel ?? "Saving..."
4886
4984
  ] }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
@@ -4888,7 +4986,7 @@ var DynamicForm = ({
4888
4986
  submitBtnLabel ?? "Save"
4889
4987
  ] }) }) })
4890
4988
  ] }) }),
4891
- withErrorsAlert && errorAlertPosition == "down" && /* @__PURE__ */ jsxRuntime.jsx(FormErrorsAlert, { formState: form.formState, fields })
4989
+ withErrorsAlert && errorAlertPosition === "down" && /* @__PURE__ */ jsxRuntime.jsx(FormErrorsAlert, { formState: form.formState, fields })
4892
4990
  ] });
4893
4991
  if (!withCard) return formContent;
4894
4992
  return /* @__PURE__ */ jsxRuntime.jsx(Card, { children: /* @__PURE__ */ jsxRuntime.jsx(CardContent, { children: formContent }) });