UNPKG

nsn-comp

Version:

NSN核心组件

182 lines (146 loc) 8.87 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } 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; } 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); 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 } }); 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; } 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 { Date.prototype.toString.call(Reflect.construct(Date, [], 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); } import classNames from 'classnames'; import { NConst } from 'nsn-const'; import React, { Component } from 'react'; import styles from './index.less'; import { EllipsisText, EllipsisTooltip } from './_comp'; import { computeLine } from './_util'; var CLS_PREFIX = "".concat(NConst.CLS_PREFIX, "-ellipsis"); var isSupportLineClamp = document.body.style.webkitLineClamp !== undefined; var Ellipsis = /*#__PURE__*/function (_Component) { _inherits(Ellipsis, _Component); var _super = _createSuper(Ellipsis); function Ellipsis() { var _this; _classCallCheck(this, Ellipsis); _this = _super.apply(this, arguments); _this.elements = { contentContainer: null, shadow: null, normal: null, content: null, toolTipChildren: null }; _this.state = { text: NConst.EMPTY, targetCount: 0 }; _this.resize = function () { return computeLine(_assertThisInitialized(_this), _this.elements); }; return _this; } _createClass(Ellipsis, [{ key: "componentDidMount", value: function componentDidMount() { var toolTipChildren = this.elements.toolTipChildren; if (toolTipChildren) { computeLine(this, this.elements); } window.addEventListener('resize', this.resize); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var lines = this.props.lines; if (lines !== prevProps.lines) { computeLine(this, this.elements); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener('resize', this.resize); } }, { key: "render", value: function render() { var _classNames, _this2 = this; var _this$state = this.state, text = _this$state.text, targetCount = _this$state.targetCount; var _this$props = this.props, children = _this$props.children, lines = _this$props.lines, length = _this$props.length, className = _this$props.className, tooltip = _this$props.tooltip, fullWidthRecognition = _this$props.fullWidthRecognition, _this$props$tail = _this$props.tail, tail = _this$props$tail === void 0 ? '...' : _this$props$tail, restProps = _objectWithoutProperties(_this$props, ["children", "lines", "length", "className", "tooltip", "fullWidthRecognition", "tail"]); var cls = classNames(styles.ellipsis, className, (_classNames = {}, _defineProperty(_classNames, styles.lines, lines && !isSupportLineClamp), _defineProperty(_classNames, styles.lineClamp, lines && isSupportLineClamp), _classNames)); // if (!lines && !length) { return React.createElement("span", Object.assign({ className: cls }, restProps), children); } // length if (!lines) { return React.createElement(EllipsisText, Object.assign({}, { tail: tail, length: length, tooltip: tooltip, fullWidthRecognition: fullWidthRecognition }, { text: "".concat(children || NConst.EMPTY), className: cls }, restProps)); } // if (isSupportLineClamp) { var id = "".concat(CLS_PREFIX, "-", "".concat(new Date().getTime()).concat(Math.floor(Math.random() * 100))); return React.createElement(EllipsisTooltip, Object.assign({}, { tooltip: tooltip }, { title: children }), React.createElement("div", Object.assign({ id: id, className: cls }, restProps), React.createElement("style", null, "#".concat(id, "{-webkit-line-clamp:").concat(lines, ";-webkit-box-orient: vertical;}")), children)); } return React.createElement("div", Object.assign({}, restProps, { ref: function ref(_ref) { _this2.elements.contentContainer = _ref; }, className: cls }), React.createElement("div", { ref: function ref(_ref2) { _this2.elements.content = _ref2; } }, React.createElement(EllipsisTooltip, Object.assign({}, { tooltip: tooltip }, { title: text }), React.createElement("span", { ref: function ref(_ref3) { _this2.elements.toolTipChildren = _ref3; } }, targetCount > 0 && text.substring(0, targetCount), targetCount > 0 && targetCount < text.length && tail)), React.createElement("div", { className: styles.shadow, ref: function ref(_ref4) { _this2.elements.normal = _ref4; } }, children), React.createElement("div", { className: styles.shadow, ref: function ref(_ref5) { _this2.elements.shadow = _ref5; } }, React.createElement("span", null, text)))); } }]); return Ellipsis; }(Component); export { Ellipsis };