【问题标题】:antd input search component not getting blurred on blur()antd 输入搜索组件在 blur() 上没有变得模糊
【发布时间】:2019-05-11 02:25:17
【问题描述】:

我正在使用 antd 库的搜索输入组件。它带有一个 onSearch 回调函数,我们可以在其中捕获事件目标,并可以使用全局输入函数,如 blur()、focus()。但这似乎不起作用。如果我附加任何其他事件处理程序,如 onClick 或 onChange 它工作。是错误还是我做错了什么。

import React from 'react';
import ReactDOM from 'react-dom';
import 'antd/dist/antd.css';
import './index.css';
import { Input } from 'antd';

const Search = Input.Search;

ReactDOM.render(
  <div>
    <Search
      placeholder="input search text"
      onSearch={(value,e) => {
        console.log(e.target);
        e.target.blur();
        console.log(value)
        }}
      enterButton
      onChange={e => {
        console.log(e.target)
        e.target.blur()}}
    />
  </div>,
  document.getElementById('container')
);

【问题讨论】:

    标签: javascript reactjs input antd


    【解决方案1】:

    这是因为antd 在处理程序之后重新聚焦它。您可以在blur 处理程序上使用setTimeout 使其正确模糊。

    但使用event.target 只能在输入键上使用,您必须在输入上使用ref 元素才能使其在按钮上起作用。

    <Search
      placeholder="input search text"
      onSearch={(value, e) => {
        const input = this.input.current;
        setTimeout(() => input.blur(), 0);
      }}
      enterButton
      ref={this.input}
    />
    

    查看示例here

    【讨论】:

    • 您的意思是说,在事件处理程序被调用后,它会重新调整自己的焦点。为什么要这样做?您能否再解释一下这种奇怪的行为......
    • @AirasYaqub 我通过在输入元素上记录模糊和焦点事件知道这一点,我不确定他们为什么这样做,将不得不通过他们的源代码来弄清楚跨度>
    猜你喜欢
    • 1970-01-01
    • 2020-10-18
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    相关资源
    最近更新 更多