From 1edb545fc08bb9a5f2637bc12d512527be756f56 Mon Sep 17 00:00:00 2001 From: linct96 <314220356@qq.com> Date: Fri, 26 Aug 2022 14:49:22 +0800 Subject: [PATCH] fix: Fix setFieldValue API --- src/Field.tsx | 8 ++++++++ tests/index.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/Field.tsx b/src/Field.tsx index b848c738..3ddc544e 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -312,6 +312,14 @@ class Field extends React.Component implements F this.reRender(); return; } + + // check dependencies when related fields changed + const hasDep = + namePathList && + namePathList.some(p => containsNamePath(dependencies.map(getNamePath), p)); + if (hasDep) { + this.reRender(); + } break; } diff --git a/tests/index.test.js b/tests/index.test.js index d24a6d20..1d27bb46 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -880,4 +880,29 @@ describe('Form.Basic', () => { 'match', ]); }); + + it('setFieldValue should check dependencies', () => { + const formRef = React.createRef(); + let rendered = false; + const Demo = () => ( +
+ + + + + {(_, __, { getFieldsValue }) => { + rendered = true; + return
{JSON.stringify(getFieldsValue(), null, 2)}
; + }} +
+
+ ); + + const wrapper = mount(); + // Set + rendered = false; + formRef.current.setFieldValue(['user', 'target'], 'setFieldValue'); + wrapper.update(); + expect(rendered).toBeTruthy(); + }); });