@@ -46,6 +46,8 @@ interface ValidateAction {
46
46
export type ReducerAction = UpdateAction | ValidateAction ;
47
47
48
48
export class FormStore {
49
+ private formHooked : boolean = false ;
50
+
49
51
private forceRootUpdate : ( ) => void ;
50
52
51
53
private subscribable : boolean = true ;
@@ -87,6 +89,8 @@ export class FormStore {
87
89
// ======================== Internal Hooks ========================
88
90
private getInternalHooks = ( key : string ) : InternalHooks | null => {
89
91
if ( key === HOOK_MARK ) {
92
+ this . formHooked = true ;
93
+
90
94
return {
91
95
dispatch : this . dispatch ,
92
96
registerField : this . registerField ,
@@ -98,7 +102,10 @@ export class FormStore {
98
102
} ;
99
103
}
100
104
101
- warning ( false , '`getInternalHooks` is internal usage. Should not call directly.' ) ;
105
+ warning (
106
+ false ,
107
+ '`getInternalHooks` is internal usage. Should not call directly.' ,
108
+ ) ;
102
109
return null ;
103
110
} ;
104
111
@@ -116,7 +123,8 @@ export class FormStore {
116
123
}
117
124
} ;
118
125
119
- private getInitialValue = ( namePath : InternalNamePath ) => getValue ( this . initialValues , namePath ) ;
126
+ private getInitialValue = ( namePath : InternalNamePath ) =>
127
+ getValue ( this . initialValues , namePath ) ;
120
128
121
129
private setCallbacks = ( callbacks : Callbacks ) => {
122
130
this . callbacks = callbacks ;
@@ -126,6 +134,15 @@ export class FormStore {
126
134
this . validateMessages = validateMessages ;
127
135
} ;
128
136
137
+ private warningUnhooked = ( ) => {
138
+ if ( process . env . NODE_ENV !== 'production' && ! this . formHooked ) {
139
+ warning (
140
+ false ,
141
+ 'Instance created by `useForm` is not connect to any Form element. Forget to pass `form` prop?' ,
142
+ ) ;
143
+ }
144
+ } ;
145
+
129
146
// ============================ Fields ============================
130
147
/**
131
148
* Get registered field entities.
@@ -149,6 +166,8 @@ export class FormStore {
149
166
} ;
150
167
151
168
private getFieldsValue = ( nameList ?: NamePath [ ] ) => {
169
+ this . warningUnhooked ( ) ;
170
+
152
171
if ( ! nameList ) {
153
172
return this . store ;
154
173
}
@@ -157,11 +176,15 @@ export class FormStore {
157
176
} ;
158
177
159
178
private getFieldValue = ( name : NamePath ) => {
179
+ this . warningUnhooked ( ) ;
180
+
160
181
const namePath : InternalNamePath = getNamePath ( name ) ;
161
182
return getValue ( this . store , namePath ) ;
162
183
} ;
163
184
164
185
private getFieldsError = ( nameList ?: NamePath [ ] ) => {
186
+ this . warningUnhooked ( ) ;
187
+
165
188
let fieldEntities = this . getFieldEntities ( true ) ;
166
189
167
190
if ( nameList ) {
@@ -189,12 +212,16 @@ export class FormStore {
189
212
} ;
190
213
191
214
private getFieldError = ( name : NamePath ) : string [ ] => {
215
+ this . warningUnhooked ( ) ;
216
+
192
217
const namePath = getNamePath ( name ) ;
193
218
const fieldError = this . getFieldsError ( [ namePath ] ) [ 0 ] ;
194
219
return fieldError . errors ;
195
220
} ;
196
221
197
222
private isFieldsTouched = ( ...args ) => {
223
+ this . warningUnhooked ( ) ;
224
+
198
225
const [ arg0 , arg1 ] = args ;
199
226
let namePathList : InternalNamePath [ ] | null ;
200
227
let isAllFieldsTouched = false ;
@@ -232,9 +259,14 @@ export class FormStore {
232
259
: this . getFieldEntities ( true ) . some ( testTouched ) ;
233
260
} ;
234
261
235
- private isFieldTouched = ( name : NamePath ) => this . isFieldsTouched ( [ name ] ) ;
262
+ private isFieldTouched = ( name : NamePath ) => {
263
+ this . warningUnhooked ( ) ;
264
+ return this . isFieldsTouched ( [ name ] ) ;
265
+ } ;
236
266
237
267
private isFieldsValidating = ( nameList ?: NamePath [ ] ) => {
268
+ this . warningUnhooked ( ) ;
269
+
238
270
const fieldEntities = this . getFieldEntities ( ) ;
239
271
if ( ! nameList ) {
240
272
return fieldEntities . some ( testField => testField . isFieldValidating ( ) ) ;
@@ -243,13 +275,22 @@ export class FormStore {
243
275
const namePathList : InternalNamePath [ ] = nameList . map ( getNamePath ) ;
244
276
return fieldEntities . some ( testField => {
245
277
const fieldNamePath = testField . getNamePath ( ) ;
246
- return containsNamePath ( namePathList , fieldNamePath ) && testField . isFieldValidating ( ) ;
278
+ return (
279
+ containsNamePath ( namePathList , fieldNamePath ) &&
280
+ testField . isFieldValidating ( )
281
+ ) ;
247
282
} ) ;
248
283
} ;
249
284
250
- private isFieldValidating = ( name : NamePath ) => this . isFieldsValidating ( [ name ] ) ;
285
+ private isFieldValidating = ( name : NamePath ) => {
286
+ this . warningUnhooked ( ) ;
287
+
288
+ return this . isFieldsValidating ( [ name ] ) ;
289
+ } ;
251
290
252
291
private resetFields = ( nameList ?: NamePath [ ] ) => {
292
+ this . warningUnhooked ( ) ;
293
+
253
294
const prevStore = this . store ;
254
295
if ( ! nameList ) {
255
296
this . store = setValues ( { } , this . initialValues ) ;
@@ -267,6 +308,8 @@ export class FormStore {
267
308
} ;
268
309
269
310
private setFields = ( fields : FieldData [ ] ) => {
311
+ this . warningUnhooked ( ) ;
312
+
270
313
const prevStore = this . store ;
271
314
272
315
fields . forEach ( ( fieldData : FieldData ) => {
@@ -278,7 +321,10 @@ export class FormStore {
278
321
this . store = setValue ( this . store , namePath , data . value ) ;
279
322
}
280
323
281
- this . notifyObservers ( prevStore , [ namePath ] , { type : 'setField' , data : fieldData } ) ;
324
+ this . notifyObservers ( prevStore , [ namePath ] , {
325
+ type : 'setField' ,
326
+ data : fieldData ,
327
+ } ) ;
282
328
} ) ;
283
329
} ;
284
330
@@ -340,7 +386,10 @@ export class FormStore {
340
386
const prevStore = this . store ;
341
387
this . store = setValue ( this . store , namePath , value ) ;
342
388
343
- this . notifyObservers ( prevStore , [ namePath ] , { type : 'valueUpdate' , source : 'internal' } ) ;
389
+ this . notifyObservers ( prevStore , [ namePath ] , {
390
+ type : 'valueUpdate' ,
391
+ source : 'internal' ,
392
+ } ) ;
344
393
345
394
// Notify dependencies children with parent update
346
395
const childrenFields = this . getDependencyChildrenFields ( namePath ) ;
@@ -364,16 +413,23 @@ export class FormStore {
364
413
365
414
// Let all child Field get update.
366
415
private setFieldsValue = ( store : Store ) => {
416
+ this . warningUnhooked ( ) ;
417
+
367
418
const prevStore = this . store ;
368
419
369
420
if ( store ) {
370
421
this . store = setValues ( this . store , store ) ;
371
422
}
372
423
373
- this . notifyObservers ( prevStore , null , { type : 'valueUpdate' , source : 'external' } ) ;
424
+ this . notifyObservers ( prevStore , null , {
425
+ type : 'valueUpdate' ,
426
+ source : 'external' ,
427
+ } ) ;
374
428
} ;
375
429
376
- private getDependencyChildrenFields = ( rootNamePath : InternalNamePath ) : InternalNamePath [ ] => {
430
+ private getDependencyChildrenFields = (
431
+ rootNamePath : InternalNamePath ,
432
+ ) : InternalNamePath [ ] => {
377
433
const children : Set < FieldEntity > = new Set ( ) ;
378
434
const childrenFields : InternalNamePath [ ] = [ ] ;
379
435
@@ -431,6 +487,8 @@ export class FormStore {
431
487
nameList ?: NamePath [ ] ,
432
488
options ?: ValidateOptions ,
433
489
) => {
490
+ this . warningUnhooked ( ) ;
491
+
434
492
const provideNameList = ! ! nameList ;
435
493
const namePathList : InternalNamePath [ ] | undefined = provideNameList
436
494
? nameList . map ( getNamePath )
@@ -486,12 +544,18 @@ export class FormStore {
486
544
summaryPromise
487
545
. catch ( results => results )
488
546
. then ( ( results : FieldError [ ] ) => {
489
- const resultNamePathList : InternalNamePath [ ] = results . map ( ( { name } ) => name ) ;
490
- this . notifyObservers ( this . store , resultNamePathList , { type : 'validateFinish' } ) ;
547
+ const resultNamePathList : InternalNamePath [ ] = results . map (
548
+ ( { name } ) => name ,
549
+ ) ;
550
+ this . notifyObservers ( this . store , resultNamePathList , {
551
+ type : 'validateFinish' ,
552
+ } ) ;
491
553
this . triggerOnFieldsChange ( resultNamePathList ) ;
492
554
} ) ;
493
555
494
- const returnPromise : Promise < Store | ValidateErrorEntity | string [ ] > = summaryPromise
556
+ const returnPromise : Promise <
557
+ Store | ValidateErrorEntity | string [ ]
558
+ > = summaryPromise
495
559
. then (
496
560
( ) : Promise < Store | string [ ] > => {
497
561
if ( this . lastValidatePromise === summaryPromise ) {
@@ -501,7 +565,9 @@ export class FormStore {
501
565
} ,
502
566
)
503
567
. catch ( ( results : { name : InternalNamePath ; errors : string [ ] } [ ] ) => {
504
- const errorList = results . filter ( result => result && result . errors . length ) ;
568
+ const errorList = results . filter (
569
+ result => result && result . errors . length ,
570
+ ) ;
505
571
return Promise . reject ( {
506
572
values : this . getFieldsValue ( namePathList ) ,
507
573
errorFields : errorList ,
@@ -517,6 +583,8 @@ export class FormStore {
517
583
518
584
// ============================ Submit ============================
519
585
private submit = ( ) => {
586
+ this . warningUnhooked ( ) ;
587
+
520
588
this . validateFields ( )
521
589
. then ( values => {
522
590
const { onFinish } = this . callbacks ;
0 commit comments