shadcn-zod-formkit 1.26.1 → 1.27.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
@@ -83,7 +83,7 @@ declare enum InputTypes {
83
83
  }
84
84
  declare const inputFieldComp: InputTypes[];
85
85
 
86
- declare const flattenFields: <T extends Record<string, any>>(fields: FieldConfig<T>[]) => FieldProps<T>[];
86
+ declare const flattenFields: <T extends Record<string, any>>(fields: FieldConfig<T>[], onAnyFieldChange?: (data: any) => void) => FieldProps<T>[];
87
87
  type FieldConfig<T, RT = Record<string, any>> = FieldProps<T, RT> | FieldConfig<T, RT>[];
88
88
  interface FieldProps<T = Record<string, any>, RT = Record<string, any>> {
89
89
  name: keyof T;
@@ -102,6 +102,7 @@ interface FieldProps<T = Record<string, any>, RT = Record<string, any>> {
102
102
  maxItems?: number;
103
103
  currencyFormat?: Intl.NumberFormatOptions;
104
104
  mask?: string | RegExp;
105
+ onAnyFieldChange?: (data: Record<string, any>) => void;
105
106
  wrapInCard?: boolean;
106
107
  placeHolder?: string;
107
108
  description?: string;
@@ -223,6 +224,7 @@ interface Props$h<T extends Record<string, any>> {
223
224
  readOnly?: boolean;
224
225
  fields: FieldConfig<T>[];
225
226
  record?: Partial<T>;
227
+ onAnyFieldChange?: (data: Record<string, any>) => void;
226
228
  onSubmit?: (resp: FormResp<T>) => void;
227
229
  onClick?: (resp: FormResp<T>) => void;
228
230
  extraValidations?: ((schema: ZodObject<any>) => ZodObject<any>)[];
@@ -239,7 +241,7 @@ interface Props$h<T extends Record<string, any>> {
239
241
  childrenHeader?: ReactNode;
240
242
  listBtnConfig?: BtnConfig[];
241
243
  }
242
- declare const DynamicForm: <T extends Record<string, any>>({ formTitle, formSubTitle, fields, readOnly, record, onSubmit, onClick, extraValidations, children, childrenHeader, showIcon, showFormHeader, withErrorsAlert, errorAlertPosition, withCard, submitBtnClass, listBtnConfig, submitBtnLabel, submitBtnLabelSubmiting, withFormWrapper, btnGroupDirection, withSubmitBtn }: Props$h<T>) => react_jsx_runtime.JSX.Element;
244
+ declare const DynamicForm: <T extends Record<string, any>>({ formTitle, formSubTitle, fields, readOnly, record, onAnyFieldChange, onSubmit, onClick, extraValidations, children, childrenHeader, showIcon, showFormHeader, withErrorsAlert, errorAlertPosition, withCard, submitBtnClass, listBtnConfig, submitBtnLabel, submitBtnLabelSubmiting, withFormWrapper, btnGroupDirection, withSubmitBtn }: Props$h<T>) => react_jsx_runtime.JSX.Element;
243
245
 
244
246
  declare const DynamicFormExample: () => react_jsx_runtime.JSX.Element;
245
247
  declare const mockFields: Array<FieldProps | FieldProps[]>;
package/dist/index.d.ts CHANGED
@@ -83,7 +83,7 @@ declare enum InputTypes {
83
83
  }
84
84
  declare const inputFieldComp: InputTypes[];
85
85
 
86
- declare const flattenFields: <T extends Record<string, any>>(fields: FieldConfig<T>[]) => FieldProps<T>[];
86
+ declare const flattenFields: <T extends Record<string, any>>(fields: FieldConfig<T>[], onAnyFieldChange?: (data: any) => void) => FieldProps<T>[];
87
87
  type FieldConfig<T, RT = Record<string, any>> = FieldProps<T, RT> | FieldConfig<T, RT>[];
88
88
  interface FieldProps<T = Record<string, any>, RT = Record<string, any>> {
89
89
  name: keyof T;
@@ -102,6 +102,7 @@ interface FieldProps<T = Record<string, any>, RT = Record<string, any>> {
102
102
  maxItems?: number;
103
103
  currencyFormat?: Intl.NumberFormatOptions;
104
104
  mask?: string | RegExp;
105
+ onAnyFieldChange?: (data: Record<string, any>) => void;
105
106
  wrapInCard?: boolean;
106
107
  placeHolder?: string;
107
108
  description?: string;
@@ -223,6 +224,7 @@ interface Props$h<T extends Record<string, any>> {
223
224
  readOnly?: boolean;
224
225
  fields: FieldConfig<T>[];
225
226
  record?: Partial<T>;
227
+ onAnyFieldChange?: (data: Record<string, any>) => void;
226
228
  onSubmit?: (resp: FormResp<T>) => void;
227
229
  onClick?: (resp: FormResp<T>) => void;
228
230
  extraValidations?: ((schema: ZodObject<any>) => ZodObject<any>)[];
@@ -239,7 +241,7 @@ interface Props$h<T extends Record<string, any>> {
239
241
  childrenHeader?: ReactNode;
240
242
  listBtnConfig?: BtnConfig[];
241
243
  }
242
- declare const DynamicForm: <T extends Record<string, any>>({ formTitle, formSubTitle, fields, readOnly, record, onSubmit, onClick, extraValidations, children, childrenHeader, showIcon, showFormHeader, withErrorsAlert, errorAlertPosition, withCard, submitBtnClass, listBtnConfig, submitBtnLabel, submitBtnLabelSubmiting, withFormWrapper, btnGroupDirection, withSubmitBtn }: Props$h<T>) => react_jsx_runtime.JSX.Element;
244
+ declare const DynamicForm: <T extends Record<string, any>>({ formTitle, formSubTitle, fields, readOnly, record, onAnyFieldChange, onSubmit, onClick, extraValidations, children, childrenHeader, showIcon, showFormHeader, withErrorsAlert, errorAlertPosition, withCard, submitBtnClass, listBtnConfig, submitBtnLabel, submitBtnLabelSubmiting, withFormWrapper, btnGroupDirection, withSubmitBtn }: Props$h<T>) => react_jsx_runtime.JSX.Element;
243
245
 
244
246
  declare const DynamicFormExample: () => react_jsx_runtime.JSX.Element;
245
247
  declare const mockFields: Array<FieldProps | FieldProps[]>;
package/dist/index.mjs CHANGED
@@ -15,6 +15,7 @@ import * as PopoverPrimitive from '@radix-ui/react-popover';
15
15
  import 'react-color-palette/css';
16
16
  import * as DialogPrimitive from '@radix-ui/react-dialog';
17
17
  import * as LabelPrimitive from '@radix-ui/react-label';
18
+ import { Label as Label$1 } from '@radix-ui/react-label';
18
19
  import * as SeparatorPrimitive from '@radix-ui/react-separator';
19
20
  import { OTPInput, OTPInputContext } from 'input-otp';
20
21
  import * as RadioGroupPrimitive from '@radix-ui/react-radio-group';
@@ -196,7 +197,9 @@ var entitiesToGroupedOption = (data, optionValue = "name") => {
196
197
  };
197
198
  var handleOnChage = (event, input, field) => {
198
199
  if (event) field?.onChange(event);
199
- input.onChange?.(event, input.form?.getValues());
200
+ const data = input.form?.getValues();
201
+ input.onChange?.(event, data);
202
+ if (input.onAnyFieldChange) input.onAnyFieldChange?.(data);
200
203
  };
201
204
  var isValidField = (input, form, defaultValue) => {
202
205
  const value = defaultValue ?? form.getValues(input.name);
@@ -209,7 +212,7 @@ var isValidField = (input, form, defaultValue) => {
209
212
  };
210
213
 
211
214
  // src/components/custom/form/inputs/base/definitions.ts
212
- var flattenFields = (fields) => {
215
+ var flattenFields = (fields, onAnyFieldChange) => {
213
216
  const result = [];
214
217
  for (const field of fields) {
215
218
  if (Array.isArray(field)) {
@@ -217,6 +220,7 @@ var flattenFields = (fields) => {
217
220
  } else if (field.fields) {
218
221
  result.push(...flattenFields(field.fields));
219
222
  } else {
223
+ if (onAnyFieldChange) field.onAnyFieldChange = (data) => onAnyFieldChange(data);
220
224
  result.push(field);
221
225
  }
222
226
  }
@@ -4523,34 +4527,40 @@ var FieldKeyValueList = ({ form, input, isSubmitting }) => {
4523
4527
  return /* @__PURE__ */ jsxs(FormItem, { className: input.className, children: [
4524
4528
  /* @__PURE__ */ jsx(FormLabel, { children: /* @__PURE__ */ jsx("b", { children: input.label }) }),
4525
4529
  /* @__PURE__ */ jsx(FormMessage, {}),
4526
- /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3 rounded-xl p-3 ", children: [
4530
+ /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3 rounded-xl ", children: [
4527
4531
  pairs.length === 0 && /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: "No pairs have been added yet." }),
4528
4532
  pairs.map((pair, index) => /* @__PURE__ */ jsxs(
4529
4533
  "div",
4530
4534
  {
4531
- className: "flex gap-2 items-center",
4535
+ className: "flex flex-row w-full gap-2 items-center py-0.5",
4532
4536
  children: [
4533
- /* @__PURE__ */ jsx(
4534
- Input,
4535
- {
4536
- placeholder: "Key",
4537
- value: pair.key,
4538
- disabled: isSubmitting,
4539
- onChange: (e) => handleChange(index, "key", e.target.value),
4540
- className: "w-1/2"
4541
- }
4542
- ),
4543
- /* @__PURE__ */ jsx(
4544
- Input,
4545
- {
4546
- placeholder: "Value",
4547
- value: pair.value,
4548
- disabled: isSubmitting,
4549
- onChange: (e) => handleChange(index, "value", e.target.value),
4550
- className: "w-1/2"
4551
- }
4552
- ),
4553
- /* @__PURE__ */ jsx(
4537
+ /* @__PURE__ */ jsxs(ButtonGroup, { className: "w-full", children: [
4538
+ /* @__PURE__ */ jsx(ButtonGroupText, { asChild: true, children: /* @__PURE__ */ jsx(Label$1, { htmlFor: "key", children: "Key" }) }),
4539
+ /* @__PURE__ */ jsx(InputGroup, { children: /* @__PURE__ */ jsx(
4540
+ InputGroupInput,
4541
+ {
4542
+ placeholder: "Key",
4543
+ value: pair.key,
4544
+ disabled: isSubmitting,
4545
+ onChange: (e) => handleChange(index, "key", e.target.value),
4546
+ className: "flex-1"
4547
+ }
4548
+ ) })
4549
+ ] }),
4550
+ /* @__PURE__ */ jsxs(ButtonGroup, { className: "w-full", children: [
4551
+ /* @__PURE__ */ jsx(ButtonGroupText, { asChild: true, children: /* @__PURE__ */ jsx(Label$1, { htmlFor: "value", children: "Value" }) }),
4552
+ /* @__PURE__ */ jsx(InputGroup, { children: /* @__PURE__ */ jsx(
4553
+ InputGroupInput,
4554
+ {
4555
+ placeholder: "Value",
4556
+ value: pair.value,
4557
+ disabled: isSubmitting,
4558
+ onChange: (e) => handleChange(index, "value", e.target.value),
4559
+ className: "flex-1"
4560
+ }
4561
+ ) })
4562
+ ] }),
4563
+ input.isRemovebleOption && /* @__PURE__ */ jsx(
4554
4564
  Button,
4555
4565
  {
4556
4566
  type: "button",
@@ -4887,7 +4897,7 @@ var FieldRepeater = ({ form, input, isSubmitting }) => {
4887
4897
  fields.map((item, index) => /* @__PURE__ */ jsxs(
4888
4898
  "div",
4889
4899
  {
4890
- className: "border p-3 rounded-md flex flex-col gap-3",
4900
+ className: "border p-3 rounded-md flex flex-col gap-4 py-3",
4891
4901
  children: [
4892
4902
  input.repeaterFields?.map((fieldGroup, groupIndex) => {
4893
4903
  const group = Array.isArray(fieldGroup) ? fieldGroup : [fieldGroup];
@@ -5549,7 +5559,7 @@ var FieldStringValueList = ({ form, input, isSubmitting }) => {
5549
5559
  items.map((value, index) => /* @__PURE__ */ jsxs(
5550
5560
  "div",
5551
5561
  {
5552
- className: "flex gap-2 items-center",
5562
+ className: "flex items-center gap-4 py-2",
5553
5563
  children: [
5554
5564
  /* @__PURE__ */ jsx(
5555
5565
  Input,
@@ -6071,6 +6081,7 @@ var DynamicForm = ({
6071
6081
  fields,
6072
6082
  readOnly = false,
6073
6083
  record = {},
6084
+ onAnyFieldChange,
6074
6085
  onSubmit,
6075
6086
  onClick,
6076
6087
  extraValidations,
@@ -6091,7 +6102,7 @@ var DynamicForm = ({
6091
6102
  }) => {
6092
6103
  const [isPending, startTransition] = useTransition();
6093
6104
  const schema = useMemo(() => {
6094
- const allFields = flattenFields(fields);
6105
+ const allFields = flattenFields(fields, onAnyFieldChange);
6095
6106
  return getDynamicSchema(allFields, extraValidations);
6096
6107
  }, [fields, extraValidations]);
6097
6108
  const resolver = zodResolver(schema);