diff --git a/src/Form.tsx b/src/Form.tsx index d49d15ca..24126dff 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'>; @@ -153,6 +154,12 @@ const Form: React.ForwardRefRenderFunction = ( formInstance.submit(); }} + onReset={(event: React.FormEvent) => { + event.preventDefault(); + + formInstance.resetFields(); + restProps.onReset?.(event); + }} > {wrapperNode} diff --git a/tests/index.test.js b/tests/index.test.js index 58e0d1d3..419fd0c5 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -240,7 +240,23 @@ describe('Form.Basic', () => { await changeValue(getField(wrapper, 'bamboo'), 'beauty'); expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { bamboo: 'beauty' }); }); - + 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); + const { value } = wrapper.find('input').props(); + expect(value).toEqual(''); + }); it('submit', async () => { const onFinish = jest.fn(); const onFinishFailed = jest.fn();