@rachelallyson/hero-hook-form 2.1.2 → 2.2.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/CHANGELOG.md +56 -3
- package/dist/index.d.ts +825 -16
- package/dist/index.js +238 -137
- package/dist/react/index.d.ts +825 -16
- package/dist/react/index.js +238 -137
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -123,7 +123,19 @@ interface DynamicSectionConfig<TFieldValues extends FieldValues> extends BaseFor
|
|
|
123
123
|
condition: (formData: Partial<TFieldValues>) => boolean;
|
|
124
124
|
fields: ZodFormFieldConfig<TFieldValues>[];
|
|
125
125
|
}
|
|
126
|
-
|
|
126
|
+
interface ContentFieldConfig<TFieldValues extends FieldValues> {
|
|
127
|
+
type: "content";
|
|
128
|
+
name?: Path<TFieldValues>;
|
|
129
|
+
title?: string;
|
|
130
|
+
description?: string;
|
|
131
|
+
render?: (field: {
|
|
132
|
+
form: UseFormReturn<TFieldValues>;
|
|
133
|
+
errors: FieldErrors<TFieldValues>;
|
|
134
|
+
isSubmitting: boolean;
|
|
135
|
+
}) => React.ReactNode;
|
|
136
|
+
className?: string;
|
|
137
|
+
}
|
|
138
|
+
type FormFieldConfig<TFieldValues extends FieldValues> = StringFieldConfig<TFieldValues> | BooleanFieldConfig<TFieldValues> | RadioFieldConfig<TFieldValues> | SliderFieldConfig<TFieldValues> | DateFieldConfig<TFieldValues> | FileFieldConfig<TFieldValues> | FontPickerFieldConfig<TFieldValues> | CustomFieldConfig<TFieldValues> | ConditionalFieldConfig<TFieldValues> | FieldArrayConfig<TFieldValues> | DynamicSectionConfig<TFieldValues> | ContentFieldConfig<TFieldValues>;
|
|
127
139
|
interface FormConfig<TFieldValues extends FieldValues> {
|
|
128
140
|
fields: FormFieldConfig<TFieldValues>[];
|
|
129
141
|
layout?: "vertical" | "horizontal" | "grid" | "custom";
|
|
@@ -137,7 +149,7 @@ interface FormConfig<TFieldValues extends FieldValues> {
|
|
|
137
149
|
className?: string;
|
|
138
150
|
defaultValues?: Partial<TFieldValues>;
|
|
139
151
|
}
|
|
140
|
-
type ZodFormFieldConfig<TFieldValues extends FieldValues> = Omit<StringFieldConfig<TFieldValues>, "rules"> | Omit<BooleanFieldConfig<TFieldValues>, "rules"> | Omit<RadioFieldConfig<TFieldValues>, "rules"> | Omit<SliderFieldConfig<TFieldValues>, "rules"> | Omit<DateFieldConfig<TFieldValues>, "rules"> | Omit<FileFieldConfig<TFieldValues>, "rules"> | Omit<FontPickerFieldConfig<TFieldValues>, "rules"> | Omit<CustomFieldConfig<TFieldValues>, "rules"> | Omit<ConditionalFieldConfig<TFieldValues>, "rules"> | Omit<FieldArrayConfig<TFieldValues>, "rules"> | Omit<DynamicSectionConfig<TFieldValues>, "rules">;
|
|
152
|
+
type ZodFormFieldConfig<TFieldValues extends FieldValues> = Omit<StringFieldConfig<TFieldValues>, "rules"> | Omit<BooleanFieldConfig<TFieldValues>, "rules"> | Omit<RadioFieldConfig<TFieldValues>, "rules"> | Omit<SliderFieldConfig<TFieldValues>, "rules"> | Omit<DateFieldConfig<TFieldValues>, "rules"> | Omit<FileFieldConfig<TFieldValues>, "rules"> | Omit<FontPickerFieldConfig<TFieldValues>, "rules"> | Omit<CustomFieldConfig<TFieldValues>, "rules"> | Omit<ConditionalFieldConfig<TFieldValues>, "rules"> | Omit<FieldArrayConfig<TFieldValues>, "rules"> | Omit<DynamicSectionConfig<TFieldValues>, "rules"> | ContentFieldConfig<TFieldValues>;
|
|
141
153
|
interface ZodFormConfig<TFieldValues extends FieldValues> extends UseFormProps<TFieldValues> {
|
|
142
154
|
schema: zod.ZodSchema<TFieldValues>;
|
|
143
155
|
fields: ZodFormFieldConfig<TFieldValues>[];
|
|
@@ -216,6 +228,11 @@ interface FormTestUtils<TFieldValues extends FieldValues> {
|
|
|
216
228
|
triggerValidation: (name?: Path<TFieldValues>) => Promise<boolean>;
|
|
217
229
|
}
|
|
218
230
|
|
|
231
|
+
/**
|
|
232
|
+
* Props for the Form component.
|
|
233
|
+
*
|
|
234
|
+
* @template T - The form data type
|
|
235
|
+
*/
|
|
219
236
|
interface FormProps$1<T extends FieldValues> {
|
|
220
237
|
className?: string;
|
|
221
238
|
columns?: 1 | 2 | 3;
|
|
@@ -233,6 +250,57 @@ interface FormProps$1<T extends FieldValues> {
|
|
|
233
250
|
title?: string;
|
|
234
251
|
defaultValues?: Partial<T>;
|
|
235
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Base form component for building forms without Zod validation.
|
|
255
|
+
*
|
|
256
|
+
* @description
|
|
257
|
+
* This component provides a flexible form solution using React Hook Form
|
|
258
|
+
* without requiring Zod schemas. It's useful when you need more control over
|
|
259
|
+
* validation or want to use React Hook Form's built-in validation rules.
|
|
260
|
+
*
|
|
261
|
+
* @template T - The form data type
|
|
262
|
+
*
|
|
263
|
+
* @param {FormProps<T>} props - Component props
|
|
264
|
+
* @param {FormFieldConfig<T>[]} props.fields - Array of field configurations
|
|
265
|
+
* @param {SubmitHandler<T>} props.onSubmit - Submit handler function
|
|
266
|
+
* @param {Partial<T>} [props.defaultValues] - Default form values
|
|
267
|
+
* @param {string} [props.title] - Optional form title
|
|
268
|
+
* @param {string} [props.subtitle] - Optional form subtitle
|
|
269
|
+
* @param {"vertical"|"horizontal"|"grid"} [props.layout="vertical"] - Form layout
|
|
270
|
+
* @param {1|2|3} [props.columns=1] - Number of columns for grid layout
|
|
271
|
+
* @param {"2"|"4"|"6"|"8"|"lg"} [props.spacing="4"] - Spacing between fields
|
|
272
|
+
* @param {string} [props.submitButtonText="Submit"] - Submit button text
|
|
273
|
+
* @param {boolean} [props.showResetButton=false] - Whether to show reset button
|
|
274
|
+
* @param {(error: FormValidationError) => void} [props.onError] - Error callback
|
|
275
|
+
* @param {(data: T) => void} [props.onSuccess] - Success callback
|
|
276
|
+
*
|
|
277
|
+
* @returns {JSX.Element} The rendered form component
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* ```tsx
|
|
281
|
+
* import { ConfigurableForm, FormFieldHelpers } from "@rachelallyson/hero-hook-form";
|
|
282
|
+
*
|
|
283
|
+
* function MyForm() {
|
|
284
|
+
* return (
|
|
285
|
+
* <ConfigurableForm
|
|
286
|
+
* fields={[
|
|
287
|
+
* FormFieldHelpers.input("name", "Name"),
|
|
288
|
+
* FormFieldHelpers.input("email", "Email", "email"),
|
|
289
|
+
* ]}
|
|
290
|
+
* defaultValues={{ name: "", email: "" }}
|
|
291
|
+
* onSubmit={async (data) => {
|
|
292
|
+
* console.log("Submitted:", data);
|
|
293
|
+
* }}
|
|
294
|
+
* title="Contact Form"
|
|
295
|
+
* />
|
|
296
|
+
* );
|
|
297
|
+
* }
|
|
298
|
+
* ```
|
|
299
|
+
*
|
|
300
|
+
* @see {@link ZodForm} for Zod-integrated form with automatic validation
|
|
301
|
+
* @see {@link FormFieldHelpers} for field creation helpers
|
|
302
|
+
* @category Components
|
|
303
|
+
*/
|
|
236
304
|
declare function ConfigurableForm<T extends FieldValues>({ className, columns, defaultValues, fields, layout, onError, onSubmit, onSuccess, resetButtonText, showResetButton, spacing, submitButtonProps, submitButtonText, subtitle, title, }: FormProps$1<T>): React$1.JSX.Element;
|
|
237
305
|
|
|
238
306
|
interface FormFieldProps<TFieldValues extends FieldValues> {
|
|
@@ -277,11 +345,13 @@ interface ServerActionFormProps<T extends FieldValues> {
|
|
|
277
345
|
title?: string;
|
|
278
346
|
}
|
|
279
347
|
/**
|
|
280
|
-
* ServerActionForm - A form component compatible with Next.js Server Actions
|
|
348
|
+
* ServerActionForm - A form component compatible with Next.js Server Actions.
|
|
281
349
|
*
|
|
350
|
+
* @description
|
|
282
351
|
* This component works with Next.js authentication patterns by using native
|
|
283
352
|
* HTML form submission with Server Actions, while still providing the
|
|
284
|
-
* beautiful HeroUI field components.
|
|
353
|
+
* beautiful HeroUI field components. It uses React's useActionState hook
|
|
354
|
+
* to manage form state and handle server responses.
|
|
285
355
|
*
|
|
286
356
|
* **Validation Options:**
|
|
287
357
|
* - **Server-side only (default)**: Form submits directly to Server Action
|
|
@@ -292,22 +362,77 @@ interface ServerActionFormProps<T extends FieldValues> {
|
|
|
292
362
|
* - If your Server Action calls `redirect()`, success messages won't display
|
|
293
363
|
* (the page navigates away). Use URL params or cookies for success messages
|
|
294
364
|
* when redirecting.
|
|
365
|
+
* - Server Actions receive FormData, not JSON, so field values are strings
|
|
366
|
+
*
|
|
367
|
+
* @template T - The form data type
|
|
368
|
+
*
|
|
369
|
+
* @param {ServerActionFormProps<T>} props - Component props
|
|
370
|
+
* @param {ServerAction<ActionState, FormData>} props.action - Next.js Server Action function
|
|
371
|
+
* @param {FormFieldConfig<T>[]} props.fields - Array of field configurations
|
|
372
|
+
* @param {z.ZodSchema<T>} [props.clientValidationSchema] - Optional Zod schema for client-side validation
|
|
373
|
+
* @param {Partial<T>} [props.defaultValues] - Default form values
|
|
374
|
+
* @param {ActionState} [props.initialState] - Initial state for useActionState
|
|
375
|
+
* @param {string} [props.title] - Optional form title
|
|
376
|
+
* @param {string} [props.subtitle] - Optional form subtitle
|
|
377
|
+
* @param {"vertical"|"horizontal"|"grid"} [props.layout="vertical"] - Form layout style
|
|
378
|
+
* @param {1|2|3} [props.columns=1] - Number of columns for grid layout
|
|
379
|
+
* @param {"2"|"4"|"6"|"8"|"lg"} [props.spacing="4"] - Spacing between form fields
|
|
380
|
+
* @param {string} [props.submitButtonText="Submit"] - Text for the submit button
|
|
381
|
+
* @param {boolean} [props.showResetButton=false] - Whether to show a reset button
|
|
382
|
+
* @param {(error: {...}) => void} [props.onError] - Error callback
|
|
383
|
+
* @param {(data: FormData) => void} [props.onSuccess] - Success callback
|
|
384
|
+
*
|
|
385
|
+
* @returns {JSX.Element} The rendered form component
|
|
295
386
|
*
|
|
296
387
|
* @example
|
|
388
|
+
* Server-side only validation:
|
|
297
389
|
* ```tsx
|
|
298
|
-
*
|
|
390
|
+
* import { ServerActionForm, FormFieldHelpers } from "@rachelallyson/hero-hook-form";
|
|
391
|
+
* import { signup } from "@/app/actions/auth";
|
|
392
|
+
*
|
|
299
393
|
* <ServerActionForm
|
|
300
394
|
* action={signup}
|
|
301
|
-
* fields={[
|
|
395
|
+
* fields={[
|
|
396
|
+
* FormFieldHelpers.input("name", "Name"),
|
|
397
|
+
* FormFieldHelpers.input("email", "Email", "email"),
|
|
398
|
+
* FormFieldHelpers.input("password", "Password", "password"),
|
|
399
|
+
* ]}
|
|
302
400
|
* />
|
|
401
|
+
* ```
|
|
402
|
+
*
|
|
403
|
+
* @example
|
|
404
|
+
* Client + Server validation:
|
|
405
|
+
* ```tsx
|
|
406
|
+
* import { ServerActionForm, FormFieldHelpers } from "@rachelallyson/hero-hook-form";
|
|
407
|
+
* import { signup } from "@/app/actions/auth";
|
|
408
|
+
* import { z } from "zod";
|
|
409
|
+
*
|
|
410
|
+
* const signupSchema = z.object({
|
|
411
|
+
* name: z.string().min(2),
|
|
412
|
+
* email: z.string().email(),
|
|
413
|
+
* password: z.string().min(8),
|
|
414
|
+
* });
|
|
303
415
|
*
|
|
304
|
-
* // Client + Server validation
|
|
305
416
|
* <ServerActionForm
|
|
306
417
|
* action={signup}
|
|
307
418
|
* clientValidationSchema={signupSchema}
|
|
308
|
-
* fields={[
|
|
419
|
+
* fields={[
|
|
420
|
+
* FormFieldHelpers.input("name", "Name"),
|
|
421
|
+
* FormFieldHelpers.input("email", "Email", "email"),
|
|
422
|
+
* FormFieldHelpers.input("password", "Password", "password"),
|
|
423
|
+
* ]}
|
|
424
|
+
* onError={(error) => {
|
|
425
|
+
* console.error("Form errors:", error.errors);
|
|
426
|
+
* }}
|
|
427
|
+
* onSuccess={(data) => {
|
|
428
|
+
* console.log("Form submitted:", data);
|
|
429
|
+
* }}
|
|
309
430
|
* />
|
|
310
431
|
* ```
|
|
432
|
+
*
|
|
433
|
+
* @see {@link ZodForm} for client-side only forms with Zod validation
|
|
434
|
+
* @see {@link ConfigurableForm} for forms without Server Actions
|
|
435
|
+
* @category Components
|
|
311
436
|
*/
|
|
312
437
|
declare function ServerActionForm<T extends FieldValues>({ action, className, clientValidationSchema, columns, defaultValues, fields, initialState, layout, onError, onSuccess, resetButtonText, showResetButton, spacing, submitButtonProps, submitButtonText, subtitle, title, }: ServerActionFormProps<T>): React$1.JSX.Element;
|
|
313
438
|
|
|
@@ -388,6 +513,11 @@ type TextareaFieldProps<TFieldValues extends FieldValues> = FieldBaseProps<TFiel
|
|
|
388
513
|
};
|
|
389
514
|
declare function TextareaField<TFieldValues extends FieldValues>(props: TextareaFieldProps<TFieldValues>): React$1.JSX.Element;
|
|
390
515
|
|
|
516
|
+
/**
|
|
517
|
+
* Options for the useFormHelper hook.
|
|
518
|
+
*
|
|
519
|
+
* @template T - The form data type
|
|
520
|
+
*/
|
|
391
521
|
interface UseFormHelperOptions<T extends FieldValues> {
|
|
392
522
|
onError?: (error: FormValidationError) => void;
|
|
393
523
|
onSubmit: SubmitHandler<T>;
|
|
@@ -395,6 +525,86 @@ interface UseFormHelperOptions<T extends FieldValues> {
|
|
|
395
525
|
defaultValues?: Partial<T>;
|
|
396
526
|
methods?: UseFormReturn<T>;
|
|
397
527
|
}
|
|
528
|
+
/**
|
|
529
|
+
* Hook for managing form state with enhanced features.
|
|
530
|
+
*
|
|
531
|
+
* @description
|
|
532
|
+
* Provides form state management with loading states, error handling,
|
|
533
|
+
* and submission tracking. Automatically handles form validation and
|
|
534
|
+
* provides callbacks for success and error cases. This hook wraps
|
|
535
|
+
* React Hook Form's useForm with additional state management.
|
|
536
|
+
*
|
|
537
|
+
* @template T - The form data type
|
|
538
|
+
*
|
|
539
|
+
* @param {UseFormHelperOptions<T>} options - Hook options
|
|
540
|
+
* @param {Partial<T>} [options.defaultValues] - Default form values
|
|
541
|
+
* @param {UseFormReturn<T>} [options.methods] - Optional existing form instance
|
|
542
|
+
* @param {SubmitHandler<T>} options.onSubmit - Submit handler function
|
|
543
|
+
* @param {(error: FormValidationError) => void} [options.onError] - Error handler callback
|
|
544
|
+
* @param {(data: T) => void} [options.onSuccess] - Success handler callback
|
|
545
|
+
*
|
|
546
|
+
* @returns {Object} Form helper with state and methods
|
|
547
|
+
* @returns {UseFormReturn<T>} form - React Hook Form instance
|
|
548
|
+
* @returns {() => Promise<void>} handleSubmit - Submit handler function
|
|
549
|
+
* @returns {() => void} resetForm - Reset form function
|
|
550
|
+
* @returns {boolean} isSubmitting - Whether form is currently submitting
|
|
551
|
+
* @returns {boolean} isSubmitted - Whether form has been submitted
|
|
552
|
+
* @returns {boolean} isSuccess - Whether last submission was successful
|
|
553
|
+
* @returns {string|undefined} error - Error message if submission failed
|
|
554
|
+
* @returns {FormSubmissionState} submissionState - Complete submission state object
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```tsx
|
|
558
|
+
* import { useFormHelper } from "@rachelallyson/hero-hook-form";
|
|
559
|
+
*
|
|
560
|
+
* function MyForm() {
|
|
561
|
+
* const { form, handleSubmit, isSubmitting, error } = useFormHelper({
|
|
562
|
+
* defaultValues: { email: "", name: "" },
|
|
563
|
+
* onSubmit: async (data) => {
|
|
564
|
+
* await submitToServer(data);
|
|
565
|
+
* },
|
|
566
|
+
* onError: (error) => {
|
|
567
|
+
* console.error("Validation errors:", error);
|
|
568
|
+
* },
|
|
569
|
+
* onSuccess: (data) => {
|
|
570
|
+
* console.log("Success:", data);
|
|
571
|
+
* },
|
|
572
|
+
* });
|
|
573
|
+
*
|
|
574
|
+
* return (
|
|
575
|
+
* <form onSubmit={(e) => { e.preventDefault(); handleSubmit(); }}>
|
|
576
|
+
* <button disabled={isSubmitting}>
|
|
577
|
+
* {isSubmitting ? "Submitting..." : "Submit"}
|
|
578
|
+
* </button>
|
|
579
|
+
* {error && <div className="error">{error}</div>}
|
|
580
|
+
* </form>
|
|
581
|
+
* );
|
|
582
|
+
* }
|
|
583
|
+
* ```
|
|
584
|
+
*
|
|
585
|
+
* @example
|
|
586
|
+
* Using with existing form instance:
|
|
587
|
+
* ```tsx
|
|
588
|
+
* import { useForm } from "react-hook-form";
|
|
589
|
+
* import { useFormHelper } from "@rachelallyson/hero-hook-form";
|
|
590
|
+
*
|
|
591
|
+
* function MyForm() {
|
|
592
|
+
* const existingForm = useForm({ defaultValues: { email: "" } });
|
|
593
|
+
* const { handleSubmit, isSubmitting } = useFormHelper({
|
|
594
|
+
* methods: existingForm,
|
|
595
|
+
* onSubmit: async (data) => {
|
|
596
|
+
* await submitToServer(data);
|
|
597
|
+
* },
|
|
598
|
+
* });
|
|
599
|
+
*
|
|
600
|
+
* // Use existingForm and handleSubmit together
|
|
601
|
+
* }
|
|
602
|
+
* ```
|
|
603
|
+
*
|
|
604
|
+
* @see {@link useHeroForm} for alternative hook with different API
|
|
605
|
+
* @see {@link ConfigurableForm} for component that uses this hook
|
|
606
|
+
* @category Hooks
|
|
607
|
+
*/
|
|
398
608
|
declare function useFormHelper<T extends FieldValues>({ defaultValues, methods, onError, onSubmit, onSuccess, }: UseFormHelperOptions<T>): {
|
|
399
609
|
error: string | undefined;
|
|
400
610
|
form: UseFormReturn<T>;
|
|
@@ -407,11 +617,19 @@ declare function useFormHelper<T extends FieldValues>({ defaultValues, methods,
|
|
|
407
617
|
};
|
|
408
618
|
|
|
409
619
|
/**
|
|
410
|
-
* Enhanced hook that provides both form methods and styling defaults
|
|
620
|
+
* Enhanced hook that provides both form methods and styling defaults.
|
|
411
621
|
*
|
|
622
|
+
* @description
|
|
412
623
|
* This hook combines React Hook Form's useFormContext with Hero Hook Form's
|
|
413
624
|
* styling defaults, giving you access to both form functionality and
|
|
414
|
-
* component styling in one convenient hook.
|
|
625
|
+
* component styling in one convenient hook. Must be used within a FormProvider
|
|
626
|
+
* context (provided by ZodForm, ConfigurableForm, or manual FormProvider).
|
|
627
|
+
*
|
|
628
|
+
* @template TFieldValues - The form data type
|
|
629
|
+
*
|
|
630
|
+
* @returns {Object} Enhanced form object with all React Hook Form methods and Hero Hook Form defaults
|
|
631
|
+
* @returns {UseFormReturn<TFieldValues>} All React Hook Form methods (formState, getValues, setValue, etc.)
|
|
632
|
+
* @returns {HeroHookFormDefaultsConfig} defaults - Hero Hook Form styling defaults
|
|
415
633
|
*
|
|
416
634
|
* @example
|
|
417
635
|
* ```tsx
|
|
@@ -433,6 +651,10 @@ declare function useFormHelper<T extends FieldValues>({ defaultValues, methods,
|
|
|
433
651
|
* const buttonDefaults = defaults.submitButton;
|
|
434
652
|
* }
|
|
435
653
|
* ```
|
|
654
|
+
*
|
|
655
|
+
* @see {@link useFormHelper} for form state management with callbacks
|
|
656
|
+
* @see {@link useFormContext} from React Hook Form for base functionality
|
|
657
|
+
* @category Hooks
|
|
436
658
|
*/
|
|
437
659
|
declare function useHeroForm<TFieldValues extends FieldValues>(): {
|
|
438
660
|
defaults: Required<Pick<HeroHookFormDefaultsConfig, "input" | "textarea" | "select" | "switch" | "radioGroup" | "checkbox" | "slider" | "dateInput" | "submitButton">>;
|
|
@@ -507,6 +729,11 @@ interface FormProps<TFieldValues extends FieldValues> {
|
|
|
507
729
|
}
|
|
508
730
|
declare function FormProvider<TFieldValues extends FieldValues>(props: FormProps<TFieldValues>): React$1.JSX.Element;
|
|
509
731
|
|
|
732
|
+
/**
|
|
733
|
+
* Enhanced form state with submission tracking and error management.
|
|
734
|
+
*
|
|
735
|
+
* @template T - The form data type
|
|
736
|
+
*/
|
|
510
737
|
interface EnhancedFormState<T extends FieldValues> {
|
|
511
738
|
status: "idle" | "submitting" | "success" | "error";
|
|
512
739
|
isSubmitting: boolean;
|
|
@@ -522,6 +749,11 @@ interface EnhancedFormState<T extends FieldValues> {
|
|
|
522
749
|
handleError: (error: string) => void;
|
|
523
750
|
reset: () => void;
|
|
524
751
|
}
|
|
752
|
+
/**
|
|
753
|
+
* Options for the useEnhancedFormState hook.
|
|
754
|
+
*
|
|
755
|
+
* @template T - The form data type
|
|
756
|
+
*/
|
|
525
757
|
interface UseEnhancedFormStateOptions<T extends FieldValues> {
|
|
526
758
|
onSuccess?: (data: T) => void;
|
|
527
759
|
onError?: (error: string) => void;
|
|
@@ -530,6 +762,81 @@ interface UseEnhancedFormStateOptions<T extends FieldValues> {
|
|
|
530
762
|
autoReset?: boolean;
|
|
531
763
|
resetDelay?: number;
|
|
532
764
|
}
|
|
765
|
+
/**
|
|
766
|
+
* Hook for managing enhanced form state with submission tracking.
|
|
767
|
+
*
|
|
768
|
+
* @description
|
|
769
|
+
* Provides advanced form state management including submission status,
|
|
770
|
+
* error tracking, touched/dirty field tracking, and automatic reset
|
|
771
|
+
* functionality. Useful for building forms with rich UI feedback.
|
|
772
|
+
*
|
|
773
|
+
* @template T - The form data type
|
|
774
|
+
*
|
|
775
|
+
* @param {UseFormReturn<T>} form - React Hook Form instance
|
|
776
|
+
* @param {UseEnhancedFormStateOptions<T>} [options] - Configuration options
|
|
777
|
+
* @param {(data: T) => void} [options.onSuccess] - Success callback
|
|
778
|
+
* @param {(error: string) => void} [options.onError] - Error callback
|
|
779
|
+
* @param {string} [options.successMessage] - Custom success message
|
|
780
|
+
* @param {string} [options.errorMessage] - Custom error message
|
|
781
|
+
* @param {boolean} [options.autoReset=true] - Automatically reset after success
|
|
782
|
+
* @param {number} [options.resetDelay=3000] - Delay before auto-reset (ms)
|
|
783
|
+
*
|
|
784
|
+
* @returns {EnhancedFormState<T>} Enhanced form state object
|
|
785
|
+
* @returns {"idle"|"submitting"|"success"|"error"} status - Current submission status
|
|
786
|
+
* @returns {boolean} isSubmitting - Whether form is currently submitting
|
|
787
|
+
* @returns {boolean} isSuccess - Whether last submission was successful
|
|
788
|
+
* @returns {boolean} isError - Whether last submission had an error
|
|
789
|
+
* @returns {string|undefined} error - Error message if submission failed
|
|
790
|
+
* @returns {T|undefined} submittedData - Data from last successful submission
|
|
791
|
+
* @returns {Set<Path<T>>} touchedFields - Set of touched field paths
|
|
792
|
+
* @returns {Set<Path<T>>} dirtyFields - Set of dirty field paths
|
|
793
|
+
* @returns {boolean} hasErrors - Whether form has validation errors
|
|
794
|
+
* @returns {number} errorCount - Number of validation errors
|
|
795
|
+
* @returns {(data: T) => void} handleSuccess - Function to mark submission as successful
|
|
796
|
+
* @returns {(error: string) => void} handleError - Function to mark submission as failed
|
|
797
|
+
* @returns {() => void} reset - Function to reset state to idle
|
|
798
|
+
*
|
|
799
|
+
* @example
|
|
800
|
+
* ```tsx
|
|
801
|
+
* import { useEnhancedFormState } from "@rachelallyson/hero-hook-form";
|
|
802
|
+
* import { useForm } from "react-hook-form";
|
|
803
|
+
*
|
|
804
|
+
* function MyForm() {
|
|
805
|
+
* const form = useForm();
|
|
806
|
+
* const state = useEnhancedFormState(form, {
|
|
807
|
+
* onSuccess: (data) => {
|
|
808
|
+
* console.log("Success:", data);
|
|
809
|
+
* },
|
|
810
|
+
* onError: (error) => {
|
|
811
|
+
* console.error("Error:", error);
|
|
812
|
+
* },
|
|
813
|
+
* autoReset: true,
|
|
814
|
+
* resetDelay: 3000,
|
|
815
|
+
* });
|
|
816
|
+
*
|
|
817
|
+
* const handleSubmit = async (data) => {
|
|
818
|
+
* try {
|
|
819
|
+
* await submitToServer(data);
|
|
820
|
+
* state.handleSuccess(data);
|
|
821
|
+
* } catch (error) {
|
|
822
|
+
* state.handleError(error.message);
|
|
823
|
+
* }
|
|
824
|
+
* };
|
|
825
|
+
*
|
|
826
|
+
* return (
|
|
827
|
+
* <form onSubmit={form.handleSubmit(handleSubmit)}>
|
|
828
|
+
* {/* form fields *\/}
|
|
829
|
+
* {state.isSubmitting && <div>Submitting...</div>}
|
|
830
|
+
* {state.isSuccess && <div>Success!</div>}
|
|
831
|
+
* {state.error && <div>Error: {state.error}</div>}
|
|
832
|
+
* </form>
|
|
833
|
+
* );
|
|
834
|
+
* }
|
|
835
|
+
* ```
|
|
836
|
+
*
|
|
837
|
+
* @see {@link ZodForm} which uses this hook internally
|
|
838
|
+
* @category Hooks
|
|
839
|
+
*/
|
|
533
840
|
declare function useEnhancedFormState<T extends FieldValues>(form: UseFormReturn<T>, options?: UseEnhancedFormStateOptions<T>): EnhancedFormState<T>;
|
|
534
841
|
|
|
535
842
|
interface SubmitButtonProps {
|
|
@@ -543,15 +850,99 @@ interface SubmitButtonProps {
|
|
|
543
850
|
}
|
|
544
851
|
declare function SubmitButton(props: SubmitButtonProps): React$1.JSX.Element;
|
|
545
852
|
|
|
853
|
+
/**
|
|
854
|
+
* Server field error structure.
|
|
855
|
+
*
|
|
856
|
+
* @template TFieldValues - The form data type
|
|
857
|
+
*/
|
|
546
858
|
interface ServerFieldError<TFieldValues extends FieldValues> {
|
|
547
859
|
path: Path<TFieldValues>;
|
|
548
860
|
message: string;
|
|
549
861
|
type?: string;
|
|
550
862
|
}
|
|
863
|
+
/**
|
|
864
|
+
* Server form error structure containing field errors.
|
|
865
|
+
*
|
|
866
|
+
* @template TFieldValues - The form data type
|
|
867
|
+
*/
|
|
551
868
|
interface ServerFormError<TFieldValues extends FieldValues> {
|
|
552
869
|
message?: string;
|
|
553
870
|
fieldErrors?: readonly ServerFieldError<TFieldValues>[];
|
|
554
871
|
}
|
|
872
|
+
/**
|
|
873
|
+
* Applies server-side validation errors to a React Hook Form instance.
|
|
874
|
+
*
|
|
875
|
+
* @description
|
|
876
|
+
* Maps server-returned field errors to React Hook Form's error state.
|
|
877
|
+
* Useful for displaying validation errors from API responses. This function
|
|
878
|
+
* iterates through the field errors and sets them on the form using
|
|
879
|
+
* React Hook Form's setError function.
|
|
880
|
+
*
|
|
881
|
+
* @template TFieldValues - The form data type
|
|
882
|
+
*
|
|
883
|
+
* @param {UseFormSetError<TFieldValues>} setError - React Hook Form's setError function
|
|
884
|
+
* @param {ServerFormError<TFieldValues>} serverError - Server error containing field errors
|
|
885
|
+
*
|
|
886
|
+
* @example
|
|
887
|
+
* ```tsx
|
|
888
|
+
* import { applyServerErrors } from "@rachelallyson/hero-hook-form";
|
|
889
|
+
* import { useForm } from "react-hook-form";
|
|
890
|
+
*
|
|
891
|
+
* function MyForm() {
|
|
892
|
+
* const form = useForm();
|
|
893
|
+
*
|
|
894
|
+
* const handleSubmit = async (data) => {
|
|
895
|
+
* try {
|
|
896
|
+
* const response = await fetch("/api/submit", {
|
|
897
|
+
* method: "POST",
|
|
898
|
+
* body: JSON.stringify(data),
|
|
899
|
+
* });
|
|
900
|
+
*
|
|
901
|
+
* if (!response.ok) {
|
|
902
|
+
* const errorData = await response.json();
|
|
903
|
+
* applyServerErrors(form.setError, errorData);
|
|
904
|
+
* }
|
|
905
|
+
* } catch (error) {
|
|
906
|
+
* console.error("Error:", error);
|
|
907
|
+
* }
|
|
908
|
+
* };
|
|
909
|
+
*
|
|
910
|
+
* return (
|
|
911
|
+
* <form onSubmit={form.handleSubmit(handleSubmit)}>
|
|
912
|
+
* {/* form fields go here *\/}
|
|
913
|
+
* </form>
|
|
914
|
+
* );
|
|
915
|
+
* }
|
|
916
|
+
* ```
|
|
917
|
+
*
|
|
918
|
+
* @example
|
|
919
|
+
* With ZodForm error handling:
|
|
920
|
+
* ```tsx
|
|
921
|
+
* import { ZodForm, applyServerErrors } from "@rachelallyson/hero-hook-form";
|
|
922
|
+
*
|
|
923
|
+
* function MyForm() {
|
|
924
|
+
* const form = useForm();
|
|
925
|
+
*
|
|
926
|
+
* const handleSubmit = async (data) => {
|
|
927
|
+
* try {
|
|
928
|
+
* await submitToServer(data);
|
|
929
|
+
* } catch (error) {
|
|
930
|
+
* if (error.fieldErrors) {
|
|
931
|
+
* applyServerErrors(form.setError, {
|
|
932
|
+
* fieldErrors: error.fieldErrors,
|
|
933
|
+
* });
|
|
934
|
+
* }
|
|
935
|
+
* }
|
|
936
|
+
* };
|
|
937
|
+
*
|
|
938
|
+
* return <ZodForm config={{ schema, fields }} onSubmit={handleSubmit} />;
|
|
939
|
+
* }
|
|
940
|
+
* ```
|
|
941
|
+
*
|
|
942
|
+
* @see {@link ServerFormError} for error structure
|
|
943
|
+
* @see {@link ServerFieldError} for field error structure
|
|
944
|
+
* @category Utilities
|
|
945
|
+
*/
|
|
555
946
|
declare function applyServerErrors<TFieldValues extends FieldValues>(setError: UseFormSetError<TFieldValues>, serverError: ServerFormError<TFieldValues>): void;
|
|
556
947
|
|
|
557
948
|
/**
|
|
@@ -700,6 +1091,11 @@ declare const commonValidations: {
|
|
|
700
1091
|
url: z.ZodString;
|
|
701
1092
|
};
|
|
702
1093
|
|
|
1094
|
+
/**
|
|
1095
|
+
* Props for the ZodForm component.
|
|
1096
|
+
*
|
|
1097
|
+
* @template T - The form data type inferred from the Zod schema
|
|
1098
|
+
*/
|
|
703
1099
|
interface ZodFormProps<T extends FieldValues> {
|
|
704
1100
|
className?: string;
|
|
705
1101
|
columns?: 1 | 2 | 3;
|
|
@@ -724,6 +1120,101 @@ interface ZodFormProps<T extends FieldValues> {
|
|
|
724
1120
|
values: T;
|
|
725
1121
|
}) => React$1.ReactNode;
|
|
726
1122
|
}
|
|
1123
|
+
/**
|
|
1124
|
+
* ZodForm component for building type-safe forms with Zod validation.
|
|
1125
|
+
*
|
|
1126
|
+
* @description
|
|
1127
|
+
* This component provides a complete form solution with automatic validation,
|
|
1128
|
+
* error handling, and type safety. It integrates React Hook Form with Zod
|
|
1129
|
+
* schemas and HeroUI components. The form automatically validates inputs based
|
|
1130
|
+
* on the provided Zod schema and displays error messages inline.
|
|
1131
|
+
*
|
|
1132
|
+
* @template T - The form data type inferred from the Zod schema
|
|
1133
|
+
*
|
|
1134
|
+
* @param {ZodFormProps<T>} props - Component props
|
|
1135
|
+
* @param {ZodFormConfig<T>} props.config - Form configuration with schema and fields
|
|
1136
|
+
* @param {SubmitHandler<T>} props.onSubmit - Submit handler function called with validated data
|
|
1137
|
+
* @param {string} [props.title] - Optional form title displayed above the form
|
|
1138
|
+
* @param {string} [props.subtitle] - Optional form subtitle displayed below the title
|
|
1139
|
+
* @param {"vertical"|"horizontal"|"grid"} [props.layout="vertical"] - Form layout style
|
|
1140
|
+
* @param {1|2|3} [props.columns=1] - Number of columns for grid layout (only applies when layout="grid")
|
|
1141
|
+
* @param {"2"|"4"|"6"|"8"|"lg"} [props.spacing="4"] - Spacing between form fields
|
|
1142
|
+
* @param {string} [props.submitButtonText="Submit"] - Text for the submit button
|
|
1143
|
+
* @param {boolean} [props.showResetButton=false] - Whether to show a reset button
|
|
1144
|
+
* @param {string} [props.resetButtonText="Reset"] - Text for the reset button
|
|
1145
|
+
* @param {(error: FormValidationError) => void} [props.onError] - Error callback for validation errors
|
|
1146
|
+
* @param {(data: T) => void} [props.onSuccess] - Success callback called after successful submission
|
|
1147
|
+
* @param {(formState: {...}) => React.ReactNode} [props.render] - Custom render function for advanced use cases
|
|
1148
|
+
*
|
|
1149
|
+
* @returns {JSX.Element} The rendered form component with validation and error handling
|
|
1150
|
+
*
|
|
1151
|
+
* @example
|
|
1152
|
+
* ```tsx
|
|
1153
|
+
* import { ZodForm, FormFieldHelpers } from "@rachelallyson/hero-hook-form";
|
|
1154
|
+
* import { z } from "zod";
|
|
1155
|
+
*
|
|
1156
|
+
* const schema = z.object({
|
|
1157
|
+
* email: z.string().email("Please enter a valid email"),
|
|
1158
|
+
* name: z.string().min(2, "Name must be at least 2 characters"),
|
|
1159
|
+
* message: z.string().min(10, "Message must be at least 10 characters"),
|
|
1160
|
+
* });
|
|
1161
|
+
*
|
|
1162
|
+
* function ContactForm() {
|
|
1163
|
+
* const handleSubmit = async (data) => {
|
|
1164
|
+
* console.log("Form submitted:", data);
|
|
1165
|
+
* // Handle form submission (e.g., API call)
|
|
1166
|
+
* };
|
|
1167
|
+
*
|
|
1168
|
+
* return (
|
|
1169
|
+
* <ZodForm
|
|
1170
|
+
* config={{
|
|
1171
|
+
* schema,
|
|
1172
|
+
* fields: [
|
|
1173
|
+
* FormFieldHelpers.input("name", "Name"),
|
|
1174
|
+
* FormFieldHelpers.input("email", "Email", "email"),
|
|
1175
|
+
* FormFieldHelpers.textarea("message", "Message"),
|
|
1176
|
+
* ],
|
|
1177
|
+
* }}
|
|
1178
|
+
* onSubmit={handleSubmit}
|
|
1179
|
+
* title="Contact Us"
|
|
1180
|
+
* subtitle="Send us a message and we'll get back to you"
|
|
1181
|
+
* />
|
|
1182
|
+
* );
|
|
1183
|
+
* }
|
|
1184
|
+
* ```
|
|
1185
|
+
*
|
|
1186
|
+
* @example
|
|
1187
|
+
* Grid layout with multiple columns:
|
|
1188
|
+
* ```tsx
|
|
1189
|
+
* <ZodForm
|
|
1190
|
+
* config={{ schema, fields }}
|
|
1191
|
+
* layout="grid"
|
|
1192
|
+
* columns={2}
|
|
1193
|
+
* spacing="6"
|
|
1194
|
+
* />
|
|
1195
|
+
* ```
|
|
1196
|
+
*
|
|
1197
|
+
* @example
|
|
1198
|
+
* Custom render function for advanced control:
|
|
1199
|
+
* ```tsx
|
|
1200
|
+
* <ZodForm
|
|
1201
|
+
* config={{ schema, fields }}
|
|
1202
|
+
* onSubmit={handleSubmit}
|
|
1203
|
+
* render={({ form, isSubmitting, errors, values }) => (
|
|
1204
|
+
* <div>
|
|
1205
|
+
* <button disabled={isSubmitting}>
|
|
1206
|
+
* {isSubmitting ? "Submitting..." : "Submit"}
|
|
1207
|
+
* </button>
|
|
1208
|
+
* </div>
|
|
1209
|
+
* )}
|
|
1210
|
+
* />
|
|
1211
|
+
* ```
|
|
1212
|
+
*
|
|
1213
|
+
* @see {@link Form} for the base form component without Zod
|
|
1214
|
+
* @see {@link FormFieldHelpers} for field creation helpers
|
|
1215
|
+
* @see {@link createBasicFormBuilder} for builder pattern alternative
|
|
1216
|
+
* @category Components
|
|
1217
|
+
*/
|
|
727
1218
|
declare function ZodForm<T extends FieldValues>({ className, columns, config, layout, onError, onSubmit, onSuccess, render, resetButtonText, showResetButton, spacing, submitButtonProps, submitButtonText, subtitle, title, }: ZodFormProps<T>): React$1.JSX.Element;
|
|
728
1219
|
|
|
729
1220
|
/**
|
|
@@ -736,8 +1227,34 @@ declare function useZodForm<TFieldValues extends FieldValues>(config: ZodFormCon
|
|
|
736
1227
|
declare function createZodFormConfig<TFieldValues extends FieldValues>(schema: z.ZodSchema<TFieldValues>, fields: ZodFormFieldConfig<TFieldValues>[], defaultValues?: Partial<TFieldValues>): ZodFormConfig<TFieldValues>;
|
|
737
1228
|
|
|
738
1229
|
/**
|
|
739
|
-
* Basic form field builder
|
|
740
|
-
*
|
|
1230
|
+
* Basic form field builder for creating form field configurations.
|
|
1231
|
+
*
|
|
1232
|
+
* @description
|
|
1233
|
+
* Provides a fluent API for building form field configurations. This builder
|
|
1234
|
+
* focuses on the most common use cases and eliminates the need for "as const"
|
|
1235
|
+
* assertions. Use this for simple forms with standard field types.
|
|
1236
|
+
*
|
|
1237
|
+
* @template T - The form data type
|
|
1238
|
+
*
|
|
1239
|
+
* @example
|
|
1240
|
+
* ```tsx
|
|
1241
|
+
* import { createBasicFormBuilder } from "@rachelallyson/hero-hook-form";
|
|
1242
|
+
*
|
|
1243
|
+
* const fields = createBasicFormBuilder()
|
|
1244
|
+
* .input("name", "Name")
|
|
1245
|
+
* .input("email", "Email", "email")
|
|
1246
|
+
* .textarea("message", "Message")
|
|
1247
|
+
* .select("country", "Country", [
|
|
1248
|
+
* { label: "US", value: "us" },
|
|
1249
|
+
* { label: "CA", value: "ca" },
|
|
1250
|
+
* ])
|
|
1251
|
+
* .checkbox("newsletter", "Subscribe to newsletter")
|
|
1252
|
+
* .build();
|
|
1253
|
+
* ```
|
|
1254
|
+
*
|
|
1255
|
+
* @see {@link createAdvancedBuilder} for more advanced features
|
|
1256
|
+
* @see {@link FormFieldHelpers} for helper function alternative
|
|
1257
|
+
* @category Builders
|
|
741
1258
|
*/
|
|
742
1259
|
declare class BasicFormBuilder<T extends FieldValues> {
|
|
743
1260
|
private fields;
|
|
@@ -760,6 +1277,18 @@ declare class BasicFormBuilder<T extends FieldValues> {
|
|
|
760
1277
|
* Add a checkbox field
|
|
761
1278
|
*/
|
|
762
1279
|
checkbox(name: Path<T>, label: string): this;
|
|
1280
|
+
/**
|
|
1281
|
+
* Add a content field for headers, questions, or custom content between fields
|
|
1282
|
+
*/
|
|
1283
|
+
content(title?: string | null, description?: string | null, options?: {
|
|
1284
|
+
render?: (field: {
|
|
1285
|
+
form: any;
|
|
1286
|
+
errors: any;
|
|
1287
|
+
isSubmitting: boolean;
|
|
1288
|
+
}) => React$1.ReactNode;
|
|
1289
|
+
className?: string;
|
|
1290
|
+
name?: Path<T>;
|
|
1291
|
+
}): this;
|
|
763
1292
|
/**
|
|
764
1293
|
* Add a switch field
|
|
765
1294
|
*/
|
|
@@ -770,17 +1299,97 @@ declare class BasicFormBuilder<T extends FieldValues> {
|
|
|
770
1299
|
build(): ZodFormFieldConfig<T>[];
|
|
771
1300
|
}
|
|
772
1301
|
/**
|
|
773
|
-
*
|
|
1302
|
+
* Creates a basic form builder for simple form construction.
|
|
1303
|
+
*
|
|
1304
|
+
* @description
|
|
1305
|
+
* Provides a fluent API for building form field configurations. Best for
|
|
1306
|
+
* simple forms with standard field types. Returns a builder instance with
|
|
1307
|
+
* chainable methods for adding fields.
|
|
1308
|
+
*
|
|
1309
|
+
* @template T - The form data type
|
|
1310
|
+
*
|
|
1311
|
+
* @returns {BasicFormBuilder<T>} Builder instance with chainable methods
|
|
1312
|
+
*
|
|
1313
|
+
* @example
|
|
1314
|
+
* ```tsx
|
|
1315
|
+
* import { createBasicFormBuilder } from "@rachelallyson/hero-hook-form";
|
|
1316
|
+
*
|
|
1317
|
+
* const fields = createBasicFormBuilder()
|
|
1318
|
+
* .input("name", "Name")
|
|
1319
|
+
* .input("email", "Email", "email")
|
|
1320
|
+
* .textarea("message", "Message")
|
|
1321
|
+
* .select("country", "Country", [
|
|
1322
|
+
* { label: "US", value: "us" },
|
|
1323
|
+
* { label: "CA", value: "ca" },
|
|
1324
|
+
* ])
|
|
1325
|
+
* .checkbox("newsletter", "Subscribe to newsletter")
|
|
1326
|
+
* .build();
|
|
1327
|
+
*
|
|
1328
|
+
* // Use with ZodForm
|
|
1329
|
+
* <ZodForm config={{ schema, fields }} onSubmit={handleSubmit} />
|
|
1330
|
+
* ```
|
|
1331
|
+
*
|
|
1332
|
+
* @see {@link createAdvancedBuilder} for more advanced features
|
|
1333
|
+
* @see {@link FormFieldHelpers} for helper function alternative
|
|
1334
|
+
* @see {@link defineInferredForm} for type-inferred forms
|
|
1335
|
+
* @category Builders
|
|
774
1336
|
*/
|
|
775
1337
|
declare function createBasicFormBuilder<T extends FieldValues>(): BasicFormBuilder<T>;
|
|
776
1338
|
/**
|
|
777
|
-
*
|
|
1339
|
+
* Helper functions for creating form field configurations.
|
|
1340
|
+
*
|
|
1341
|
+
* @description
|
|
1342
|
+
* Simple helper functions for common field types. This is the recommended
|
|
1343
|
+
* approach for most use cases as it's straightforward and doesn't require
|
|
1344
|
+
* method chaining. Each helper returns a field configuration object.
|
|
1345
|
+
*
|
|
1346
|
+
* @example
|
|
1347
|
+
* ```tsx
|
|
1348
|
+
* import { FormFieldHelpers } from "@rachelallyson/hero-hook-form";
|
|
1349
|
+
*
|
|
1350
|
+
* const fields = [
|
|
1351
|
+
* FormFieldHelpers.input("name", "Name"),
|
|
1352
|
+
* FormFieldHelpers.input("email", "Email", "email"),
|
|
1353
|
+
* FormFieldHelpers.textarea("message", "Message"),
|
|
1354
|
+
* FormFieldHelpers.select("country", "Country", [
|
|
1355
|
+
* { label: "US", value: "us" },
|
|
1356
|
+
* { label: "CA", value: "ca" },
|
|
1357
|
+
* ]),
|
|
1358
|
+
* FormFieldHelpers.checkbox("newsletter", "Subscribe to newsletter"),
|
|
1359
|
+
* ];
|
|
1360
|
+
* ```
|
|
1361
|
+
*
|
|
1362
|
+
* @see {@link createBasicFormBuilder} for builder pattern alternative
|
|
1363
|
+
* @category Builders
|
|
778
1364
|
*/
|
|
779
1365
|
declare const FormFieldHelpers: {
|
|
780
1366
|
/**
|
|
781
1367
|
* Create a checkbox field
|
|
782
1368
|
*/
|
|
783
1369
|
checkbox: <T extends FieldValues>(name: Path<T>, label: string) => ZodFormFieldConfig<T>;
|
|
1370
|
+
/**
|
|
1371
|
+
* Create a content field for headers, questions, or custom content between fields
|
|
1372
|
+
*
|
|
1373
|
+
* @example
|
|
1374
|
+
* ```tsx
|
|
1375
|
+
* // Simple header
|
|
1376
|
+
* FormFieldHelpers.content("Personal Information", "Please provide your details")
|
|
1377
|
+
*
|
|
1378
|
+
* // Custom render
|
|
1379
|
+
* FormFieldHelpers.content(null, null, {
|
|
1380
|
+
* render: () => <div>Custom content</div>
|
|
1381
|
+
* })
|
|
1382
|
+
* ```
|
|
1383
|
+
*/
|
|
1384
|
+
content: <T extends FieldValues>(title?: string | null, description?: string | null, options?: {
|
|
1385
|
+
render?: (field: {
|
|
1386
|
+
form: any;
|
|
1387
|
+
errors: any;
|
|
1388
|
+
isSubmitting: boolean;
|
|
1389
|
+
}) => React$1.ReactNode;
|
|
1390
|
+
className?: string;
|
|
1391
|
+
name?: Path<T>;
|
|
1392
|
+
}) => ZodFormFieldConfig<T>;
|
|
784
1393
|
/**
|
|
785
1394
|
* Create a date field
|
|
786
1395
|
*/
|
|
@@ -1189,15 +1798,115 @@ declare class FieldTemplateBuilder<T extends FieldValues> {
|
|
|
1189
1798
|
*/
|
|
1190
1799
|
declare function createNestedPathBuilder<T extends FieldValues>(): NestedPathBuilder<T>;
|
|
1191
1800
|
|
|
1801
|
+
/**
|
|
1802
|
+
* Options for the useDebouncedValidation hook.
|
|
1803
|
+
*/
|
|
1192
1804
|
interface UseDebouncedValidationOptions {
|
|
1193
1805
|
delay?: number;
|
|
1194
1806
|
fields?: string[];
|
|
1195
1807
|
enabled?: boolean;
|
|
1196
1808
|
}
|
|
1809
|
+
/**
|
|
1810
|
+
* Hook for debouncing form validation to improve performance.
|
|
1811
|
+
*
|
|
1812
|
+
* @description
|
|
1813
|
+
* Delays validation until the user stops typing, reducing unnecessary
|
|
1814
|
+
* validation calls and improving form performance. Useful for forms with
|
|
1815
|
+
* expensive validation logic or many fields.
|
|
1816
|
+
*
|
|
1817
|
+
* @template T - The form data type
|
|
1818
|
+
*
|
|
1819
|
+
* @param {UseFormReturn<T>} form - React Hook Form instance
|
|
1820
|
+
* @param {UseDebouncedValidationOptions} [options] - Configuration options
|
|
1821
|
+
* @param {number} [options.delay=300] - Delay in milliseconds before validation
|
|
1822
|
+
* @param {string[]} [options.fields] - Specific fields to watch (if not provided, watches all)
|
|
1823
|
+
* @param {boolean} [options.enabled=true] - Whether debouncing is enabled
|
|
1824
|
+
*
|
|
1825
|
+
* @returns {Object} Debounced validation utilities
|
|
1826
|
+
* @returns {() => void} debouncedTrigger - Function to trigger debounced validation
|
|
1827
|
+
* @returns {boolean} isDebouncing - Whether validation is currently debouncing
|
|
1828
|
+
*
|
|
1829
|
+
* @example
|
|
1830
|
+
* ```tsx
|
|
1831
|
+
* import { useDebouncedValidation } from "@rachelallyson/hero-hook-form";
|
|
1832
|
+
* import { useForm } from "react-hook-form";
|
|
1833
|
+
*
|
|
1834
|
+
* function MyForm() {
|
|
1835
|
+
* const form = useForm();
|
|
1836
|
+
* const { debouncedTrigger, isDebouncing } = useDebouncedValidation(form, {
|
|
1837
|
+
* delay: 500,
|
|
1838
|
+
* fields: ["email", "username"], // Only watch these fields
|
|
1839
|
+
* });
|
|
1840
|
+
*
|
|
1841
|
+
* return (
|
|
1842
|
+
* <form>
|
|
1843
|
+
* <input
|
|
1844
|
+
* {...form.register("email")}
|
|
1845
|
+
* onChange={(e) => {
|
|
1846
|
+
* form.setValue("email", e.target.value);
|
|
1847
|
+
* debouncedTrigger();
|
|
1848
|
+
* }}
|
|
1849
|
+
* />
|
|
1850
|
+
* {isDebouncing && <span>Validating...</span>}
|
|
1851
|
+
* </form>
|
|
1852
|
+
* );
|
|
1853
|
+
* }
|
|
1854
|
+
* ```
|
|
1855
|
+
*
|
|
1856
|
+
* @see {@link useDebouncedFieldValidation} for single field debouncing
|
|
1857
|
+
* @category Hooks
|
|
1858
|
+
*/
|
|
1197
1859
|
declare function useDebouncedValidation<T extends Record<string, any>>(form: UseFormReturn<T>, options?: UseDebouncedValidationOptions): {
|
|
1198
1860
|
debouncedTrigger: () => void;
|
|
1199
1861
|
isDebouncing: boolean;
|
|
1200
1862
|
};
|
|
1863
|
+
/**
|
|
1864
|
+
* Hook for debouncing validation of a single field.
|
|
1865
|
+
*
|
|
1866
|
+
* @description
|
|
1867
|
+
* Similar to useDebouncedValidation but optimized for a single field.
|
|
1868
|
+
* Automatically triggers validation when the specified field changes.
|
|
1869
|
+
*
|
|
1870
|
+
* @template T - The form data type
|
|
1871
|
+
*
|
|
1872
|
+
* @param {UseFormReturn<T>} form - React Hook Form instance
|
|
1873
|
+
* @param {keyof T} fieldName - Name of the field to debounce
|
|
1874
|
+
* @param {Object} [options] - Configuration options
|
|
1875
|
+
* @param {number} [options.delay=300] - Delay in milliseconds before validation
|
|
1876
|
+
* @param {boolean} [options.enabled=true] - Whether debouncing is enabled
|
|
1877
|
+
*
|
|
1878
|
+
* @returns {Object} Debounced field validation utilities
|
|
1879
|
+
* @returns {() => void} debouncedFieldTrigger - Function to trigger debounced validation for this field
|
|
1880
|
+
* @returns {boolean} isDebouncing - Whether validation is currently debouncing
|
|
1881
|
+
*
|
|
1882
|
+
* @example
|
|
1883
|
+
* ```tsx
|
|
1884
|
+
* import { useDebouncedFieldValidation } from "@rachelallyson/hero-hook-form";
|
|
1885
|
+
* import { useForm } from "react-hook-form";
|
|
1886
|
+
*
|
|
1887
|
+
* function MyForm() {
|
|
1888
|
+
* const form = useForm();
|
|
1889
|
+
* const { debouncedFieldTrigger, isDebouncing } = useDebouncedFieldValidation(
|
|
1890
|
+
* form,
|
|
1891
|
+
* "email",
|
|
1892
|
+
* { delay: 500 }
|
|
1893
|
+
* );
|
|
1894
|
+
*
|
|
1895
|
+
* return (
|
|
1896
|
+
* <input
|
|
1897
|
+
* {...form.register("email")}
|
|
1898
|
+
* onChange={(e) => {
|
|
1899
|
+
* form.setValue("email", e.target.value);
|
|
1900
|
+
* debouncedFieldTrigger();
|
|
1901
|
+
* }}
|
|
1902
|
+
* />
|
|
1903
|
+
* );
|
|
1904
|
+
* }
|
|
1905
|
+
* ```
|
|
1906
|
+
*
|
|
1907
|
+
* @see {@link useDebouncedValidation} for multiple fields
|
|
1908
|
+
* @category Hooks
|
|
1909
|
+
*/
|
|
1201
1910
|
declare function useDebouncedFieldValidation<T extends Record<string, any>>(form: UseFormReturn<T>, fieldName: keyof T, options?: {
|
|
1202
1911
|
delay?: number;
|
|
1203
1912
|
enabled?: boolean;
|
|
@@ -1206,6 +1915,11 @@ declare function useDebouncedFieldValidation<T extends Record<string, any>>(form
|
|
|
1206
1915
|
isDebouncing: boolean;
|
|
1207
1916
|
};
|
|
1208
1917
|
|
|
1918
|
+
/**
|
|
1919
|
+
* Options for the useInferredForm hook.
|
|
1920
|
+
*
|
|
1921
|
+
* @template T - The form data type
|
|
1922
|
+
*/
|
|
1209
1923
|
interface UseInferredFormOptions<T extends FieldValues> {
|
|
1210
1924
|
defaultValues?: Partial<T>;
|
|
1211
1925
|
mode?: "onChange" | "onBlur" | "onSubmit" | "onTouched" | "all";
|
|
@@ -1214,9 +1928,97 @@ interface UseInferredFormOptions<T extends FieldValues> {
|
|
|
1214
1928
|
shouldUnregister?: boolean;
|
|
1215
1929
|
delayError?: number;
|
|
1216
1930
|
}
|
|
1931
|
+
/**
|
|
1932
|
+
* Hook for creating a form instance from an inferred form configuration.
|
|
1933
|
+
*
|
|
1934
|
+
* @description
|
|
1935
|
+
* Creates a React Hook Form instance with Zod validation resolver
|
|
1936
|
+
* from a type-inferred form configuration. Automatically sets up
|
|
1937
|
+
* validation based on the provided schema and fields.
|
|
1938
|
+
*
|
|
1939
|
+
* @template T - The form data type
|
|
1940
|
+
*
|
|
1941
|
+
* @param {any} schema - Zod schema for validation
|
|
1942
|
+
* @param {ZodFormFieldConfig<T>[]} fields - Field configurations
|
|
1943
|
+
* @param {UseInferredFormOptions<T>} [options] - Form options
|
|
1944
|
+
* @param {Partial<T>} [options.defaultValues] - Default form values
|
|
1945
|
+
* @param {"onChange"|"onBlur"|"onSubmit"|"onTouched"|"all"} [options.mode="onChange"] - Validation mode
|
|
1946
|
+
* @param {"onChange"|"onBlur"|"onSubmit"} [options.reValidateMode="onChange"] - Re-validation mode
|
|
1947
|
+
* @param {boolean} [options.shouldFocusError=true] - Focus first error field
|
|
1948
|
+
* @param {boolean} [options.shouldUnregister=false] - Unregister fields on unmount
|
|
1949
|
+
* @param {number} [options.delayError=0] - Delay before showing errors
|
|
1950
|
+
*
|
|
1951
|
+
* @returns {UseFormReturn<T>} React Hook Form instance
|
|
1952
|
+
*
|
|
1953
|
+
* @example
|
|
1954
|
+
* ```tsx
|
|
1955
|
+
* import { useInferredForm, defineInferredForm, field } from "@rachelallyson/hero-hook-form";
|
|
1956
|
+
*
|
|
1957
|
+
* const formConfig = defineInferredForm({
|
|
1958
|
+
* name: field.string("Name").min(2),
|
|
1959
|
+
* email: field.email("Email"),
|
|
1960
|
+
* });
|
|
1961
|
+
*
|
|
1962
|
+
* function MyForm() {
|
|
1963
|
+
* const form = useInferredForm(
|
|
1964
|
+
* formConfig.schema,
|
|
1965
|
+
* formConfig.fields,
|
|
1966
|
+
* { mode: "onBlur" }
|
|
1967
|
+
* );
|
|
1968
|
+
*
|
|
1969
|
+
* return (
|
|
1970
|
+
* <form onSubmit={form.handleSubmit(handleSubmit)}>
|
|
1971
|
+
* {/* form fields *\/}
|
|
1972
|
+
* </form>
|
|
1973
|
+
* );
|
|
1974
|
+
* }
|
|
1975
|
+
* ```
|
|
1976
|
+
*
|
|
1977
|
+
* @see {@link defineInferredForm} for creating type-inferred form configurations
|
|
1978
|
+
* @see {@link useTypeInferredForm} for alternative API
|
|
1979
|
+
* @category Hooks
|
|
1980
|
+
*/
|
|
1217
1981
|
declare function useInferredForm<T extends FieldValues>(schema: any, fields: ZodFormFieldConfig<T>[], options?: UseInferredFormOptions<T>): UseFormReturn<T>;
|
|
1218
1982
|
/**
|
|
1219
|
-
* Hook that works with type-inferred
|
|
1983
|
+
* Hook that works with type-inferred form configurations.
|
|
1984
|
+
*
|
|
1985
|
+
* @description
|
|
1986
|
+
* Alternative API for useInferredForm that accepts a form configuration
|
|
1987
|
+
* object instead of separate schema and fields parameters. Useful when
|
|
1988
|
+
* working with defineInferredForm results.
|
|
1989
|
+
*
|
|
1990
|
+
* @template T - The form data type
|
|
1991
|
+
*
|
|
1992
|
+
* @param {Object} formConfig - Form configuration object
|
|
1993
|
+
* @param {any} formConfig.schema - Zod schema for validation
|
|
1994
|
+
* @param {ZodFormFieldConfig<T>[]} formConfig.fields - Field configurations
|
|
1995
|
+
* @param {UseInferredFormOptions<T>} [options] - Form options
|
|
1996
|
+
*
|
|
1997
|
+
* @returns {UseFormReturn<T>} React Hook Form instance
|
|
1998
|
+
*
|
|
1999
|
+
* @example
|
|
2000
|
+
* ```tsx
|
|
2001
|
+
* import { useTypeInferredForm, defineInferredForm, field } from "@rachelallyson/hero-hook-form";
|
|
2002
|
+
*
|
|
2003
|
+
* const formConfig = defineInferredForm({
|
|
2004
|
+
* name: field.string("Name").min(2),
|
|
2005
|
+
* email: field.email("Email"),
|
|
2006
|
+
* });
|
|
2007
|
+
*
|
|
2008
|
+
* function MyForm() {
|
|
2009
|
+
* const form = useTypeInferredForm(formConfig);
|
|
2010
|
+
*
|
|
2011
|
+
* return (
|
|
2012
|
+
* <form onSubmit={form.handleSubmit(handleSubmit)}>
|
|
2013
|
+
* {/* form fields *\/}
|
|
2014
|
+
* </form>
|
|
2015
|
+
* );
|
|
2016
|
+
* }
|
|
2017
|
+
* ```
|
|
2018
|
+
*
|
|
2019
|
+
* @see {@link useInferredForm} for direct schema/fields API
|
|
2020
|
+
* @see {@link defineInferredForm} for creating type-inferred form configurations
|
|
2021
|
+
* @category Hooks
|
|
1220
2022
|
*/
|
|
1221
2023
|
declare function useTypeInferredForm<T extends FieldValues>(formConfig: {
|
|
1222
2024
|
schema: any;
|
|
@@ -1230,6 +2032,13 @@ interface ConditionalFieldProps<TFieldValues extends FieldValues> {
|
|
|
1230
2032
|
}
|
|
1231
2033
|
declare function ConditionalField<TFieldValues extends FieldValues>({ className, config, control, }: ConditionalFieldProps<TFieldValues>): React$1.JSX.Element | null;
|
|
1232
2034
|
|
|
2035
|
+
interface ContentFieldProps<TFieldValues extends FieldValues> {
|
|
2036
|
+
config: ContentFieldConfig<TFieldValues>;
|
|
2037
|
+
form: UseFormReturn<TFieldValues>;
|
|
2038
|
+
submissionState: FormSubmissionState;
|
|
2039
|
+
}
|
|
2040
|
+
declare function ContentField<TFieldValues extends FieldValues>({ config, form, submissionState, }: ContentFieldProps<TFieldValues>): React$1.JSX.Element;
|
|
2041
|
+
|
|
1233
2042
|
interface FieldArrayFieldProps<TFieldValues extends FieldValues> {
|
|
1234
2043
|
config: FieldArrayConfig<TFieldValues>;
|
|
1235
2044
|
className?: string;
|
|
@@ -1380,4 +2189,4 @@ declare const validationUtils: {
|
|
|
1380
2189
|
}>;
|
|
1381
2190
|
};
|
|
1382
2191
|
|
|
1383
|
-
export { AdvancedFieldBuilder, type BaseFormFieldConfig, BasicFormBuilder, type BooleanFieldConfig, type ButtonDefaults, type CheckboxDefaults, CheckboxField, type CheckboxFieldProps, type CommonFieldDefaults, CommonFields, ConditionalField, type ConditionalFieldConfig, type ConditionalFieldProps, type ConditionalValidation, ConfigurableForm, type CustomFieldConfig, DateField, type DateFieldConfig, type DateFieldProps, type DateInputDefaults, type DynamicSectionConfig, DynamicSectionField, type DynamicSectionFieldProps, type EnhancedFormState, FieldArrayBuilder, type FieldArrayConfig, FieldArrayField, type FieldArrayFieldProps, FieldArrayItemBuilder, type FieldBaseProps, type FieldGroup, FileField, type FileFieldConfig, type FileFieldProps, FontPickerField, type FontPickerFieldConfig, type FontPickerFieldProps, type FormConfig, FormField, type FormFieldConfig, FormFieldHelpers, type FormProps, FormProvider, FormStatus, type FormStatusProps, type FormStep, type FormSubmissionState, type FormTestUtils, FormToast, type FormToastProps, type FormValidationError, type HeroHookFormDefaultsConfig, HeroHookFormProvider, type HeroHookFormProviderProps, type InputDefaults, InputField, type InputFieldProps, type RadioFieldConfig, type RadioGroupDefaults, RadioGroupField, type RadioGroupFieldProps, type SelectDefaults, SelectField, type SelectFieldProps, ServerActionForm, type ServerFieldError, type ServerFormError, type SliderDefaults, SliderField, type SliderFieldConfig, type SliderFieldProps, type StringFieldConfig, SubmitButton, type SubmitButtonProps, type SwitchDefaults, SwitchField, type SwitchFieldProps, type TextareaDefaults, TextareaField, type TextareaFieldProps, TypeInferredBuilder, type UseDebouncedValidationOptions, type UseEnhancedFormStateOptions, type UseInferredFormOptions, type ValidationUtils, type WithControl, type WizardFormConfig, ZodForm, type ZodFormConfig, type ZodFormFieldConfig, applyServerErrors, asyncValidation, commonValidations, createAdvancedBuilder, createBasicFormBuilder, createDateSchema, createEmailSchema, createField, createFieldArrayBuilder, createFieldArrayItemBuilder, createFileSchema, createFormTestUtils, createFutureDateSchema, createMaxLengthSchema, createMinLengthSchema, createMockFormData, createMockFormErrors, createNestedPathBuilder, createNumberRangeSchema, createOptimizedFieldHandler, createPasswordSchema, createPastDateSchema, createPhoneSchema, createRequiredCheckboxSchema, createRequiredSchema, createTypeInferredBuilder, createUrlSchema, createZodFormConfig, crossFieldValidation, debounce, deepEqual, defineInferredForm, errorMessages, field, getFieldError, getFormErrors, hasFieldError, hasFormErrors, serverValidation, shallowEqual, simulateFieldInput, simulateFormSubmission, throttle, useDebouncedFieldValidation, useDebouncedValidation, useEnhancedFormState, useFormHelper, useHeroForm, useHeroHookFormDefaults, useInferredForm, useMemoizedCallback, useMemoizedFieldProps, usePerformanceMonitor, useTypeInferredForm, useZodForm, validationPatterns, validationUtils, waitForFormState };
|
|
2192
|
+
export { AdvancedFieldBuilder, type BaseFormFieldConfig, BasicFormBuilder, type BooleanFieldConfig, type ButtonDefaults, type CheckboxDefaults, CheckboxField, type CheckboxFieldProps, type CommonFieldDefaults, CommonFields, ConditionalField, type ConditionalFieldConfig, type ConditionalFieldProps, type ConditionalValidation, ConfigurableForm, ContentField, type ContentFieldConfig, type CustomFieldConfig, DateField, type DateFieldConfig, type DateFieldProps, type DateInputDefaults, type DynamicSectionConfig, DynamicSectionField, type DynamicSectionFieldProps, type EnhancedFormState, FieldArrayBuilder, type FieldArrayConfig, FieldArrayField, type FieldArrayFieldProps, FieldArrayItemBuilder, type FieldBaseProps, type FieldGroup, FileField, type FileFieldConfig, type FileFieldProps, FontPickerField, type FontPickerFieldConfig, type FontPickerFieldProps, type FormConfig, FormField, type FormFieldConfig, FormFieldHelpers, type FormProps, FormProvider, FormStatus, type FormStatusProps, type FormStep, type FormSubmissionState, type FormTestUtils, FormToast, type FormToastProps, type FormValidationError, type HeroHookFormDefaultsConfig, HeroHookFormProvider, type HeroHookFormProviderProps, type InputDefaults, InputField, type InputFieldProps, type RadioFieldConfig, type RadioGroupDefaults, RadioGroupField, type RadioGroupFieldProps, type SelectDefaults, SelectField, type SelectFieldProps, ServerActionForm, type ServerFieldError, type ServerFormError, type SliderDefaults, SliderField, type SliderFieldConfig, type SliderFieldProps, type StringFieldConfig, SubmitButton, type SubmitButtonProps, type SwitchDefaults, SwitchField, type SwitchFieldProps, type TextareaDefaults, TextareaField, type TextareaFieldProps, TypeInferredBuilder, type UseDebouncedValidationOptions, type UseEnhancedFormStateOptions, type UseInferredFormOptions, type ValidationUtils, type WithControl, type WizardFormConfig, ZodForm, type ZodFormConfig, type ZodFormFieldConfig, applyServerErrors, asyncValidation, commonValidations, createAdvancedBuilder, createBasicFormBuilder, createDateSchema, createEmailSchema, createField, createFieldArrayBuilder, createFieldArrayItemBuilder, createFileSchema, createFormTestUtils, createFutureDateSchema, createMaxLengthSchema, createMinLengthSchema, createMockFormData, createMockFormErrors, createNestedPathBuilder, createNumberRangeSchema, createOptimizedFieldHandler, createPasswordSchema, createPastDateSchema, createPhoneSchema, createRequiredCheckboxSchema, createRequiredSchema, createTypeInferredBuilder, createUrlSchema, createZodFormConfig, crossFieldValidation, debounce, deepEqual, defineInferredForm, errorMessages, field, getFieldError, getFormErrors, hasFieldError, hasFormErrors, serverValidation, shallowEqual, simulateFieldInput, simulateFormSubmission, throttle, useDebouncedFieldValidation, useDebouncedValidation, useEnhancedFormState, useFormHelper, useHeroForm, useHeroHookFormDefaults, useInferredForm, useMemoizedCallback, useMemoizedFieldProps, usePerformanceMonitor, useTypeInferredForm, useZodForm, validationPatterns, validationUtils, waitForFormState };
|