From f13825c2e66853696569e10b5c3ed4086e275c7f Mon Sep 17 00:00:00 2001 From: chenliang Date: Tue, 2 Mar 2021 17:30:53 +0800 Subject: [PATCH 1/4] feature: add onReset props --- src/Form.tsx | 13 ++++++++++--- tests/index.test.js | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Form.tsx b/src/Form.tsx index d49d15ca..ce6e1314 100644 --- a/src/Form.tsx +++ b/src/Form.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { +import type { Store, FormInstance, FieldData, @@ -9,7 +9,8 @@ import { } from './interface'; import useForm from './useForm'; import FieldContext, { HOOK_MARK } from './FieldContext'; -import FormContext, { FormContextProps } from './FormContext'; +import type { FormContextProps } from './FormContext'; +import FormContext from './FormContext'; import { isSimilar } from './utils/valueUtil'; type BaseFormProps = Omit, 'onSubmit'>; @@ -146,13 +147,19 @@ const Form: React.ForwardRefRenderFunction = ( return ( ) => { event.preventDefault(); event.stopPropagation(); formInstance.submit(); }} + onReset={(event: React.FormEvent) => { + event.preventDefault(); + event.stopPropagation(); + + formInstance.resetFields(); + }} + {...restProps} > {wrapperNode} diff --git a/tests/index.test.js b/tests/index.test.js index 58e0d1d3..c432dd99 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -240,7 +240,21 @@ describe('Form.Basic', () => { await changeValue(getField(wrapper, 'bamboo'), 'beauty'); expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { bamboo: 'beauty' }); }); - + it('reset', async () => { + const wrapper = mount( +
+ + + + +
, + ); + await changeValue(getField(wrapper), 'Bamboo'); + wrapper.find('button').simulate('reset'); + await timeout(); + const { value } = wrapper.find('input').props(); + expect(value).toEqual(''); + }); it('submit', async () => { const onFinish = jest.fn(); const onFinishFailed = jest.fn(); From 75d794e2565952baf557b9235f711043d75f7721 Mon Sep 17 00:00:00 2001 From: chenliang Date: Tue, 2 Mar 2021 17:32:46 +0800 Subject: [PATCH 2/4] fix: remove unused code From 2d7560d8b147720d5111b5f194f6983c7aff5896 Mon Sep 17 00:00:00 2001 From: chenliang Date: Thu, 4 Mar 2021 10:01:10 +0800 Subject: [PATCH 3/4] fix: not call onReset props --- src/Form.tsx | 2 +- tests/index.test.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Form.tsx b/src/Form.tsx index ce6e1314..d7e7458d 100644 --- a/src/Form.tsx +++ b/src/Form.tsx @@ -155,9 +155,9 @@ const Form: React.ForwardRefRenderFunction = ( }} onReset={(event: React.FormEvent) => { event.preventDefault(); - event.stopPropagation(); formInstance.resetFields(); + restProps.onReset?.(event); }} {...restProps} > diff --git a/tests/index.test.js b/tests/index.test.js index c432dd99..d3bf7cf5 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -255,6 +255,21 @@ describe('Form.Basic', () => { const { value } = wrapper.find('input').props(); expect(value).toEqual(''); }); + it('should call onReset fn, when the button is clicked', async () => { + const resetFn = jest.fn(); + const wrapper = mount( +
+ + + + +
, + ); + await changeValue(getField(wrapper), 'Bamboo'); + wrapper.find('button').simulate('reset'); + await timeout(); + expect(resetFn).toHaveBeenCalledTimes(1); + }); it('submit', async () => { const onFinish = jest.fn(); const onFinishFailed = jest.fn(); From 5b2247436d0461d9ba4590f4b3ac9a3fc73a7248 Mon Sep 17 00:00:00 2001 From: chenliang Date: Thu, 4 Mar 2021 14:14:40 +0800 Subject: [PATCH 4/4] fix: a potential bug(ignore user passed onReset prop) --- src/Form.tsx | 2 +- tests/index.test.js | 17 ++--------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/Form.tsx b/src/Form.tsx index d7e7458d..24126dff 100644 --- a/src/Form.tsx +++ b/src/Form.tsx @@ -147,6 +147,7 @@ const Form: React.ForwardRefRenderFunction = ( return ( ) => { event.preventDefault(); event.stopPropagation(); @@ -159,7 +160,6 @@ const Form: React.ForwardRefRenderFunction = ( formInstance.resetFields(); restProps.onReset?.(event); }} - {...restProps} > {wrapperNode} diff --git a/tests/index.test.js b/tests/index.test.js index d3bf7cf5..419fd0c5 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -240,21 +240,6 @@ describe('Form.Basic', () => { await changeValue(getField(wrapper, 'bamboo'), 'beauty'); expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { bamboo: 'beauty' }); }); - it('reset', async () => { - const wrapper = mount( -
- - - - -
, - ); - await changeValue(getField(wrapper), 'Bamboo'); - wrapper.find('button').simulate('reset'); - await timeout(); - const { value } = wrapper.find('input').props(); - expect(value).toEqual(''); - }); it('should call onReset fn, when the button is clicked', async () => { const resetFn = jest.fn(); const wrapper = mount( @@ -269,6 +254,8 @@ describe('Form.Basic', () => { wrapper.find('button').simulate('reset'); await timeout(); expect(resetFn).toHaveBeenCalledTimes(1); + const { value } = wrapper.find('input').props(); + expect(value).toEqual(''); }); it('submit', async () => { const onFinish = jest.fn();