Skip to content

Instantly share code, notes, and snippets.

@chengjianhua
Created September 18, 2016 08:22
Show Gist options
  • Save chengjianhua/bd24fdaa7b46d71c80199e0b70038722 to your computer and use it in GitHub Desktop.
Save chengjianhua/bd24fdaa7b46d71c80199e0b70038722 to your computer and use it in GitHub Desktop.
React check clicked target is whether contained in a another node.
handleOutSideClick(event) {
const {onRowUnSelected} = this.props;
const isDescendantOfRoot = ReactDOM.findDOMNode(this.tbody).contains(event.target);
if (!isDescendantOfRoot) {
event.stopPropagation();
this.setState({
selectedRowIndex: -1
}, () => {
onRowUnSelected && onRowUnSelected();
});
}
}
componentDidMount() {
if (document.addEventListener) {
document.addEventListener('click', this.handleOutSideClick, true);
} else {
document.attachEvent('onclick', this.handleOutSideClick);
}
}
componentWillUnmount() {
if (document.addEventListener) {
document.addEventListener('click', this.handleOutSideClick, true);
} else {
document.attachEvent('onclick', this.handleOutSideClick);
}
}
@chengjianhua
Copy link
Author

判断点击的元素节点是否在指定的结点的内部.

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