1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-08 18:30:53 +00:00
Files
seahub/frontend/src/components/click-outside.js

44 lines
883 B
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
class ClickOutside extends React.Component {
isClickedInside = false;
componentDidMount() {
document.addEventListener('mousedown', this.handleDocumentClick);
}
componentWillUnmount() {
document.removeEventListener('mousedown', this.handleDocumentClick);
}
handleDocumentClick = (e) => {
if (this.isClickedInside) {
this.isClickedInside = false;
return;
}
this.props.onClickOutside(e);
};
handleMouseDown = () => {
this.isClickedInside = true;
};
render() {
return React.cloneElement(
React.Children.only(this.props.children), {
onMouseDownCapture: this.handleMouseDown
}
);
}
}
ClickOutside.propTypes = {
children: PropTypes.element.isRequired,
onClickOutside: PropTypes.func.isRequired,
};
export default ClickOutside;