insomnia/app/components/base/Dropdown.js

54 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-05-01 19:56:30 +00:00
import React, {Component, PropTypes} from 'react'
import {HotKeys} from 'react-hotkeys'
2016-05-01 19:56:30 +00:00
import classnames from 'classnames'
2016-03-21 05:47:49 +00:00
class Dropdown extends Component {
2016-06-20 06:05:40 +00:00
constructor (props) {
super(props);
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-22 05:01:58 +00:00
_handleClick (e) {
2016-03-21 05:47:49 +00:00
e.preventDefault();
if (this.state.open) {
// TODO: Is this the best thing to do here? Maybe we should focus the last thing
document.getElementById('wrapper').focus();
}
2016-03-21 05:47:49 +00:00
this.setState({open: !this.state.open});
}
hide () {
this.setState({open: false});
}
2016-03-21 05:47:49 +00:00
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 (
<HotKeys
handlers={{escape: () => this.hide()}}
className={className}
onClick={this._handleClick.bind(this)}>
2016-03-21 05:47:49 +00:00
{this.props.children}
2016-06-20 06:05:40 +00:00
<div className="dropdown__backdrop"></div>
</HotKeys>
2016-03-21 05:47:49 +00:00
)
}
}
2016-03-21 06:36:39 +00:00
Dropdown.propTypes = {
right: PropTypes.bool
};
2016-03-21 05:47:49 +00:00
export default Dropdown;