I am trying to create a text input field with a calculation function. Using a component previously made for Photoshop, I noticed that for some reason it does not trigger the change event.
I need this event separate from the input event because I want the calculation to be performed the moment the user confirms the input or removes focus from the textfield.
Is there any way to force the change event to happen or alternatively achieve this purpose?
Thank you for your response, confirming that it does not work with sp-textfield.
I tried and onKeyUp responded, but onChange and onBlur were unresponsive. Is this your intended result? Or is onChange also working in your environment?
Now Iâm using a similar approach to what you are doing with onKeyUp to get around the limitation. When onKeyDown and the key is Enter, Escape, or Tab, I do what onChange should to do.
sp-textfield is a custom tag and is implemented similar to a web component inside UXP. React has issues with adding event listeners on web components.
From React Documentation:
Events emitted by a Web Component may not properly propagate through a React render tree. You will need to manually attach event handlers to handle these events within your React components.
You can use addEventListener instead of onChange. The below code works.
@sttk3 In the above code, I was able to print âchange event fired when event listener is addedâ log and wasnât able to print âchange firedâ log which is understandable as per React limitations. You can remove the onChange and make your code work with addEventListener. Input event was working either way with or without addEventListener.
In the end I could not get the change event in InDesign, even with addEventListener. It might have something to do with the fact that change events in React are equivalent to input events in Preact. Another reason may be that clicking anywhere on the panel does not take focus away from the text field.
Therefore, I defined the following two processes
when the âEnterâ, âTabâ, or âEscapeâ key is down, execute the calculation that is planned to perform in the change event and remove the focus from the text field
when a user clicks anywhere on the panel, execute the same process.
This seems to be an InDesign specific issue. It is not caused by web components. Even if it is just <input type='text'> and Vanilla JS, it does not trigger the change event.