@txdfe/at
Version:
一个设计体系组件库
351 lines (348 loc) • 17.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _util = require("../util");
var _overlay = _interopRequireDefault(require("./overlay"));
var _excluded = ["target", "safeNode", "followTrigger", "triggerType", "hasMask", "wrapperStyle"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var noop = _util.func.noop,
makeChain = _util.func.makeChain;
/**
* Overlay.Popup
* @description 继承 Overlay 的 API,除非特别说明
* */
var Popup = exports["default"] = /*#__PURE__*/function (_Component) {
function Popup(_props) {
var _this;
_classCallCheck(this, Popup);
_this = _callSuper(this, Popup, [_props]);
// 获取overlay实例
_defineProperty(_this, "getOverlay", function () {
return _this.overlay;
});
_defineProperty(_this, "handleVisibleChange", function (visible, type, e) {
if (!('visible' in _this.props)) {
_this.setState({
visible: visible
});
}
_this.props.onVisibleChange(visible, type, e);
});
_defineProperty(_this, "handleTriggerClick", function (e) {
if (_this.state.visible && !_this.props.canCloseByTrigger) {
return;
}
_this.handleVisibleChange(!_this.state.visible, 'fromTrigger', e);
});
_defineProperty(_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(_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(_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(_this, "handleTriggerFocus", function (e) {
_this.handleVisibleChange(true, 'fromTrigger', e);
});
_defineProperty(_this, "handleTriggerBlur", function (e) {
if (!_this._isForwardContent) {
_this.handleVisibleChange(false, 'fromTrigger', e);
}
_this._isForwardContent = false;
});
_defineProperty(_this, "handleContentMouseDown", function () {
_this._isForwardContent = true;
});
_defineProperty(_this, "handleContentMouseEnter", function () {
clearTimeout(_this._hideTimer);
});
_defineProperty(_this, "handleContentMouseLeave", function (e) {
_this.handleTriggerMouseLeave(e, 'fromContent');
});
_defineProperty(_this, "handleMaskMouseEnter", function () {
if (!_this._mouseNotFirstOnMask) {
clearTimeout(_this._hideTimer);
_this._hideTimer = null;
_this._mouseNotFirstOnMask = false;
}
});
_defineProperty(_this, "handleMaskMouseLeave", function () {
_this._mouseNotFirstOnMask = true;
});
_defineProperty(_this, "handleRequestClose", function (type, e) {
_this.handleVisibleChange(false, type, e);
});
_defineProperty(_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["default"].cloneElement(trigger, props);
});
_defineProperty(_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 = _react.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["default"].cloneElement(content, props);
});
_defineProperty(_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 (0, _reactDom.findDOMNode)(_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["default"].createElement(_overlay["default"], _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;
}
_inherits(Popup, _Component);
return _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]);
});
}
}, {
key: "render",
value: function render() {
return [this.renderTrigger(), this.renderPortal()];
}
}]);
}(_react.Component);
_defineProperty(Popup, "propTypes", {
/**
* 弹层内容
*/
children: _propTypes["default"].node,
/**
* 触发弹层显示或隐藏的元素
*/
trigger: _propTypes["default"].element,
/**
* 触发弹层显示或隐藏的操作类型,可以是 'click','hover','focus',或者它们组成的数组,如 ['hover', 'focus']
*/
triggerType: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].array]),
/**
* 当 triggerType 为 click 时才生效,可自定义触发弹层显示的键盘码
*/
triggerClickKeycode: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].array]),
/**
* 弹层当前是否显示
*/
visible: _propTypes["default"].bool,
/**
* 弹层默认是否显示
*/
defaultVisible: _propTypes["default"].bool,
/**
* 弹层显示或隐藏时触发的回调函数
* @param {Boolean} visible 弹层是否显示
* @param {String} type 触发弹层显示或隐藏的来源 fromTrigger 表示由trigger的点击触发; docClick 表示由document的点击触发
* @param {Object} e DOM事件
*/
onVisibleChange: _propTypes["default"].func,
/**
* 设置此属性,弹层无法显示或隐藏
*/
disabled: _propTypes["default"].bool,
/**
* 弹层显示或隐藏的延时时间(以毫秒为单位),在 triggerType 被设置为 hover 时生效
*/
delay: _propTypes["default"].number,
/**
* trigger 是否可以关闭弹层
*/
canCloseByTrigger: _propTypes["default"].bool,
/**
* 弹层定位的参照元素
* @default target 属性,即触发元素
*/
target: _propTypes["default"].any,
safeNode: _propTypes["default"].any,
/**
* 是否跟随trigger滚动
*/
followTrigger: _propTypes["default"].bool,
container: _propTypes["default"].any,
hasMask: _propTypes["default"].bool,
wrapperStyle: _propTypes["default"].object,
rtl: _propTypes["default"].bool
});
_defineProperty(Popup, "defaultProps", {
triggerType: 'hover',
triggerClickKeycode: [_util.KEYCODE.SPACE, _util.KEYCODE.ENTER],
defaultVisible: false,
onVisibleChange: noop,
disabled: false,
delay: 200,
canCloseByTrigger: true,
followTrigger: false,
container: function container() {
return document.body;
},
rtl: false
});