Skip to content

Commit 762929f

Browse files
authored
fix: setField or setFields or setFieldValue should update field (#618)
* test: test driven * fix: match test
1 parent 5101a41 commit 762929f

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/Field.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,8 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
301301
}
302302

303303
case 'setField': {
304+
const { data } = info;
304305
if (namePathMatch) {
305-
const { data } = info;
306-
307306
if ('touched' in data) {
308307
this.touched = data.touched;
309308
}
@@ -320,6 +319,10 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
320319

321320
this.triggerMetaEvent();
322321

322+
this.reRender();
323+
return;
324+
} else if ('value' in data && containsNamePath(namePathList, namePath, true)) {
325+
// Contains path with value should also check
323326
this.reRender();
324327
return;
325328
}

tests/index.test.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ describe('Form.Basic', () => {
322322
// Not trigger
323323
fireEvent.submit(container.querySelector('form'));
324324
await timeout();
325-
console.log(container.innerHTML);
326325
matchError(container, "'user' is required");
327326
expect(onFinish).not.toHaveBeenCalled();
328327
expect(onFinishFailed).toHaveBeenCalledWith({
@@ -883,4 +882,38 @@ describe('Form.Basic', () => {
883882

884883
expect(onMetaChange).toHaveBeenCalledTimes(0);
885884
});
885+
886+
describe('set to null value', () => {
887+
function test(name: string, callback: (form: FormInstance) => void) {
888+
it(name, async () => {
889+
const form = React.createRef<FormInstance>();
890+
891+
const { container } = render(
892+
<div>
893+
<Form ref={form}>
894+
<InfoField name={['user', 'name']} initialValue="bamboo" />
895+
</Form>
896+
</div>,
897+
);
898+
899+
expect(container.querySelector('input').value).toBe('bamboo');
900+
expect(form.current.getFieldsValue()).toEqual({ user: { name: 'bamboo' } });
901+
902+
// Set it
903+
act(() => {
904+
callback(form.current!);
905+
});
906+
expect(form.current.getFieldValue(['user', 'name'])).toBeFalsy();
907+
expect(container.querySelector('input').value).toBe('');
908+
});
909+
}
910+
911+
test('by setFieldsValue', form => {
912+
form.setFieldsValue({ user: null });
913+
});
914+
915+
test('by setFieldValue', form => {
916+
form.setFieldValue('user', null);
917+
});
918+
});
886919
});

tests/initialValue.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ describe('Form.InitialValues', () => {
172172
console.log('Form Value:', refForm.getFieldsValue(true));
173173

174174
fireEvent.click(container.querySelector('button'));
175-
console.log(container.innerHTML);
176175

177176
expect(container.querySelector<HTMLInputElement>('.first-name-input').value).toEqual('aaa');
178177
});

0 commit comments

Comments
 (0)