@@ -2,9 +2,16 @@ import type { FormInstance } from '.';
22import { FieldContext } from '.' ;
33import warning from 'rc-util/lib/warning' ;
44import { HOOK_MARK } from './FieldContext' ;
5- import type { InternalFormInstance , InternalNamePath , NamePath , Store } from './interface' ;
5+ import type {
6+ InternalFormInstance ,
7+ InternalNamePath ,
8+ NamePath ,
9+ Store ,
10+ WatchOptions ,
11+ } from './interface' ;
612import { useState , useContext , useEffect , useRef , useMemo } from 'react' ;
713import { getNamePath , getValue } from './utils/valueUtil' ;
14+ import { isFormInstance } from './utils/typeUtil' ;
815
916type ReturnPromise < T > = T extends Promise < infer ValueType > ? ValueType : never ;
1017type GetGeneric < TForm extends FormInstance > = ReturnPromise < ReturnType < TForm [ 'validateFields' ] > > ;
@@ -38,7 +45,7 @@ function useWatch<
3845 TDependencies4 extends keyof GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] [ TDependencies3 ] ,
3946> (
4047 dependencies : [ TDependencies1 , TDependencies2 , TDependencies3 , TDependencies4 ] ,
41- form ?: TForm ,
48+ form ?: TForm | WatchOptions < TForm > ,
4249) : GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] [ TDependencies3 ] [ TDependencies4 ] ;
4350
4451function useWatch <
@@ -48,7 +55,7 @@ function useWatch<
4855 TDependencies3 extends keyof GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] ,
4956> (
5057 dependencies : [ TDependencies1 , TDependencies2 , TDependencies3 ] ,
51- form ?: TForm ,
58+ form ?: TForm | WatchOptions < TForm > ,
5259) : GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] [ TDependencies3 ] ;
5360
5461function useWatch <
@@ -57,22 +64,34 @@ function useWatch<
5764 TDependencies2 extends keyof GetGeneric < TForm > [ TDependencies1 ] ,
5865> (
5966 dependencies : [ TDependencies1 , TDependencies2 ] ,
60- form ?: TForm ,
67+ form ?: TForm | WatchOptions < TForm > ,
6168) : GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] ;
6269
6370function useWatch < TDependencies extends keyof GetGeneric < TForm > , TForm extends FormInstance > (
6471 dependencies : TDependencies | [ TDependencies ] ,
65- form ?: TForm ,
72+ form ?: TForm | WatchOptions < TForm > ,
6673) : GetGeneric < TForm > [ TDependencies ] ;
6774
68- function useWatch < TForm extends FormInstance > ( dependencies : [ ] , form ?: TForm ) : GetGeneric < TForm > ;
75+ function useWatch < TForm extends FormInstance > (
76+ dependencies : [ ] ,
77+ form ?: TForm | WatchOptions < TForm > ,
78+ ) : GetGeneric < TForm > ;
6979
70- function useWatch < TForm extends FormInstance > ( dependencies : NamePath , form ?: TForm ) : any ;
80+ function useWatch < TForm extends FormInstance > (
81+ dependencies : NamePath ,
82+ form ?: TForm | WatchOptions < TForm > ,
83+ ) : any ;
7184
72- function useWatch < ValueType = Store > ( dependencies : NamePath , form ?: FormInstance ) : ValueType ;
85+ function useWatch < ValueType = Store > (
86+ dependencies : NamePath ,
87+ form ?: FormInstance | WatchOptions < FormInstance > ,
88+ ) : ValueType ;
89+
90+ function useWatch ( ...args : [ NamePath , FormInstance | WatchOptions < FormInstance > ] ) {
91+ const [ dependencies = [ ] , _form = { } ] = args ;
92+ const options = isFormInstance ( _form ) ? { form : _form } : _form ;
93+ const form = options . form ;
7394
74- function useWatch ( ...args : [ NamePath , FormInstance ] ) {
75- const [ dependencies = [ ] , form ] = args ;
7695 const [ value , setValue ] = useState < any > ( ) ;
7796
7897 const valueStr = useMemo ( ( ) => stringify ( value ) , [ value ] ) ;
@@ -107,8 +126,8 @@ function useWatch(...args: [NamePath, FormInstance]) {
107126 const { getFieldsValue, getInternalHooks } = formInstance ;
108127 const { registerWatch } = getInternalHooks ( HOOK_MARK ) ;
109128
110- const cancelRegister = registerWatch ( store => {
111- const newValue = getValue ( store , namePathRef . current ) ;
129+ const cancelRegister = registerWatch ( ( values , allValues ) => {
130+ const newValue = getValue ( options . preserve ? allValues : values , namePathRef . current ) ;
112131 const nextValueStr = stringify ( newValue ) ;
113132
114133 // Compare stringify in case it's nest object
@@ -119,7 +138,10 @@ function useWatch(...args: [NamePath, FormInstance]) {
119138 } ) ;
120139
121140 // TODO: We can improve this perf in future
122- const initialValue = getValue ( getFieldsValue ( ) , namePathRef . current ) ;
141+ const initialValue = getValue (
142+ options . preserve ? getFieldsValue ( true ) : getFieldsValue ( ) ,
143+ namePathRef . current ,
144+ ) ;
123145 setValue ( initialValue ) ;
124146
125147 return cancelRegister ;
0 commit comments