NK
Size: a a a
NK
DZ
DZ
NK
DZ
DZ
DZ
NK
DZ
// hook
function useOutsideClick(event, cb) {
const ref = useRef(null);
useEffect(() => {
function handleClickOutside(e) {
if ((!ref.current || !ref.current.contains(e.target)) && cb) {
cb(e);
}
}
document.addEventListener(event, handleClickOutside, true);
return () => document.removeEventListener(event, handleClickOutside, true);
}, []);
return ref;
}
// hoc
export const enhanceWithClickOutside = params => Component => {
const componentName = Component.displayName || Component.name;
class EnhancedComponent extends React.Component {
__domNode;
__wrappedInstance;
constructor(props) {
super(props);
this.handleClickOutside = this.handleClickOutside.bind(this);
}
componentDidMount() {}
componentWillUnmount() {
document.removeEventListener(params.event, this.handleClickOutside, true);
}
handleClickOutside(e) {
const domNode = this.__domNode;
if (
(!domNode || !domNode.contains(e.target)) &&
this.__wrappedInstance &&
typeof this.__wrappedInstance.handleClickOutside === "function"
) {
this.__wrappedInstance.handleClickOutside(e);
}
}
render() {
const { wrappedRef, ...rest } = this.props;
return (
<Component
{...rest}
ref={c => {
this.__wrappedInstance = c;
this.__domNode = ReactDOM.findDOMNode(c);
wrappedRef && wrappedRef(c);
}}
/>
);
}
}
EnhancedComponent.displayName = `clickOutside(${componentName})`;
return hoistNonReactStatic(EnhancedComponent, Component);
};
NK
DZ
NK
NK
NK
Т
NK
Т
l
l
DZ