UNPKG

antd

Version:

An enterprise-class UI design language and React components implementation

591 lines (477 loc) 21.3 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof3 = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper")); var React = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray")); var _copyToClipboard = _interopRequireDefault(require("copy-to-clipboard")); var _omit = _interopRequireDefault(require("rc-util/lib/omit")); var _EditOutlined = _interopRequireDefault(require("@ant-design/icons/EditOutlined")); var _CheckOutlined = _interopRequireDefault(require("@ant-design/icons/CheckOutlined")); var _CopyOutlined = _interopRequireDefault(require("@ant-design/icons/CopyOutlined")); var _rcResizeObserver = _interopRequireDefault(require("rc-resize-observer")); var _configProvider = require("../config-provider"); var _LocaleReceiver = _interopRequireDefault(require("../locale-provider/LocaleReceiver")); var _devWarning = _interopRequireDefault(require("../_util/devWarning")); var _transButton = _interopRequireDefault(require("../_util/transButton")); var _raf = _interopRequireDefault(require("../_util/raf")); var _styleChecker = require("../_util/styleChecker"); var _tooltip = _interopRequireDefault(require("../tooltip")); var _Typography = _interopRequireDefault(require("./Typography")); var _Editable = _interopRequireDefault(require("./Editable")); var _util = _interopRequireDefault(require("./util")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof3(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var __rest = void 0 && (void 0).__rest || function (s, e) { var t = {}; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; } if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var isLineClampSupport = (0, _styleChecker.isStyleSupport)('webkitLineClamp'); var isTextOverflowSupport = (0, _styleChecker.isStyleSupport)('textOverflow'); function wrapperDecorations(_ref, content) { var mark = _ref.mark, code = _ref.code, underline = _ref.underline, del = _ref["delete"], strong = _ref.strong, keyboard = _ref.keyboard, italic = _ref.italic; var currentContent = content; function wrap(needed, tag) { if (!needed) return; currentContent = /*#__PURE__*/React.createElement(tag, {}, currentContent); } wrap(strong, 'strong'); wrap(underline, 'u'); wrap(del, 'del'); wrap(code, 'code'); wrap(mark, 'mark'); wrap(keyboard, 'kbd'); wrap(italic, 'i'); return currentContent; } function getNode(dom, defaultNode, needDom) { if (dom === true || dom === undefined) { return defaultNode; } return dom || needDom && defaultNode; } var ELLIPSIS_STR = '...'; var Base = /*#__PURE__*/function (_React$Component) { (0, _inherits2["default"])(Base, _React$Component); var _super = (0, _createSuper2["default"])(Base); function Base() { var _this; (0, _classCallCheck2["default"])(this, Base); _this = _super.apply(this, arguments); _this.contentRef = /*#__PURE__*/React.createRef(); _this.state = { edit: false, copied: false, ellipsisText: '', ellipsisContent: null, isEllipsis: false, expanded: false, clientRendered: false }; _this.getPrefixCls = function () { var customizePrefixCls = _this.props.prefixCls; var getPrefixCls = _this.context.getPrefixCls; return getPrefixCls('typography', customizePrefixCls); }; // =============== Expand =============== _this.onExpandClick = function (e) { var _a; var _this$getEllipsis = _this.getEllipsis(), onExpand = _this$getEllipsis.onExpand; _this.setState({ expanded: true }); (_a = onExpand) === null || _a === void 0 ? void 0 : _a(e); }; // ================ Edit ================ _this.onEditClick = function (e) { e.preventDefault(); _this.triggerEdit(true); }; _this.onEditChange = function (value) { var _this$getEditable = _this.getEditable(), onChange = _this$getEditable.onChange; onChange === null || onChange === void 0 ? void 0 : onChange(value); _this.triggerEdit(false); }; _this.onEditCancel = function () { var _a, _b; (_b = (_a = _this.getEditable()).onCancel) === null || _b === void 0 ? void 0 : _b.call(_a); _this.triggerEdit(false); }; // ================ Copy ================ _this.onCopyClick = function (e) { e.preventDefault(); var _this$props = _this.props, children = _this$props.children, copyable = _this$props.copyable; var copyConfig = (0, _extends2["default"])({}, (0, _typeof2["default"])(copyable) === 'object' ? copyable : null); if (copyConfig.text === undefined) { copyConfig.text = String(children); } (0, _copyToClipboard["default"])(copyConfig.text || ''); _this.setState({ copied: true }, function () { if (copyConfig.onCopy) { copyConfig.onCopy(); } _this.copyId = window.setTimeout(function () { _this.setState({ copied: false }); }, 3000); }); }; _this.setEditRef = function (node) { _this.editIcon = node; }; _this.triggerEdit = function (edit) { var _this$getEditable2 = _this.getEditable(), onStart = _this$getEditable2.onStart; if (edit && onStart) { onStart(); } _this.setState({ edit: edit }, function () { if (!edit && _this.editIcon) { _this.editIcon.focus(); } }); }; // ============== Ellipsis ============== _this.resizeOnNextFrame = function () { _raf["default"].cancel(_this.rafId); _this.rafId = (0, _raf["default"])(function () { // Do not bind `syncEllipsis`. It need for test usage on prototype _this.syncEllipsis(); }); }; return _this; } (0, _createClass2["default"])(Base, [{ key: "componentDidMount", value: function componentDidMount() { this.setState({ clientRendered: true }); this.resizeOnNextFrame(); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var children = this.props.children; var ellipsis = this.getEllipsis(); var prevEllipsis = this.getEllipsis(prevProps); if (children !== prevProps.children || ellipsis.rows !== prevEllipsis.rows) { this.resizeOnNextFrame(); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.clearTimeout(this.copyId); _raf["default"].cancel(this.rafId); } }, { key: "getEditable", value: function getEditable(props) { var edit = this.state.edit; var _ref2 = props || this.props, editable = _ref2.editable; if (!editable) return { editing: edit }; return (0, _extends2["default"])({ editing: edit }, (0, _typeof2["default"])(editable) === 'object' ? editable : null); } }, { key: "getEllipsis", value: function getEllipsis(props) { var _ref3 = props || this.props, ellipsis = _ref3.ellipsis; if (!ellipsis) return {}; return (0, _extends2["default"])({ rows: 1, expandable: false }, (0, _typeof2["default"])(ellipsis) === 'object' ? ellipsis : null); } }, { key: "canUseCSSEllipsis", value: function canUseCSSEllipsis() { var clientRendered = this.state.clientRendered; var _this$props2 = this.props, editable = _this$props2.editable, copyable = _this$props2.copyable; var _this$getEllipsis2 = this.getEllipsis(), rows = _this$getEllipsis2.rows, expandable = _this$getEllipsis2.expandable, suffix = _this$getEllipsis2.suffix, onEllipsis = _this$getEllipsis2.onEllipsis, tooltip = _this$getEllipsis2.tooltip; if (suffix || tooltip) return false; // Can't use css ellipsis since we need to provide the place for button if (editable || copyable || expandable || !clientRendered || onEllipsis) { return false; } if (rows === 1) { return isTextOverflowSupport; } return isLineClampSupport; } }, { key: "syncEllipsis", value: function syncEllipsis() { var _this$state = this.state, ellipsisText = _this$state.ellipsisText, isEllipsis = _this$state.isEllipsis, expanded = _this$state.expanded; var _this$getEllipsis3 = this.getEllipsis(), rows = _this$getEllipsis3.rows, suffix = _this$getEllipsis3.suffix, onEllipsis = _this$getEllipsis3.onEllipsis; var children = this.props.children; if (!rows || rows < 0 || !this.contentRef.current || expanded) return; // Do not measure if css already support ellipsis if (this.canUseCSSEllipsis()) return; (0, _devWarning["default"])((0, _toArray["default"])(children).every(function (child) { return typeof child === 'string'; }), 'Typography', '`ellipsis` should use string as children only.'); var _measure = (0, _util["default"])(this.contentRef.current, { rows: rows, suffix: suffix }, children, this.renderOperations(true), ELLIPSIS_STR), content = _measure.content, text = _measure.text, ellipsis = _measure.ellipsis; if (ellipsisText !== text || isEllipsis !== ellipsis) { this.setState({ ellipsisText: text, ellipsisContent: content, isEllipsis: ellipsis }); if (isEllipsis !== ellipsis && onEllipsis) { onEllipsis(ellipsis); } } } }, { key: "renderExpand", value: function renderExpand(forceRender) { var _this$getEllipsis4 = this.getEllipsis(), expandable = _this$getEllipsis4.expandable, symbol = _this$getEllipsis4.symbol; var _this$state2 = this.state, expanded = _this$state2.expanded, isEllipsis = _this$state2.isEllipsis; if (!expandable) return null; // force render expand icon for measure usage or it will cause dead loop if (!forceRender && (expanded || !isEllipsis)) return null; var expandContent; if (symbol) { expandContent = symbol; } else { expandContent = this.expandStr; } return /*#__PURE__*/React.createElement("a", { key: "expand", className: "".concat(this.getPrefixCls(), "-expand"), onClick: this.onExpandClick, "aria-label": this.expandStr }, expandContent); } }, { key: "renderEdit", value: function renderEdit() { var editable = this.props.editable; if (!editable) return; var icon = editable.icon, tooltip = editable.tooltip; var title = (0, _toArray["default"])(tooltip)[0] || this.editStr; var ariaLabel = typeof title === 'string' ? title : ''; return /*#__PURE__*/React.createElement(_tooltip["default"], { key: "edit", title: tooltip === false ? '' : title }, /*#__PURE__*/React.createElement(_transButton["default"], { ref: this.setEditRef, className: "".concat(this.getPrefixCls(), "-edit"), onClick: this.onEditClick, "aria-label": ariaLabel }, icon || /*#__PURE__*/React.createElement(_EditOutlined["default"], { role: "button" }))); } }, { key: "renderCopy", value: function renderCopy() { var copied = this.state.copied; var copyable = this.props.copyable; if (!copyable) return; var prefixCls = this.getPrefixCls(); var tooltips = copyable.tooltips, icon = copyable.icon; var tooltipNodes = Array.isArray(tooltips) ? tooltips : [tooltips]; var iconNodes = Array.isArray(icon) ? icon : [icon]; var title = copied ? getNode(tooltipNodes[1], this.copiedStr) : getNode(tooltipNodes[0], this.copyStr); var systemStr = copied ? this.copiedStr : this.copyStr; var ariaLabel = typeof title === 'string' ? title : systemStr; return /*#__PURE__*/React.createElement(_tooltip["default"], { key: "copy", title: title }, /*#__PURE__*/React.createElement(_transButton["default"], { className: (0, _classnames["default"])("".concat(prefixCls, "-copy"), copied && "".concat(prefixCls, "-copy-success")), onClick: this.onCopyClick, "aria-label": ariaLabel }, copied ? getNode(iconNodes[1], /*#__PURE__*/React.createElement(_CheckOutlined["default"], null), true) : getNode(iconNodes[0], /*#__PURE__*/React.createElement(_CopyOutlined["default"], null), true))); } }, { key: "renderEditInput", value: function renderEditInput() { var _this$props3 = this.props, children = _this$props3.children, className = _this$props3.className, style = _this$props3.style; var direction = this.context.direction; var _this$getEditable3 = this.getEditable(), maxLength = _this$getEditable3.maxLength, autoSize = _this$getEditable3.autoSize, onEnd = _this$getEditable3.onEnd; return /*#__PURE__*/React.createElement(_Editable["default"], { value: typeof children === 'string' ? children : '', onSave: this.onEditChange, onCancel: this.onEditCancel, onEnd: onEnd, prefixCls: this.getPrefixCls(), className: className, style: style, direction: direction, maxLength: maxLength, autoSize: autoSize }); } }, { key: "renderOperations", value: function renderOperations(forceRenderExpanded) { return [this.renderExpand(forceRenderExpanded), this.renderEdit(), this.renderCopy()].filter(function (node) { return node; }); } }, { key: "renderContent", value: function renderContent() { var _this2 = this; var _this$state3 = this.state, ellipsisContent = _this$state3.ellipsisContent, isEllipsis = _this$state3.isEllipsis, expanded = _this$state3.expanded; var _a = this.props, component = _a.component, children = _a.children, className = _a.className, type = _a.type, disabled = _a.disabled, style = _a.style, restProps = __rest(_a, ["component", "children", "className", "type", "disabled", "style"]); var direction = this.context.direction; var _this$getEllipsis5 = this.getEllipsis(), rows = _this$getEllipsis5.rows, suffix = _this$getEllipsis5.suffix, tooltip = _this$getEllipsis5.tooltip; var prefixCls = this.getPrefixCls(); var textProps = (0, _omit["default"])(restProps, ['prefixCls', 'editable', 'copyable', 'ellipsis', 'mark', 'code', 'delete', 'underline', 'strong', 'keyboard'].concat((0, _toConsumableArray2["default"])(_configProvider.configConsumerProps))); var cssEllipsis = this.canUseCSSEllipsis(); var cssTextOverflow = rows === 1 && cssEllipsis; var cssLineClamp = rows && rows > 1 && cssEllipsis; var textNode = children; // Only use js ellipsis when css ellipsis not support if (rows && isEllipsis && !expanded && !cssEllipsis) { var title = restProps.title; var restContent = title || ''; if (!title && (typeof children === 'string' || typeof children === 'number')) { restContent = String(children); } // show rest content as title on symbol restContent = restContent.slice(String(ellipsisContent || '').length); // We move full content to outer element to avoid repeat read the content by accessibility textNode = /*#__PURE__*/React.createElement(React.Fragment, null, ellipsisContent, /*#__PURE__*/React.createElement("span", { title: restContent, "aria-hidden": "true" }, ELLIPSIS_STR), suffix); // If provided tooltip, we need wrap with span to let Tooltip inject events if (tooltip) { textNode = /*#__PURE__*/React.createElement(_tooltip["default"], { title: tooltip === true ? children : tooltip }, /*#__PURE__*/React.createElement("span", null, textNode)); } } else { textNode = /*#__PURE__*/React.createElement(React.Fragment, null, children, suffix); } textNode = wrapperDecorations(this.props, textNode); return /*#__PURE__*/React.createElement(_LocaleReceiver["default"], { componentName: "Text" }, function (_ref4) { var _classNames; var edit = _ref4.edit, copyStr = _ref4.copy, copied = _ref4.copied, expand = _ref4.expand; _this2.editStr = edit; _this2.copyStr = copyStr; _this2.copiedStr = copied; _this2.expandStr = expand; return /*#__PURE__*/React.createElement(_rcResizeObserver["default"], { onResize: _this2.resizeOnNextFrame, disabled: cssEllipsis }, /*#__PURE__*/React.createElement(_Typography["default"], (0, _extends2["default"])({ className: (0, _classnames["default"])((_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-").concat(type), type), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-ellipsis"), rows), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-single-line"), rows === 1), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-ellipsis-single-line"), cssTextOverflow), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-ellipsis-multiple-line"), cssLineClamp), _classNames), className), style: (0, _extends2["default"])((0, _extends2["default"])({}, style), { WebkitLineClamp: cssLineClamp ? rows : undefined }), component: component, ref: _this2.contentRef, direction: direction }, textProps), textNode, _this2.renderOperations())); }); } }, { key: "render", value: function render() { var _this$getEditable4 = this.getEditable(), editing = _this$getEditable4.editing; if (editing) { return this.renderEditInput(); } return this.renderContent(); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(nextProps) { var children = nextProps.children, editable = nextProps.editable; (0, _devWarning["default"])(!editable || typeof children === 'string', 'Typography', 'When `editable` is enabled, the `children` should use string.'); return {}; } }]); return Base; }(React.Component); Base.contextType = _configProvider.ConfigContext; Base.defaultProps = { children: '' }; var _default = Base; exports["default"] = _default;