Skip to content
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

onBlur event triggers when trying to set focus on input[type"number"] on conditional rendering in Firefox #11062

Closed
mdekalka opened this issue Oct 3, 2017 · 6 comments

Comments

@mdekalka
Copy link

mdekalka commented Oct 3, 2017

Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When trying to render input[type="number"] with active focus on click

state = {
    isShown: true,
    value: 0
}
componentDidUpdate() {
    this.input && this.input.focus();
}
//...
render() {
    return (
      <td className="editable">
        {!this.state.isShown ? (
          <input
            type="number"
            ref={(input) => { this.input = input }}
            value={this.state.value}
            onChange={e =>
              this.setState({
                value: e.target.value
              })}
            onBlur={() => {this.setState({ isShown: true })}}
          />
        ) : (
            <span onClick={() => {this.setState({ isShown: false })}}>Click me</span>
          )}
      </td>
    )
}

onBlur event triggers in Firefox, before even focus is set.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem:
Click on name column to render input[type="number"] with active focus(works in Chrome, IE11, Edge, does not in Firefox). Please see live example:
https://codepen.io/piupiupiu/pen/KXXQdb?editors=0010

What is the expected behavior?
After clicking on name column input should appears with active focus

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React v15.4.2
Firefox v56.0 Win10

I'm not sure if this React issue actually, because I noticed that if you will change input type to text if will works perfectly in Firefox(so the problem related only to input[type="number"])

@mdekalka mdekalka changed the title onBlue event triggers when trying to set focus on input[type"number"] on conditional rendering in Firefox onBlur event triggers when trying to set focus on input[type"number"] on conditional rendering in Firefox Oct 3, 2017
@gwing33
Copy link

gwing33 commented Dec 8, 2017

I have run into this as well. I don't believe this is related to react. I found several bugs on bugzilla (not sure if they maintain it?), but they are all like 6 months to 4 years old.

Currently, adding the onFocus handler fixes this issue, though it seems like extra overhead.

@nhunzaker
Copy link
Contributor

It is curious to me that adding onFocus would fix the issue. Is it a no-operation handler?

Only reason I ask is that focus and blur get attached at the same time:
https://github.com/facebook/react/blob/master/packages/react-dom/src/events/ReactBrowserEventEmitter.js#L128-L135

I think it's important to know what is different when the onFocus handler is applied.

salchichongallo added a commit to salchichongallo/carnicos that referenced this issue Jun 3, 2018
Solución temporal a bug con evento onBlur en elementos tipo number en
Firefox.

Los elementos input tipo number que tienen un handler para el evento
onBlur y además, tienen el atributo autofocus produce que ejecute el
handler para onBlur de forma automática. Cambiado el tipo del input
por el momento.

Ver: facebook/react#11062
@mathieuprog
Copy link

I have the same problem. Adding onFocus didn't help though for my case. Simply changed number to text for the moment...

@mummybot
Copy link

mummybot commented Jul 6, 2018

I solved this for our project in a very hacky way: attaching the onBlur event listener via the input ref.

Remove it before changing the input type to number, then using setTimeout to jump out of the js processing thread and reattach the event listener.

render() {
        const step = this.props.step || this.getStep();
        let hasFocusProps;
        !!this.domNode && this.domNode.removeEventListener('blur', this.onBlur);
        if (this.state.hasFocus) {
            hasFocusProps = {
                step,
                type: 'number',
                min: step,
            };
            setTimeout(
                () => {
                    this.domNode.addEventListener('blur', this.onBlur, false);
                },
                100,
            );
        } else {
            hasFocusProps = {
                type: 'text',
            };
        }
        return (
                <input
                    {...hasFocusProps}
                    ref={domNode => this.domNode = domNode}
                    value={this.getCurrentValue()}
                    onChange={this.setValue}
                    onFocus={this.setHasFocus}
                />
        );
    },
    componentWillUnmount() {
        !!this.domNode && this.domNode.removeEventListener('blur', this.onBlur);
    }

@gaearon
Copy link
Collaborator

gaearon commented Aug 17, 2018

It doesn't seem like there's any strong evidence this is a React issue, rather than a Firefox bug.

If this is something that only happens with React (but doesn't happen with vanilla DOM) please file another issue with a vanilla DOM example demonstrating the issue is specific to React.

Thanks!

@gaearon gaearon closed this as completed Aug 17, 2018
@riquito
Copy link

riquito commented Sep 30, 2018

Here's the link to track the bug in Firefox https://bugzilla.mozilla.org/show_bug.cgi?id=1057858

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants