base.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. import React, { Component, CSSProperties } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import cls from 'classnames';
  4. import PropTypes from 'prop-types';
  5. import { cssClasses, strings } from '@douyinfe/semi-foundation/typography/constants';
  6. import Typography from './typography';
  7. import Copyable from './copyable';
  8. import { IconSize as Size } from '../icons/index';
  9. import { isUndefined, omit, merge, isString, isNull, isFunction } from 'lodash';
  10. import Tooltip from '../tooltip/index';
  11. import Popover from '../popover/index';
  12. import getRenderText from './util';
  13. import warning from '@douyinfe/semi-foundation/utils/warning';
  14. import isEnterPress from '@douyinfe/semi-foundation/utils/isEnterPress';
  15. import LocaleConsumer from '../locale/localeConsumer';
  16. import { Locale } from '../locale/interface';
  17. import { Ellipsis, EllipsisPos, ShowTooltip, TypographyBaseSize, TypographyBaseType } from './interface';
  18. import { CopyableConfig, LinkType } from './title';
  19. import { BaseProps } from '../_base/baseComponent';
  20. import { isSemiIcon } from '../_utils';
  21. import ResizeObserver from '../resizeObserver';
  22. export interface BaseTypographyProps extends BaseProps {
  23. copyable?: CopyableConfig | boolean;
  24. delete?: boolean;
  25. disabled?: boolean;
  26. icon?: React.ReactNode;
  27. /**
  28. * ellipsis 用于设置截断相关参数.
  29. * Ellipsis is used to set ellipsis related parameters.
  30. * ellipsis 仅支持纯文本的截断,不支持 reactNode 等复杂类型,请确保 children 传入内容类型为 string.
  31. * Ellipsis only supports ellipsis of plain text, and does not support complex types such as reactNode.
  32. * Please ensure that the content type of children is string.
  33. * Semi 截断有两种策略, CSS 截断和 JS 截断。
  34. * Semi ellipsis has two strategies, CSS ellipsis and JS ellipsis.
  35. * - 当设置中间截断(pos='middle')、可展开(expandable)、有后缀(suffix 非空)、可复制(copyable),启用 JS 截断策略
  36. * - When setting middle ellipsis (pos='middle')、expandable、suffix is not empty string、copyable,
  37. * the JS ellipsis strategy is enabled
  38. * - 非以上场景,启用 CSS 截断策略
  39. * - Otherwise, enable the CSS ellipsis strategy
  40. *
  41. * 通常来说 CSS 截断的性能优于 JS 截断。在 children 不变, 容器尺寸不变的情况下,CSS 截断只涉及 1-2 次计算,js 截断基于二分法,可能涉及多次计算。
  42. * In general CSS ellipsis performs better than JS ellipsis. when the children and container size remain unchanged,
  43. * CSS ellipsis only involves 1-2 calculations, while JS ellipsis is based on dichotomy and may require multiple calculations.
  44. * 同时使用大量带有截断功能的 Typography 需注意性能消耗,如在 Table 中,可通过设置合理的页容量进行分页减少性能损耗
  45. * Pay attention to performance consumption when using a large number of Typography with ellipsis. For example, in Table,
  46. * you can reduce performance loss by setting a reasonable pageSize for paging
  47. */
  48. ellipsis?: Ellipsis | boolean;
  49. mark?: boolean;
  50. underline?: boolean;
  51. link?: LinkType;
  52. strong?: boolean;
  53. type?: TypographyBaseType;
  54. size?: TypographyBaseSize;
  55. style?: React.CSSProperties;
  56. className?: string;
  57. code?: boolean;
  58. children?: React.ReactNode;
  59. component?: React.ElementType;
  60. spacing?: string;
  61. heading?: string;
  62. weight?: string | number
  63. }
  64. interface BaseTypographyState {
  65. editable: boolean;
  66. copied: boolean;
  67. isOverflowed: boolean;
  68. ellipsisContent: React.ReactNode;
  69. expanded: boolean;
  70. isTruncated: boolean;
  71. prevChildren: React.ReactNode
  72. }
  73. const prefixCls = cssClasses.PREFIX;
  74. const ELLIPSIS_STR = '...';
  75. const wrapperDecorations = (props: BaseTypographyProps, content: React.ReactNode) => {
  76. const { mark, code, underline, strong, link, disabled } = props;
  77. let wrapped = content;
  78. const wrap = (isNeeded: boolean | LinkType, tag: string) => {
  79. let wrapProps = {};
  80. if (!isNeeded) {
  81. return;
  82. }
  83. if (typeof isNeeded === 'object') {
  84. wrapProps = { ...isNeeded };
  85. }
  86. wrapped = React.createElement(tag, wrapProps, wrapped);
  87. };
  88. wrap(mark, 'mark');
  89. wrap(code, 'code');
  90. wrap(underline && !link, 'u');
  91. wrap(strong, 'strong');
  92. wrap(props.delete, 'del');
  93. wrap(link, disabled ? 'span' : 'a');
  94. return wrapped;
  95. };
  96. export default class Base extends Component<BaseTypographyProps, BaseTypographyState> {
  97. static propTypes = {
  98. children: PropTypes.node,
  99. copyable: PropTypes.oneOfType([
  100. PropTypes.shape({
  101. text: PropTypes.string,
  102. onCopy: PropTypes.func,
  103. successTip: PropTypes.node,
  104. copyTip: PropTypes.node,
  105. }),
  106. PropTypes.bool,
  107. ]),
  108. delete: PropTypes.bool,
  109. disabled: PropTypes.bool,
  110. // editable: PropTypes.bool,
  111. ellipsis: PropTypes.oneOfType([
  112. PropTypes.shape({
  113. rows: PropTypes.number,
  114. expandable: PropTypes.bool,
  115. expandText: PropTypes.string,
  116. onExpand: PropTypes.func,
  117. suffix: PropTypes.string,
  118. showTooltip: PropTypes.oneOfType([
  119. PropTypes.shape({
  120. type: PropTypes.string,
  121. opts: PropTypes.object,
  122. }),
  123. PropTypes.bool,
  124. ]),
  125. collapsible: PropTypes.bool,
  126. collapseText: PropTypes.string,
  127. pos: PropTypes.oneOf(['end', 'middle']),
  128. }),
  129. PropTypes.bool,
  130. ]),
  131. mark: PropTypes.bool,
  132. underline: PropTypes.bool,
  133. link: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  134. spacing: PropTypes.oneOf(strings.SPACING),
  135. strong: PropTypes.bool,
  136. size: PropTypes.oneOf(strings.SIZE),
  137. type: PropTypes.oneOf(strings.TYPE),
  138. style: PropTypes.object,
  139. className: PropTypes.string,
  140. icon: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
  141. heading: PropTypes.string,
  142. component: PropTypes.string,
  143. };
  144. static defaultProps = {
  145. children: null as React.ReactNode,
  146. copyable: false,
  147. delete: false,
  148. disabled: false,
  149. // editable: false,
  150. ellipsis: false,
  151. icon: '',
  152. mark: false,
  153. underline: false,
  154. strong: false,
  155. link: false,
  156. type: 'primary',
  157. spacing: 'normal',
  158. size: 'normal',
  159. style: {},
  160. className: '',
  161. };
  162. wrapperRef: React.RefObject<any>;
  163. expandRef: React.RefObject<any>;
  164. copyRef: React.RefObject<any>;
  165. rafId: ReturnType<typeof requestAnimationFrame>;
  166. expandStr: string;
  167. collapseStr: string;
  168. constructor(props: BaseTypographyProps) {
  169. super(props);
  170. this.state = {
  171. editable: false,
  172. copied: false,
  173. // ellipsis
  174. // if text is overflow in container
  175. isOverflowed: true,
  176. ellipsisContent: props.children,
  177. expanded: false,
  178. // if text is truncated with js
  179. isTruncated: true,
  180. prevChildren: null,
  181. };
  182. this.wrapperRef = React.createRef();
  183. this.expandRef = React.createRef();
  184. this.copyRef = React.createRef();
  185. }
  186. componentDidMount() {
  187. if (this.props.ellipsis) {
  188. this.onResize();
  189. }
  190. }
  191. static getDerivedStateFromProps(props: BaseTypographyProps, prevState: BaseTypographyState) {
  192. const { prevChildren } = prevState;
  193. const newState: Partial<BaseTypographyState> = {};
  194. newState.prevChildren = props.children;
  195. if (props.ellipsis && prevChildren !== props.children) {
  196. // reset ellipsis state if children update
  197. newState.isOverflowed = true;
  198. newState.ellipsisContent = props.children;
  199. newState.expanded = false;
  200. newState.isTruncated = true;
  201. }
  202. return newState;
  203. }
  204. componentDidUpdate(prevProps: BaseTypographyProps) {
  205. // Render was based on outdated refs and needs to be rerun
  206. if (this.props.children !== prevProps.children) {
  207. this.forceUpdate();
  208. if (this.props.ellipsis) {
  209. this.onResize();
  210. }
  211. }
  212. }
  213. componentWillUnmount() {
  214. if (this.rafId) {
  215. window.cancelAnimationFrame(this.rafId);
  216. }
  217. }
  218. onResize = () => {
  219. if (this.rafId) {
  220. window.cancelAnimationFrame(this.rafId);
  221. }
  222. this.rafId = window.requestAnimationFrame(this.getEllipsisState.bind(this));
  223. };
  224. // if it needs to use js overflowed:
  225. // 1. text is expandable 2. expandText need to be shown 3. has extra operation 4. text need to ellipse from mid
  226. canUseCSSEllipsis = () => {
  227. const { copyable } = this.props;
  228. const { expandable, expandText, pos, suffix } = this.getEllipsisOpt();
  229. return !expandable && isUndefined(expandText) && !copyable && pos === 'end' && !suffix.length;
  230. };
  231. /**
  232. * whether truncated
  233. * rows < = 1 if there is overflow content, return true
  234. * rows > 1 if there is overflow height, return true
  235. * @param {Number} rows
  236. * @returns {Boolean}
  237. */
  238. shouldTruncated = (rows: number) => {
  239. if (!rows || rows < 1) {
  240. return false;
  241. }
  242. const updateOverflow =
  243. rows <= 1 ?
  244. this.wrapperRef.current.scrollWidth > this.wrapperRef.current.offsetWidth :
  245. this.wrapperRef.current.scrollHeight > this.wrapperRef.current.offsetHeight;
  246. return updateOverflow;
  247. };
  248. showTooltip = () => {
  249. const { isOverflowed, isTruncated, expanded } = this.state;
  250. const { showTooltip, expandable, expandText } = this.getEllipsisOpt();
  251. const overflowed = !expanded && (isOverflowed || isTruncated);
  252. const noExpandText = !expandable && isUndefined(expandText);
  253. const show = noExpandText && overflowed && showTooltip;
  254. if (!show) {
  255. return show;
  256. }
  257. const defaultOpts = {
  258. type: 'tooltip',
  259. };
  260. if (typeof showTooltip === 'object') {
  261. if (showTooltip.type && showTooltip.type.toLowerCase() === 'popover') {
  262. return merge(
  263. {
  264. opts: {
  265. // style: { width: '240px' },
  266. showArrow: true,
  267. },
  268. },
  269. showTooltip,
  270. {
  271. opts: {
  272. className: cls({
  273. [`${prefixCls}-ellipsis-popover`]: true,
  274. [showTooltip?.opts?.className]: Boolean(showTooltip?.opts?.className)
  275. }),
  276. }
  277. }
  278. );
  279. }
  280. return { ...defaultOpts, ...showTooltip };
  281. }
  282. return defaultOpts;
  283. };
  284. getEllipsisState() {
  285. const { rows, suffix, pos } = this.getEllipsisOpt();
  286. const { children } = this.props;
  287. // wait until element mounted
  288. if (!this.wrapperRef || !this.wrapperRef.current) {
  289. this.onResize();
  290. return false;
  291. }
  292. const { expanded } = this.state;
  293. const canUseCSSEllipsis = this.canUseCSSEllipsis();
  294. // If children is null, css/js truncated flag isTruncate is false
  295. if (isNull(children)) {
  296. this.setState({
  297. isTruncated: false,
  298. isOverflowed: false
  299. });
  300. return undefined;
  301. }
  302. // Currently only text truncation is supported, if there is non-text,
  303. // both css truncation and js truncation should throw a warning
  304. warning(
  305. 'children' in this.props && typeof children !== 'string',
  306. "[Semi Typography] Only children with pure text could be used with ellipsis at this moment."
  307. );
  308. if (!rows || rows < 0 || expanded) {
  309. return undefined;
  310. }
  311. if (canUseCSSEllipsis) {
  312. const updateOverflow = this.shouldTruncated(rows);
  313. // isOverflowed needs to be updated to show tooltip when using css ellipsis
  314. this.setState({
  315. isOverflowed: updateOverflow,
  316. isTruncated: false
  317. });
  318. return undefined;
  319. }
  320. const extraNode = { expand: this.expandRef.current, copy: this.copyRef && this.copyRef.current };
  321. const content = getRenderText(
  322. this.wrapperRef.current,
  323. rows,
  324. // Perform type conversion on children to prevent component crash due to non-string type of children
  325. String(children),
  326. extraNode,
  327. ELLIPSIS_STR,
  328. suffix,
  329. pos
  330. );
  331. this.setState({
  332. isOverflowed: false,
  333. ellipsisContent: content,
  334. isTruncated: children !== content,
  335. });
  336. return undefined;
  337. }
  338. /**
  339. * Triggered when the fold button is clicked to save the latest expanded state
  340. * @param {Event} e
  341. */
  342. toggleOverflow = (e: React.MouseEvent<HTMLAnchorElement>) => {
  343. const { onExpand, expandable, collapsible } = this.getEllipsisOpt();
  344. const { expanded } = this.state;
  345. onExpand && onExpand(!expanded, e);
  346. if ((expandable && !expanded) || (collapsible && expanded)) {
  347. this.setState({ expanded: !expanded });
  348. }
  349. };
  350. getEllipsisOpt = (): Ellipsis => {
  351. const { ellipsis } = this.props;
  352. if (!ellipsis) {
  353. return {};
  354. }
  355. const opt = {
  356. rows: 1,
  357. expandable: false,
  358. pos: 'end' as EllipsisPos,
  359. suffix: '',
  360. showTooltip: false,
  361. collapsible: false,
  362. expandText: (ellipsis as Ellipsis).expandable ? this.expandStr : undefined,
  363. collapseText: (ellipsis as Ellipsis).collapsible ? this.collapseStr : undefined,
  364. ...(typeof ellipsis === 'object' ? ellipsis : null),
  365. };
  366. return opt;
  367. };
  368. renderExpandable = () => {
  369. const { expanded, isTruncated } = this.state;
  370. if (!isTruncated) return null;
  371. const { expandText, expandable, collapseText, collapsible } = this.getEllipsisOpt();
  372. const noExpandText = !expandable && isUndefined(expandText);
  373. const noCollapseText = !collapsible && isUndefined(collapseText);
  374. let text;
  375. if (!expanded && !noExpandText) {
  376. text = expandText;
  377. } else if (expanded && !noCollapseText) {
  378. text = collapseText;
  379. }
  380. if (!noExpandText || !noCollapseText) {
  381. return (
  382. // TODO: replace `a` tag with `span` in next major version
  383. // NOTE: may have effect on style
  384. // eslint-disable-next-line jsx-a11y/anchor-is-valid
  385. <a
  386. role="button"
  387. tabIndex={0}
  388. className={`${prefixCls}-ellipsis-expand`}
  389. key="expand"
  390. ref={this.expandRef}
  391. aria-label={text}
  392. onClick={this.toggleOverflow}
  393. onKeyPress={e => isEnterPress(e) && this.toggleOverflow(e as any)}
  394. >
  395. {text}
  396. </a>
  397. );
  398. }
  399. return null;
  400. };
  401. /**
  402. * 获取文本的缩略class和style
  403. *
  404. * 截断类型:
  405. * - 当设置中间截断(pos='middle')、可展开(expandable)、有后缀(suffix 非空)、可复制(copyable),启用 JS 截断策略
  406. * - 非以上场景,启用 CSS 截断策略
  407. * 相关变量
  408. * props:
  409. * - ellipsis:
  410. * - rows
  411. * - expandable
  412. * - pos
  413. * - suffix
  414. * state:
  415. * - isOverflowed,文本是否处于overflow状态
  416. * - expanded,文本是否处于折叠状态
  417. * - isTruncated,文本是否被js截断
  418. *
  419. * Get the abbreviated class and style of the text
  420. *
  421. * Truncation type:
  422. * -When setting middle ellipsis (pos='middle')、expandable、suffix is not empty、copyable, the JS ellipsis strategy is enabled
  423. * -Otherwise, enable the CSS ellipsis strategy
  424. * related variables
  425. * props:
  426. * -ellipsis:
  427. * -rows
  428. * -expandable
  429. * -pos
  430. * -suffix
  431. * state:
  432. * -isOverflowed, whether the text is in an overflow state
  433. * -expanded, whether the text is in a collapsed state
  434. * -isTruncated, whether the text is truncated by js
  435. * @returns {Object}
  436. */
  437. getEllipsisStyle = () => {
  438. const { ellipsis, component } = this.props;
  439. if (!ellipsis) {
  440. return {
  441. ellipsisCls: '',
  442. ellipsisStyle: {},
  443. // ellipsisAttr: {}
  444. };
  445. }
  446. const { rows } = this.getEllipsisOpt();
  447. const { expanded } = this.state;
  448. const useCSS = !expanded && this.canUseCSSEllipsis();
  449. const ellipsisCls = cls({
  450. [`${prefixCls}-ellipsis`]: true,
  451. [`${prefixCls}-ellipsis-single-line`]: rows === 1,
  452. [`${prefixCls}-ellipsis-multiple-line`]: rows > 1,
  453. // component === 'span', Text component, It should be externally displayed inline
  454. [`${prefixCls}-ellipsis-multiple-line-text`]: rows > 1 && component === 'span',
  455. [`${prefixCls}-ellipsis-overflow-ellipsis`]: rows === 1 && useCSS,
  456. // component === 'span', Text component, It should be externally displayed inline
  457. [`${prefixCls}-ellipsis-overflow-ellipsis-text`]: rows === 1 && useCSS && component === 'span',
  458. });
  459. const ellipsisStyle = useCSS && rows > 1 ? { WebkitLineClamp: rows } : {};
  460. return {
  461. ellipsisCls,
  462. ellipsisStyle,
  463. };
  464. };
  465. renderEllipsisText = (opt: Ellipsis) => {
  466. const { suffix } = opt;
  467. const { children } = this.props;
  468. const { isTruncated, expanded, ellipsisContent } = this.state;
  469. if (expanded || !isTruncated) {
  470. return (
  471. <>
  472. {children}
  473. {suffix && suffix.length ? suffix : null}
  474. </>
  475. );
  476. }
  477. return (
  478. <span>
  479. {ellipsisContent}
  480. {/* {ELLIPSIS_STR} */}
  481. {suffix}
  482. </span>
  483. );
  484. };
  485. renderOperations() {
  486. return (
  487. <>
  488. {this.renderExpandable()}
  489. {this.renderCopy()}
  490. </>
  491. );
  492. }
  493. renderCopy() {
  494. const { copyable, children } = this.props;
  495. if (!copyable) {
  496. return null;
  497. }
  498. // If it is configured in the content of copyable, the copied content will be the content in copyable
  499. const willCopyContent = (copyable as CopyableConfig)?.content ?? children;
  500. let copyContent: string;
  501. let hasObject = false;
  502. if (Array.isArray(willCopyContent)) {
  503. copyContent = '';
  504. willCopyContent.forEach(value => {
  505. if (typeof value === 'object') {
  506. hasObject = true;
  507. }
  508. copyContent += String(value);
  509. });
  510. } else if (typeof willCopyContent !== 'object') {
  511. copyContent = String(willCopyContent);
  512. } else {
  513. hasObject = true;
  514. copyContent = String(willCopyContent);
  515. }
  516. warning(
  517. hasObject,
  518. 'Content to be copied in Typography is a object, it will case a [object Object] mistake when copy to clipboard.'
  519. );
  520. const copyConfig = {
  521. content: copyContent,
  522. duration: 3,
  523. ...(typeof copyable === 'object' ? copyable : null),
  524. };
  525. return <Copyable {...copyConfig} forwardRef={this.copyRef}/>;
  526. }
  527. renderIcon() {
  528. const { icon, size } = this.props;
  529. if (!icon) {
  530. return null;
  531. }
  532. const iconSize: Size = size === 'small' ? 'small' : 'default';
  533. return (
  534. <span className={`${prefixCls}-icon`} x-semi-prop="icon">
  535. {isSemiIcon(icon) ? React.cloneElement((icon as React.ReactElement), { size: iconSize }) : icon}
  536. </span>
  537. );
  538. }
  539. renderContent() {
  540. const {
  541. component,
  542. children,
  543. className,
  544. type,
  545. spacing,
  546. disabled,
  547. style,
  548. ellipsis,
  549. icon,
  550. size,
  551. link,
  552. heading,
  553. weight,
  554. ...rest
  555. } = this.props;
  556. const textProps = omit(rest, [
  557. 'strong',
  558. 'editable',
  559. 'mark',
  560. 'copyable',
  561. 'underline',
  562. 'code',
  563. // 'link',
  564. 'delete',
  565. ]);
  566. const iconNode = this.renderIcon();
  567. const ellipsisOpt = this.getEllipsisOpt();
  568. const { ellipsisCls, ellipsisStyle } = this.getEllipsisStyle();
  569. let textNode = ellipsis ? this.renderEllipsisText(ellipsisOpt) : children;
  570. const linkCls = cls({
  571. [`${prefixCls}-link-text`]: link,
  572. [`${prefixCls}-link-underline`]: this.props.underline && link,
  573. });
  574. textNode = wrapperDecorations(
  575. this.props,
  576. <>
  577. {iconNode}
  578. {this.props.link ? <span className={linkCls}>{textNode}</span> : textNode}
  579. </>
  580. );
  581. const hTagReg = /^h[1-6]$/;
  582. const isHeader = isString(heading) && hTagReg.test(heading);
  583. const wrapperCls = cls(className, ellipsisCls, {
  584. // [`${prefixCls}-primary`]: !type || type === 'primary',
  585. [`${prefixCls}-${type}`]: type && !link,
  586. [`${prefixCls}-${size}`]: size,
  587. [`${prefixCls}-link`]: link,
  588. [`${prefixCls}-disabled`]: disabled,
  589. [`${prefixCls}-${spacing}`]: spacing,
  590. [`${prefixCls}-${heading}`]: isHeader,
  591. [`${prefixCls}-${heading}-weight-${weight}`]: isHeader && weight && isNaN(Number(weight)),
  592. });
  593. const textStyle: CSSProperties = {
  594. ...(
  595. isNaN(Number(weight)) ? {} : { fontWeight: weight }
  596. ),
  597. ...style
  598. };
  599. return (
  600. <Typography
  601. className={wrapperCls}
  602. style={{ ...textStyle, ...ellipsisStyle }}
  603. component={component}
  604. forwardRef={this.wrapperRef}
  605. {...textProps}
  606. >
  607. {textNode}
  608. {this.renderOperations()}
  609. </Typography>
  610. );
  611. }
  612. renderTipWrapper() {
  613. const { children } = this.props;
  614. const showTooltip = this.showTooltip();
  615. const content = this.renderContent();
  616. if (showTooltip) {
  617. const { type, opts, renderTooltip } = showTooltip as ShowTooltip;
  618. if (isFunction(renderTooltip)) {
  619. return renderTooltip(children, content);
  620. } else if (type.toLowerCase() === 'popover') {
  621. return (
  622. <Popover content={children} position="top" {...opts}>
  623. {content}
  624. </Popover>
  625. );
  626. }
  627. return (
  628. <Tooltip content={children} position="top" {...opts}>
  629. {content}
  630. </Tooltip>
  631. );
  632. } else {
  633. return content;
  634. }
  635. }
  636. render() {
  637. const content = (
  638. <LocaleConsumer componentName="Typography">
  639. {(locale: Locale['Typography']) => {
  640. this.expandStr = locale.expand;
  641. this.collapseStr = locale.collapse;
  642. return this.renderTipWrapper();
  643. }}
  644. </LocaleConsumer>
  645. );
  646. if (this.props.ellipsis) {
  647. return (
  648. <ResizeObserver onResize={this.onResize} observeParent>
  649. {content}
  650. </ResizeObserver>
  651. );
  652. }
  653. return content;
  654. }
  655. }