-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dispatchEvent on input/textarea is ignored #10135
Comments
cc @nhunzaker |
Oops, I meant to cc @jquense |
Yes this is expected and unavoidable to some extent. React dedupe change and input events so they don't fire too often, in this case even tho you intentionally want to trigger the event react is swallowing it because it the input value has not changed. you can use the SimulateNative helper to get the behavior your looking for. Tho that will break again in v16 |
What's the workaround? |
I guess this is grey area and unclear if we supported this. As @jquense mentioned you can use |
cypress-io/cypress#536 from an additional use case affected similarly. They outline their long term "workaround" as well. I do think this falls into a gray area, complete transparent DOM interoperability is always nice but hard to do and potentially limiting give Reacts model for targeting and it :/ |
@jquense Btw. this happens no matter if the value is changed or not - I probably should've included that info in this issue. I understand that this is unexpected (the event should not be swallowed by React if the value has changed), so I'm including an updated example: https://jsfiddle.net/6bv1581z/1/ |
I'll need some time to process the use case in cypress (esp. the part about the setters - don't really know what the spec says about this), so I'm not strongly opinionated for now. In any case, the I'm just wondering - won't this affect the ability to make integration tests for React-based apps using the DOM api? Sorry if that's all answered in the linked thread, I have yet to connect the dots. |
Just leaving a solution for future reference (checked in Edge 15, IE 11, FF 53, Chrome 59): function setNativeValue(element, value) {
const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
const prototype = Object.getPrototypeOf(element);
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
if (valueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value);
} else {
valueSetter.call(element, value);
}
} Use it like so: setNativeValue(textarea, 'some text');
textarea.dispatchEvent(new Event('input', { bubbles: true })); |
Yes, i also left that bit off because its a bit hard to explain, but React tracks manual DOMNode.value changes as well, so when you do
It shouldn't, if you are using something like Selenium, which fires "real" events when you ask it too, vs "fake" ones we can fire in the DOM. Comparative tools like Cypress or nightmare, etc should use workarounds that mimic the real browsers behavior so if something breaks (as in this case) it was more of a bug in Cypress :) |
Hi friends! I think I must be doing something wrong, but I have a pretty simple demo of the In the example I'm getting an error that seems to indicate that the To save you a click, here's the whole example: <!doctype html>
<html>
<body>
<textarea></textarea>
<script>
const textarea = document.getElementsByTagName('textarea')[0]
function setNativeValue(element, value) {
const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set
const prototype = Object.getPrototypeOf(element)
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set
if (valueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value)
} else {
valueSetter.call(element, value)
}
}
setNativeValue(textarea, 'some text')
textarea.dispatchEvent(new Event('input', { bubbles: true }))
</script>
</body>
</html> What am I missing? |
I'm not sure why I missed this or why this is, but it looks like there actually is a value setter on the prototype but there isn't always one on the element. So here's my adjusted version that works: <!doctype html>
<html>
<body>
<textarea></textarea>
<script>
const textarea = document.getElementsByTagName('textarea')[0]
function setNativeValue(element, value) {
const { set: valueSetter } = Object.getOwnPropertyDescriptor(element, 'value') || {}
const prototype = Object.getPrototypeOf(element)
const { set: prototypeValueSetter } = Object.getOwnPropertyDescriptor(prototype, 'value') || {}
if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value)
} else if (valueSetter) {
valueSetter.call(element, value)
} else {
throw new Error('The given element does not have a value setter')
}
}
setNativeValue(textarea, 'some text')
textarea.dispatchEvent(new Event('input', { bubbles: true }))
</script>
</body>
</html> Good luck friends. Stay safe out there! |
@fatfisz @kentcdodds Could you please show how to do this for radio inputs? |
Found another issue talking about this that proposes an alternative solution: /**
* See [Modify React Component's State using jQuery/Plain Javascript from Chrome Extension](https://stackoverflow.com/q/41166005)
* See https://github.com/facebook/react/issues/11488#issuecomment-347775628
* See [How to programmatically fill input elements built with React?](https://stackoverflow.com/q/40894637)
* See https://github.com/facebook/react/issues/10135#issuecomment-401496776
*
* @param {HTMLInputElement} input
* @param {string} value
*/
function setReactInputValue(input, value) {
const previousValue = input.value;
// eslint-disable-next-line no-param-reassign
input.value = value;
const tracker = input._valueTracker;
if (tracker) {
tracker.setValue(previousValue);
}
// 'change' instead of 'input', see https://github.com/facebook/react/issues/11488#issuecomment-381590324
input.dispatchEvent(new Event('change', { bubbles: true }));
} Usage: setReactInputValue(document.getElementById('name'), 'Your name');
document.getElementById('radio').click();
document.getElementById('checkbox').click(); |
Hi Everyone, |
Actually we're using a software implementation of the WebDriver interface in order to automate web apps on Smart TVs and other devices where no native WebDriver interface is available. |
In order to trigger an animation, my controlled text inputs were required to dispatch a change event by clearing their stateful values after the form data was validated and submitted. While the above solution works well, at least within my application, it was causing the following warning:
Adding the dispatched event to the next available cycle in the event loop queue solved the issue:
|
Oh, kinda forgot about this issue, but now that I'm here I thought I'd mention that the |
Implemented the AdminCredentials component and copied its ErrorResponse component styling to the Login component, which appropriately displays an "Invalid Credentials" error message outside of the relevant TextInput components group. Also, added a SetNativeValue utility function to dispatch "change" events on TextInput components when their values are cleared by updates to their state. For performance reasons React does not dispatch onChange or onInput events when a value of an HTMLElement is updated by state (RE: facebook/react#10135).
I just tried the Here is my use case:
Is this by any means possible? Because if I'm going to deal with caret jumping anyway, I can always call my What I did so far:
|
Coursemology uses React and simply changing the value attribute of the multiplier input will not change Coursemology's state. As such, every time an onChange event is fired from any text input (which in Coursemology's case, all has), all the values are re-rendered (since changing grades and multipliers ultimately affect EXP, and they must be displayed. This was an issue raised in React in 2017, but ultimately deemed as a security feature. To solve this issue, we will have to directly invoke the onChange event listener in the input prototype (as prescribed in coursemology.js in webpack output). Read more here facebook/react#10135 (comment)
i have tried this with tiktok input for comment but they have a span nested in div continuously and the setNativeValue doesn't work for it because divs and spans doesn't have a .set property... anyone has any idea to make it work ? |
Hi! Anyone tried the same thing for drop down menus built with React? |
Hello, anyone knows why it all salvations above are not working with Redux state? https://codepen.io/eqwfsdgfdg/pen/MWOmaQJ |
Is there a similar workaround if its a contentEditable div? |
I need to know too. ;Is there a similar workaround if its a contentEditable div? |
bumping this for contentEditable div |
the code next is worked for me. // a login form simulate
const $ = document.querySelector.bind(document);
const username = $('[name=username]');
username.value = 'your username';
const password = $('[name=password]');
password.value = 'userpassword';
const button = $('button');
// find the key
const key = Object.keys(username).find(key=>key.includes('__reactEventHandlers'));
// invoke onchange
username[key].onChange({target:username});
password[key].onChange({target:password});
button.click() |
Unfortunately, the proposed solutions did not work for Textara. The page for which I need this code is designed with React and works for all elements except textarea. I want to automatically enter values in the elements in Firefox and from the console. help me please |
Hey @mehdijalili2000, 👋 This is an old issue, and React has been updated since. How about you open a new issue for this bug? Thanks! ❤️ |
hi. I know has been updated but i have this problem.
…On Tue, Feb 13, 2024 at 2:48 AM Peter Briggs ***@***.***> wrote:
Hey @mehdijalili2000 <https://github.com/mehdijalili2000>, 👋
This is an old issue, and React has been updated since. How about you open
a new issue <https://github.com/facebook/react/issues/new/choose> for
this bug?
Thanks! ❤️
—
Reply to this email directly, view it on GitHub
<#10135 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ATOY3EIMKW2NQBJWSGV4EKTYTKPK3AVCNFSM4DSMMFZKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJTHE3TMOBRGM3Q>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Cross-reference for future readers: see #28311. |
hi. I know has been updated but i have this problem. |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
The
dispatchEvent
method has no effect on input/textarea elements.If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/84v837e9/).
v. 15.5.4: https://jsfiddle.net/c8tp5mqf/ (working)
v. 15.6.1: https://jsfiddle.net/6bv1581z/ (not working)
What is the expected behavior?
The
dispatchEvent
method results in an event being handled.Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
I'm using the latest Chrome browser. This has worked in 15.5.x, stopped working in 15.6.0 and is still not working in 15.6.1.
Usecase for this: some old tests that I'd happily remove, but have to support for now.
The text was updated successfully, but these errors were encountered: