简体   繁体   English

在React中专注于Firefox后,文本输入模糊

[英]Text input blurs after focusing in Firefox in React

I have a text input: 我有文字输入:

{this.state.showInput 
    && (<input
        type="number"
        min="0"
        max="5"
        onKeyPress={this.onPressKey}
        onChange={this.onChangeHandler}
        onBlur={e => this.revertValue(e)}
        defaultValue={this.props.initialMark}
        ref={this.inputRef}
    />)
}

By default showInput is false. 默认情况下, showInput为false。 And with click on a button I do: 通过单击按钮,我可以执行以下操作:

this.setState({ showInput: true }, () => {
    if (this.inputRef.current)
        this.inputRef.current.focus();
});

And with onBlur I revert showInput value to false: 并使用onBlur将showInput值还原为false:

revertValue(e) {
    e.target.value = this.props.initialMark;
    this.setState({ showInput: false });
}

The issue is why just after triggering onFocus I see on blur is triggered, so my input doesn't appear? 问题是,为什么在触发onFocus之后立即触发我看到的模糊效果,所以我的输入没有出现? How can I avoid this behavior? 如何避免这种行为?

This relates to Firefox only. 这仅与Firefox有关。

This issue is related to the strange behavior (bug in other words) of <input type="number"> in firefox itself rather than React . 这个问题与firefox本身而不是React<input type="number">的奇怪行为(换句话说就是错误)有关。 Firefox triggers onBlur immediately after .focus() was called on the newly created element with type="number" . 新创建的 type="number" 元素上调用.focus()之后,Firefox会立即触发onBlur Of course you can replace a type="number" to the type="text" , but this is not a good way to fix the problem. 当然,您可以将type="number"替换为type="number" type="text" ,但这不是解决问题的好方法。

Depending on the situation (or preferences) there are two options. 根据情况(或首选项),有两种选择。

Click here to see demos. 单击此处查看演示。

  1. Asynchronously . 异步地 Just wrap your this.inputRef.current.focus(); 只需包装您的this.inputRef.current.focus(); into setTimeout , like this setTimeout(() => this.inputRef.current.focus(), 100); 进入setTimeout ,就像这样setTimeout(() => this.inputRef.current.focus(), 100); . And 100 ms after the input was created that input get focused. 在创建输入之后100毫秒,该输入将变得集中。
  2. Or synchronously , which I prefer more. 同步 ,我更喜欢。

For this case you need additional property in component state, eg initialized: false . 对于这种情况,您需要组件状态下的其他属性,例如, initialized: false

Next, you need to set initialized to true in onBlur handler (or revertValue in your example) , this code will be working only in firefox, because of strange behavior, about which I wrote above. 接下来,您需要在onBlur处理程序(或示例中的revertValue)中将initialized设置为true ,由于我上面写的奇怪行为,此代码仅在firefox中有效。

if (!this.state.initialized) {
  return this.setState({
    initialized: true,
  });
}

And at the last step you need to "reset" this property for other browser using this code after .focus() . 在最后一步,您需要在.focus()之后使用此代码为其他浏览器“重置”此属性。 Notice that this is an asynchronous call. 请注意,这是一个异步调用。

this.setState(() => ({
  initialized: true,
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM