react-toolbox/components/menu/IconMenu.js

104 lines
2.8 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
2016-05-22 21:15:45 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
2017-01-26 20:05:32 +03:00
import { MENU } from '../identifiers';
import InjectIconButton from '../button/IconButton';
import InjectMenu from './Menu';
2015-10-17 23:25:15 +03:00
2016-05-29 22:24:22 +03:00
const factory = (IconButton, Menu) => {
class IconMenu extends Component {
2016-05-29 22:24:22 +03:00
static propTypes = {
2017-08-28 00:17:53 +03:00
active: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string,
icon: PropTypes.oneOfType([
PropTypes.string,
2017-01-26 20:05:32 +03:00
PropTypes.element,
2016-05-29 22:24:22 +03:00
]),
iconRipple: PropTypes.bool,
2017-07-13 21:19:16 +03:00
inverse: PropTypes.bool,
menuRipple: PropTypes.bool,
onClick: PropTypes.func,
onHide: PropTypes.func,
onSelect: PropTypes.func,
onShow: PropTypes.func,
position: PropTypes.string,
selectable: PropTypes.bool,
2017-01-26 20:05:32 +03:00
selected: PropTypes.node,
theme: PropTypes.shape({
icon: PropTypes.string,
2017-01-26 20:05:32 +03:00
iconMenu: PropTypes.string,
}),
2016-05-29 22:24:22 +03:00
};
2015-10-17 23:25:15 +03:00
2016-05-29 22:24:22 +03:00
static defaultProps = {
2017-08-28 00:17:53 +03:00
active: false,
2016-05-29 22:24:22 +03:00
className: '',
icon: 'more_vert',
iconRipple: true,
menuRipple: true,
position: 'auto',
2017-01-26 20:05:32 +03:00
selectable: false,
2016-05-29 22:24:22 +03:00
};
2015-10-17 23:25:15 +03:00
2016-05-29 22:24:22 +03:00
state = {
2017-08-28 00:17:53 +03:00
active: this.props.active,
}
componentWillReceiveProps(nextProps) {
if (this.state.active !== nextProps.active) {
2017-08-28 00:17:53 +03:00
this.setState({ active: nextProps.active });
}
2016-05-29 22:24:22 +03:00
}
2016-05-22 21:15:45 +03:00
2016-05-29 22:24:22 +03:00
handleButtonClick = (event) => {
this.setState({ active: !this.state.active });
if (this.props.onClick) this.props.onClick(event);
};
2015-10-17 23:25:15 +03:00
2016-05-29 22:24:22 +03:00
handleMenuHide = () => {
this.setState({ active: false });
if (this.props.onHide) this.props.onHide();
}
2015-10-17 23:25:15 +03:00
2017-01-26 20:05:32 +03:00
render() {
2016-11-23 11:59:51 +03:00
const {
2017-08-28 02:10:01 +03:00
active, children, className, icon, iconRipple, inverse, menuRipple, onHide, // eslint-disable-line
onSelect, onShow, position, selectable, selected, theme, ...other
2016-11-23 11:59:51 +03:00
} = this.props;
2016-05-29 22:24:22 +03:00
return (
2016-11-23 11:59:51 +03:00
<div {...other} className={classnames(theme.iconMenu, className)}>
2016-05-29 22:24:22 +03:00
<IconButton
2016-11-23 11:59:51 +03:00
className={theme.icon}
icon={icon}
2017-01-17 15:56:50 +03:00
inverse={inverse}
2016-05-29 22:24:22 +03:00
onClick={this.handleButtonClick}
2016-11-23 11:59:51 +03:00
ripple={iconRipple}
2016-05-29 22:24:22 +03:00
/>
<Menu
active={this.state.active}
onHide={this.handleMenuHide}
2016-11-23 11:59:51 +03:00
onSelect={onSelect}
onShow={onShow}
position={position}
ripple={menuRipple}
selectable={selectable}
selected={selected}
theme={theme}
2016-05-29 22:24:22 +03:00
>
2016-11-23 11:59:51 +03:00
{children}
2016-05-29 22:24:22 +03:00
</Menu>
</div>
);
}
2015-10-17 23:25:15 +03:00
}
2015-10-22 02:31:17 +03:00
2016-05-29 22:24:22 +03:00
return IconMenu;
};
const IconMenu = factory(InjectIconButton, InjectMenu);
export default themr(MENU)(IconMenu);
export { factory as iconMenuFactory };
export { IconMenu };