Skip to content

Commit

Permalink
feat: pass the changed attribute though the onChange form callback
Browse files Browse the repository at this point in the history
Ref: #143
  • Loading branch information
AdeAttwood committed Mar 25, 2023
1 parent 7682422 commit e751896
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface FormProps<T extends {}> {
/**
* Callback that is called whenever an attribute is changed
*/
onChange?: (context: ReturnType<Form<T>["getContextValue"]>) => void;
onChange?: (context: ReturnType<Form<T>["getContextValue"]>, attribute: string) => void;
/**
*
* TODO(ade): sort out prop with children
Expand Down Expand Up @@ -193,7 +193,7 @@ export class Form<T extends Record<string, any>> extends React.Component<FormPro
}

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

/**
Expand Down
1 change: 1 addition & 0 deletions tests/on-change.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ it("will call onChange when a attribute is changed", async () => {
});

expect(onChange).toHaveBeenCalled();
expect(onChange).toBeCalledWith(expect.objectContaining({ formState: { "test-input": "A" } }), "test-input");
});

1 comment on commit e751896

@vercel
Copy link

@vercel vercel bot commented on e751896 Mar 25, 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-git-0x-adeattwood.vercel.app
react-form-sandy.vercel.app
react-form-adeattwood.vercel.app

Please sign in to comment.