UNPKG

material-ui

Version:

React Components that Implement Google's Material Design.

415 lines (360 loc) 13.6 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties'); var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _simpleAssign = require('simple-assign'); var _simpleAssign2 = _interopRequireDefault(_simpleAssign); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _events = require('../utils/events'); var _events2 = _interopRequireDefault(_events); var _propTypes3 = require('../utils/propTypes'); var _propTypes4 = _interopRequireDefault(_propTypes3); var _Menu = require('../Menu/Menu'); var _Menu2 = _interopRequireDefault(_Menu); var _Popover = require('../Popover/Popover'); var _Popover2 = _interopRequireDefault(_Popover); var _warning = require('warning'); var _warning2 = _interopRequireDefault(_warning); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var IconMenu = function (_Component) { (0, _inherits3.default)(IconMenu, _Component); function IconMenu() { var _ref; var _temp, _this, _ret; (0, _classCallCheck3.default)(this, IconMenu); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = IconMenu.__proto__ || (0, _getPrototypeOf2.default)(IconMenu)).call.apply(_ref, [this].concat(args))), _this), _this.state = { menuInitiallyKeyboardFocused: false, open: false }, _this.handleItemClick = function (event, child) { if (_this.props.clickCloseDelay !== 0 && !child.props.hasOwnProperty('menuItems')) { var isKeyboard = _events2.default.isKeyboard(event); _this.timerCloseId = setTimeout(function () { _this.close(isKeyboard ? 'enter' : 'itemTap', isKeyboard); }, _this.props.clickCloseDelay); } _this.props.onItemClick(event, child); }, _this.handleRequestClose = function (reason) { _this.close(reason); }, _this.handleEscKeyDownMenu = function (event) { _this.close('escape', event); }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret); } (0, _createClass3.default)(IconMenu, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if (nextProps.open != null) { this.setState({ open: nextProps.open, anchorEl: this.refs.iconMenuContainer }); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { clearTimeout(this.timerCloseId); } }, { key: 'isOpen', value: function isOpen() { return this.state.open; } }, { key: 'close', value: function close(reason, isKeyboard) { var _this2 = this; if (!this.state.open) { return; } if (this.props.open !== null) { this.props.onRequestChange(false, reason); } else { this.setState({ open: false }, function () { // Set focus on the icon button when the menu close if (isKeyboard) { var iconButton = _this2.refs.iconButton; _reactDom2.default.findDOMNode(iconButton).focus(); iconButton.setKeyboardFocus(); } }); } } }, { key: 'open', value: function open(reason, event) { if (this.props.open !== null) { this.props.onRequestChange(true, reason); return this.setState({ menuInitiallyKeyboardFocused: _events2.default.isKeyboard(event), anchorEl: event.currentTarget }); } this.setState({ open: true, menuInitiallyKeyboardFocused: _events2.default.isKeyboard(event), anchorEl: event.currentTarget }); } }, { key: 'render', value: function render() { var _this3 = this; var _props = this.props, anchorOrigin = _props.anchorOrigin, className = _props.className, animated = _props.animated, animation = _props.animation, iconButtonElement = _props.iconButtonElement, iconStyle = _props.iconStyle, onItemClick = _props.onItemClick, onKeyboardFocus = _props.onKeyboardFocus, onMouseDown = _props.onMouseDown, onMouseLeave = _props.onMouseLeave, onMouseEnter = _props.onMouseEnter, onMouseUp = _props.onMouseUp, onRequestChange = _props.onRequestChange, onClick = _props.onClick, listStyle = _props.listStyle, menuStyle = _props.menuStyle, style = _props.style, targetOrigin = _props.targetOrigin, clickCloseDelay = _props.clickCloseDelay, useLayerForClickAway = _props.useLayerForClickAway, other = (0, _objectWithoutProperties3.default)(_props, ['anchorOrigin', 'className', 'animated', 'animation', 'iconButtonElement', 'iconStyle', 'onItemClick', 'onKeyboardFocus', 'onMouseDown', 'onMouseLeave', 'onMouseEnter', 'onMouseUp', 'onRequestChange', 'onClick', 'listStyle', 'menuStyle', 'style', 'targetOrigin', 'clickCloseDelay', 'useLayerForClickAway']); var prepareStyles = this.context.muiTheme.prepareStyles; var _state = this.state, open = _state.open, anchorEl = _state.anchorEl; var styles = { root: { display: 'inline-block', position: 'relative' }, menu: { position: 'relative' } }; var mergedRootStyles = (0, _simpleAssign2.default)(styles.root, style); var mergedMenuStyles = (0, _simpleAssign2.default)(styles.menu, menuStyle); process.env.NODE_ENV !== "production" ? (0, _warning2.default)(iconButtonElement.type.muiName !== 'SvgIcon', 'Material-UI: You shoud not provide an <SvgIcon /> to the \'iconButtonElement\' property of <IconMenu />.\nYou should wrapped it with an <IconButton />.') : void 0; var iconButtonProps = { onKeyboardFocus: onKeyboardFocus, onClick: function onClick(event) { _this3.open(_events2.default.isKeyboard(event) ? 'keyboard' : 'iconTap', event); if (iconButtonElement.props.onClick) { iconButtonElement.props.onClick(event); } }, ref: 'iconButton' }; if (iconStyle || iconButtonElement.props.iconStyle) { iconButtonProps.iconStyle = iconStyle ? (0, _simpleAssign2.default)({}, iconStyle, iconButtonElement.props.iconStyle) : iconButtonElement.props.iconStyle; } var iconButton = _react2.default.cloneElement(iconButtonElement, iconButtonProps); var menu = _react2.default.createElement( _Menu2.default, (0, _extends3.default)({}, other, { initiallyKeyboardFocused: this.state.menuInitiallyKeyboardFocused, onEscKeyDown: this.handleEscKeyDownMenu, onItemClick: this.handleItemClick, style: mergedMenuStyles, listStyle: listStyle }), this.props.children ); return _react2.default.createElement( 'div', { ref: 'iconMenuContainer', className: className, onMouseDown: onMouseDown, onMouseLeave: onMouseLeave, onMouseEnter: onMouseEnter, onMouseUp: onMouseUp, onClick: onClick, style: prepareStyles(mergedRootStyles) }, iconButton, _react2.default.createElement( _Popover2.default, { anchorOrigin: anchorOrigin, targetOrigin: targetOrigin, open: open, anchorEl: anchorEl, childContextTypes: this.constructor.childContextTypes, useLayerForClickAway: useLayerForClickAway, onRequestClose: this.handleRequestClose, animated: animated, animation: animation, context: this.context }, menu ) ); } }]); return IconMenu; }(_react.Component); IconMenu.muiName = 'IconMenu'; IconMenu.defaultProps = { anchorOrigin: { vertical: 'top', horizontal: 'left' }, animated: true, multiple: false, open: null, onItemClick: function onItemClick() {}, onKeyboardFocus: function onKeyboardFocus() {}, onMouseDown: function onMouseDown() {}, onMouseLeave: function onMouseLeave() {}, onMouseEnter: function onMouseEnter() {}, onMouseUp: function onMouseUp() {}, onRequestChange: function onRequestChange() {}, onClick: function onClick() {}, targetOrigin: { vertical: 'top', horizontal: 'left' }, clickCloseDelay: 200, useLayerForClickAway: false }; IconMenu.contextTypes = { muiTheme: _propTypes2.default.object.isRequired }; IconMenu.propTypes = process.env.NODE_ENV !== "production" ? { /** * This is the point on the icon where the menu * `targetOrigin` will attach. * Options: * vertical: [top, center, bottom] * horizontal: [left, middle, right]. */ anchorOrigin: _propTypes4.default.origin, /** * If true, the popover will apply transitions when * it gets added to the DOM. */ animated: _propTypes2.default.bool, /** * Override the default animation component used. */ animation: _propTypes2.default.func, /** * Should be used to pass `MenuItem` components. */ children: _propTypes2.default.node, /** * The CSS class name of the root element. */ className: _propTypes2.default.string, /** * Sets the delay in milliseconds before closing the * menu when an item is clicked. * If set to 0 then the auto close functionality * will be disabled. */ clickCloseDelay: _propTypes2.default.number, /** * This is the `IconButton` to render. This button will open the menu. */ iconButtonElement: _propTypes2.default.element.isRequired, /** * Override the inline-styles of the underlying icon element. */ iconStyle: _propTypes2.default.object, /** * Override the inline-styles of the underlying `List` element. */ listStyle: _propTypes2.default.object, /** * Override the inline-styles of the menu element. */ menuStyle: _propTypes2.default.object, /** * If true, the value can an be array and allow the menu to be a multi-select. */ multiple: _propTypes2.default.bool, /** * Callback function fired when the `IconButton` element is clicked. * * @param {object} event Click event targeting the `IconButton` element. */ onClick: _propTypes2.default.func, /** * Callback function fired when a menu item is selected with a click. * * @param {object} event Click event targeting the selected menu item element. * @param {object} child The selected element. */ onItemClick: _propTypes2.default.func, /** * Callback function fired when the `IconButton` element is focused or blurred by the keyboard. * * @param {object} event `focus` or `blur` event targeting the `IconButton` element. * @param {boolean} keyboardFocused If true, the `IconButton` element is focused. */ onKeyboardFocus: _propTypes2.default.func, /** @ignore */ onMouseDown: _propTypes2.default.func, /** @ignore */ onMouseEnter: _propTypes2.default.func, /** @ignore */ onMouseLeave: _propTypes2.default.func, /** @ignore */ onMouseUp: _propTypes2.default.func, /** * Callback function fired when the `open` state of the menu is requested to be changed. * * @param {boolean} open If true, the menu was requested to be opened. * @param {string} reason The reason for the open or close request. Possible values are * 'keyboard' and 'iconTap' for open requests; 'enter', 'escape', 'itemTap', and 'clickAway' * for close requests. */ onRequestChange: _propTypes2.default.func, /** * If true, the `IconMenu` is opened. */ open: _propTypes2.default.bool, /** * Override the inline-styles of the root element. */ style: _propTypes2.default.object, /** * This is the point on the menu which will stick to the menu * origin. * Options: * vertical: [top, center, bottom] * horizontal: [left, middle, right]. */ targetOrigin: _propTypes4.default.origin, /** * If true, the popover will render on top of an invisible * layer, which will prevent clicks to the underlying elements. */ useLayerForClickAway: _propTypes2.default.bool } : {}; exports.default = IconMenu;