diff --git a/components/form/FormItem.tsx b/components/form/FormItem.tsx index 196ae76ec3..54b9e2b71a 100644 --- a/components/form/FormItem.tsx +++ b/components/form/FormItem.tsx @@ -122,12 +122,15 @@ function FormItem(props: FormItemProps): React.ReactElement { const updateChildItemErrors = noStyle ? updateItemErrors : (subName: string, subErrors: string[]) => { - if (!isEqual(inlineErrors[subName], subErrors)) { - setInlineErrors(prevInlineErrors => ({ - ...prevInlineErrors, - [subName]: subErrors, - })); - } + setInlineErrors((prevInlineErrors = {}) => { + if (!isEqual(prevInlineErrors[subName], subErrors)) { + return { + ...prevInlineErrors, + [subName]: subErrors, + }; + } + return prevInlineErrors; + }); }; // ===================== Children Ref ===================== diff --git a/components/form/__tests__/index.test.js b/components/form/__tests__/index.test.js index 65e5fe1c71..ba15bbe44c 100644 --- a/components/form/__tests__/index.test.js +++ b/components/form/__tests__/index.test.js @@ -40,24 +40,78 @@ describe('Form', () => { scrollIntoView.mockRestore(); }); - it('noStyle Form.Item', async () => { - const onChange = jest.fn(); + describe('noStyle Form.Item', () => { + it('work', async () => { + const onChange = jest.fn(); - const wrapper = mount( -
- - - + const wrapper = mount( + + + + + - - , - ); + , + ); - await change(wrapper, 0, ''); - expect(wrapper.find('.ant-form-item-explain').length).toBeTruthy(); - expect(wrapper.find('.ant-form-item-has-error').length).toBeTruthy(); + await change(wrapper, 0, ''); + expect(wrapper.find('.ant-form-item-explain').length).toBeTruthy(); + expect(wrapper.find('.ant-form-item-has-error').length).toBeTruthy(); - expect(onChange).toHaveBeenCalled(); + expect(onChange).toHaveBeenCalled(); + }); + + it('should clean up', async () => { + const Demo = () => { + const [form] = Form.useForm(); + + return ( +
+ + { + await sleep(0); + try { + await form.validateFields(); + } catch (e) { + // do nothing + } + }} + /> + + + {() => { + const aaa = form.getFieldValue('aaa'); + + if (aaa === '1') { + return ( + + + + ); + } + + return ( + + + + + + ); + }} + +
+ ); + }; + + const wrapper = mount(); + await change(wrapper, 0, '1'); + expect(wrapper.find('.ant-form-item-explain').text()).toEqual('aaa'); + await change(wrapper, 0, '2'); + expect(wrapper.find('.ant-form-item-explain').text()).toEqual('ccc'); + await change(wrapper, 0, '1'); + expect(wrapper.find('.ant-form-item-explain').text()).toEqual('aaa'); + }); }); it('`shouldUpdate` should work with render props', () => {