@txdfe/at
Version:
一个设计体系组件库
278 lines (216 loc) • 12.4 kB
JavaScript
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 = ["id", "type", "className", "align", "style", "prefix", "trigger", "children", "popupContainer", "popupProps", "popupClassName", "popupStyle", "followTrigger", "triggerType", "autoFocus", "rtl"];
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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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 from 'react';
import PropTypes from 'prop-types';
import Overlay from '../overlay';
import BalloonInner from './inner';
import { edgeMap as alignMap } from './alignMap';
import getDisabledCompatibleTrigger from './util';
var Popup = Overlay.Popup;
/** Balloon.Tooltip */
var Tooltip = /*#__PURE__*/function (_React$Component) {
_inherits(Tooltip, _React$Component);
var _super = _createSuper(Tooltip);
function Tooltip(props) {
var _this;
_classCallCheck(this, Tooltip);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "getSimpleAlign", function (expectedAlign, rtl) {
var simpleKeys = Object.keys(alignMap);
if (rtl) {
return simpleKeys.find(function (k) {
return alignMap[k].rtlAlign === expectedAlign;
});
}
return simpleKeys.find(function (k) {
return alignMap[k].align === expectedAlign;
});
});
_defineProperty(_assertThisInitialized(_this), "onPosition", function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (_this.state.expectedAlign !== args[0].align.join(' ')) {
var _this$props;
var expectedAlign = args[0].align.join(' ');
_this.setState({
expectedAlign: expectedAlign
});
_this.props.onPosition && (_this$props = _this.props).onPosition.apply(_this$props, args);
}
});
var _expectedAlign = alignMap[props.align][props.rlt ? 'rtlAlign' : 'align'];
_this.state = {
expectedAlign: _expectedAlign
};
return _this;
} // 通过overylay的align可选值、alignMap、dir获取toolotip align的简写值
_createClass(Tooltip, [{
key: "render",
value: function render() {
var _this$props2 = this.props,
id = _this$props2.id,
type = _this$props2.type,
className = _this$props2.className,
align = _this$props2.align,
style = _this$props2.style,
prefix = _this$props2.prefix,
trigger = _this$props2.trigger,
children = _this$props2.children,
popupContainer = _this$props2.popupContainer,
popupProps = _this$props2.popupProps,
popupClassName = _this$props2.popupClassName,
popupStyle = _this$props2.popupStyle,
followTrigger = _this$props2.followTrigger,
triggerType = _this$props2.triggerType,
autoFocus = _this$props2.autoFocus,
rtl = _this$props2.rtl,
others = _objectWithoutProperties(_this$props2, _excluded);
var trOrigin = 'trOrigin';
if (rtl) {
others.rtl = true;
trOrigin = 'rtlTrOrigin';
} // 开启对齐方式的自适应调整
var simpleAlign = this.getSimpleAlign(this.state.expectedAlign, rtl);
var transformOrigin = alignMap[simpleAlign][trOrigin];
var _offset = alignMap[simpleAlign].offset;
var _style = _objectSpread({
transformOrigin: transformOrigin
}, style);
var content = /*#__PURE__*/React.createElement(BalloonInner, _extends({}, others, {
id: id,
prefix: prefix,
closable: false,
type: type,
isTooltip: true,
className: className,
style: _style,
align: simpleAlign,
rtl: rtl
}), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefix, "balloon-tooltip-content")
}, children));
var triggerProps = {};
triggerProps['aria-describedby'] = id;
triggerProps.tabIndex = '0';
var newTriggerType = triggerType;
if (triggerType === 'hover' && id) {
newTriggerType = ['focus', 'hover'];
}
var ariaTrigger = id ? /*#__PURE__*/React.cloneElement(trigger, triggerProps) : trigger;
var newTrigger = getDisabledCompatibleTrigger( /*#__PURE__*/React.isValidElement(ariaTrigger) ? ariaTrigger : /*#__PURE__*/React.createElement("span", null, ariaTrigger)); // 不能采用有transform: scale相关的动画,因node.offsetWidth获取的值不精确,
// 是四舍五入的,而node.getBoundingClientRect()获取的值虽然精确,但是值是视觉大小的
// 受scale影响
return /*#__PURE__*/React.createElement(Popup, _extends({
role: "tooltip",
container: popupContainer,
followTrigger: followTrigger,
trigger: newTrigger,
triggerType: newTriggerType,
align: this.state.expectedAlign,
offset: _offset,
delay: 300,
className: popupClassName,
style: popupStyle,
rtl: rtl,
autoFocus: triggerType === 'focus' ? false : autoFocus,
shouldUpdatePosition: true // needAdjust
}, popupProps, {
onPosition: this.onPosition,
animation: {
"in": 'fadeIn',
out: 'fadeOut'
}
}), content);
}
}]);
return Tooltip;
}(React.Component);
_defineProperty(Tooltip, "propTypes", {
/**
* 样式类名的品牌前缀
*/
prefix: PropTypes.string,
/**
* 自定义类名
*/
className: PropTypes.string,
/**
* 自定义内联样式
*/
style: PropTypes.object,
/**
* 类型
*/
type: PropTypes.oneOf(['normal', 'primary']),
/**
* tooltip的内容
*/
children: PropTypes.any,
/**
* 弹出层位置
* @enumdesc 上, 右, 下, 左, 上左, 上右, 下左, 下右, 左上, 左下, 右上, 右下 及其 两两组合
*/
align: PropTypes.oneOf(['t', 'r', 'b', 'l', 'tl', 'tr', 'bl', 'br', 'lt', 'lb', 'rt', 'rb']),
/**
* 触发元素
*/
trigger: PropTypes.any,
/**
* 触发行为
* 鼠标悬浮, 鼠标点击('hover', 'click')或者它们组成的数组,如 ['hover', 'click'], 强烈不建议使用'focus',若有复杂交互,推荐使用triggerType为click的Balloon组件
*/
triggerType: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
/**
* 弹层组件style,透传给Popup
*/
popupStyle: PropTypes.object,
/**
* 弹层组件className,透传给Popup
*/
popupClassName: PropTypes.string,
/**
* 弹层组件属性,透传给Popup
*/
popupProps: PropTypes.object,
/**
* 是否pure render
*/
pure: PropTypes.bool,
/**
* 指定浮层渲染的父节点, 可以为节点id的字符串,也可以返回节点的函数。
*/
popupContainer: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
/**
* 是否跟随滚动
*/
followTrigger: PropTypes.bool,
/**
* 弹层id, 传入值才会支持无障碍
*/
id: PropTypes.string
});
_defineProperty(Tooltip, "defaultProps", {
type: 'primary',
triggerType: 'hover',
prefix: 'next-',
align: 'b',
trigger: /*#__PURE__*/React.createElement("span", null)
});
export { Tooltip as default };