22/* eslint-disable react-hooks/exhaustive-deps */
33
44import type { ComponentPropsWithoutRef , ComponentRef , ElementType , HTMLProps , Ref } from 'react' ;
5- import { forwardRef , useEffect , useImperativeHandle , useRef } from 'react' ;
5+ import { forwardRef , useEffect , useImperativeHandle , useMemo , useRef } from 'react' ;
66
7- import type { FormInstance , InternalFormInstance , RegisterCallbackOptions } from './FieldContext' ;
7+ import type { FormInstance , InternalFormContext , InternalFormInstance , RegisterCallbackOptions } from './FieldContext' ;
88import { FieldContextProvider } from './FieldContext' ;
99import type { ValidateMessages } from './form-core/validate' ;
1010import useForm from './useForm' ;
@@ -16,6 +16,7 @@ interface FormBaseProps<Values = any> extends RegisterCallbackOptions<Values> {
1616 initialValues ?: Partial < Values > ;
1717 preserve ?: boolean ;
1818 validateMessages ?: ValidateMessages ;
19+ validateTrigger ?: string | string [ ] ;
1920}
2021
2122type PolymorphicProps < As extends ElementType , Own > = Own &
@@ -40,6 +41,7 @@ const Form = <Values=any, As extends ElementType = 'form'>(props: FormProps<Valu
4041 onValuesChange,
4142 preserve = true ,
4243 validateMessages,
44+ validateTrigger = 'onChange' ,
4345 ...rest
4446 } = props ;
4547
@@ -53,6 +55,14 @@ const Form = <Values=any, As extends ElementType = 'form'>(props: FormProps<Valu
5355 formInstance as InternalFormInstance < Values >
5456 ) . getInternalHooks ( ) ;
5557
58+ const formContextValue = useMemo < InternalFormContext < Values > > (
59+ ( ) => ( {
60+ ...formInstance ,
61+ validateTrigger
62+ } ) ,
63+ [ formInstance , validateTrigger ]
64+ ) ;
65+
5666 useImperativeHandle ( ref as any , ( ) => nativeElement . current ) ;
5767
5868 if ( ! mountRef . current ) {
@@ -78,14 +88,14 @@ const Form = <Values=any, As extends ElementType = 'form'>(props: FormProps<Valu
7888 } , [ ] ) ;
7989
8090 if ( Component === false ) {
81- return < FieldContextProvider value = { formInstance } > { children } </ FieldContextProvider > ;
91+ return < FieldContextProvider value = { formContextValue } > { children } </ FieldContextProvider > ;
8292 }
8393
8494 if ( Component === 'form' ) {
8595 const { onReset, onSubmit, ...formProps } = rest as HTMLProps < HTMLFormElement > ;
8696
8797 return (
88- < FieldContextProvider value = { formInstance } >
98+ < FieldContextProvider value = { formContextValue } >
8999 < Component
90100 { ...formProps }
91101 ref = { nativeElement as Ref < HTMLFormElement > }
@@ -108,7 +118,7 @@ const Form = <Values=any, As extends ElementType = 'form'>(props: FormProps<Valu
108118 }
109119
110120 return (
111- < FieldContextProvider value = { formInstance } >
121+ < FieldContextProvider value = { formContextValue } >
112122 < Component
113123 { ...( rest as any ) }
114124 ref = { nativeElement }
0 commit comments