@txdfe/at
Version:
一个设计体系组件库
197 lines (196 loc) • 8.97 kB
JavaScript
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 _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); }
import React, { Component, Children } from 'react';
import PropTypes from 'prop-types';
import Overlay from '../overlay';
import { func } from '../util';
var noop = func.noop,
makeChain = func.makeChain;
var Popup = Overlay.Popup;
/**
* Dropdown
* @description 继承 Popup 的 API,除非特别说明
*/
var Dropdown = /*#__PURE__*/function (_Component) {
function Dropdown(_props) {
var _this;
_classCallCheck(this, Dropdown);
_this = _callSuper(this, Dropdown, [_props]);
_defineProperty(_this, "open", function () {
_this.onVisibleChange(true, 'fromComponentMethod');
});
_defineProperty(_this, "close", function () {
_this.onVisibleChange(false, 'fromComponentMethod');
});
_defineProperty(_this, "toggle", function () {
if (_this.getVisible()) {
_this.close();
} else {
_this.open();
}
});
_defineProperty(_this, "getVisible", function () {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props;
return 'visible' in props ? props.visible : _this.state.visible;
});
// 实例方法,获取overlay实例
_defineProperty(_this, "getOverlay", function () {
try {
return _this.popup.getInstance().getOverlay();
} catch (err) {
return null;
}
});
_defineProperty(_this, "onMenuClick", function () {
_this.onVisibleChange(false, 'fromContent');
});
_defineProperty(_this, "onVisibleChange", function (visible, from) {
_this.setState({
visible: visible
});
_this.props.onVisibleChange(visible, from);
});
_defineProperty(_this, "onTriggerKeyDown", function () {
var autoFocus = true;
if ('autoFocus' in _this.props) {
autoFocus = _this.props.autoFocus;
}
_this.setState({
autoFocus: autoFocus
});
});
_this.state = {
visible: 'visible' in _props ? _props.visible : _props.defaultVisible || false,
autoFocus: 'autoFocus' in _props ? _props.autoFocus : false
};
_this.popup = null;
return _this;
}
_inherits(Dropdown, _Component);
return _createClass(Dropdown, [{
key: "render",
value: function render() {
var _this2 = this;
var child = Children.only(this.props.children);
if (typeof child.type === 'function' && child.type.isNextMenu) {
child = /*#__PURE__*/React.cloneElement(child, {
onItemClick: makeChain(this.onMenuClick, child.props.onItemClick)
});
}
var _this$props = this.props,
trigger = _this$props.trigger,
rtl = _this$props.rtl;
var newTrigger = /*#__PURE__*/React.cloneElement(trigger, {
onKeyDown: makeChain(this.onTriggerKeyDown, trigger.props.onKeyDown)
});
return /*#__PURE__*/React.createElement(Popup, _extends({
animation: {
"in": 'fadeInDown',
out: 'fadeOutUp'
}
}, this.props, {
ref: function ref(popup) {
_this2.popup = popup;
},
rtl: rtl,
autoFocus: this.state.autoFocus,
trigger: newTrigger,
visible: this.getVisible(),
onVisibleChange: this.onVisibleChange
}), child);
}
}]);
}(Component);
_defineProperty(Dropdown, "propTypes", {
prefix: PropTypes.string,
pure: PropTypes.bool,
rtl: PropTypes.bool,
className: PropTypes.string,
/**
* 弹层内容
*/
children: PropTypes.node,
/**
* 弹层当前是否显示
*/
visible: PropTypes.bool,
/**
* 弹层默认是否显示
*/
defaultVisible: PropTypes.bool,
/**
* 弹层显示或隐藏时触发的回调函数
* @param {Boolean} visible 弹层是否显示
* @param {String} type 触发弹层显示或隐藏的来源 fromContent 表示由Dropdown内容触发; fromTrigger 表示由trigger的点击触发; docClick 表示由document的点击触发
*/
onVisibleChange: PropTypes.func,
/**
* 触发弹层显示或者隐藏的元素
*/
trigger: PropTypes.node,
/**
* 触发弹层显示或隐藏的操作类型,可以是 'click','hover',或者它们组成的数组,如 ['hover', 'click']
*/
triggerType: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
/**
* 设置此属性,弹层无法显示或隐藏
*/
disabled: PropTypes.bool,
/**
* 弹层相对于触发元素的定位, 详见 Overlay 的定位部分
*/
align: PropTypes.string,
/**
* 弹层相对于trigger的定位的微调, 接收数组[hoz, ver], 表示弹层在 left / top 上的增量
* e.g. [100, 100] 表示往右(RTL 模式下是往左) 、下分布偏移100px
*/
offset: PropTypes.array,
/**
* 弹层显示或隐藏的延时时间(以毫秒为单位),在 triggerType 被设置为 hover 时生效
*/
delay: PropTypes.number,
/**
* 弹层打开时是否让其中的元素自动获取焦点
*/
autoFocus: PropTypes.bool,
/**
* 是否显示遮罩
*/
hasMask: PropTypes.bool,
/**
* 隐藏时是否保留子节点
*/
cache: PropTypes.bool,
/**
* 配置动画的播放方式,支持 { in: 'enter-class', out: 'leave-class' } 的对象参数,如果设置为 false,则不播放动画
* @default { in: 'expandInDown', out: 'expandOutUp' }
*/
animation: PropTypes.oneOfType([PropTypes.object, PropTypes.bool])
});
_defineProperty(Dropdown, "defaultProps", {
prefix: 'next-',
pure: false,
defaultVisible: false,
onVisibleChange: noop,
triggerType: 'hover',
disabled: false,
align: 'tl bl',
offset: [0, 4],
delay: 200,
hasMask: false,
cache: false
});
export { Dropdown as default };