Skip to content

Commit 74a22db

Browse files
jueininchenliang
andauthored
feature: add default onReset props on Form component (#278)
* feature: add onReset props * fix: remove unused code * fix: not call onReset props * fix: a potential bug(ignore user passed onReset prop) Co-authored-by: chenliang <[email protected]>
1 parent dda8717 commit 74a22db

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Form.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import {
2+
import type {
33
Store,
44
FormInstance,
55
FieldData,
@@ -9,7 +9,8 @@ import {
99
} from './interface';
1010
import useForm from './useForm';
1111
import FieldContext, { HOOK_MARK } from './FieldContext';
12-
import FormContext, { FormContextProps } from './FormContext';
12+
import type { FormContextProps } from './FormContext';
13+
import FormContext from './FormContext';
1314
import { isSimilar } from './utils/valueUtil';
1415

1516
type BaseFormProps = Omit<React.FormHTMLAttributes<HTMLFormElement>, 'onSubmit'>;
@@ -153,6 +154,12 @@ const Form: React.ForwardRefRenderFunction<FormInstance, FormProps> = (
153154

154155
formInstance.submit();
155156
}}
157+
onReset={(event: React.FormEvent<HTMLFormElement>) => {
158+
event.preventDefault();
159+
160+
formInstance.resetFields();
161+
restProps.onReset?.(event);
162+
}}
156163
>
157164
{wrapperNode}
158165
</Component>

tests/index.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,23 @@ describe('Form.Basic', () => {
240240
await changeValue(getField(wrapper, 'bamboo'), 'beauty');
241241
expect(onValuesChange).toHaveBeenCalledWith(expect.anything(), { bamboo: 'beauty' });
242242
});
243-
243+
it('should call onReset fn, when the button is clicked', async () => {
244+
const resetFn = jest.fn();
245+
const wrapper = mount(
246+
<Form onReset={resetFn}>
247+
<InfoField name={'user'}>
248+
<Input />
249+
</InfoField>
250+
<button type="reset">reset</button>
251+
</Form>,
252+
);
253+
await changeValue(getField(wrapper), 'Bamboo');
254+
wrapper.find('button').simulate('reset');
255+
await timeout();
256+
expect(resetFn).toHaveBeenCalledTimes(1);
257+
const { value } = wrapper.find('input').props();
258+
expect(value).toEqual('');
259+
});
244260
it('submit', async () => {
245261
const onFinish = jest.fn();
246262
const onFinishFailed = jest.fn();

0 commit comments

Comments
 (0)