shadcn-zod-formkit 1.8.0 → 1.9.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
@@ -5065,6 +5065,7 @@ var InputFactory = class {
5065
5065
  };
5066
5066
  function getDefaultValues(entity) {
5067
5067
  const defaults = {};
5068
+ if (!entity) return defaults;
5068
5069
  for (const key in entity) {
5069
5070
  const value = entity[key];
5070
5071
  if (value === null || value === void 0) {
@@ -5095,26 +5096,31 @@ function getDefaultValues(entity) {
5095
5096
  return defaults;
5096
5097
  }
5097
5098
  var getDynamicSchema = (fields, extraValidations) => {
5098
- const flatFields = fields.flatMap((f) => Array.isArray(f) ? f : [f]);
5099
- const shape = {};
5100
- flatFields.forEach((f) => {
5101
- shape[f.name] = f.zodType ?? z2__default.default.any();
5102
- });
5099
+ const flatFields = fields.flatMap(
5100
+ (f) => Array.isArray(f) ? f : [f]
5101
+ );
5102
+ const shape = flatFields.reduce((acc, f) => {
5103
+ acc[f.name] = f.zodType ?? z2__default.default.any();
5104
+ return acc;
5105
+ }, {});
5103
5106
  let schema = z2__default.default.object(shape);
5104
- if (extraValidations && extraValidations.length > 0) {
5105
- extraValidations.forEach((fn) => {
5107
+ if (extraValidations?.length) {
5108
+ for (const fn of extraValidations) {
5106
5109
  schema = fn(schema);
5107
- });
5110
+ }
5108
5111
  }
5109
5112
  return schema;
5110
5113
  };
5111
- var FormErrorsAlert = ({ formState, fields }) => {
5114
+ var FormErrorsAlert = ({
5115
+ formState,
5116
+ fields
5117
+ }) => {
5112
5118
  const flatFields = fields.flatMap((f) => Array.isArray(f) ? f : [f]);
5113
5119
  return /* @__PURE__ */ jsxRuntime.jsx("div", { style: { marginTop: 4 }, children: Object.entries(formState.errors).length > 0 && /* @__PURE__ */ jsxRuntime.jsx(
5114
5120
  CustomAlert,
5115
5121
  {
5116
5122
  title: "Revisar los siguientes criterios",
5117
- description: /* @__PURE__ */ jsxRuntime.jsx("ul", { children: Object.entries(formState?.errors).map(([key, value]) => /* @__PURE__ */ jsxRuntime.jsxs("li", { children: [
5123
+ description: /* @__PURE__ */ jsxRuntime.jsx("ul", { children: Object.entries(formState.errors).map(([key, value]) => /* @__PURE__ */ jsxRuntime.jsxs("li", { children: [
5118
5124
  /* @__PURE__ */ jsxRuntime.jsxs("strong", { children: [
5119
5125
  getFieldLabel(key, flatFields),
5120
5126
  ":"
@@ -5128,15 +5134,36 @@ var FormErrorsAlert = ({ formState, fields }) => {
5128
5134
  ) });
5129
5135
  };
5130
5136
  var getFieldLabel = (fieldErrorKey, fields) => {
5131
- const findedField = fields.find((field) => field.name == fieldErrorKey);
5132
- return findedField?.label ?? fieldErrorKey;
5137
+ const foundField = fields.find((field) => field.name === fieldErrorKey);
5138
+ return foundField?.label ?? fieldErrorKey;
5133
5139
  };
5134
- var FormFieldsGrid = ({ fields, form, isPending, readOnly, className = "", gap = "gap-2" }) => {
5135
- return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: fields.map(
5136
- (input, idx) => Array.isArray(input) ? /* @__PURE__ */ jsxRuntime.jsx("span", { className: "w-full flex flex-row justify-between py-3", children: input.map((field, subIdx) => {
5137
- if (readOnly) field.disabled = readOnly;
5138
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full px-2", children: InputFactory.create(field, form, isPending) }, subIdx);
5139
- }) }, `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}`)
5140
+ var FormFieldsGrid = ({
5141
+ fields,
5142
+ form,
5143
+ isPending,
5144
+ readOnly,
5145
+ className = "",
5146
+ gap = "gap-2"
5147
+ }) => {
5148
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: `w-full grid grid-cols-1 ${gap} ${className}`, children: fields.map(
5149
+ (input, idx) => Array.isArray(input) ? /* @__PURE__ */ jsxRuntime.jsx(
5150
+ "span",
5151
+ {
5152
+ className: "w-full flex flex-row justify-between py-3",
5153
+ children: input.map((field, subIdx) => {
5154
+ if (readOnly) field.disabled = true;
5155
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full px-2", children: InputFactory.create(field, form, isPending) }, subIdx);
5156
+ })
5157
+ },
5158
+ `field-group-${idx}`
5159
+ ) : /* @__PURE__ */ jsxRuntime.jsx(
5160
+ "span",
5161
+ {
5162
+ className: "flex flex-col justify-between py-3 w-full px-2",
5163
+ children: InputFactory.create(input, form, isPending)
5164
+ },
5165
+ `field-group-${idx}`
5166
+ )
5140
5167
  ) });
5141
5168
  };
5142
5169
  var DynamicForm = ({
@@ -5155,14 +5182,15 @@ var DynamicForm = ({
5155
5182
  }) => {
5156
5183
  const [isPending, startTransition] = React3.useTransition();
5157
5184
  const schema = React3.useMemo(() => getDynamicSchema(fields, extraValidations), [fields, extraValidations]);
5158
- const defaultValues = React3.useMemo(() => getDefaultValues(record), [record]);
5185
+ const resolver = zod.zodResolver(schema);
5186
+ const initialValues = React3.useMemo(() => getDefaultValues(record), [record]);
5159
5187
  const form = reactHookForm.useForm({
5160
- resolver: zod.zodResolver(schema),
5161
- defaultValues
5188
+ resolver,
5189
+ defaultValues: initialValues
5162
5190
  });
5163
5191
  React3.useEffect(() => {
5164
- form.reset(defaultValues);
5165
- }, [defaultValues, form]);
5192
+ form.reset(initialValues);
5193
+ }, [initialValues, form]);
5166
5194
  const handleSubmit = (data) => {
5167
5195
  if (readOnly) return;
5168
5196
  try {
@@ -5171,7 +5199,7 @@ var DynamicForm = ({
5171
5199
  onSubmit?.(resp);
5172
5200
  });
5173
5201
  } catch (error) {
5174
- console.error("Ocurri\xF3 un error al enviar el formulario.");
5202
+ console.error("Ocurri\xF3 un error al enviar el formulario.", error);
5175
5203
  }
5176
5204
  };
5177
5205
  const formContent = /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
@@ -5199,8 +5227,7 @@ var DynamicForm = ({
5199
5227
  ] }) }) })
5200
5228
  ]
5201
5229
  }
5202
- ) }),
5203
- withErrorsAlert && errorAlertPosition === "down" && /* @__PURE__ */ jsxRuntime.jsx(FormErrorsAlert, { formState: form.formState, fields })
5230
+ ) })
5204
5231
  ] });
5205
5232
  if (!withCard) return formContent;
5206
5233
  return /* @__PURE__ */ jsxRuntime.jsx(Card, { children: /* @__PURE__ */ jsxRuntime.jsx(CardContent, { children: formContent }) });
@@ -5480,6 +5507,7 @@ exports.entityToGroupedOption = entityToGroupedOption;
5480
5507
  exports.entityToInputOption = entityToInputOption;
5481
5508
  exports.getDefaultValues = getDefaultValues;
5482
5509
  exports.getDynamicSchema = getDynamicSchema;
5510
+ exports.getFieldLabel = getFieldLabel;
5483
5511
  exports.inputFieldComp = inputFieldComp;
5484
5512
  exports.mockFields = mockFields;
5485
5513
  exports.useFormField = useFormField;