Skip to content

Commit

Permalink
feat: add onChange prop
Browse files Browse the repository at this point in the history
Ref: #94
  • Loading branch information
AdeAttwood committed Jan 20, 2023
1 parent 64d6d0d commit b7c7cda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export interface FormProps<T extends {}> {
*/
onSubmit: (params: { formState: T }) => any | Promise<any>;
/**
* Callback that is called whenever an attribute is changed
*/
onChange?: (context: ReturnType<Form<T>["getContextValue"]>) => void;
/**
*
* TODO(ade): sort out prop with children
*/
children: any;
Expand Down Expand Up @@ -158,6 +163,7 @@ export class Form<T extends Record<string, any>> extends React.Component<FormPro
}

this.setState({ formState, errors, status });
this.props.onChange?.(this.getContextValue());
};

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/on-change.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";
import { render, screen, cleanup, act } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

import Form from "../src/form";
import Input from "./input-component";

afterEach(cleanup);

it("will call onChange when a attribute is changed", async () => {
const onChange = jest.fn();
const onSubmit = jest.fn();

render(
<Form onChange={onChange} onSubmit={onSubmit}>
<Input attribute="test-input" />
</Form>
);

await act(async () => {
await userEvent.type(screen.getByLabelText("test-input"), "A");
});

expect(onChange).toHaveBeenCalled();
});

1 comment on commit b7c7cda

@vercel
Copy link

@vercel vercel bot commented on b7c7cda Jan 20, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

react-form – ./

react-form-sandy.vercel.app
react-form-git-0x-adeattwood.vercel.app
react-form-adeattwood.vercel.app

Please sign in to comment.