2016-07-16 02:06:10 +00:00
|
|
|
import React, {Component, PropTypes} from 'react';
|
2016-07-20 19:09:21 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
2016-07-16 02:06:10 +00:00
|
|
|
import classnames from 'classnames';
|
2016-03-21 05:47:49 +00:00
|
|
|
|
2016-07-16 02:06:10 +00:00
|
|
|
import Mousetrap from '../../lib/mousetrap';
|
2016-07-06 22:11:37 +00:00
|
|
|
|
2016-03-21 05:47:49 +00:00
|
|
|
class Dropdown extends Component {
|
2016-07-06 22:11:37 +00:00
|
|
|
constructor(props) {
|
2016-06-20 06:05:40 +00:00
|
|
|
super(props);
|
2016-03-22 05:01:58 +00:00
|
|
|
this.state = {
|
2016-07-20 19:09:21 +00:00
|
|
|
open: false,
|
|
|
|
dropUp: false
|
2016-03-22 05:01:58 +00:00
|
|
|
};
|
2016-03-21 05:47:49 +00:00
|
|
|
}
|
2016-03-21 06:36:39 +00:00
|
|
|
|
2016-07-06 22:11:37 +00:00
|
|
|
_handleClick(e) {
|
2016-07-20 19:09:21 +00:00
|
|
|
// e.preventDefault();
|
2016-07-06 20:18:26 +00:00
|
|
|
|
2016-07-06 22:11:37 +00:00
|
|
|
this.toggle();
|
2016-03-21 05:47:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 22:11:37 +00:00
|
|
|
hide() {
|
2016-07-06 20:18:26 +00:00
|
|
|
this.setState({open: false});
|
|
|
|
}
|
2016-07-06 22:11:37 +00:00
|
|
|
|
|
|
|
show() {
|
|
|
|
Mousetrap.bind('esc', () => {
|
|
|
|
this.hide();
|
|
|
|
});
|
2016-07-20 19:09:21 +00:00
|
|
|
|
|
|
|
const bodyHeight = document.body.getBoundingClientRect().height;
|
|
|
|
const dropdownTop = ReactDOM.findDOMNode(this).getBoundingClientRect().top;
|
|
|
|
const dropUp = dropdownTop > bodyHeight * 0.75;
|
|
|
|
|
|
|
|
this.setState({open: true, dropUp});
|
2016-07-06 22:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
|
|
|
if (this.state.open) {
|
|
|
|
this.hide();
|
|
|
|
} else {
|
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
}
|
2016-07-06 20:18:26 +00:00
|
|
|
|
2016-07-06 22:11:37 +00:00
|
|
|
render() {
|
2016-07-20 19:09:21 +00:00
|
|
|
const {right, className} = this.props;
|
|
|
|
const {dropUp, open} = this.state;
|
|
|
|
|
|
|
|
const classes = classnames(
|
2016-05-01 19:56:30 +00:00
|
|
|
'dropdown',
|
2016-07-20 19:09:21 +00:00
|
|
|
className,
|
|
|
|
{'dropdown--open': open},
|
|
|
|
{'dropdown--up': dropUp},
|
|
|
|
{'dropdown--right': right}
|
2016-05-01 19:56:30 +00:00
|
|
|
);
|
2016-03-22 05:01:58 +00:00
|
|
|
|
2016-03-21 05:47:49 +00:00
|
|
|
return (
|
2016-07-06 22:11:37 +00:00
|
|
|
<div
|
2016-07-20 19:09:21 +00:00
|
|
|
className={classes}
|
2016-07-06 20:18:26 +00:00
|
|
|
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>
|
2016-07-06 22:11:37 +00:00
|
|
|
</div>
|
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;
|