From 15929fb56f89134527937a8a4eb6e11f003ccb30 Mon Sep 17 00:00:00 2001 From: zombiej Date: Fri, 18 Sep 2020 11:38:47 +0800 Subject: [PATCH 1/3] test: test driven --- tests/index.test.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/index.test.js b/tests/index.test.js index 1b90d83b..cc84e950 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -212,6 +212,35 @@ describe('Form.Basic', () => { expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ target: { value: 'Bamboo' } })); }); + it('onValuesChange should not return fully value', async () => { + const onValuesChange = jest.fn(); + + const Demo = ({ hideField = false }) => ( +
+ {hideField && ( + + + + )} + + + +
+ ); + + const wrapper = mount(); + await changeValue(getField(wrapper, 'bamboo'), 'cute'); + expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { + light: 'little', + bamboo: 'cute', + }); + + onValuesChange.mockReset(); + wrapper.setProps({ hideField: true }); + await changeValue(getField(wrapper, 'bamboo'), 'beauty'); + expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { bamboo: 'beauty' }); + }); + it('submit', async () => { const onFinish = jest.fn(); const onFinishFailed = jest.fn(); From 5ad25e73ae1d8a1af1dc9a0e6f9254cd2f999658 Mon Sep 17 00:00:00 2001 From: zombiej Date: Fri, 18 Sep 2020 11:41:55 +0800 Subject: [PATCH 2/3] fix: onValueChange logic --- src/useForm.ts | 2 +- tests/index.test.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/useForm.ts b/src/useForm.ts index a0aa8eef..42274758 100644 --- a/src/useForm.ts +++ b/src/useForm.ts @@ -566,7 +566,7 @@ export class FormStore { if (onValuesChange) { const changedValues = cloneByNamePathList(this.store, [namePath]); - onValuesChange(changedValues, this.store); + onValuesChange(changedValues, this.getFieldsValue()); } this.triggerOnFieldsChange([namePath, ...childrenFields]); diff --git a/tests/index.test.js b/tests/index.test.js index cc84e950..58e0d1d3 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -215,9 +215,9 @@ describe('Form.Basic', () => { it('onValuesChange should not return fully value', async () => { const onValuesChange = jest.fn(); - const Demo = ({ hideField = false }) => ( + const Demo = ({ showField = true }) => (
- {hideField && ( + {showField && ( @@ -236,7 +236,7 @@ describe('Form.Basic', () => { }); onValuesChange.mockReset(); - wrapper.setProps({ hideField: true }); + wrapper.setProps({ showField: false }); await changeValue(getField(wrapper, 'bamboo'), 'beauty'); expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { bamboo: 'beauty' }); }); From d17b70a191ce289d84e798766b8edfb532595b23 Mon Sep 17 00:00:00 2001 From: zombiej Date: Fri, 18 Sep 2020 11:46:24 +0800 Subject: [PATCH 3/3] fix ts --- src/useForm.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/useForm.ts b/src/useForm.ts index 42274758..895da920 100644 --- a/src/useForm.ts +++ b/src/useForm.ts @@ -780,7 +780,7 @@ export class FormStore { function useForm(form?: FormInstance): [FormInstance] { const formRef = React.useRef(); - const [, forceUpdate] = React.useState(); + const [, forceUpdate] = React.useState({}); if (!formRef.current) { if (form) {