import _ from 'lodash'; import classnames from 'classnames'; import CSSTransitionGroup from 'react-addons-css-transition-group'; import React from 'react'; const METHODS_TO_BIND = [ 'getDropdownButton', 'getDropdownMenu', 'getDropdownMenuItems', 'handleDropdownBlur', 'handleDropdownClick', 'handleDropdownFocus', 'handleItemSelect', 'handleKeyPress' ]; export default class Dropdown extends React.Component { constructor() { super(); this.state = { isExpanded: false }; METHODS_TO_BIND.forEach((method) => { this[method] = this[method].bind(this); }); this.handleKeyPress = _.throttle(this.handleKeyPress, 1000); } componentDidMount() { window.addEventListener('keydown', this.handleKeyPress); } componentWillUnmount() { window.removeEventListener('keydown', this.handleKeyPress); } closeDropdown() { this.refs.dropdown.blur(); } handleDropdownBlur() { this.setState({ isExpanded: false }); } handleDropdownClick() { if (this.state.isExpanded) { this.closeDropdown(); } else { this.refs.dropdown.focus(); } } handleDropdownFocus(event) { this.setState({ isExpanded: true }); } handleItemSelect(item) { this.closeDropdown(); this.props.handleItemSelect(item); } handleKeyPress(event) { if (this.state.isExpanded && event.keyCode === 27) { this.closeDropdown(); } } getDropdownButton() { return (