Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

82 рядки
4.6 KiB

  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  6. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  7. import React, { Component } from 'react';
  8. import ContextMenuTrigger from './ContextMenuTrigger';
  9. import listener from './globalEventListener';
  10. // collect ContextMenuTrigger's expected props to NOT pass them on as part of the context
  11. var ignoredTriggerProps = [].concat(_toConsumableArray(Object.keys(ContextMenuTrigger.propTypes)), ['children']);
  12. // expect the id of the menu to be responsible for as outer parameter
  13. export default function (menuId) {
  14. // expect menu component to connect as inner parameter
  15. // <Child/> is presumably a wrapper of <ContextMenu/>
  16. return function connect(Child) {
  17. // return wrapper for <Child/> that forwards the ContextMenuTrigger's additional props
  18. return function (_Component) {
  19. _inherits(ConnectMenu, _Component);
  20. function ConnectMenu(props) {
  21. _classCallCheck(this, ConnectMenu);
  22. var _this = _possibleConstructorReturn(this, (ConnectMenu.__proto__ || Object.getPrototypeOf(ConnectMenu)).call(this, props));
  23. _this.handleShow = function (e) {
  24. if (e.detail.id !== menuId) return;
  25. // the onShow event's detail.data object holds all ContextMenuTrigger props
  26. var data = e.detail.data;
  27. var filteredData = {};
  28. for (var key in data) {
  29. // exclude props the ContextMenuTrigger is expecting itself
  30. if (!ignoredTriggerProps.includes(key)) {
  31. filteredData[key] = data[key];
  32. }
  33. }
  34. _this.setState({ trigger: filteredData });
  35. };
  36. _this.handleHide = function () {
  37. _this.setState({ trigger: null });
  38. };
  39. _this.state = { trigger: null };
  40. return _this;
  41. }
  42. _createClass(ConnectMenu, [{
  43. key: 'componentDidMount',
  44. value: function componentDidMount() {
  45. this.listenId = listener.register(this.handleShow, this.handleHide);
  46. }
  47. }, {
  48. key: 'componentWillUnmount',
  49. value: function componentWillUnmount() {
  50. if (this.listenId) {
  51. listener.unregister(this.listenId);
  52. }
  53. }
  54. }, {
  55. key: 'render',
  56. value: function render() {
  57. return React.createElement(Child, _extends({}, this.props, { id: menuId, trigger: this.state.trigger }));
  58. }
  59. }]);
  60. return ConnectMenu;
  61. }(Component);
  62. };
  63. }