Skip to content

Commit

Permalink
Pass submit event to onSubmitX methods, closes #549 (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh authored Aug 27, 2020
1 parent 12a2871 commit 800dff3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
39 changes: 37 additions & 2 deletions __tests__/Formsy.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ describe('mapModel', () => {
{ 'parent.child': 'test', testChange: true },
expect.any(Function),
expect.any(Function),
expect.any(Object),
);
});
});
Expand Down Expand Up @@ -556,7 +557,12 @@ describe('value === false', () => {

const form = mount(<TestForm />);
form.simulate('submit');
expect(onSubmit).toHaveBeenCalledWith({ foo: false }, expect.any(Function), expect.any(Function));
expect(onSubmit).toHaveBeenCalledWith(
{ foo: false },
expect.any(Function),
expect.any(Function),
expect.any(Object),
);
});

it('should allow dynamic changes to false', () => {
Expand Down Expand Up @@ -589,7 +595,12 @@ describe('value === false', () => {
const form = mount(<TestForm />);
(form.instance() as TestForm).changeValue();
form.simulate('submit');
expect(onSubmit).toHaveBeenCalledWith({ foo: false }, expect.any(Function), expect.any(Function));
expect(onSubmit).toHaveBeenCalledWith(
{ foo: false },
expect.any(Function),
expect.any(Function),
expect.any(Object),
);
});

it('should say the form is submitted', () => {
Expand Down Expand Up @@ -961,3 +972,27 @@ describe('form valid state', () => {
expect(isValid).toEqual(true);
});
});

describe('onSubmit/onValidSubmit/onInvalidSubmit', () => {
['onSubmit', 'onValidSubmit', 'onInvalidSubmit'].forEach((key) => {
it(`should pass submit event to "${key}"`, () => {
const submitSpy = jest.fn();

const form = mount(
<Formsy {...{ [key]: submitSpy }}>
<button id="submit">submit</button>
{key === 'onInvalidSubmit' && <TestInput name="test" required={true} />}
</Formsy>,
);
const button = form.find('#submit');
button.simulate('submit');

expect(submitSpy).toHaveBeenCalledWith(
expect.any(Object),
expect.any(Function),
expect.any(Function),
expect.objectContaining({ type: 'submit' }),
);
});
});
});
21 changes: 14 additions & 7 deletions src/Formsy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@ import { PassDownProps } from './withFormsy';

type FormHTMLAttributesCleaned = Omit<React.FormHTMLAttributes<HTMLFormElement>, 'onChange' | 'onSubmit'>;

type OnSubmitCallback = (
model: IModel,
resetModel: IResetModel,
updateInputsWithError: IUpdateInputsWithError,
event: React.SyntheticEvent<React.FormHTMLAttributes<any>>,
) => void;

export interface FormsyProps extends FormHTMLAttributesCleaned {
disabled: boolean;
mapping: null | ((model: IModel) => IModel);
onChange: (model: IModel, isChanged: boolean) => void;
onInvalid: () => void;
onInvalidSubmit: (model: IModel, resetModel: IResetModel, updateInputsWithError: IUpdateInputsWithError) => void;
onReset?: () => void;
onSubmit?: (model: IModel, resetModel: IResetModel, updateInputsWithError: IUpdateInputsWithError) => void;
onSubmit?: OnSubmitCallback;
onValidSubmit?: OnSubmitCallback;
onInvalidSubmit: OnSubmitCallback;
onValid: () => void;
onValidSubmit?: (model: IModel, resetModel: IResetModel, updateInputsWithError: IUpdateInputsWithError) => void;
preventDefaultSubmit?: boolean;
preventExternalInvalidation?: boolean;
validationErrors?: null | object;
Expand Down Expand Up @@ -331,7 +338,7 @@ export class Formsy extends React.Component<FormsyProps, FormsyState> {
public isChanged = () => !utils.isSame(this.getPristineValues(), this.getCurrentValues());

// Update model, submit to url prop and send the model
public submit = (event?: any) => {
public submit = (event?: React.SyntheticEvent) => {
const { onSubmit, onValidSubmit, onInvalidSubmit, preventDefaultSubmit } = this.props;
const { isValid } = this.state;

Expand All @@ -344,12 +351,12 @@ export class Formsy extends React.Component<FormsyProps, FormsyState> {
// so validation becomes visible (if based on isPristine)
this.setFormPristine(false);
const model = this.getModel();
onSubmit(model, this.resetModel, this.updateInputsWithError);
onSubmit(model, this.resetModel, this.updateInputsWithError, event);

if (isValid) {
onValidSubmit(model, this.resetModel, this.updateInputsWithError);
onValidSubmit(model, this.resetModel, this.updateInputsWithError, event);
} else {
onInvalidSubmit(model, this.resetModel, this.updateInputsWithError);
onInvalidSubmit(model, this.resetModel, this.updateInputsWithError, event);
}
};

Expand Down

0 comments on commit 800dff3

Please sign in to comment.