2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2016-07-20 19:09:21 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2016-07-16 02:06:10 +00:00
|
|
|
import classnames from 'classnames';
|
2017-03-08 05:52:17 +00:00
|
|
|
import DropdownButton from './dropdown-button';
|
|
|
|
import DropdownItem from './dropdown-item';
|
|
|
|
import DropdownDivider from './dropdown-divider';
|
2016-03-21 05:47:49 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class Dropdown extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
open: false,
|
|
|
|
dropUp: false,
|
2017-03-03 20:09:08 +00:00
|
|
|
focused: false
|
2017-03-03 01:44:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_setRef (n) {
|
|
|
|
this._node = n;
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleKeyDown (e) {
|
2016-11-27 21:42:38 +00:00
|
|
|
// Catch all key presses if we're open
|
|
|
|
if (this.state.open) {
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
2016-03-21 06:36:39 +00:00
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
// Pressed escape?
|
|
|
|
if (this.state.open && e.keyCode === 27) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.hide();
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-03-21 05:47:49 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_checkSize () {
|
2016-11-27 21:42:38 +00:00
|
|
|
if (!this.state.open) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-14 23:25:19 +00:00
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
// Make the dropdown scroll if it drops off screen.
|
2017-03-03 01:44:07 +00:00
|
|
|
const rect = ReactDOM.findDOMNode(this._dropdownList).getBoundingClientRect();
|
2016-11-27 21:42:38 +00:00
|
|
|
const maxHeight = document.body.clientHeight - rect.top - 10;
|
|
|
|
this._dropdownList.style.maxHeight = `${maxHeight}px`;
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-09-14 23:25:19 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleClick () {
|
2016-11-27 21:42:38 +00:00
|
|
|
this.toggle();
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleMouseDown (e) {
|
2016-11-27 21:42:38 +00:00
|
|
|
// Intercept mouse down so that clicks don't trigger things like
|
|
|
|
// drag and drop.
|
|
|
|
e.preventDefault();
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_addDropdownListRef (n) {
|
|
|
|
this._dropdownList = n;
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2017-03-03 01:44:07 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
return newChildren;
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-09-14 23:25:19 +00:00
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
componentDidUpdate () {
|
|
|
|
this._checkSize();
|
2016-09-14 23:25:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
componentDidMount () {
|
|
|
|
document.body.addEventListener('keydown', this._handleKeyDown);
|
|
|
|
window.addEventListener('resize', this._checkSize);
|
2016-09-14 23:25:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
componentWillUnmount () {
|
|
|
|
document.body.removeEventListener('keydown', this._handleKeyDown);
|
|
|
|
window.removeEventListener('resize', this._checkSize);
|
2016-11-25 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 16:14:27 +00:00
|
|
|
hide () {
|
2016-07-06 20:18:26 +00:00
|
|
|
this.setState({open: false});
|
2016-12-23 19:31:38 +00:00
|
|
|
this.props.onHide && this.props.onHide();
|
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;
|
2017-03-03 01:44:07 +00:00
|
|
|
const dropdownTop = this._node.getBoundingClientRect().top;
|
2016-12-01 01:50:59 +00:00
|
|
|
const dropUp = dropdownTop > bodyHeight - 200;
|
2016-07-20 19:09:21 +00:00
|
|
|
|
|
|
|
this.setState({open: true, dropUp});
|
2016-12-23 19:31:38 +00:00
|
|
|
this.props.onOpen && this.props.onOpen();
|
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-07-22 16:14:27 +00:00
|
|
|
render () {
|
2016-12-21 23:37:48 +00:00
|
|
|
const {right, outline, wide, className, style, children} = 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 = [];
|
2016-12-21 23:37:48 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
const listedChildren = Array.isArray(children) ? children : [children];
|
|
|
|
const allChildren = this._getFlattenedChildren(listedChildren);
|
2016-12-21 23:37:48 +00:00
|
|
|
for (const child of allChildren) {
|
2017-03-03 01:44:07 +00:00
|
|
|
if (child.type.name === DropdownButton.name) {
|
2016-11-11 22:01:21 +00:00
|
|
|
dropdownButtons.push(child);
|
2017-03-03 01:44:07 +00:00
|
|
|
} else if (child.type.name === DropdownItem.name) {
|
2016-11-11 22:01:21 +00:00
|
|
|
dropdownItems.push(child);
|
2017-03-03 01:44:07 +00:00
|
|
|
} else if (child.type.name === DropdownDivider.name) {
|
2016-11-11 22:01:21 +00:00
|
|
|
dropdownItems.push(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 23:37:48 +00:00
|
|
|
let finalChildren = [];
|
2016-11-11 23:06:24 +00:00
|
|
|
if (dropdownButtons.length !== 1) {
|
2017-03-03 01:44:07 +00:00
|
|
|
console.error(
|
|
|
|
`Dropdown needs exactly one DropdownButton! Got ${dropdownButtons.length}`,
|
|
|
|
{allChildren}
|
|
|
|
);
|
2016-11-11 22:01:21 +00:00
|
|
|
} else {
|
2016-12-21 23:37:48 +00:00
|
|
|
finalChildren = [
|
2016-11-25 19:01:41 +00:00
|
|
|
dropdownButtons[0],
|
2017-01-23 22:41:31 +00:00
|
|
|
<ul key="items" className="dropdown__menu" ref={this._addDropdownListRef}>
|
2016-11-27 21:42:38 +00:00
|
|
|
{dropdownItems}
|
|
|
|
</ul>
|
2017-03-03 20:09:08 +00:00
|
|
|
];
|
2016-11-11 22:01:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 05:47:49 +00:00
|
|
|
return (
|
2016-11-29 20:55:31 +00:00
|
|
|
<div style={style}
|
|
|
|
className={classes}
|
2017-03-03 01:44:07 +00:00
|
|
|
ref={this._setRef}
|
2016-11-27 21:42:38 +00:00
|
|
|
onClick={this._handleClick}
|
|
|
|
onMouseDown={this._handleMouseDown}>
|
2016-12-21 23:37:48 +00:00
|
|
|
{finalChildren}
|
2016-06-20 06:05:40 +00:00
|
|
|
<div className="dropdown__backdrop"></div>
|
2016-07-06 22:11:37 +00:00
|
|
|
</div>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-03-21 05:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 06:36:39 +00:00
|
|
|
Dropdown.propTypes = {
|
2017-03-08 05:52:17 +00:00
|
|
|
// Required
|
|
|
|
children: PropTypes.node.isRequired,
|
|
|
|
|
|
|
|
// Optional
|
2016-08-15 17:04:36 +00:00
|
|
|
right: PropTypes.bool,
|
2016-11-11 22:01:21 +00:00
|
|
|
outline: PropTypes.bool,
|
2016-12-23 19:31:38 +00:00
|
|
|
wide: PropTypes.bool,
|
|
|
|
onOpen: PropTypes.func,
|
2017-03-08 05:52:17 +00:00
|
|
|
onHide: PropTypes.func,
|
|
|
|
className: PropTypes.string,
|
|
|
|
style: PropTypes.string
|
2016-03-21 06:36:39 +00:00
|
|
|
};
|
2016-03-21 05:47:49 +00:00
|
|
|
|
|
|
|
export default Dropdown;
|