Skip to content

Commit 6d539aa

Browse files
authored
fix: setFieldValue should be touched (#759)
1 parent b0209d0 commit 6d539aa

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/useForm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ export class FormStore {
812812
value,
813813
errors: [],
814814
warnings: [],
815+
touched: true,
815816
},
816817
]);
817818
};

tests/index.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,10 @@ describe('Form.Basic', () => {
885885
Array.from(container.querySelectorAll<HTMLInputElement>('input')).map(input => input?.value),
886886
).toEqual(['bamboo', 'little', 'light', 'nested']);
887887

888+
// Check initial touched state
889+
expect(formRef.current.isFieldTouched(['list', 1])).toBeFalsy();
890+
expect(formRef.current.isFieldTouched(['nest', 'target'])).toBeFalsy();
891+
888892
// Set
889893
act(() => {
890894
formRef.current.setFieldValue(['list', 1], 'tiny');
@@ -894,6 +898,15 @@ describe('Form.Basic', () => {
894898
expect(
895899
Array.from(container.querySelectorAll<HTMLInputElement>('input')).map(input => input?.value),
896900
).toEqual(['bamboo', 'tiny', 'light', 'match']);
901+
902+
// Check that setFieldValue DOES set touched to true
903+
// (setFieldValue internally calls setFields with touched: true)
904+
expect(formRef.current.isFieldTouched(['list', 1])).toBeTruthy();
905+
expect(formRef.current.isFieldTouched(['nest', 'target'])).toBeTruthy();
906+
907+
// Verify other fields remain untouched
908+
expect(formRef.current.isFieldTouched(['list', 0])).toBeFalsy();
909+
expect(formRef.current.isFieldTouched(['list', 2])).toBeFalsy();
897910
});
898911

899912
it('onMetaChange should only trigger when meta changed', () => {

0 commit comments

Comments
 (0)