Skip to content

Commit 613687b

Browse files
committed
Field support onReset
1 parent 19e2105 commit 613687b

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Field.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface FieldProps {
5151
trigger?: string;
5252
validateTrigger?: string | string[] | false;
5353
valuePropName?: string;
54+
onReset?: () => void;
5455
}
5556

5657
export interface FieldState {
@@ -157,7 +158,7 @@ class Field extends React.Component<FieldProps, FieldState> implements FieldEnti
157158
namePathList: InternalNamePath[] | null,
158159
info: NotifyInfo,
159160
) => {
160-
const { shouldUpdate, dependencies = [] } = this.props;
161+
const { shouldUpdate, dependencies = [], onReset } = this.props;
161162
const { getFieldsValue, getFieldError }: FormInstance = this.context;
162163
const values = getFieldsValue();
163164
const namePath = this.getNamePath();
@@ -171,6 +172,10 @@ class Field extends React.Component<FieldProps, FieldState> implements FieldEnti
171172
this.touched = false;
172173
this.validatePromise = null;
173174

175+
if (onReset) {
176+
onReset();
177+
}
178+
174179
this.refresh();
175180
return;
176181
}

tests/index.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ describe('Basic', () => {
9191
function resetTest(name, ...args) {
9292
it(name, async () => {
9393
let form;
94+
const onReset = jest.fn();
9495

9596
const wrapper = mount(
9697
<div>
@@ -99,7 +100,7 @@ describe('Basic', () => {
99100
form = instance;
100101
}}
101102
>
102-
<Field name="username" rules={[{ required: true }]}>
103+
<Field name="username" rules={[{ required: true }]} onReset={onReset}>
103104
<Input />
104105
</Field>
105106
</Form>
@@ -111,20 +112,25 @@ describe('Basic', () => {
111112
expect(form.getFieldError('username')).toEqual([]);
112113
expect(form.isFieldTouched('username')).toBeTruthy();
113114

115+
expect(onReset).not.toHaveBeenCalled();
114116
form.resetFields(...args);
115117
expect(form.getFieldValue('username')).toEqual(undefined);
116118
expect(form.getFieldError('username')).toEqual([]);
117119
expect(form.isFieldTouched('username')).toBeFalsy();
120+
expect(onReset).toHaveBeenCalled();
121+
onReset.mockRestore();
118122

119123
await changeValue(getField(wrapper, 'username'), '');
120124
expect(form.getFieldValue('username')).toEqual('');
121125
expect(form.getFieldError('username')).toEqual(["'username' is required"]);
122126
expect(form.isFieldTouched('username')).toBeTruthy();
123127

128+
expect(onReset).not.toHaveBeenCalled();
124129
form.resetFields(...args);
125130
expect(form.getFieldValue('username')).toEqual(undefined);
126131
expect(form.getFieldError('username')).toEqual([]);
127132
expect(form.isFieldTouched('username')).toBeFalsy();
133+
expect(onReset).toHaveBeenCalled();
128134
});
129135
}
130136

0 commit comments

Comments
 (0)