UNPKG

react-cqtoolbox

Version:

[![Travis][build-badge]][build] [![npm package][npm-badge]][npm] [![Coveralls][coveralls-badge]][coveralls]

257 lines (210 loc) 8.42 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; 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; }; }(); var _class, _temp, _initialiseProps, _jsxFileName = '/Users/chenyutian0510/Documents/dev-js/chuangqi/react-cqtoolbox/components/trigger/Trigger.js'; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactDom = require('react-dom'); var _popup = require('../popup'); var _popup2 = _interopRequireDefault(_popup); var _pureRender = require('../decorator/pureRender.js'); var _pureRender2 = _interopRequireDefault(_pureRender); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } var Trigger = (_temp = _class = function (_Component) { _inherits(Trigger, _Component); function Trigger(props) { _classCallCheck(this, Trigger); var _this = _possibleConstructorReturn(this, (Trigger.__proto__ || Object.getPrototypeOf(Trigger)).call(this, props)); _initialiseProps.call(_this); var popupVisible = void 0; if ('popupVisible' in props) { popupVisible = _this.props.popupVisible; } else { popupVisible = false; } _this.state = { popupVisible: popupVisible }; return _this; } _createClass(Trigger, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if ('popupVisible' in nextProps && this.props.popupVisible !== nextProps.popupVisible) { this.setState({ popupVisible: nextProps.popupVisible }); } } }, { key: 'delaySetPopupVisible', value: function delaySetPopupVisible(popupVisible, delay) { var _this2 = this; this.clearDelayTimer(); if (delay) { this.delayTimer = setTimeout(function () { _this2.setPopupVisible(popupVisible); _this2.clearDelayTimer(); }, delay); } else { this.setPopupVisible(popupVisible); } } }, { key: 'clearDelayTimer', value: function clearDelayTimer() { if (this.delayTimer) { clearTimeout(this.delayTimer); this.delayTimer = null; } } }, { key: 'isClickAction', value: function isClickAction() { var action = this.props.action; return action.indexOf('click') !== -1; } }, { key: 'isHoverAction', value: function isHoverAction() { var action = this.props.action; return action.indexOf('hover') !== -1; } }, { key: 'render', value: function render() { var _this3 = this; var children = this.props.children; var child = _react2.default.Children.only(children); var newChildProps = { ref: "_rootComponent" }; if (this.isClickAction()) { newChildProps.onClick = this.onClick; } else { newChildProps.onClick = function (e) { return _this3.fireEvents('onClick', e); }; } if (this.isHoverAction()) { newChildProps.onMouseEnter = this.onMouseEnter; newChildProps.onMouseLeave = this.onMouseLeave; } else { newChildProps.onMouseEnter = function (e) { return _this3.fireEvents('onMouseEnter', e); }; newChildProps.onMouseLeave = function (e) { return _this3.fireEvents('onMouseLeave', e); }; } return (0, _react.cloneElement)(child, newChildProps); } }]); return Trigger; }(_react.Component), _class.propTypes = { children: _react.PropTypes.any, action: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.arrayOf(_react.PropTypes.string)]), popup: _react.PropTypes.oneOfType([_react.PropTypes.node, _react.PropTypes.func]).isRequired, mask: _react.PropTypes.bool, maskClosable: _react.PropTypes.bool, popupTheme: _react.PropTypes.object, popupAlign: _react.PropTypes.object, popupVisible: _react.PropTypes.bool, matchTargetWidth: _react.PropTypes.bool, onPopupVisibleChange: _react.PropTypes.func }, _class.defaultProps = { action: ['click'], mask: false, popupTheme: {}, maskClosable: false, popupAlign: { points: ['tl', 'bl'], // align top left point of sourceNode with top right point of targetNode offset: [0, 0] }, onPopupVisibleChange: function onPopupVisibleChange() { return void 0; } }, _initialiseProps = function _initialiseProps() { var _this4 = this; this.setPopupVisible = function (popupVisible) { if (_this4.state.popupVisible !== popupVisible) { if (!('popupVisible' in _this4.props)) { _this4.setState({ popupVisible: popupVisible }); } _this4.props.onPopupVisibleChange(popupVisible); } }; this.getRootDomNode = function () { return (0, _reactDom.findDOMNode)(_this4.refs._rootComponent); }; this.getPopupElement = function () { var _props = _this4.props, mask = _props.mask, popup = _props.popup, popupAlign = _props.popupAlign, popupTheme = _props.popupTheme, popupVisible = _props.popupVisible, matchTargetWidth = _props.matchTargetWidth; var popupProps = {}; if (_this4.isHoverAction()) { popupProps.onMouseEnter = _this4.onPopupMouseEnter; popupProps.onMouseLeave = _this4.onPopupMouseLeave; } return _react2.default.createElement( _popup2.default, _extends({ active: popupVisible, theme: popupTheme, mask: mask, align: popupAlign }, popupProps, { matchTargetWidth: matchTargetWidth, getRootDomNode: _this4.getRootDomNode, onRequestClose: _this4.setPopupVisible, __source: { fileName: _jsxFileName, lineNumber: 114 }, __self: _this4 }), typeof popup === 'function' ? popup() : popup ); }; this.fireEvents = function (type, e) { var childCallback = _this4.props.children.props[type]; if (childCallback) { childCallback(e); } var callback = _this4.props[type]; if (callback) { callback(e); } }; this.onClick = function (event) { event.preventDefault(); _this4.fireEvents('onClick', event); _this4.setPopupVisible(!_this4.props.popupVisible); }; this.onMouseEnter = function (event) { _this4.fireEvents('onMouseEnter', event); _this4.delaySetPopupVisible(true); }; this.onMouseLeave = function (event) { _this4.fireEvents('onMouseLeave', event); _this4.delaySetPopupVisible(false, 100); }; this.onPopupMouseEnter = function () { _this4.clearDelayTimer(); }; this.onPopupMouseLeave = function () { _this4.delaySetPopupVisible(false, 100); }; }, _temp); exports.default = (0, _pureRender2.default)(Trigger);