react-toolbox/components/menu/Menu.js

186 lines
5.8 KiB
JavaScript
Raw Normal View History

import React from 'react';
import ReactDOM from 'react-dom';
2015-11-28 18:08:00 +03:00
import ClassNames from 'classnames';
import MenuItem from './MenuItem';
import { events, utils } from '../utils';
import style from './style.menu';
const POSITION = {
AUTO: 'auto',
STATIC: 'static',
TOP_LEFT: 'top-left',
TOP_RIGHT: 'top-right',
BOTTOM_LEFT: 'bottom-left',
BOTTOM_RIGHT: 'bottom-right'
};
2015-10-22 02:31:17 +03:00
class Menu extends React.Component {
static propTypes = {
active: React.PropTypes.bool,
children: React.PropTypes.node,
className: React.PropTypes.string,
2015-10-17 23:25:15 +03:00
onHide: React.PropTypes.func,
onSelect: React.PropTypes.func,
2015-10-17 23:25:15 +03:00
onShow: React.PropTypes.func,
outline: React.PropTypes.bool,
position: React.PropTypes.string,
ripple: React.PropTypes.bool,
selectable: React.PropTypes.bool,
2015-11-12 02:59:39 +03:00
selected: React.PropTypes.any
};
static defaultProps = {
active: false,
outline: true,
position: POSITION.STATIC,
ripple: true,
selectable: true
};
state = {
active: this.props.active,
2015-11-12 02:59:39 +03:00
rippled: false
};
componentDidMount () {
this.positionTimeoutHandle = setTimeout(() => {
const { width, height } = this.refs.menu.getBoundingClientRect();
const position = this.props.position === POSITION.AUTO ? this.calculatePosition() : this.props.position;
this.setState({ position, width, height });
});
}
componentWillReceiveProps (nextProps) {
if (this.props.position !== nextProps.position) {
const position = nextProps.position === POSITION.AUTO ? this.calculatePosition() : nextProps.position;
this.setState({ position });
}
}
shouldComponentUpdate (nextProps, nextState) {
if (!this.state.active && nextState.active && this.props.position === POSITION.AUTO) {
const position = this.calculatePosition();
if (this.state.position !== position) {
this.setState({ position, active: false }, () => {
this.activateTimeoutHandle = setTimeout(() => {this.setState({active: true}); }, 20);
});
return false;
}
}
return true;
}
componentWillUpdate (nextProps, nextState) {
if (!this.state.active && nextState.active) {
events.addEventsToDocument({click: this.handleDocumentClick});
}
}
componentDidUpdate (prevProps, prevState) {
if (prevState.active && !this.state.active) {
if (this.props.onHide) this.props.onHide();
events.removeEventsFromDocument({click: this.handleDocumentClick});
} else if (!prevState.active && this.state.active && this.props.onShow) {
this.props.onShow();
}
}
componentWillUnmount () {
if (this.state.active) {
events.removeEventsFromDocument({click: this.handleDocumentClick});
}
2016-04-15 23:27:22 +03:00
clearTimeout(this.positionTimeoutHandle);
clearTimeout(this.activateTimeoutHandle);
}
2015-10-22 02:31:17 +03:00
handleDocumentClick = (event) => {
if (this.state.active && !events.targetIsDescendant(event, ReactDOM.findDOMNode(this))) {
this.setState({active: false, rippled: false});
}
2015-10-22 02:31:17 +03:00
};
handleSelect = (item) => {
const { value, onClick } = item.props;
2015-11-12 02:59:39 +03:00
this.setState({ active: false, rippled: this.props.ripple }, () => {
2015-10-22 02:31:17 +03:00
if (onClick) onClick();
2015-11-12 02:59:39 +03:00
if (this.props.onSelect) this.props.onSelect(value);
2015-10-22 02:31:17 +03:00
});
};
calculatePosition () {
2016-04-10 20:22:15 +03:00
const parentNode = ReactDOM.findDOMNode(this).parentNode;
if (!parentNode) return;
const {top, left, height, width} = parentNode.getBoundingClientRect();
const {height: wh, width: ww} = utils.getViewport();
const toTop = top < ((wh / 2) - height / 2);
2015-10-17 23:25:15 +03:00
const toLeft = left < ((ww / 2) - width / 2);
return `${toTop ? 'top' : 'bottom'}-${toLeft ? 'left' : 'right'}`;
}
getMenuStyle () {
const { width, height, position } = this.state;
if (position !== POSITION.STATIC) {
if (this.state.active) {
return { clip: `rect(0 ${width}px ${height}px 0)` };
} else if (position === POSITION.TOP_RIGHT) {
return { clip: `rect(0 ${width}px 0 ${width}px)` };
} else if (position === POSITION.BOTTOM_RIGHT) {
return { clip: `rect(${height}px ${width}px ${height}px ${width}px)` };
} else if (position === POSITION.BOTTOM_LEFT) {
return { clip: `rect(${height}px 0 ${height}px 0)` };
} else if (position === POSITION.TOP_LEFT) {
2016-04-09 21:34:34 +03:00
return { clip: 'rect(0 0 0 0)' };
}
}
}
getRootStyle () {
if (this.state.position !== POSITION.STATIC) {
return { width: this.state.width, height: this.state.height };
}
}
renderItems () {
return React.Children.map(this.props.children, (item) => {
if (!item) return item;
if (item.type === MenuItem) {
return React.cloneElement(item, {
ripple: item.props.ripple || this.props.ripple,
2016-03-03 15:44:42 +03:00
selected: typeof item.props.value !== 'undefined' && this.props.selectable && item.props.value === this.props.selected,
onClick: this.handleSelect.bind(this, item)
});
} else {
return React.cloneElement(item);
}
});
}
2015-11-24 02:29:02 +03:00
show () {
2016-03-06 22:25:39 +03:00
const { width, height } = this.refs.menu.getBoundingClientRect();
this.setState({active: true, width, height});
2015-11-24 02:29:02 +03:00
}
hide () {
this.setState({active: false});
}
render () {
const outlineStyle = { width: this.state.width, height: this.state.height };
2015-11-28 18:08:00 +03:00
const className = ClassNames([style.root, style[this.state.position]], {
[style.active]: this.state.active,
[style.rippled]: this.state.rippled
}, this.props.className);
return (
2016-02-08 21:36:42 +03:00
<div data-react-toolbox='menu' className={className} style={this.getRootStyle()}>
{this.props.outline ? <div className={style.outline} style={outlineStyle}></div> : null}
<ul ref='menu' className={style.menu} style={this.getMenuStyle()}>
{this.renderItems()}
</ul>
</div>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Menu;