@@ -4,7 +4,7 @@ import { mount, ReactWrapper } from 'enzyme';
44import { resetWarned } from 'rc-util/lib/warning' ;
55import Form , { Field , List , FormProps } from '../src' ;
66import { ListField , ListOperations , ListProps } from '../src/List' ;
7- import { Meta } from '../src/interface' ;
7+ import { FormInstance , Meta } from '../src/interface' ;
88import { Input } from './common/InfoField' ;
99import { changeValue , getField } from './common' ;
1010import timeout from './common/timeout' ;
@@ -646,4 +646,97 @@ describe('Form.List', () => {
646646 wrapper . find ( 'button' ) . simulate ( 'click' ) ;
647647 expect ( onValuesChange ) . toHaveBeenCalledWith ( expect . anything ( ) , { list : [ { first : 'light' } ] } ) ;
648648 } ) ;
649+
650+ describe ( 'isFieldTouched edge case' , ( ) => {
651+ it ( 'virtual object' , ( ) => {
652+ const formRef = React . createRef < FormInstance > ( ) ;
653+ const wrapper = mount (
654+ < Form ref = { formRef } >
655+ < Form . Field name = { [ 'user' , 'name' ] } >
656+ < Input />
657+ </ Form . Field >
658+ < Form . Field name = { [ 'user' , 'age' ] } >
659+ < Input />
660+ </ Form . Field >
661+ </ Form > ,
662+ ) ;
663+
664+ // Not changed
665+ expect ( formRef . current . isFieldTouched ( 'user' ) ) . toBeFalsy ( ) ;
666+ expect ( formRef . current . isFieldsTouched ( [ 'user' ] , false ) ) . toBeFalsy ( ) ;
667+ expect ( formRef . current . isFieldsTouched ( [ 'user' ] , true ) ) . toBeFalsy ( ) ;
668+
669+ // Changed
670+ wrapper
671+ . find ( 'input' )
672+ . first ( )
673+ . simulate ( 'change' , { target : { value : '' } } ) ;
674+
675+ expect ( formRef . current . isFieldTouched ( 'user' ) ) . toBeTruthy ( ) ;
676+ expect ( formRef . current . isFieldsTouched ( [ 'user' ] , false ) ) . toBeTruthy ( ) ;
677+ expect ( formRef . current . isFieldsTouched ( [ 'user' ] , true ) ) . toBeTruthy ( ) ;
678+ } ) ;
679+
680+ it ( 'List children change' , ( ) => {
681+ const [ wrapper ] = generateForm (
682+ fields => (
683+ < div >
684+ { fields . map ( field => (
685+ < Field { ...field } >
686+ < Input />
687+ </ Field >
688+ ) ) }
689+ </ div >
690+ ) ,
691+ {
692+ initialValues : { list : [ 'light' , 'bamboo' ] } ,
693+ } ,
694+ ) ;
695+
696+ // Not changed yet
697+ expect ( form . isFieldTouched ( 'list' ) ) . toBeFalsy ( ) ;
698+ expect ( form . isFieldsTouched ( [ 'list' ] , false ) ) . toBeFalsy ( ) ;
699+ expect ( form . isFieldsTouched ( [ 'list' ] , true ) ) . toBeFalsy ( ) ;
700+
701+ // Change children value
702+ wrapper
703+ . find ( 'input' )
704+ . first ( )
705+ . simulate ( 'change' , { target : { value : 'little' } } ) ;
706+
707+ expect ( form . isFieldTouched ( 'list' ) ) . toBeTruthy ( ) ;
708+ expect ( form . isFieldsTouched ( [ 'list' ] , false ) ) . toBeTruthy ( ) ;
709+ expect ( form . isFieldsTouched ( [ 'list' ] , true ) ) . toBeTruthy ( ) ;
710+ } ) ;
711+
712+ it ( 'List self change' , ( ) => {
713+ const [ wrapper ] = generateForm ( ( fields , opt ) => (
714+ < div >
715+ { fields . map ( field => (
716+ < Field { ...field } >
717+ < Input />
718+ </ Field >
719+ ) ) }
720+ < button
721+ type = "button"
722+ onClick = { ( ) => {
723+ opt . add ( ) ;
724+ } }
725+ />
726+ </ div >
727+ ) ) ;
728+
729+ // Not changed yet
730+ expect ( form . isFieldTouched ( 'list' ) ) . toBeFalsy ( ) ;
731+ expect ( form . isFieldsTouched ( [ 'list' ] , false ) ) . toBeFalsy ( ) ;
732+ expect ( form . isFieldsTouched ( [ 'list' ] , true ) ) . toBeFalsy ( ) ;
733+
734+ // Change children value
735+ wrapper . find ( 'button' ) . simulate ( 'click' ) ;
736+
737+ expect ( form . isFieldTouched ( 'list' ) ) . toBeTruthy ( ) ;
738+ expect ( form . isFieldsTouched ( [ 'list' ] , false ) ) . toBeTruthy ( ) ;
739+ expect ( form . isFieldsTouched ( [ 'list' ] , true ) ) . toBeTruthy ( ) ;
740+ } ) ;
741+ } ) ;
649742} ) ;
0 commit comments