import React, { ReactNode } from 'react' type DropdownProps = { title: string up?: boolean disabled?: boolean className?: string children?: ReactNode } type DropdownState = { active: boolean } class Dropdown extends React.Component { button?: HTMLButtonElement static defaultProps = { up: false, disabled: false, className: '', children: null, } constructor(props: any) { super(props) this.state = { active: false } this.handleBodyClick = this.handleBodyClick.bind(this) } componentDidMount() { window.document.body.addEventListener('click', this.handleBodyClick) } componentWillUnmount() { window.document.body.removeEventListener('click', this.handleBodyClick) } handleBodyClick(e: MouseEvent) { if (e.target !== this.button && !this.button?.contains(e.target as Node)) { this.setState({ active: false }) } } render() { const { active } = this.state const isActive = active ? 'is-active' : '' const { title, up, disabled, className, children } = this.props return (
{children}
) } } export default Dropdown