import React from 'react'; import DropDownBase from './DropDownBase.js'; export default class DropDownButton extends React.PureComponent { state = { pressed: false, checked: false } componentDidUpdate(prevProps, prevState) { if (prevProps.hidden && !this.props.hidden && DropDownBase.instances[this.props.dropdownId].isVisible()) { DropDownBase.instances[this.props.dropdownId].showAt(this.refs.btn, this.unpress); } } render() { return {this.props.icon ? : null} {this.state.checked && this.props.checkedText || this.props.text || null} {this.props.dropdownId ? : null} } toggle = () => { if (!this.state.pressed) { DropDownBase.hideAll(); DropDownBase.instances[this.props.dropdownId].showAt(this.refs.btn, this.unpress); } else DropDownBase.instances[this.props.dropdownId].hide(); this.setState({ pressed: !this.state.pressed }); } unpress = () => { this.setState({ pressed: false }); } onClickButton = (ev) => { if (this.props.whole || this.props.checkable && this.state.pressed) this.toggle(); if (this.props.checkable) { this.setState({ checked: !this.state.checked }); if (this.props.onClick) this.props.onClick(this.state.checked); } else if (this.props.onClick) this.props.onClick(); ev.stopPropagation(); } onClickDown = (ev) => { this.toggle(); ev.stopPropagation(); } }