Created
March 14, 2018 17:22
-
-
Save BitOfUniverse/ecee2c3fa2ae41d0a31d7baec92f19d8 to your computer and use it in GitHub Desktop.
Send onChange event in React
This file contains hidden or 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
// in React after 15.6 | |
const triggerInputChange = (node, value = '') => { | |
const inputTypes = [ | |
window.HTMLInputElement, | |
window.HTMLSelectElement, | |
window.HTMLTextAreaElement | |
]; | |
if (inputTypes.indexOf(node.__proto__.constructor) > -1) { | |
const setValue = Object.getOwnPropertyDescriptor(node.__proto__, 'value') | |
.set; | |
setValue.call(node, value); | |
const event = new Event('input', { bubbles: true }); | |
node.dispatchEvent(event); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment