@@ -929,4 +929,49 @@ describe('Form.Basic', () => {
929929 form . setFieldValue ( 'user' , null ) ;
930930 } ) ;
931931 } ) ;
932+
933+ it ( 'setFieldValue should always set touched' , async ( ) => {
934+ const EMPTY_VALUES = { light : '' , bamboo : [ ] } ;
935+ const formRef = React . createRef < FormInstance > ( ) ;
936+
937+ const Demo : React . FC = ( ) => (
938+ < Form ref = { formRef } initialValues = { EMPTY_VALUES } >
939+ < Field name = "light" rules = { [ { required : true } ] } >
940+ < Input />
941+ </ Field >
942+ < Field name = "bamboo" rules = { [ { required : true , type : 'array' } ] } >
943+ < Input />
944+ </ Field >
945+ </ Form >
946+ ) ;
947+
948+ render ( < Demo /> ) ;
949+
950+ await act ( async ( ) => {
951+ await formRef . current ?. validateFields ( ) . catch ( ( ) => { } ) ;
952+ } ) ;
953+ expect ( formRef . current ?. isFieldTouched ( 'light' ) ) . toBeFalsy ( ) ;
954+ expect ( formRef . current ?. isFieldTouched ( 'bamboo' ) ) . toBeFalsy ( ) ;
955+ expect ( formRef . current ?. getFieldError ( 'light' ) ) . toHaveLength ( 1 ) ;
956+ expect ( formRef . current ?. getFieldError ( 'bamboo' ) ) . toHaveLength ( 1 ) ;
957+
958+ act ( ( ) => {
959+ formRef . current ?. setFieldsValue ( EMPTY_VALUES ) ;
960+ } ) ;
961+ expect ( formRef . current ?. isFieldTouched ( 'light' ) ) . toBeFalsy ( ) ;
962+ expect ( formRef . current ?. isFieldTouched ( 'bamboo' ) ) . toBeFalsy ( ) ;
963+ expect ( formRef . current ?. getFieldError ( 'light' ) ) . toHaveLength ( 1 ) ;
964+ expect ( formRef . current ?. getFieldError ( 'bamboo' ) ) . toHaveLength ( 1 ) ;
965+
966+ act ( ( ) => {
967+ formRef . current ?. setFieldsValue ( {
968+ light : 'Bamboo' ,
969+ bamboo : [ 'Light' ] ,
970+ } ) ;
971+ } ) ;
972+ expect ( formRef . current ?. isFieldTouched ( 'light' ) ) . toBeTruthy ( ) ;
973+ expect ( formRef . current ?. isFieldTouched ( 'bamboo' ) ) . toBeTruthy ( ) ;
974+ expect ( formRef . current ?. getFieldError ( 'light' ) ) . toHaveLength ( 0 ) ;
975+ expect ( formRef . current ?. getFieldError ( 'bamboo' ) ) . toHaveLength ( 0 ) ;
976+ } ) ;
932977} ) ;
0 commit comments