Skip to content

Commit 274dcd3

Browse files
committed
chore: onMetaChange should trigger on unmount
1 parent be4f130 commit 274dcd3

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Field.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export interface InternalFieldProps<Values = any> {
7676
messageVariables?: Record<string, string>;
7777
initialValue?: any;
7878
onReset?: () => void;
79-
onMetaChange?: (meta: Meta) => void;
79+
onMetaChange?: (meta: Meta & { destroy?: boolean }) => void;
8080
preserve?: boolean;
8181

8282
/** @private Passed by Form.List props. Do not use since it will break by path check. */
@@ -168,6 +168,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
168168

169169
public componentWillUnmount() {
170170
this.cancelRegister();
171+
this.triggerMetaEvent(true);
171172
this.mounted = false;
172173
}
173174

@@ -215,10 +216,10 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
215216
}));
216217
};
217218

218-
public triggerMetaEvent = () => {
219+
public triggerMetaEvent = (destroy?: boolean) => {
219220
const { onMetaChange } = this.props;
220221

221-
onMetaChange?.(this.getMeta());
222+
onMetaChange?.({ ...this.getMeta(), destroy });
222223
};
223224

224225
// ========================= Field Entity Interfaces =========================

tests/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ describe('Form.Basic', () => {
217217
expect(form.getFieldError('password')).toEqual(["'password' is required"]);
218218
expect(form.isFieldTouched('password')).toBeTruthy();
219219
});
220+
221+
it('remove Field should trigger onMetaChange', () => {
222+
const onMetaChange = jest.fn();
223+
const wrapper = mount(
224+
<Form>
225+
<Field name="username" onMetaChange={onMetaChange}>
226+
<Input />
227+
</Field>
228+
</Form>,
229+
);
230+
231+
wrapper.unmount();
232+
expect(onMetaChange).toHaveBeenCalledWith(expect.objectContaining({ destroy: true }));
233+
});
220234
});
221235

222236
it('should throw if no Form in use', () => {

0 commit comments

Comments
 (0)