@@ -315,5 +315,128 @@ describe('Form.Preserve', () => {
315
315
316
316
wrapper . unmount ( ) ;
317
317
} ) ;
318
+
319
+ // https://github.com/ant-design/ant-design/issues/31297
320
+ describe ( 'A -> B -> C should keep trigger refresh' , ( ) => {
321
+ it ( 'shouldUpdate' , ( ) => {
322
+ const DepDemo = ( ) => {
323
+ const [ form ] = Form . useForm ( ) ;
324
+
325
+ return (
326
+ < Form form = { form } preserve = { false } >
327
+ < Form . Field name = "name" >
328
+ < Input id = "name" placeholder = "Username" />
329
+ </ Form . Field >
330
+
331
+ < Form . Field shouldUpdate >
332
+ { ( ) => {
333
+ return form . getFieldValue ( 'name' ) === '1' ? (
334
+ < Form . Field name = "password" >
335
+ < Input id = "password" placeholder = "Password" />
336
+ </ Form . Field >
337
+ ) : null ;
338
+ } }
339
+ </ Form . Field >
340
+
341
+ < Form . Field shouldUpdate >
342
+ { ( ) => {
343
+ const password = form . getFieldValue ( 'password' ) ;
344
+ return password ? (
345
+ < Form . Field name = "password2" >
346
+ < Input id = "password2" placeholder = "Password 2" />
347
+ </ Form . Field >
348
+ ) : null ;
349
+ } }
350
+ </ Form . Field >
351
+ </ Form >
352
+ ) ;
353
+ } ;
354
+
355
+ const wrapper = mount ( < DepDemo /> ) ;
356
+
357
+ // Input name to show password
358
+ wrapper
359
+ . find ( '#name' )
360
+ . last ( )
361
+ . simulate ( 'change' , { target : { value : '1' } } ) ;
362
+ expect ( wrapper . exists ( '#password' ) ) . toBeTruthy ( ) ;
363
+ expect ( wrapper . exists ( '#password2' ) ) . toBeFalsy ( ) ;
364
+
365
+ // Input password to show password2
366
+ wrapper
367
+ . find ( '#password' )
368
+ . last ( )
369
+ . simulate ( 'change' , { target : { value : '1' } } ) ;
370
+ expect ( wrapper . exists ( '#password2' ) ) . toBeTruthy ( ) ;
371
+
372
+ // Change name to hide password
373
+ wrapper
374
+ . find ( '#name' )
375
+ . last ( )
376
+ . simulate ( 'change' , { target : { value : '2' } } ) ;
377
+ expect ( wrapper . exists ( '#password' ) ) . toBeFalsy ( ) ;
378
+ expect ( wrapper . exists ( '#password2' ) ) . toBeFalsy ( ) ;
379
+ } ) ;
380
+
381
+ it ( 'dependencies' , ( ) => {
382
+ const DepDemo = ( ) => {
383
+ const [ form ] = Form . useForm ( ) ;
384
+
385
+ return (
386
+ < Form form = { form } preserve = { false } >
387
+ < Form . Field name = "name" >
388
+ < Input id = "name" placeholder = "Username" />
389
+ </ Form . Field >
390
+
391
+ < Form . Field dependencies = { [ 'name' ] } >
392
+ { ( ) => {
393
+ return form . getFieldValue ( 'name' ) === '1' ? (
394
+ < Form . Field name = "password" >
395
+ < Input id = "password" placeholder = "Password" />
396
+ </ Form . Field >
397
+ ) : null ;
398
+ } }
399
+ </ Form . Field >
400
+
401
+ < Form . Field dependencies = { [ 'password' ] } >
402
+ { ( ) => {
403
+ const password = form . getFieldValue ( 'password' ) ;
404
+ return password ? (
405
+ < Form . Field name = "password2" >
406
+ < Input id = "password2" placeholder = "Password 2" />
407
+ </ Form . Field >
408
+ ) : null ;
409
+ } }
410
+ </ Form . Field >
411
+ </ Form >
412
+ ) ;
413
+ } ;
414
+
415
+ const wrapper = mount ( < DepDemo /> ) ;
416
+
417
+ // Input name to show password
418
+ wrapper
419
+ . find ( '#name' )
420
+ . last ( )
421
+ . simulate ( 'change' , { target : { value : '1' } } ) ;
422
+ expect ( wrapper . exists ( '#password' ) ) . toBeTruthy ( ) ;
423
+ expect ( wrapper . exists ( '#password2' ) ) . toBeFalsy ( ) ;
424
+
425
+ // Input password to show password2
426
+ wrapper
427
+ . find ( '#password' )
428
+ . last ( )
429
+ . simulate ( 'change' , { target : { value : '1' } } ) ;
430
+ expect ( wrapper . exists ( '#password2' ) ) . toBeTruthy ( ) ;
431
+
432
+ // Change name to hide password
433
+ wrapper
434
+ . find ( '#name' )
435
+ . last ( )
436
+ . simulate ( 'change' , { target : { value : '2' } } ) ;
437
+ expect ( wrapper . exists ( '#password' ) ) . toBeFalsy ( ) ;
438
+ expect ( wrapper . exists ( '#password2' ) ) . toBeFalsy ( ) ;
439
+ } ) ;
440
+ } ) ;
318
441
} ) ;
319
442
/* eslint-enable no-template-curly-in-string */
0 commit comments