Skip to content

Commit

Permalink
chore: onMetaChange should trigger on unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jun 2, 2021
1 parent be4f130 commit 274dcd3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export interface InternalFieldProps<Values = any> {
messageVariables?: Record<string, string>;
initialValue?: any;
onReset?: () => void;
onMetaChange?: (meta: Meta) => void;
onMetaChange?: (meta: Meta & { destroy?: boolean }) => void;
preserve?: boolean;

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

public componentWillUnmount() {
this.cancelRegister();
this.triggerMetaEvent(true);
this.mounted = false;
}

Expand Down Expand Up @@ -215,10 +216,10 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
}));
};

public triggerMetaEvent = () => {
public triggerMetaEvent = (destroy?: boolean) => {
const { onMetaChange } = this.props;

onMetaChange?.(this.getMeta());
onMetaChange?.({ ...this.getMeta(), destroy });
};

// ========================= Field Entity Interfaces =========================
Expand Down
14 changes: 14 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ describe('Form.Basic', () => {
expect(form.getFieldError('password')).toEqual(["'password' is required"]);
expect(form.isFieldTouched('password')).toBeTruthy();
});

it('remove Field should trigger onMetaChange', () => {
const onMetaChange = jest.fn();
const wrapper = mount(
<Form>
<Field name="username" onMetaChange={onMetaChange}>
<Input />
</Field>
</Form>,
);

wrapper.unmount();
expect(onMetaChange).toHaveBeenCalledWith(expect.objectContaining({ destroy: true }));
});
});

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

1 comment on commit 274dcd3

@vercel
Copy link

@vercel vercel bot commented on 274dcd3 Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.