2016-11-22 21:19:08 +01:00
|
|
|
import React from 'react';
|
|
|
|
import {Icon} from './common.js';
|
2017-04-07 07:15:22 +02:00
|
|
|
import Link from 'component/link';
|
2016-11-22 21:19:08 +01:00
|
|
|
|
2017-05-17 10:10:25 +02:00
|
|
|
export class DropDownMenuItem extends React.Component {
|
|
|
|
static propTypes = {
|
2016-08-07 22:10:44 +02:00
|
|
|
href: React.PropTypes.string,
|
|
|
|
label: React.PropTypes.string,
|
|
|
|
icon: React.PropTypes.string,
|
|
|
|
onClick: React.PropTypes.func,
|
2017-05-17 10:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
iconPosition: 'left',
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-08-07 22:10:44 +02:00
|
|
|
var icon = (this.props.icon ? <Icon icon={this.props.icon} fixed /> : null);
|
|
|
|
|
|
|
|
return (
|
2017-01-13 03:03:34 +01:00
|
|
|
<a className="menu__menu-item" onClick={this.props.onClick}
|
2016-08-07 22:10:44 +02:00
|
|
|
href={this.props.href || 'javascript:'} label={this.props.label}>
|
|
|
|
{this.props.iconPosition == 'left' ? icon : null}
|
|
|
|
{this.props.label}
|
|
|
|
{this.props.iconPosition == 'left' ? null : icon}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
2017-05-17 10:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DropDownMenu extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-01-13 03:03:34 +01:00
|
|
|
|
2017-05-17 10:10:25 +02:00
|
|
|
this._isWindowClickBound = false;
|
|
|
|
this._menuDiv = null;
|
2017-01-13 03:03:34 +01:00
|
|
|
|
2017-05-17 10:10:25 +02:00
|
|
|
this.state = {
|
2017-01-13 03:03:34 +01:00
|
|
|
menuOpen: false,
|
|
|
|
};
|
2017-05-17 10:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2017-01-13 03:03:34 +01:00
|
|
|
if (this._isWindowClickBound) {
|
|
|
|
window.removeEventListener('click', this.handleWindowClick, false);
|
|
|
|
}
|
2017-05-17 10:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMenuIconClick(e) {
|
2017-01-13 03:03:34 +01:00
|
|
|
this.setState({
|
|
|
|
menuOpen: !this.state.menuOpen,
|
|
|
|
});
|
|
|
|
if (!this.state.menuOpen && !this._isWindowClickBound) {
|
|
|
|
this._isWindowClickBound = true;
|
|
|
|
window.addEventListener('click', this.handleWindowClick, false);
|
2017-01-13 16:44:23 +01:00
|
|
|
e.stopPropagation();
|
2017-01-13 03:03:34 +01:00
|
|
|
}
|
|
|
|
return false;
|
2017-05-17 10:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMenuClick(e) {
|
2017-02-24 10:13:52 +01:00
|
|
|
// Event bubbles up to the menu after a link is clicked
|
|
|
|
this.setState({
|
|
|
|
menuOpen: false,
|
|
|
|
});
|
2017-05-17 10:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handleWindowClick(e) {
|
2017-01-13 03:03:34 +01:00
|
|
|
if (this.state.menuOpen &&
|
|
|
|
(!this._menuDiv || !this._menuDiv.contains(e.target))) {
|
|
|
|
this.setState({
|
|
|
|
menuOpen: false
|
|
|
|
});
|
|
|
|
}
|
2017-05-17 10:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-01-13 03:03:34 +01:00
|
|
|
if (!this.state.menuOpen && this._isWindowClickBound) {
|
|
|
|
this._isWindowClickBound = false;
|
|
|
|
window.removeEventListener('click', this.handleWindowClick, false);
|
|
|
|
}
|
|
|
|
return (
|
2017-02-21 07:26:59 +01:00
|
|
|
<div className="menu-container">
|
2017-05-18 03:37:39 +02:00
|
|
|
<Link ref={(span) => this._menuButton = span} button="text" icon="icon-ellipsis-v" onClick={(event) => { this.handleMenuIconClick(event) }} />
|
2017-01-13 03:03:34 +01:00
|
|
|
{this.state.menuOpen
|
2017-05-18 03:37:39 +02:00
|
|
|
? <div ref={(div) => this._menuDiv = div} className="menu" onClick={(event) => { this.handleMenuClick(event) }}>
|
2017-01-13 03:03:34 +01:00
|
|
|
{this.props.children}
|
|
|
|
</div>
|
|
|
|
: null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-05-17 10:10:25 +02:00
|
|
|
}
|