-
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathuseWatch.ts
More file actions
170 lines (140 loc) · 5.42 KB
/
useWatch.ts
File metadata and controls
170 lines (140 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import warning from '@rc-component/util/lib/warning';
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
import FieldContext, { HOOK_MARK } from './FieldContext';
import type {
FormInstance,
InternalFormInstance,
NamePath,
Store,
WatchOptions,
} from './interface';
import { isFormInstance } from './utils/typeUtil';
import { getNamePath, getValue } from './utils/valueUtil';
import { useEvent } from '@rc-component/util';
type ReturnPromise<T> = T extends Promise<infer ValueType> ? ValueType : never;
type GetGeneric<TForm extends FormInstance> = ReturnPromise<ReturnType<TForm['validateFields']>>;
export function stringify(value: any) {
try {
return JSON.stringify(value);
} catch {
return Math.random();
}
}
function useWatch<
TDependencies1 extends keyof GetGeneric<TForm>,
TForm extends FormInstance,
TDependencies2 extends keyof GetGeneric<TForm>[TDependencies1],
TDependencies3 extends keyof GetGeneric<TForm>[TDependencies1][TDependencies2],
TDependencies4 extends keyof GetGeneric<TForm>[TDependencies1][TDependencies2][TDependencies3],
>(
dependencies: [TDependencies1, TDependencies2, TDependencies3, TDependencies4],
form?: TForm | WatchOptions<TForm>,
): GetGeneric<TForm>[TDependencies1][TDependencies2][TDependencies3][TDependencies4];
function useWatch<
TDependencies1 extends keyof GetGeneric<TForm>,
TForm extends FormInstance,
TDependencies2 extends keyof GetGeneric<TForm>[TDependencies1],
TDependencies3 extends keyof GetGeneric<TForm>[TDependencies1][TDependencies2],
>(
dependencies: [TDependencies1, TDependencies2, TDependencies3],
form?: TForm | WatchOptions<TForm>,
): GetGeneric<TForm>[TDependencies1][TDependencies2][TDependencies3];
function useWatch<
TDependencies1 extends keyof GetGeneric<TForm>,
TForm extends FormInstance,
TDependencies2 extends keyof GetGeneric<TForm>[TDependencies1],
>(
dependencies: [TDependencies1, TDependencies2],
form?: TForm | WatchOptions<TForm>,
): GetGeneric<TForm>[TDependencies1][TDependencies2];
function useWatch<TDependencies extends keyof GetGeneric<TForm>, TForm extends FormInstance>(
dependencies: TDependencies | [TDependencies],
form?: TForm | WatchOptions<TForm>,
): GetGeneric<TForm>[TDependencies];
function useWatch<TForm extends FormInstance>(
dependencies: [],
form?: TForm | WatchOptions<TForm>,
): GetGeneric<TForm>;
// ------- selector type -------
function useWatch<TForm extends FormInstance, TSelected = unknown>(
selector: (values: GetGeneric<TForm>) => TSelected,
form?: TForm | WatchOptions<TForm>,
): TSelected;
function useWatch<ValueType = Store, TSelected = unknown>(
selector: (values: ValueType) => TSelected,
form?: FormInstance | WatchOptions<FormInstance>,
): TSelected;
// ------- selector type end -------
function useWatch<TForm extends FormInstance>(
dependencies: NamePath,
form?: TForm | WatchOptions<TForm>,
): any;
function useWatch<ValueType = Store>(
dependencies: NamePath,
form?: FormInstance | WatchOptions<FormInstance>,
): ValueType;
function useWatch(
...args: [NamePath | ((values: Store) => any), FormInstance | WatchOptions<FormInstance>]
) {
const [dependencies, _form = {}] = args;
const options = isFormInstance(_form) ? { form: _form } : _form;
const form = options.form;
const [value, setValue] = useState<any>(() =>
typeof dependencies === 'function' ? dependencies({}) : undefined,
);
const valueStr = useMemo(() => stringify(value), [value]);
const valueStrRef = useRef(valueStr);
valueStrRef.current = valueStr;
const fieldContext = useContext(FieldContext);
const formInstance = (form as InternalFormInstance) || fieldContext;
const isValidForm = formInstance && formInstance._init;
// Warning if not exist form instance
if (process.env.NODE_ENV !== 'production') {
warning(
args.length === 2 ? (form ? isValidForm : true) : isValidForm,
'useWatch requires a form instance since it can not auto detect from context.',
);
}
// ============================== Form ==============================
const { getFieldsValue, getInternalHooks } = formInstance;
const { registerWatch } = getInternalHooks(HOOK_MARK);
// ============================= Update =============================
const triggerUpdate = useEvent((values?: any, allValues?: any) => {
const watchValue = options.preserve
? (allValues ?? getFieldsValue(true))
: (values ?? getFieldsValue());
const nextValue =
typeof dependencies === 'function'
? dependencies(watchValue)
: getValue(watchValue, getNamePath(dependencies));
if (stringify(value) !== stringify(nextValue)) {
setValue(nextValue);
}
});
// ============================= Effect =============================
const flattenDeps =
typeof dependencies === 'function' ? dependencies : JSON.stringify(dependencies);
// Deps changed
useEffect(() => {
// Skip if not exist form instance
if (!isValidForm) {
return;
}
triggerUpdate();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isValidForm, flattenDeps]);
// Value changed
useEffect(() => {
// Skip if not exist form instance
if (!isValidForm) {
return;
}
const cancelRegister = registerWatch((values, allValues) => {
triggerUpdate(values, allValues);
});
return cancelRegister;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isValidForm]);
return value;
}
export default useWatch;