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

134 строки
4.9 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. 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; }; }();
  6. var _react = require('react');
  7. var _react2 = _interopRequireDefault(_react);
  8. var _reactDom = require('react-dom');
  9. var _reactDom2 = _interopRequireDefault(_reactDom);
  10. var _propTypes = require('prop-types');
  11. var _propTypes2 = _interopRequireDefault(_propTypes);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  14. 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; }
  15. 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; }
  16. var propTypes = {
  17. children: _propTypes2.default.node,
  18. onOutsideClick: _propTypes2.default.func
  19. };
  20. var defaultProps = {
  21. children: _react2.default.createElement('span', null),
  22. onOutsideClick: Function.prototype
  23. };
  24. var OutsideClickHandler = function (_React$PureComponent) {
  25. _inherits(OutsideClickHandler, _React$PureComponent);
  26. function OutsideClickHandler(props) {
  27. _classCallCheck(this, OutsideClickHandler);
  28. var _this = _possibleConstructorReturn(this, (OutsideClickHandler.__proto__ || Object.getPrototypeOf(OutsideClickHandler)).call(this, props));
  29. _this.hasAction = false;
  30. _this.onOutsideClick = _this.onOutsideClick.bind(_this);
  31. return _this;
  32. }
  33. _createClass(OutsideClickHandler, [{
  34. key: 'componentDidMount',
  35. value: function componentDidMount() {
  36. this.bindActions();
  37. }
  38. }, {
  39. key: 'componentDidUpdate',
  40. value: function componentDidUpdate() {
  41. this.bindActions();
  42. }
  43. }, {
  44. key: 'componentWillUnmount',
  45. value: function componentWillUnmount() {
  46. this.unbindActions();
  47. }
  48. }, {
  49. key: 'bindActions',
  50. value: function bindActions() {
  51. var closeOnOutsideClick = this.props.closeOnOutsideClick;
  52. if (closeOnOutsideClick) {
  53. if (this.hasAction) return;
  54. if (document.addEventListener) {
  55. document.addEventListener('mousedown', this.onOutsideClick, true);
  56. } else {
  57. document.attachEvent('onmousedown', this.onOutsideClick);
  58. }
  59. this.hasAction = true;
  60. }
  61. }
  62. }, {
  63. key: 'unbindActions',
  64. value: function unbindActions() {
  65. if (!this.hasAction) return;
  66. var closeOnOutsideClick = this.props.closeOnOutsideClick;
  67. if (closeOnOutsideClick) {
  68. if (document.removeEventListener) {
  69. document.removeEventListener('mousedown', this.onOutsideClick, true);
  70. } else {
  71. document.detachEvent('onmousedown', this.onOutsideClick);
  72. }
  73. this.hasAction = false;
  74. }
  75. }
  76. }, {
  77. key: 'onOutsideClick',
  78. value: function onOutsideClick(e) {
  79. var event = e || window.event;
  80. var mouseTarget = typeof event.which !== 'undefined' ? event.which : event.button;
  81. var isDescendantOfRoot = _reactDom2.default.findDOMNode(this.childNode).contains(event.target);
  82. if (!isDescendantOfRoot && mouseTarget === 1) {
  83. var onOutsideClick = this.props.onOutsideClick;
  84. onOutsideClick && onOutsideClick(event);
  85. }
  86. }
  87. }, {
  88. key: 'render',
  89. value: function render() {
  90. var _this2 = this;
  91. var focused = this.props.focused;
  92. var outsideClass = focused ? 'outside_container active' : 'outside_container';
  93. return _react2.default.createElement(
  94. 'div',
  95. { ref: function ref(c) {
  96. return _this2.childNode = c;
  97. }, className: outsideClass },
  98. this.props.children
  99. );
  100. }
  101. }]);
  102. return OutsideClickHandler;
  103. }(_react2.default.PureComponent);
  104. OutsideClickHandler.propTypes = propTypes;
  105. OutsideClickHandler.defaultProps = defaultProps;
  106. exports.default = OutsideClickHandler;