Created
July 12, 2019 16:53
-
-
Save manuelbieh/b1a6715e2c2e2a96b5195ee66acc1a9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onChange = (e: React.ChangeEvent) => { | |
e.persist(); | |
const { | |
target: { checked, name, type, value }, | |
} = e; | |
if (!name && process.env.NODE_ENV === 'development') { | |
console.warn( | |
`Change event with value '${value}' happened for an element without name prop. This is probably by mistake.` | |
); | |
} | |
// TODO: figure out if this is really such a good idea | |
this.resetError(name); | |
// TODO: make this more flexible (see the work that has been done in array-parser.js for more) | |
if (type === 'checkbox') { | |
if (!value || value === 'on') { | |
return this.setState((state) => ({ | |
values: { | |
...state.values, | |
[name]: checked ? 'on' : undefined, | |
}, | |
})); | |
} | |
return this.setState((state) => ({ | |
values: { | |
...state.values, | |
[name]: (checked | |
? (Array.isArray(state.values[name]) | |
? state.values[name] | |
: [] | |
).concat(value) | |
: (Array.isArray(state.values[name]) | |
? state.values[name] | |
: [] | |
).filter((item) => item !== value) | |
).reduce( | |
(acc, item) => (item ? (acc || []).concat(item) : acc), | |
undefined | |
), | |
}, | |
})); | |
} | |
this.setValue(name, value); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment