formsy-react
formsy-react copied to clipboard
onChange event should not fire for programmatic changes
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 onChange
event 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?)
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.
[Closing out issues I authored that appear to be stagnant.]