formsy-react icon indicating copy to clipboard operation
formsy-react copied to clipboard

onChange event should not fire for programmatic changes

Open broofa opened this issue 5 years ago • 1 comments

Start with a Component that updates props state asynchronously when form values change, something like this:

function MyFormThing(props) {
  function handleChange(formState, isChanged) {
    console.log('isChanged:', isChanged)

    // (trigger async action that changes `props.foo`, below)
  }

  return (<Form onChange={handleChange}>
    <Input value={props.foo} />
  </Form>);
}

Then type something into Input field and hit TAB or whatever to cause the onChangeevent to fire. Notice the log output will be:

isChanged: true
isChanged: false

onChange appears to be getting called a second time, when the form rerenders as a result of the props changing. That should not happen - programmatic changes to form values should not trigger onChange events.

(Note: That Formsy is passing an isChanged argument to the event handler is conceptually bizarre. Why is onChange being called if nothing has changed?)

broofa avatar Oct 01 '19 20:10 broofa

So in summary, onChange should only fire if the values differ. This seems like something we could probably write a test for and then implement internally without breaking anything or changing the API at all. Unless I'm missing some complexity here, very open to contributions.

rkuykendall avatar Feb 22 '20 20:02 rkuykendall

[Closing out issues I authored that appear to be stagnant.]

broofa avatar Apr 03 '23 17:04 broofa