import * as React from "react"; import { PlatformProps } from "../../../shared/types"; import withPlatform from "../../platform/withPlatform"; type Props = { onClickOutside: () => void; style?: any; } & PlatformProps; class ClickOutside extends React.Component { wrapperRef: any; constructor(props: Props) { super(props); this.setWrapperRef = this.setWrapperRef.bind(this); this.handleClickOutside = this.handleClickOutside.bind(this); } UNSAFE_componentWillMount() { document.addEventListener("mousedown", this.handleClickOutside); } componentWillUnmount() { document.removeEventListener("mousedown", this.handleClickOutside); } setWrapperRef(node: any) { this.wrapperRef = node; } handleClickOutside(event: any) { if (this.wrapperRef && !this.wrapperRef.contains(event.target)) { this.props.onClickOutside(); } } render() { return (
{this.props.children}
); } } export default withPlatform(ClickOutside);