insomnia/app/components/Dropdown.js

45 lines
1.0 KiB
JavaScript
Raw Normal View History

2016-03-21 05:47:49 +00:00
import React, {Component, PropTypes} from 'react';
class Dropdown extends Component {
constructor () {
super();
this.state = {open: false};
}
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-03-21 05:47:49 +00:00
document.addEventListener('click', (e) => {
if (!this.refs.container.contains(e.target)) {
e.preventDefault();
this.setState({open: false});
}
2016-03-21 06:36:39 +00:00
});
2016-03-21 05:47:49 +00:00
}
handleClick (e) {
e.preventDefault();
this.setState({open: !this.state.open});
}
render () {
const {initialValue, value} = this.props;
return (
<div ref="container"
2016-03-21 06:36:39 +00:00
className={'dropdown ' +
(this.state.open ? 'dropdown--open ' : ' ') +
(this.props.right ? 'dropdown--right ' : ' ')
}
2016-03-21 05:47:49 +00:00
onClick={this.handleClick.bind(this)}>
{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;