react-number-format icon indicating copy to clipboard operation
react-number-format copied to clipboard

onBlur event is being called incorrectly when user is typing

Open rohiniseetul opened this issue 5 years ago • 8 comments

Thanks very much for this library.

Problem

The NumberFormat component is calling the onBlur event handler incorrectly. I can also reproduce this in my own project. I can also reproduce this using "@testing-library/react" and "@testing-library/user-event" in this sandbox EDITED (to update all dependencies in the sandbox to the latest versions)

Expected Behaviour

The NumberFormat component should not call onBlur in response to the user typing when they have not tabbed out.

Experienced Behaviour

onBlur is called while typing "123"

It would be much appreciated if you can take a look at the issue.

rohiniseetul avatar Aug 27 '19 12:08 rohiniseetul

Hi there, I am a colleague of @rohiniseetul working with her on this issue. Together we discovered that this is caused by a bug in JSDOM which is used by Jest. JSDOM is incorrectly sending focus and blur events when you call the focus() method on an input element.

In the browser if you call input.focus() and then call input.focus() again you will get at most one focus event and no blur events for input. But in JSDOM there is a bug which will cause you to get two focus and one blur events for input.

We have found a workaround to fix this problem in JSDOM and we are going to share it with the JSDOM team. I will paste a link to the issue here when I have it.

However we have had a good look at the source code of number-format.js and we cannot see any good reason for the .focus to be continually called as clearly the input already has focus or the user could not be typing in it. What is the reason for this?

Kind regards, Jack

jackgeek avatar Aug 29 '19 15:08 jackgeek

@jackgeek extra call to elm.focus() is happening on setCaretPosition function. (This was required for old browsers. Will check if it is needed now)

@rohiniseetul is this only the test issue or happens on the browser as well?

s-yadav avatar Sep 02 '19 18:09 s-yadav

@s-yadav as far as I have tested it, the reported issue is happening in the test and not in the browser.

rohiniseetul avatar Sep 06 '19 07:09 rohiniseetul

Hi all, is this still an open issue or is there a fix in place? Thanks!

lainermeister avatar Oct 09 '20 15:10 lainermeister

I don't think this is picked. Happy to receive a PR for this. This is the place to look for. https://github.com/s-yadav/react-number-format/blob/master/src/utils.js#L112

s-yadav avatar Oct 10 '20 16:10 s-yadav

I don't think this is picked. Happy to receive a PR for this. This is the place to look for. https://github.com/s-yadav/react-number-format/blob/master/src/utils.js#L112

It's been more than 3 years since this comment; and that link doesn't even work anymore 😅

I believe this is still happening with jest (JSDOM). @s-yadav could you please link me to the line you think it's the culprit? I'd like to play with this and see if we can remove it

csantos1113 avatar Nov 17 '23 21:11 csantos1113

This is the function we need to check if we can get newer version of setCaretPosition. https://github.com/s-yadav/react-number-format/blob/master/src/utils.tsx#L216

s-yadav avatar Nov 18 '23 05:11 s-yadav

I think we can just remove the el.focus call from setCaretPosition. I don't see any case where we want to set the caret position, when input is not focused already.

s-yadav avatar Nov 18 '23 05:11 s-yadav