2016-05-01 19:56:30 +00:00
|
|
|
import React, {Component, PropTypes} from 'react'
|
|
|
|
import classnames from 'classnames'
|
2016-03-21 05:47:49 +00:00
|
|
|
|
|
|
|
class Dropdown extends Component {
|
|
|
|
constructor () {
|
|
|
|
super();
|
2016-03-22 05:01:58 +00:00
|
|
|
this.state = {
|
|
|
|
open: false
|
|
|
|
};
|
2016-03-21 05:47:49 +00:00
|
|
|
}
|
2016-03-21 06:36:39 +00:00
|
|
|
|
2016-03-21 05:47:49 +00:00
|
|
|
componentDidMount () {
|
|
|
|
// Capture clicks outside the component and close the dropdown
|
2016-03-21 06:36:39 +00:00
|
|
|
// TODO: Remove this listener when component unmounts
|
2016-04-07 01:11:16 +00:00
|
|
|
document.addEventListener('click', this._clickCallback.bind(this));
|
2016-03-21 05:47:49 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:26:27 +00:00
|
|
|
componentWillUnmount () {
|
2016-04-07 01:11:16 +00:00
|
|
|
document.removeEventListener('click', this._clickCallback);
|
2016-03-23 05:26:27 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 01:11:16 +00:00
|
|
|
_clickCallback (e) {
|
|
|
|
const container = this.refs.container;
|
|
|
|
|
|
|
|
if (container && !container.contains(e.target)) {
|
2016-03-22 05:01:58 +00:00
|
|
|
e.preventDefault();
|
|
|
|
this.setState({open: false});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleClick (e) {
|
2016-03-21 05:47:49 +00:00
|
|
|
e.preventDefault();
|
|
|
|
this.setState({open: !this.state.open});
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2016-05-01 19:56:30 +00:00
|
|
|
const className = classnames(
|
|
|
|
'dropdown',
|
|
|
|
this.props.className,
|
|
|
|
{'dropdown--open': this.state.open},
|
|
|
|
{'dropdown--right': this.props.right}
|
|
|
|
);
|
2016-03-22 05:01:58 +00:00
|
|
|
|
2016-03-21 05:47:49 +00:00
|
|
|
return (
|
2016-05-01 19:56:30 +00:00
|
|
|
<div ref="container" className={className} onClick={this._handleClick.bind(this)}>
|
2016-03-21 05:47:49 +00:00
|
|
|
{this.props.children}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 06:36:39 +00:00
|
|
|
Dropdown.propTypes = {
|
|
|
|
right: PropTypes.bool
|
|
|
|
};
|
2016-03-21 05:47:49 +00:00
|
|
|
|
|
|
|
export default Dropdown;
|