UNPKG

@txdfe/at

Version:

一个设计体系组件库

427 lines (340 loc) 17.4 kB
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } var _excluded = ["target", "safeNode", "followTrigger", "triggerType", "hasMask", "wrapperStyle"]; function _extends() { _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; }; return _extends.apply(this, arguments); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import React, { Component, Children } from 'react'; import { findDOMNode } from 'react-dom'; import PropTypes from 'prop-types'; import { func, KEYCODE } from '../util'; import Overlay from './overlay'; var noop = func.noop, makeChain = func.makeChain; /** * Overlay.Popup * @description 继承 Overlay 的 API,除非特别说明 * */ var Popup = /*#__PURE__*/function (_Component) { _inherits(Popup, _Component); var _super = _createSuper(Popup); function Popup(_props) { var _this; _classCallCheck(this, Popup); _this = _super.call(this, _props); _defineProperty(_assertThisInitialized(_this), "getOverlay", function () { return _this.overlay; }); _defineProperty(_assertThisInitialized(_this), "handleVisibleChange", function (visible, type, e) { if (!('visible' in _this.props)) { _this.setState({ visible: visible }); } _this.props.onVisibleChange(visible, type, e); }); _defineProperty(_assertThisInitialized(_this), "handleTriggerClick", function (e) { if (_this.state.visible && !_this.props.canCloseByTrigger) { return; } _this.handleVisibleChange(!_this.state.visible, 'fromTrigger', e); }); _defineProperty(_assertThisInitialized(_this), "handleTriggerKeyDown", function (e) { var triggerClickKeycode = _this.props.triggerClickKeycode; var keycodes = Array.isArray(triggerClickKeycode) ? triggerClickKeycode : [triggerClickKeycode]; if (keycodes.includes(e.keyCode)) { e.preventDefault(); _this.handleTriggerClick(e); } }); _defineProperty(_assertThisInitialized(_this), "handleTriggerMouseEnter", function (e) { _this._mouseNotFirstOnMask = false; if (_this._hideTimer) { clearTimeout(_this._hideTimer); _this._hideTimer = null; } if (_this._showTimer) { clearTimeout(_this._showTimer); _this._showTimer = null; } if (!_this.state.visible) { _this._showTimer = setTimeout(function () { _this.handleVisibleChange(true, 'fromTrigger', e); }, _this.props.delay); } }); _defineProperty(_assertThisInitialized(_this), "handleTriggerMouseLeave", function (e, type) { if (_this._showTimer) { clearTimeout(_this._showTimer); _this._showTimer = null; } if (_this.state.visible) { _this._hideTimer = setTimeout(function () { _this.handleVisibleChange(false, type || 'fromTrigger', e); }, _this.props.delay); } }); _defineProperty(_assertThisInitialized(_this), "handleTriggerFocus", function (e) { _this.handleVisibleChange(true, 'fromTrigger', e); }); _defineProperty(_assertThisInitialized(_this), "handleTriggerBlur", function (e) { if (!_this._isForwardContent) { _this.handleVisibleChange(false, 'fromTrigger', e); } _this._isForwardContent = false; }); _defineProperty(_assertThisInitialized(_this), "handleContentMouseDown", function () { _this._isForwardContent = true; }); _defineProperty(_assertThisInitialized(_this), "handleContentMouseEnter", function () { clearTimeout(_this._hideTimer); }); _defineProperty(_assertThisInitialized(_this), "handleContentMouseLeave", function (e) { _this.handleTriggerMouseLeave(e, 'fromContent'); }); _defineProperty(_assertThisInitialized(_this), "handleMaskMouseEnter", function () { if (!_this._mouseNotFirstOnMask) { clearTimeout(_this._hideTimer); _this._hideTimer = null; _this._mouseNotFirstOnMask = false; } }); _defineProperty(_assertThisInitialized(_this), "handleMaskMouseLeave", function () { _this._mouseNotFirstOnMask = true; }); _defineProperty(_assertThisInitialized(_this), "handleRequestClose", function (type, e) { _this.handleVisibleChange(false, type, e); }); _defineProperty(_assertThisInitialized(_this), "renderTrigger", function () { var _this$props = _this.props, trigger = _this$props.trigger, disabled = _this$props.disabled; var props = { key: 'trigger', 'aria-haspopup': true, 'aria-expanded': _this.state.visible }; if (!_this.state.visible) { props['aria-describedby'] = undefined; } if (!disabled) { var triggerType = _this.props.triggerType; var triggerTypes = Array.isArray(triggerType) ? triggerType : [triggerType]; var _trigger$props = trigger.props, onClick = _trigger$props.onClick, onKeyDown = _trigger$props.onKeyDown, onMouseEnter = _trigger$props.onMouseEnter, onMouseLeave = _trigger$props.onMouseLeave, onFocus = _trigger$props.onFocus, onBlur = _trigger$props.onBlur; triggerTypes.forEach(function (triggerType) { switch (triggerType) { case 'click': props.onClick = makeChain(_this.handleTriggerClick, onClick); props.onKeyDown = makeChain(_this.handleTriggerKeyDown, onKeyDown); break; case 'hover': props.onMouseEnter = makeChain(_this.handleTriggerMouseEnter, onMouseEnter); props.onMouseLeave = makeChain(_this.handleTriggerMouseLeave, onMouseLeave); break; case 'focus': props.onFocus = makeChain(_this.handleTriggerFocus, onFocus); props.onBlur = makeChain(_this.handleTriggerBlur, onBlur); break; default: break; } }); } return /*#__PURE__*/React.cloneElement(trigger, props); }); _defineProperty(_assertThisInitialized(_this), "renderContent", function () { var _this$props2 = _this.props, children = _this$props2.children, triggerType = _this$props2.triggerType; var triggerTypes = Array.isArray(triggerType) ? triggerType : [triggerType]; var content = Children.only(children); var _content$props = content.props, onMouseDown = _content$props.onMouseDown, onMouseEnter = _content$props.onMouseEnter, onMouseLeave = _content$props.onMouseLeave; var props = { key: 'portal' }; triggerTypes.forEach(function (triggerType) { switch (triggerType) { case 'focus': props.onMouseDown = makeChain(_this.handleContentMouseDown, onMouseDown); break; case 'hover': props.onMouseEnter = makeChain(_this.handleContentMouseEnter, onMouseEnter); props.onMouseLeave = makeChain(_this.handleContentMouseLeave, onMouseLeave); break; default: break; } }); return /*#__PURE__*/React.cloneElement(content, props); }); _defineProperty(_assertThisInitialized(_this), "renderPortal", function () { var _this$props3 = _this.props, target = _this$props3.target, safeNode = _this$props3.safeNode, followTrigger = _this$props3.followTrigger, triggerType = _this$props3.triggerType, hasMask = _this$props3.hasMask, wrapperStyle = _this$props3.wrapperStyle, others = _objectWithoutProperties(_this$props3, _excluded); var container = _this.props.container; var findTriggerNode = function findTriggerNode() { return findDOMNode(_assertThisInitialized(_this)) || {}; }; var safeNodes = Array.isArray(safeNode) ? _toConsumableArray(safeNode) : [safeNode]; safeNodes.unshift(findTriggerNode); var newWrapperStyle = wrapperStyle || {}; if (followTrigger) { container = function container(trigger) { return trigger && trigger.parentNode || trigger; }; newWrapperStyle.position = 'relative'; } if (triggerType === 'hover' && hasMask) { others.onMaskMouseEnter = _this.handleMaskMouseEnter; others.onMaskMouseLeave = _this.handleMaskMouseLeave; } return /*#__PURE__*/React.createElement(Overlay, _extends({}, others, { key: "overlay", ref: function ref(overlay) { _this.overlay = overlay; }, visible: _this.state.visible, target: target || findTriggerNode, container: container, safeNode: safeNodes, wrapperStyle: newWrapperStyle, triggerType: triggerType, hasMask: hasMask, onRequestClose: _this.handleRequestClose }), _this.renderContent()); }); _this.state = { visible: typeof _props.visible === 'undefined' ? _props.defaultVisible : _props.visible }; _this.overlay = null; return _this; } _createClass(Popup, [{ key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { if ('visible' in nextProps) { this.setState({ visible: nextProps.visible }); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var _this2 = this; ['_timer', '_hideTimer', '_showTimer'].forEach(function (time) { _this2[time] && clearTimeout(_this2[time]); }); } // 获取overlay实例 }, { key: "render", value: function render() { return [this.renderTrigger(), this.renderPortal()]; } }]); return Popup; }(Component); _defineProperty(Popup, "propTypes", { /** * 弹层内容 */ children: PropTypes.node, /** * 触发弹层显示或隐藏的元素 */ trigger: PropTypes.element, /** * 触发弹层显示或隐藏的操作类型,可以是 'click','hover','focus',或者它们组成的数组,如 ['hover', 'focus'] */ triggerType: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), /** * 当 triggerType 为 click 时才生效,可自定义触发弹层显示的键盘码 */ triggerClickKeycode: PropTypes.oneOfType([PropTypes.number, PropTypes.array]), /** * 弹层当前是否显示 */ visible: PropTypes.bool, /** * 弹层默认是否显示 */ defaultVisible: PropTypes.bool, /** * 弹层显示或隐藏时触发的回调函数 * @param {Boolean} visible 弹层是否显示 * @param {String} type 触发弹层显示或隐藏的来源 fromTrigger 表示由trigger的点击触发; docClick 表示由document的点击触发 * @param {Object} e DOM事件 */ onVisibleChange: PropTypes.func, /** * 设置此属性,弹层无法显示或隐藏 */ disabled: PropTypes.bool, /** * 弹层显示或隐藏的延时时间(以毫秒为单位),在 triggerType 被设置为 hover 时生效 */ delay: PropTypes.number, /** * trigger 是否可以关闭弹层 */ canCloseByTrigger: PropTypes.bool, /** * 弹层定位的参照元素 * @default target 属性,即触发元素 */ target: PropTypes.any, safeNode: PropTypes.any, /** * 是否跟随trigger滚动 */ followTrigger: PropTypes.bool, container: PropTypes.any, hasMask: PropTypes.bool, wrapperStyle: PropTypes.object, rtl: PropTypes.bool }); _defineProperty(Popup, "defaultProps", { triggerType: 'hover', triggerClickKeycode: [KEYCODE.SPACE, KEYCODE.ENTER], defaultVisible: false, onVisibleChange: noop, disabled: false, delay: 200, canCloseByTrigger: true, followTrigger: false, container: function container() { return document.body; }, rtl: false }); export { Popup as default };