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-11-11 22:01:21 +00:00
|
|
|
import DropdownButton from './DropdownButton';
|
|
|
|
import DropdownItem from './DropdownItem';
|
|
|
|
import DropdownDivider from './DropdownDivider';
|
2016-03-21 05:47:49 +00:00
|
|
|
|
|
|
|
class Dropdown extends Component {
|
2016-11-26 08:29:16 +00:00
|
|
|
state = {open: false, dropUp: false};
|
2016-03-21 06:36:39 +00:00
|
|
|
|
2016-07-22 16:14:27 +00:00
|
|
|
_handleClick () {
|
2016-07-06 22:11:37 +00:00
|
|
|
this.toggle();
|
2016-03-21 05:47:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 23:25:19 +00:00
|
|
|
_addKeyListener () {
|
|
|
|
this._bodyKeydownHandler = e => {
|
|
|
|
if (!this.state.open) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Catch all key presses if we're open
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
// Pressed escape?
|
|
|
|
if (e.keyCode === 27) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.body.addEventListener('keydown', this._bodyKeydownHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
_removeKeyListener () {
|
|
|
|
document.body.removeEventListener('keydown', this._bodyKeydownHandler);
|
|
|
|
}
|
|
|
|
|
2016-11-25 19:01:41 +00:00
|
|
|
componentDidUpdate () {
|
|
|
|
// Make the dropdown scroll if it drops off screen.
|
|
|
|
if (this.state.open) {
|
|
|
|
const rect = this._dropdownList.getBoundingClientRect();
|
|
|
|
const maxHeight = document.body.clientHeight - rect.top - 10;
|
|
|
|
this._dropdownList.style.maxHeight = `${maxHeight}px`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 16:14:27 +00:00
|
|
|
hide () {
|
2016-07-06 20:18:26 +00:00
|
|
|
this.setState({open: false});
|
2016-09-14 23:25:19 +00:00
|
|
|
this._removeKeyListener();
|
2016-07-06 20:18:26 +00:00
|
|
|
}
|
2016-07-22 16:14:27 +00:00
|
|
|
|
|
|
|
show () {
|
2016-07-20 19:09:21 +00:00
|
|
|
const bodyHeight = document.body.getBoundingClientRect().height;
|
|
|
|
const dropdownTop = ReactDOM.findDOMNode(this).getBoundingClientRect().top;
|
2016-07-22 16:14:27 +00:00
|
|
|
const dropUp = dropdownTop > bodyHeight * 0.65;
|
2016-07-20 19:09:21 +00:00
|
|
|
|
|
|
|
this.setState({open: true, dropUp});
|
2016-09-14 23:25:19 +00:00
|
|
|
this._addKeyListener();
|
2016-07-06 22:11:37 +00:00
|
|
|
}
|
2016-07-22 16:14:27 +00:00
|
|
|
|
|
|
|
toggle () {
|
2016-07-06 22:11:37 +00:00
|
|
|
if (this.state.open) {
|
|
|
|
this.hide();
|
|
|
|
} else {
|
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
}
|
2016-07-06 20:18:26 +00:00
|
|
|
|
2016-09-10 18:30:50 +00:00
|
|
|
componentWillUnmount () {
|
2016-09-14 23:25:19 +00:00
|
|
|
this._removeKeyListener();
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 22:01:21 +00:00
|
|
|
_getFlattenedChildren (children) {
|
|
|
|
let newChildren = [];
|
|
|
|
|
|
|
|
for (const child of children) {
|
|
|
|
if (!child) {
|
|
|
|
// Ignore null components
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(child)) {
|
|
|
|
newChildren = [...newChildren, ...this._getFlattenedChildren(child)];
|
|
|
|
} else {
|
|
|
|
newChildren.push(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newChildren
|
|
|
|
}
|
|
|
|
|
2016-07-22 16:14:27 +00:00
|
|
|
render () {
|
2016-11-11 22:01:21 +00:00
|
|
|
const {right, className, outline, wide} = this.props;
|
2016-07-20 19:09:21 +00:00
|
|
|
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},
|
2016-11-11 22:01:21 +00:00
|
|
|
{'dropdown--wide': wide},
|
2016-08-15 17:04:36 +00:00
|
|
|
{'dropdown--outlined': outline},
|
2016-07-20 19:09:21 +00:00
|
|
|
{'dropdown--up': dropUp},
|
|
|
|
{'dropdown--right': right}
|
2016-05-01 19:56:30 +00:00
|
|
|
);
|
2016-03-22 05:01:58 +00:00
|
|
|
|
2016-11-11 22:01:21 +00:00
|
|
|
const dropdownButtons = [];
|
|
|
|
const dropdownItems = [];
|
|
|
|
for (const child of this._getFlattenedChildren(this.props.children)) {
|
|
|
|
if (child.type === DropdownButton) {
|
|
|
|
dropdownButtons.push(child);
|
|
|
|
} else if (child.type === DropdownItem) {
|
|
|
|
dropdownItems.push(child);
|
|
|
|
} else if (child.type === DropdownDivider) {
|
|
|
|
dropdownItems.push(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let children = [];
|
2016-11-11 23:06:24 +00:00
|
|
|
if (dropdownButtons.length !== 1) {
|
|
|
|
console.error(`Dropdown needs exactly one DropdownButton! Got ${dropdownButtons.length}`, this.props);
|
2016-11-11 22:01:21 +00:00
|
|
|
} else if (dropdownItems.length === 0) {
|
|
|
|
console.error(`Dropdown needs at least one DropdownItem!`);
|
|
|
|
} else {
|
2016-11-25 19:01:41 +00:00
|
|
|
children = [
|
|
|
|
dropdownButtons[0],
|
|
|
|
<ul key="items" ref={n => this._dropdownList = n}>{dropdownItems}</ul>
|
|
|
|
]
|
2016-11-11 22:01:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 05:47:49 +00:00
|
|
|
return (
|
2016-07-22 16:14:27 +00:00
|
|
|
<div className={classes}
|
2016-09-07 22:21:10 +00:00
|
|
|
onClick={this._handleClick.bind(this)}
|
2016-09-10 18:30:50 +00:00
|
|
|
onMouseDown={e => e.preventDefault()}>
|
2016-11-11 22:01:21 +00:00
|
|
|
{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 = {
|
2016-08-15 17:04:36 +00:00
|
|
|
right: PropTypes.bool,
|
2016-11-11 22:01:21 +00:00
|
|
|
outline: PropTypes.bool,
|
|
|
|
wide: PropTypes.bool
|
2016-03-21 06:36:39 +00:00
|
|
|
};
|
2016-03-21 05:47:49 +00:00
|
|
|
|
|
|
|
export default Dropdown;
|