反应和模糊事件

Pet*_*rot 24 html javascript html5 dom-events reactjs

我有一个关于React和事件处理的简单问题.我的组件看起来像这样(基本上是一个表):

const MyList = ({ items, onBlur }) =>
<table onBlur={onBlur}}>
    <thead>
    <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Publisher</th>
        <th>Year</th>
        <th>Author</th>
        <th>System</th>
        <th/>
    </tr>
    </thead>
    <tbody>
    {items.map(item => <MyListRow key={item.Id} item={item}/>)}
    </tbody>
</table>;
Run Code Online (Sandbox Code Playgroud)

我希望只有当焦点超出表格时才会blur触发事件.相反,当事件失去焦点时,事件将触发表的每个子元素.

根据文档,React让焦点事件冒出来.

问题是:onBlur只有当焦点离开桌子时,我怎样才能触发我的方法?IOW:如何过滤掉并丢弃冒泡的不需要的事件,以便仅显示表示桌子失去焦点的事件?

The*_*nke 28

问题是表实际上没有焦点的概念,因为它本身不是输入.

当onBlur触发包含的输入时,我们将检查应该设置为具有RECEIVED焦点(或)的元素relatedTargetonBlur事件null.然后,我们使用一个函数,该函数parentNode将从新聚焦的元素向上遍历s,并确保我们的事件currentTarget(表)不是新聚焦元素的祖先.如果条件通过,则假定该表不再具有任何焦点.

const focusInCurrentTarget = ({ relatedTarget, currentTarget }) => {
  if (relatedTarget === null) return false;
  
  var node = relatedTarget.parentNode;
        
  while (node !== null) {
    if (node === currentTarget) return true;
    node = node.parentNode;
  }

  return false;
}

const onBlur = (e) => {
  if (!focusInCurrentTarget(e)) {
    console.log('table blurred');
  }
}

const MyList = ({ items, onBlur }) => (
  <table onBlur={onBlur}>
    <thead>
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Publisher</th>
        <th>Year</th>
        <th>Author</th>
        <th>System</th>
        <th/>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>
          <input type="text" />
        </td>
        <td>
          <input type="text" />
        </td>
        <td>
          <input type="text" />
        </td>
        <td>
          <input type="text" />
        </td>
        <td>
          <input type="text" />
        </td>
      </tr>
    </tbody>
  </table>
);
    
ReactDOM.render(
  <MyList onBlur={onBlur} />,
  document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)
table {
  padding: 10px;
  border: 1px solid red;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
<br />
<input type="text" />
Run Code Online (Sandbox Code Playgroud)

参考文献:

更新:

删除了ReactDOM.findDOMNode的使用


Dav*_*lli 5

自Internet Explorer 5 开始并受其支持,由于没有自定义功能和Internet Explorer兼容,因此该方法有效:node.containsdocument.activeElement

const onBlur = (e) => { if ( !e.currentTarget.contains( document.activeElement ) ) { console.log('table blurred'); } }

  • Node.contains()方法返回一个布尔值,该值指示节点是否为给定节点的后代。
  • Document.activeElement返回当前关注的元素,即,如果用户键入任何元素,它将获得击键事件的元素。

  • 在我的使用中,总是以`&lt;body /&gt;`作为活动元素来调用onBlur,例如[this question](/sf/ask/790988271/) (2认同)

Chr*_*aul 5

只是为了希望综合 David Riccitelli 的有用指针和 wintondeshong 后来的见解,这对我有用:

class ActionRow extends Component {

  onBlur = (e) => {
    if ( !e.currentTarget.contains( e.relatedTarget ) ) {
      console.log('blur event');
    }
  }

  render() {
    return (
      <div onBlur={this.onBlur}>
        ..
      </div>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

I was trying to trigger a save action based on when focus left a div full of form fields.