Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

415 строки
13 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends2 = require('babel-runtime/helpers/extends');
  6. var _extends3 = _interopRequireDefault(_extends2);
  7. var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
  8. var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
  9. var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
  10. var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
  11. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  12. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  13. var _createClass2 = require('babel-runtime/helpers/createClass');
  14. var _createClass3 = _interopRequireDefault(_createClass2);
  15. var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  16. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  17. var _inherits2 = require('babel-runtime/helpers/inherits');
  18. var _inherits3 = _interopRequireDefault(_inherits2);
  19. var _simpleAssign = require('simple-assign');
  20. var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
  21. var _react = require('react');
  22. var _react2 = _interopRequireDefault(_react);
  23. var _propTypes = require('prop-types');
  24. var _propTypes2 = _interopRequireDefault(_propTypes);
  25. var _reactDom = require('react-dom');
  26. var _reactDom2 = _interopRequireDefault(_reactDom);
  27. var _events = require('../utils/events');
  28. var _events2 = _interopRequireDefault(_events);
  29. var _propTypes3 = require('../utils/propTypes');
  30. var _propTypes4 = _interopRequireDefault(_propTypes3);
  31. var _Menu = require('../Menu/Menu');
  32. var _Menu2 = _interopRequireDefault(_Menu);
  33. var _Popover = require('../Popover/Popover');
  34. var _Popover2 = _interopRequireDefault(_Popover);
  35. var _warning = require('warning');
  36. var _warning2 = _interopRequireDefault(_warning);
  37. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  38. var IconMenu = function (_Component) {
  39. (0, _inherits3.default)(IconMenu, _Component);
  40. function IconMenu() {
  41. var _ref;
  42. var _temp, _this, _ret;
  43. (0, _classCallCheck3.default)(this, IconMenu);
  44. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  45. args[_key] = arguments[_key];
  46. }
  47. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = IconMenu.__proto__ || (0, _getPrototypeOf2.default)(IconMenu)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  48. menuInitiallyKeyboardFocused: false,
  49. open: false
  50. }, _this.handleItemClick = function (event, child) {
  51. if (_this.props.clickCloseDelay !== 0 && !child.props.hasOwnProperty('menuItems')) {
  52. var isKeyboard = _events2.default.isKeyboard(event);
  53. _this.timerCloseId = setTimeout(function () {
  54. _this.close(isKeyboard ? 'enter' : 'itemTap', isKeyboard);
  55. }, _this.props.clickCloseDelay);
  56. }
  57. _this.props.onItemClick(event, child);
  58. }, _this.handleRequestClose = function (reason) {
  59. _this.close(reason);
  60. }, _this.handleEscKeyDownMenu = function (event) {
  61. _this.close('escape', event);
  62. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  63. }
  64. (0, _createClass3.default)(IconMenu, [{
  65. key: 'componentWillReceiveProps',
  66. value: function componentWillReceiveProps(nextProps) {
  67. if (nextProps.open != null) {
  68. this.setState({
  69. open: nextProps.open,
  70. anchorEl: this.refs.iconMenuContainer
  71. });
  72. }
  73. }
  74. }, {
  75. key: 'componentWillUnmount',
  76. value: function componentWillUnmount() {
  77. clearTimeout(this.timerCloseId);
  78. }
  79. }, {
  80. key: 'isOpen',
  81. value: function isOpen() {
  82. return this.state.open;
  83. }
  84. }, {
  85. key: 'close',
  86. value: function close(reason, isKeyboard) {
  87. var _this2 = this;
  88. if (!this.state.open) {
  89. return;
  90. }
  91. if (this.props.open !== null) {
  92. this.props.onRequestChange(false, reason);
  93. } else {
  94. this.setState({ open: false }, function () {
  95. // Set focus on the icon button when the menu close
  96. if (isKeyboard) {
  97. var iconButton = _this2.refs.iconButton;
  98. _reactDom2.default.findDOMNode(iconButton).focus();
  99. iconButton.setKeyboardFocus();
  100. }
  101. });
  102. }
  103. }
  104. }, {
  105. key: 'open',
  106. value: function open(reason, event) {
  107. if (this.props.open !== null) {
  108. this.props.onRequestChange(true, reason);
  109. return this.setState({
  110. menuInitiallyKeyboardFocused: _events2.default.isKeyboard(event),
  111. anchorEl: event.currentTarget
  112. });
  113. }
  114. this.setState({
  115. open: true,
  116. menuInitiallyKeyboardFocused: _events2.default.isKeyboard(event),
  117. anchorEl: event.currentTarget
  118. });
  119. }
  120. }, {
  121. key: 'render',
  122. value: function render() {
  123. var _this3 = this;
  124. var _props = this.props,
  125. anchorOrigin = _props.anchorOrigin,
  126. className = _props.className,
  127. animated = _props.animated,
  128. animation = _props.animation,
  129. iconButtonElement = _props.iconButtonElement,
  130. iconStyle = _props.iconStyle,
  131. onItemClick = _props.onItemClick,
  132. onKeyboardFocus = _props.onKeyboardFocus,
  133. onMouseDown = _props.onMouseDown,
  134. onMouseLeave = _props.onMouseLeave,
  135. onMouseEnter = _props.onMouseEnter,
  136. onMouseUp = _props.onMouseUp,
  137. onRequestChange = _props.onRequestChange,
  138. onClick = _props.onClick,
  139. listStyle = _props.listStyle,
  140. menuStyle = _props.menuStyle,
  141. style = _props.style,
  142. targetOrigin = _props.targetOrigin,
  143. clickCloseDelay = _props.clickCloseDelay,
  144. useLayerForClickAway = _props.useLayerForClickAway,
  145. 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']);
  146. var prepareStyles = this.context.muiTheme.prepareStyles;
  147. var _state = this.state,
  148. open = _state.open,
  149. anchorEl = _state.anchorEl;
  150. var styles = {
  151. root: {
  152. display: 'inline-block',
  153. position: 'relative'
  154. },
  155. menu: {
  156. position: 'relative'
  157. }
  158. };
  159. var mergedRootStyles = (0, _simpleAssign2.default)(styles.root, style);
  160. var mergedMenuStyles = (0, _simpleAssign2.default)(styles.menu, menuStyle);
  161. 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;
  162. var iconButtonProps = {
  163. onKeyboardFocus: onKeyboardFocus,
  164. onClick: function onClick(event) {
  165. _this3.open(_events2.default.isKeyboard(event) ? 'keyboard' : 'iconTap', event);
  166. if (iconButtonElement.props.onClick) {
  167. iconButtonElement.props.onClick(event);
  168. }
  169. },
  170. ref: 'iconButton'
  171. };
  172. if (iconStyle || iconButtonElement.props.iconStyle) {
  173. iconButtonProps.iconStyle = iconStyle ? (0, _simpleAssign2.default)({}, iconStyle, iconButtonElement.props.iconStyle) : iconButtonElement.props.iconStyle;
  174. }
  175. var iconButton = _react2.default.cloneElement(iconButtonElement, iconButtonProps);
  176. var menu = _react2.default.createElement(
  177. _Menu2.default,
  178. (0, _extends3.default)({}, other, {
  179. initiallyKeyboardFocused: this.state.menuInitiallyKeyboardFocused,
  180. onEscKeyDown: this.handleEscKeyDownMenu,
  181. onItemClick: this.handleItemClick,
  182. style: mergedMenuStyles,
  183. listStyle: listStyle
  184. }),
  185. this.props.children
  186. );
  187. return _react2.default.createElement(
  188. 'div',
  189. {
  190. ref: 'iconMenuContainer',
  191. className: className,
  192. onMouseDown: onMouseDown,
  193. onMouseLeave: onMouseLeave,
  194. onMouseEnter: onMouseEnter,
  195. onMouseUp: onMouseUp,
  196. onClick: onClick,
  197. style: prepareStyles(mergedRootStyles)
  198. },
  199. iconButton,
  200. _react2.default.createElement(
  201. _Popover2.default,
  202. {
  203. anchorOrigin: anchorOrigin,
  204. targetOrigin: targetOrigin,
  205. open: open,
  206. anchorEl: anchorEl,
  207. childContextTypes: this.constructor.childContextTypes,
  208. useLayerForClickAway: useLayerForClickAway,
  209. onRequestClose: this.handleRequestClose,
  210. animated: animated,
  211. animation: animation,
  212. context: this.context
  213. },
  214. menu
  215. )
  216. );
  217. }
  218. }]);
  219. return IconMenu;
  220. }(_react.Component);
  221. IconMenu.muiName = 'IconMenu';
  222. IconMenu.defaultProps = {
  223. anchorOrigin: {
  224. vertical: 'top',
  225. horizontal: 'left'
  226. },
  227. animated: true,
  228. multiple: false,
  229. open: null,
  230. onItemClick: function onItemClick() {},
  231. onKeyboardFocus: function onKeyboardFocus() {},
  232. onMouseDown: function onMouseDown() {},
  233. onMouseLeave: function onMouseLeave() {},
  234. onMouseEnter: function onMouseEnter() {},
  235. onMouseUp: function onMouseUp() {},
  236. onRequestChange: function onRequestChange() {},
  237. onClick: function onClick() {},
  238. targetOrigin: {
  239. vertical: 'top',
  240. horizontal: 'left'
  241. },
  242. clickCloseDelay: 200,
  243. useLayerForClickAway: false
  244. };
  245. IconMenu.contextTypes = {
  246. muiTheme: _propTypes2.default.object.isRequired
  247. };
  248. IconMenu.propTypes = process.env.NODE_ENV !== "production" ? {
  249. /**
  250. * This is the point on the icon where the menu
  251. * `targetOrigin` will attach.
  252. * Options:
  253. * vertical: [top, center, bottom]
  254. * horizontal: [left, middle, right].
  255. */
  256. anchorOrigin: _propTypes4.default.origin,
  257. /**
  258. * If true, the popover will apply transitions when
  259. * it gets added to the DOM.
  260. */
  261. animated: _propTypes2.default.bool,
  262. /**
  263. * Override the default animation component used.
  264. */
  265. animation: _propTypes2.default.func,
  266. /**
  267. * Should be used to pass `MenuItem` components.
  268. */
  269. children: _propTypes2.default.node,
  270. /**
  271. * The CSS class name of the root element.
  272. */
  273. className: _propTypes2.default.string,
  274. /**
  275. * Sets the delay in milliseconds before closing the
  276. * menu when an item is clicked.
  277. * If set to 0 then the auto close functionality
  278. * will be disabled.
  279. */
  280. clickCloseDelay: _propTypes2.default.number,
  281. /**
  282. * This is the `IconButton` to render. This button will open the menu.
  283. */
  284. iconButtonElement: _propTypes2.default.element.isRequired,
  285. /**
  286. * Override the inline-styles of the underlying icon element.
  287. */
  288. iconStyle: _propTypes2.default.object,
  289. /**
  290. * Override the inline-styles of the underlying `List` element.
  291. */
  292. listStyle: _propTypes2.default.object,
  293. /**
  294. * Override the inline-styles of the menu element.
  295. */
  296. menuStyle: _propTypes2.default.object,
  297. /**
  298. * If true, the value can an be array and allow the menu to be a multi-select.
  299. */
  300. multiple: _propTypes2.default.bool,
  301. /**
  302. * Callback function fired when the `IconButton` element is clicked.
  303. *
  304. * @param {object} event Click event targeting the `IconButton` element.
  305. */
  306. onClick: _propTypes2.default.func,
  307. /**
  308. * Callback function fired when a menu item is selected with a click.
  309. *
  310. * @param {object} event Click event targeting the selected menu item element.
  311. * @param {object} child The selected element.
  312. */
  313. onItemClick: _propTypes2.default.func,
  314. /**
  315. * Callback function fired when the `IconButton` element is focused or blurred by the keyboard.
  316. *
  317. * @param {object} event `focus` or `blur` event targeting the `IconButton` element.
  318. * @param {boolean} keyboardFocused If true, the `IconButton` element is focused.
  319. */
  320. onKeyboardFocus: _propTypes2.default.func,
  321. /** @ignore */
  322. onMouseDown: _propTypes2.default.func,
  323. /** @ignore */
  324. onMouseEnter: _propTypes2.default.func,
  325. /** @ignore */
  326. onMouseLeave: _propTypes2.default.func,
  327. /** @ignore */
  328. onMouseUp: _propTypes2.default.func,
  329. /**
  330. * Callback function fired when the `open` state of the menu is requested to be changed.
  331. *
  332. * @param {boolean} open If true, the menu was requested to be opened.
  333. * @param {string} reason The reason for the open or close request. Possible values are
  334. * 'keyboard' and 'iconTap' for open requests; 'enter', 'escape', 'itemTap', and 'clickAway'
  335. * for close requests.
  336. */
  337. onRequestChange: _propTypes2.default.func,
  338. /**
  339. * If true, the `IconMenu` is opened.
  340. */
  341. open: _propTypes2.default.bool,
  342. /**
  343. * Override the inline-styles of the root element.
  344. */
  345. style: _propTypes2.default.object,
  346. /**
  347. * This is the point on the menu which will stick to the menu
  348. * origin.
  349. * Options:
  350. * vertical: [top, center, bottom]
  351. * horizontal: [left, middle, right].
  352. */
  353. targetOrigin: _propTypes4.default.origin,
  354. /**
  355. * If true, the popover will render on top of an invisible
  356. * layer, which will prevent clicks to the underlying elements.
  357. */
  358. useLayerForClickAway: _propTypes2.default.bool
  359. } : {};
  360. exports.default = IconMenu;