@@ -2,9 +2,16 @@ import type { FormInstance } from '.';
2
2
import { FieldContext } from '.' ;
3
3
import warning from 'rc-util/lib/warning' ;
4
4
import { 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' ;
6
12
import { useState , useContext , useEffect , useRef , useMemo } from 'react' ;
7
13
import { getNamePath , getValue } from './utils/valueUtil' ;
14
+ import { isFormInstance } from './utils/typeUtil' ;
8
15
9
16
type ReturnPromise < T > = T extends Promise < infer ValueType > ? ValueType : never ;
10
17
type GetGeneric < TForm extends FormInstance > = ReturnPromise < ReturnType < TForm [ 'validateFields' ] > > ;
@@ -38,7 +45,7 @@ function useWatch<
38
45
TDependencies4 extends keyof GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] [ TDependencies3 ] ,
39
46
> (
40
47
dependencies : [ TDependencies1 , TDependencies2 , TDependencies3 , TDependencies4 ] ,
41
- form ?: TForm ,
48
+ form ?: TForm | WatchOptions < TForm > ,
42
49
) : GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] [ TDependencies3 ] [ TDependencies4 ] ;
43
50
44
51
function useWatch <
@@ -48,7 +55,7 @@ function useWatch<
48
55
TDependencies3 extends keyof GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] ,
49
56
> (
50
57
dependencies : [ TDependencies1 , TDependencies2 , TDependencies3 ] ,
51
- form ?: TForm ,
58
+ form ?: TForm | WatchOptions < TForm > ,
52
59
) : GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] [ TDependencies3 ] ;
53
60
54
61
function useWatch <
@@ -57,22 +64,34 @@ function useWatch<
57
64
TDependencies2 extends keyof GetGeneric < TForm > [ TDependencies1 ] ,
58
65
> (
59
66
dependencies : [ TDependencies1 , TDependencies2 ] ,
60
- form ?: TForm ,
67
+ form ?: TForm | WatchOptions < TForm > ,
61
68
) : GetGeneric < TForm > [ TDependencies1 ] [ TDependencies2 ] ;
62
69
63
70
function useWatch < TDependencies extends keyof GetGeneric < TForm > , TForm extends FormInstance > (
64
71
dependencies : TDependencies | [ TDependencies ] ,
65
- form ?: TForm ,
72
+ form ?: TForm | WatchOptions < TForm > ,
66
73
) : GetGeneric < TForm > [ TDependencies ] ;
67
74
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 > ;
69
79
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 ;
71
84
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 ;
73
94
74
- function useWatch ( ...args : [ NamePath , FormInstance ] ) {
75
- const [ dependencies = [ ] , form ] = args ;
76
95
const [ value , setValue ] = useState < any > ( ) ;
77
96
78
97
const valueStr = useMemo ( ( ) => stringify ( value ) , [ value ] ) ;
@@ -107,8 +126,8 @@ function useWatch(...args: [NamePath, FormInstance]) {
107
126
const { getFieldsValue, getInternalHooks } = formInstance ;
108
127
const { registerWatch } = getInternalHooks ( HOOK_MARK ) ;
109
128
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 ) ;
112
131
const nextValueStr = stringify ( newValue ) ;
113
132
114
133
// Compare stringify in case it's nest object
@@ -119,7 +138,10 @@ function useWatch(...args: [NamePath, FormInstance]) {
119
138
} ) ;
120
139
121
140
// 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
+ ) ;
123
145
setValue ( initialValue ) ;
124
146
125
147
return cancelRegister ;
0 commit comments