-
Notifications
You must be signed in to change notification settings - Fork 47.6k
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
Trigger simulated input value change for React 16 (after react-dom 15.6.0 updated)? #11488
Comments
I'm not sure if that Can you provide a full isolated example of what you're trying to do? Maybe this answer could be helpful: #10003 (comment) |
Thanks for the response! Basically I have to use pure JS or jQuery (without React access) to enter some values in some input fields within a form rendered by React 16. Earlier the form and input fields was rendered with React 15.6.x but after the form/site updated to React 16 the pure JS way of simulating input value change stopped working. So looking to quickly know how to trigger or simulate input value update so it follows the right dispatch event sequence or method in React 16. I'm unsure if I can use ReactTestUtils as a simulate method just by injecting the react and react-dom 16.0.0 standard scripts as I don't have access to the original form's React implementation. I prefer to trigger this with HTML dom and JS. But guess that is unreliable so wondering if using ReactTestUtils as a standalone to trigger input change will work? |
Can you expand on why you need this? It is not exactly clear what your constraints are. |
It is to interact with a React based website's form from a proprietary web browser which has to use the external form. The browser has limited access to external scripts. It is just one of those legacy systems that has to interact with a modern SPA web page to trigger a form submission. The legacy system which supports JS and jQuery may be deprecated. But really just need to figure out how to trigger this input value change as it worked in the past. I can at most put inline JS code or possibly a external JS URL to refer to external library if I have use ReactTestUtils as a simulate method. Was hoping to not be in this situation, but trying to resolve with what is possible in the interim. |
Can you just call |
Yep I could, but how can do that using the console or outside the React scope? for example in this codepen: https://codepen.io/gkitarp/pen/pdNRKP I don't have direct access to the React component, only the global scope of console is used. So in the example the //Outside the React scope is what I have access to and can edit. |
You can put the reference to your component onto the input, for example. |
yeah not intended as a public API, its wasn't even intended to make it into v16, it was a workaround to prevent a breaking change in v15 minor bump. We should have a better story for this perhaps, but "correct" way to work around this is outlined here: cypress-io/cypress#536 (comment) There should be vanishingly few use-cases though where you need to get underneath the event system like this. I'm not suggesting you don't have one of those use-cases, but i would try almost anything else before this :P since this is generally outside what i'd consider "reasonable" interop of none react interactions |
After some research of react source code, I got a hack method for react 16:
NOTICE: JUST A HACK |
@whosesmile any way to get this to work with '.selectedIndex' instead of '.value'? Or just on a in general, even if applying change to '.value' (not working for me)? |
For me the event type
|
See this for a solution: |
this worked for me. changing a select menu on a compiled react page, and making sure the appropriate change events fire.
|
A good example of where this might be necessary is when interacting with https://github.com/escaladesports/react-hubspot-form. The component interface does not provide any way to initialize values for a form, the component does not store input values in "state" so Anyway, the only way (I've found) to initialize this form is to do so by manipulating the inputs on the DOM and manually triggering react to pick up the changes. FWIW: I would agree that this is a problem with the react-hubspot-form interface (https://github.com/escaladesports/react-hubspot-form/issues/13), and not necessarily with React itself (I'll leave that to you all). I just thought I'd provide some context. |
@whosesmile thanks for the hack! My use case is I have a bunch of bookmark scripts that do things such as auto-login, or auto-fill forms with various test data. When you have a constant stream of new development servers, autofill usually doesn't work. I've had to update these scripts several times with new React versions. |
This is very needed for Selenium tests automation - it's hard to set value for input type color |
Just needed a little cleanup and this worked for me. |
Browser extensions expect to be able to influence form values. |
The reason we need native change events is that we have a modal form with a change listener, if a change event has bubbled up to the form we consider the modal dirty. If the modal is dirty we show a confirm dialog when closing the modal, otherwise not. |
@whosesmile you're a king.. thank you. |
@whosesmile what is _valueTracker? cant find it in any docs |
With text input (text area included) the event "input" works, but not "change". With select tags the event "change" works but not "input". Thanks again. |
@AndyNovo thanks! FINALLY! after hours and hours trying to make React keep my input value changes your code did it! |
|
It seems like this workaround broke in 16.13 for us. Did anyone else run into issues with upgrading? |
@gaearon I have the most basic possible use case for this, I'm making a typical input with clear button component, to be a drop-in replacement for an Calling |
Here's a sandbox demonstrating the |
I ended up using the Simulate instance in if (input._valueTracker) {
Simulate.change(input);
} else {
input.dispatchEvent(new Event('change', { bubbles: true }));
} |
My simple scenario:I must find a fix to trigger https://github.com/yairEO/tagify When using the component through the React wrapper, a developer would write code like this:
Then then wrapper is creating an The
|
You have no idea how long I have been searching for this. THANK YOU. |
I've found the
|
This comment has been minimized.
This comment has been minimized.
Thx all! This is what I ended up using in typescript: const forceReactInputOnChange = (input: HTMLInputElement) => {
// @ts-expect-error NOTE: clear the interal value to force an actual change
input._valueTracker?.setValue("");
input.dispatchEvent(new Event("input", { bubbles: true }));
}; |
Hello, anyone knows why all salvations above are not working with Redux state? https://codepen.io/eqwfsdgfdg/pen/MWOmaQJ |
Generic method for changing input in React (Shamelessly taken from here)
Answers in this thread weren't working for me when editing a TextArea, but this does! Also to @gaearon some of us are building browser extensions, which can come with a plethora of constraints |
Just a note to everyone else, I was using a lib, thats it. Because too many cases cant be covered by all snippets above |
Added this definition to avoid using @ts-expect-error
|
h2. Описание Немного отрефакторил. - `Chip` перенеёс в `ChipInputBase`. - Хук `useChipsInput` в `ChipsInput`. - Хук `useChipsSelect` в `ChipsSelect`. - Вынес шаренные типы в файлы `types`. - Вынес шаренные константы типы в файлы `constants`. - Теперь `ChipsInput` и `ChipsSelect` могут быть неконтролируемыми. - В `ChipsSelect` параметры `creatable` и `emptyText` унёс в хук `useChipsSelect`. <details><summary>Под катом изменения по `ChipsSelect`</summary> <p> h2. ChipsSelect ```diff <ChipsSelect - value={[]} + defaultValue={[]} - value={[]} + value={[]} + onChange={[]} - inputValue="" + defaultInputValue="" - popupDirection="bottom" + placement="bottom" - options={[]} + presets={[]} - showSelected={true} + selectedBehavior="highlight" - showSelected={false} + selectedBehavior="hide" - creatableText="Lorem Ipsum" + creatable="Lorem Ipsum" /> ``` - Компонент теперь может быть контролируемым и неконтролируемым. - `creatable` – может быть всё ещё `boolean`, при этом теперь можно передать и текст, чтобы переопределить текст по умолчанию. - `getOptionValue`, `getOptionLabel`, `getNewOptionData` – все аргументы функции теперь обязательны. - `renderChip` – вторым аргументов приходит `option`. </p> </details> h3. Доступность Обговорили с нашим специалистом по цифровой доступности следующие правила и поведение: - Контейнеры `ChipsInput` и `ChipsSelect` имеют роль `listbox` и `aria-orientation="horizontal"`, а выбранные опции имеют роль `option`, а также атрибуты: - `aria-selected` со значением true; - `aria-posinset` со значением текущего места в списке; - `aria-setsize` со значением общего количества выбранных опций. - В `ChipsSelect`: - поле ввода имеет роль `combobox` и ссылаться на всплывающее окно через `aria-haspopup`, `aria-controls` и `aria-expanded` - Всплывающее окно имеет роль `listbox`, а его опции роль `option`. - Навигация с клавиатуры: - **Tab** фокусирует на поле ввода. - Если поле пустое, то стрелка **ArrowUp**/**ArrowLeft** выставит фокус на опцию с конца. Далее с помощью стрелок **ArrowUp**/**ArrowLeft** и **ArrowDown**/**ArrowRIght** можно перемещаться между опциями. - Нажатие **Tab** переведёт обратно на поле ввода, а **Shift + Tab** на другой фокусируемый элемент до `ChipsInput`/`ChipsSelect`. Другими словами опции не реагируют на **Tab**, на них можно сфокусироваться только кнопками-стрелками. h2. Изменения - `hooks/useEnsureControl` удалил `undefined` из возвращаемого типа. `value` не может быть `undefined`, потому что `useCustomEnsuredControl` возвращает `defaultValue`. - `lib/select` немного отрефакторил, удалил легаси код связанный с флагом `u` у **RegExp**. - `lib/appearance` - Добавил `Keys.BACKSPACE`. - Новая ф-я `getHorizontalFocusGoTo`. - `lib/react/simulateReactInput` – так как теперь для `input` используется `useEnsureControl`, то нужна возможность очистить поле через событие. Т.к. у в DOM API нет события `clear`, вызываем событие через API в React (см. facebook/react#11488 (comment)). - `components/Popper` – поправил проверку для вызова `onPlacementChange` из #6185. - в `ChipsSelect` задаём значение по умолчанию для `placement`. - в `CustomSelectDropdown` задаём значение по умолчанию для `placement`.
I'm trying to trigger the input/change event on a React form outside react using pure JS or jQuery. With react-dom 15.6.0 you were able to use simulated flag on the event object for the event to pass through
Note I cannot use React even though the form trying to trigger on is based on React, have to pure JS or jQuery to trigger the input value change event.
So the original suggestion from this comment used to work: cypress-io/cypress#536 (comment)
But after React 16 release this is not triggering the input and change event as expected.
What are the internal changes as to how it handles changes to input data in React 16?
Believe there is a point here which would give the hint: reactjs.org/blog/2017/09/26/react-v16.0.html#breaking-changes Any idea what it could be?
The text was updated successfully, but these errors were encountered: