Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import {
import type {
Store,
FormInstance,
FieldData,
Expand All @@ -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<React.FormHTMLAttributes<HTMLFormElement>, 'onSubmit'>;
Expand Down Expand Up @@ -153,6 +154,12 @@ const Form: React.ForwardRefRenderFunction<FormInstance, FormProps> = (

formInstance.submit();
}}
onReset={(event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

formInstance.resetFields();
restProps.onReset?.(event);
}}
>
{wrapperNode}
</Component>
Expand Down
18 changes: 17 additions & 1 deletion tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Form onReset={resetFn}>
<InfoField name={'user'}>
<Input />
</InfoField>
<button type="reset">reset</button>
</Form>,
);
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();
Expand Down