react-number-format: The focus timeout introduced in v5 makes the input difficult to test
Describe the issue and the actual behavior
The timeout introduced in v5 to manage the caret position makes some of my (Cypress based) tests fail. In certain conditions, the timeout triggers after Cypress inputs value in the field, messing with the caret position. There is currently no reliable way to detect when the caret position is stable, so I used an arbitrary delay. It works most time but makes for flaky tests.
Describe the expected behavior
Ideally, we would have a way to know that the caret position is stable. A simple data attribute would work for me. I could then wait for the attribute to be set (or removed). Another option could be a JS callback triggered once the caret position is stable one can hook into and execute their behaviour.
Provide a CodeSandbox link illustrating the issue
This is an approximation since it is impossible to simulate a keyboard interaction in a non-privileged context, but it shows the issue. See the difference between the native input and NumericFormat.
https://stackblitz.com/edit/react-ts-y69m7x?file=App.tsx
Please check the browsers where the issue is seen
- Chrome
- Chrome (Android)
- Safari (OSX)
- Safari (iOS)
- Firefox
- Firefox (Android)
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 16 (10 by maintainers)
Hey @jraoult, was occupied with other things since last three weeks, so couldn’t look into this. I understood, what you are saying, and I think I have a proper fix for this without a need of finding when the caret is stable.
Basically will check if the caret position is reset by the browser then only set again inside the time out.
I will fix this. Thanks for your patience.
But even if it the autoselect algo is in parent, you can still have a wrapper component on rnf and do this double effect thing in the wrapper component. Basically in parent instead of doing autoSelect there if the input you are trying to select is
rnf input(you can have some class to identify this), set a prop on parent, and forward this to the wrapper and autoselect here.I know this is patchy solution, but should work, given that correct solution within rnf would take time to come.
Hey @jraoult, so there are two problems.
Here’s an example for the same. https://stackblitz.com/edit/react-ts-rq99uj?file=App.tsx
The proper fix for this would take longer, as it may require some breaking changes.
Humm, I am trying to avoid adding an additional prop, but can’t think of a better solution. I will give it another thought, otherwise, I will add a prop called onCaretPositionChange.
Haven’t got a chance to look into this. Will look into this over this weekend.