react-toolbox/components/menu/Menu.js

244 lines
7.6 KiB
JavaScript
Raw Normal View History

import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
2016-05-22 21:15:45 +03:00
import classnames from 'classnames';
2016-05-29 22:24:22 +03:00
import { themr } from 'react-css-themr';
2017-01-26 20:05:32 +03:00
import { MENU } from '../identifiers';
Migrate styles to PostCSS (#666) * Add postcss-next postcss-include and reporter * Add stylelint * Add CSS colors * Add CSS custom media queries * Use dashes for CSS colors * Add base CSS variables * Remove AppBar SASS dependency from spec page * Migrate AppBar style to PostCSS * Migrate Avatar style to PostCSS * Migrate Ripple style to PostCSS * Remove unneeded media CSS import in Avatar * Add shadows to CSS variables * Migrate Button style to PostCSS * Update webpack test config and linting from npm * Migrate Input style to PostCSS * Add missing input config variables for Dropdown and Autocomplete * Migrate Chip style to PostCSS * Migrate Autocomplete style to PostCSS * Migrate Dropdown style to PostCSS * Migrate animations to PostCSS * Migrate Card style to PostCSS * Migrate Checkbox style to PostCSS * Migrate DataPicker style to PostCSS * Migrate Dialog style to PostCSS * Migrate Drawer style to PostCSS * Add postcss-mixins and postcss-each * Migrate Layout style to PostCSS * Fix bug in button theme * Bugfix in avatar css * Add some missing nesting notations * Migrate Link style to PostCSS * Migrate List style to PostCSS * Migrate Menu style to PostCSS * Migrate Navigation style to PostCSS * Migrate Overlay style to PostCSS * Migrate ProgressBar style to PostCSS * Migrate Radio style to PostCSS * Migrate Slider style to PostCSS * Migrate Snackbar style to PostCSS * Migrate Switch style to PostCSS * Migrate Table style to PostCSS * Migrate Tabs style to PostCSS * Migrate TimePicker to PostCSS * Migrate Tooltip styles to PostCSS * Update webpack config for testing and tests * Migrate commons to PostCSS * Remove sass from main project * Bye from docs to sass * Build with CSS * Remove unneded deps for sass in docs subproject * Fix tests * use 4p shadow in AppBar as spec indicates * Fixed typo in list/config.css * Fix tests * Fix linter errors * Latest build * Release 2.0.0-beta.0 * Remove sass lint * fixes old sass var in css config * Update linter * New Table implementation * Fix old sass var in list/config.css See da0c47041ecf2d8b118b06fba9f53cc4d7e1e79f. * Remove normalize.css from commons.css * Update dependencies * Latest build * Input ready to accept visible hint * Prepare slider and progress to be disabled * Render Snackbar using Portal * Refactor Dialog, Drawer and Overlay to be used in Layout * Add inner layer to AppBar * New layout * Use Layout in spec * Latest build * remove layout playground example * add smTablet and lgTablet to NavDrawer in Layout readme * add default prop className to Layout, NavDrawer and Sidebar * fix css linter errors in card and slider * Typings for Table component * Add missing Drawer identifier * Update lib build * Adds onQueryChange callback property to Autocomplete The onQueryChange callback is called when the value of the query changes in Autocomplete. It is called with the new query value. * Fix #966 * Fix #965 * Fixes #976 * Updated css-related dependencies Removed usages of `addDependencyTo` since it's deprecated and not needed anymore. See https://github.com/postcss/postcss-import#adddependencyto * Enabled HMR for styles by disabling extracting them to a CSS file * Remove lib from repo * Remove lib * Fixes #1021 * Build using Gulp * Export ThemeProvider from react-css-themr * Add CHANGELOG to release command * Release 2.0.0-beta.1 * Remove immutability helper * Update dependencies * Fix Tooltip trying to render after it's been unmounted * Fixes #1038 * Release 2.0.0-beta.2 * Add ramda, refactor utils and remove slide animation modules * Remove separate slide animations modules * Remove box-sizing reset and body rule * Remove commons.css * Release 2.0.0-beta.4 * Update dependencies * Fixes #1061 * More aggresive guard condition for multiple autocomplete * Fix typeof check in isBrowser function * Fixes an issue when specs are opened with browsers that do not support Object.entries(). * Import from ramda using the 'import XXX from "ramda/src/XXX"' pattern so that bundle sizes will be smaller for not including the whole ramda package. * Fix #1032 * Fix tests * Update yarn.lock * Fixes #1064
2017-01-05 04:42:18 +03:00
import { events } from '../utils';
import { getViewport } from '../utils/utils';
2017-01-26 20:05:32 +03:00
import InjectMenuItem from './MenuItem';
const POSITION = {
AUTO: 'auto',
STATIC: 'static',
2016-05-22 21:36:06 +03:00
TOP_LEFT: 'topLeft',
TOP_RIGHT: 'topRight',
BOTTOM_LEFT: 'bottomLeft',
2017-01-26 20:05:32 +03:00
BOTTOM_RIGHT: 'bottomRight',
};
2016-05-29 22:24:22 +03:00
const factory = (MenuItem) => {
class Menu extends Component {
2016-05-29 22:24:22 +03:00
static propTypes = {
active: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string,
onHide: PropTypes.func,
onSelect: PropTypes.func,
onShow: PropTypes.func,
outline: PropTypes.bool,
position: PropTypes.oneOf(Object.keys(POSITION).map(key => POSITION[key])),
ripple: PropTypes.bool,
selectable: PropTypes.bool,
2017-01-26 20:05:32 +03:00
selected: PropTypes.node,
theme: PropTypes.shape({
active: PropTypes.string,
bottomLeft: PropTypes.string,
bottomRight: PropTypes.string,
menu: PropTypes.string,
menuInner: PropTypes.string,
outline: PropTypes.string,
rippled: PropTypes.string,
static: PropTypes.string,
topLeft: PropTypes.string,
2017-01-26 20:05:32 +03:00
topRight: PropTypes.string,
}),
2016-05-29 22:24:22 +03:00
};
static defaultProps = {
active: false,
outline: true,
position: POSITION.STATIC,
ripple: true,
2017-01-26 20:05:32 +03:00
selectable: true,
2016-05-29 22:24:22 +03:00
};
state = {
active: this.props.active,
2017-01-26 20:05:32 +03:00
rippled: false,
2016-05-29 22:24:22 +03:00
};
2017-01-26 20:05:32 +03:00
componentDidMount() {
2016-05-29 22:24:22 +03:00
this.positionTimeoutHandle = setTimeout(() => {
2017-01-26 20:05:32 +03:00
const { width, height } = this.menuNode.getBoundingClientRect();
2016-07-18 11:45:03 +03:00
const position = this.props.position === POSITION.AUTO
? this.calculatePosition()
: this.props.position;
2016-05-29 22:24:22 +03:00
this.setState({ position, width, height });
});
}
2016-05-22 21:15:45 +03:00
2017-01-26 20:05:32 +03:00
componentWillReceiveProps(nextProps) {
2016-05-29 22:24:22 +03:00
if (this.props.position !== nextProps.position) {
2016-07-18 11:45:03 +03:00
const position = nextProps.position === POSITION.AUTO
? this.calculatePosition()
: nextProps.position;
2016-05-29 22:24:22 +03:00
this.setState({ position });
}
2016-07-18 11:45:03 +03:00
/**
* If the menu is going to be activated via props and its not active, verify
* the position is appropriated and then show it recalculating position if its
* wrong. It should be shown in two consecutive setState.
*/
2016-05-29 22:24:22 +03:00
if (!this.props.active && nextProps.active && !this.state.active) {
2016-07-18 11:45:03 +03:00
if (nextProps.position === POSITION.AUTO) {
const position = this.calculatePosition();
if (this.state.position !== position) {
this.setState({ position, active: false }, () => {
this.activateTimeoutHandle = setTimeout(() => { this.show(); }, 20);
});
} else {
this.show();
}
} else {
this.show();
}
2016-05-29 22:24:22 +03:00
}
2016-07-18 11:45:03 +03:00
/**
* If the menu is being deactivated via props and the current state is
* active, it should be hid.
*/
2016-05-29 22:24:22 +03:00
if (this.props.active && !nextProps.active && this.state.active) {
this.hide();
}
2016-05-22 21:15:45 +03:00
}
2017-01-26 20:05:32 +03:00
componentWillUpdate(nextProps, nextState) {
2016-05-29 22:24:22 +03:00
if (!this.state.active && nextState.active) {
2016-10-06 21:18:35 +03:00
events.addEventsToDocument({
click: this.handleDocumentClick,
2017-01-26 20:05:32 +03:00
touchstart: this.handleDocumentClick,
2016-10-06 21:18:35 +03:00
});
}
}
2017-01-26 20:05:32 +03:00
componentDidUpdate(prevProps, prevState) {
2016-05-29 22:24:22 +03:00
if (prevState.active && !this.state.active) {
if (this.props.onHide) this.props.onHide();
2016-10-06 21:18:35 +03:00
events.removeEventsFromDocument({
click: this.handleDocumentClick,
2017-01-26 20:05:32 +03:00
touchstart: this.handleDocumentClick,
2016-10-06 21:18:35 +03:00
});
2016-05-29 22:24:22 +03:00
} else if (!prevState.active && this.state.active && this.props.onShow) {
this.props.onShow();
}
}
2017-01-26 20:05:32 +03:00
componentWillUnmount() {
2016-10-06 21:18:35 +03:00
if (this.state.active) {
events.removeEventsFromDocument({
click: this.handleDocumentClick,
2017-01-26 20:05:32 +03:00
touchstart: this.handleDocumentClick,
2016-10-06 21:18:35 +03:00
});
}
2016-05-29 22:24:22 +03:00
clearTimeout(this.positionTimeoutHandle);
clearTimeout(this.activateTimeoutHandle);
}
2017-01-26 20:05:32 +03:00
getMenuStyle() {
2016-05-29 22:24:22 +03:00
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) {
return { clip: 'rect(0 0 0 0)' };
}
}
2017-01-26 20:05:32 +03:00
return undefined;
}
getRootStyle() {
return this.state.position !== POSITION.STATIC
? { width: this.state.width, height: this.state.height }
: undefined;
}
2017-01-26 20:05:32 +03:00
calculatePosition() {
const parentNode = ReactDOM.findDOMNode(this).parentNode;
if (!parentNode) return undefined;
const { top, left, height, width } = parentNode.getBoundingClientRect();
const { height: wh, width: ww } = getViewport();
const toTop = top < ((wh / 2) - (height / 2));
const toLeft = left < ((ww / 2) - (width / 2));
return `${toTop ? 'top' : 'bottom'}${toLeft ? 'Left' : 'Right'}`;
}
handleDocumentClick = (event) => {
if (this.state.active && !events.targetIsDescendant(event, ReactDOM.findDOMNode(this))) {
this.setState({ active: false, rippled: false });
}
2017-01-26 20:05:32 +03:00
};
handleSelect = (item, event) => {
const { value, onClick } = item.props;
if (onClick) event.persist();
this.setState({ active: false, rippled: this.props.ripple }, () => {
if (onClick) onClick(event);
if (this.props.onSelect) this.props.onSelect(value);
});
};
show() {
const { width, height } = this.menuNode.getBoundingClientRect();
this.setState({ active: true, width, height });
}
hide() {
this.setState({ active: false });
}
2017-01-26 20:05:32 +03:00
renderItems() {
2016-05-29 22:24:22 +03:00
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,
2017-01-26 20:05:32 +03:00
selected: typeof item.props.value !== 'undefined'
&& this.props.selectable
&& item.props.value === this.props.selected,
onClick: this.handleSelect.bind(this, item),
2016-05-29 22:24:22 +03:00
});
}
2017-01-26 20:05:32 +03:00
return React.cloneElement(item);
2016-05-29 22:24:22 +03:00
});
}
2017-01-26 20:05:32 +03:00
render() {
2016-05-29 22:24:22 +03:00
const { theme } = this.props;
const outlineStyle = { width: this.state.width, height: this.state.height };
const className = classnames([theme.menu, theme[this.state.position]], {
[theme.active]: this.state.active,
2017-01-26 20:05:32 +03:00
[theme.rippled]: this.state.rippled,
2016-05-29 22:24:22 +03:00
}, this.props.className);
return (
2017-01-26 20:05:32 +03:00
<div data-react-toolbox="menu" className={className} style={this.getRootStyle()}>
2016-08-07 22:23:14 +03:00
{this.props.outline ? <div className={theme.outline} style={outlineStyle} /> : null}
2017-01-26 20:05:32 +03:00
<ul
ref={(node) => { this.menuNode = node; }}
className={theme.menuInner}
style={this.getMenuStyle()}
>
2016-05-29 22:24:22 +03:00
{this.renderItems()}
</ul>
</div>
);
}
2015-11-24 02:29:02 +03:00
}
2016-05-29 22:24:22 +03:00
return Menu;
};
2015-10-22 02:31:17 +03:00
2016-05-29 22:24:22 +03:00
const Menu = factory(InjectMenuItem);
export default themr(MENU)(Menu);
export { factory as menuFactory };
export { Menu };