base.tsx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. import React, { Component, CSSProperties } from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { cssClasses, strings } from '@douyinfe/semi-foundation/typography/constants';
  5. import Typography from './typography';
  6. import Copyable from './copyable';
  7. import { IconSize as Size } from '../icons/index';
  8. import { isUndefined, omit, merge, isString, isNull, isFunction } from 'lodash';
  9. import Tooltip from '../tooltip/index';
  10. import Popover from '../popover/index';
  11. import getRenderText from './util';
  12. import warning from '@douyinfe/semi-foundation/utils/warning';
  13. import isEnterPress from '@douyinfe/semi-foundation/utils/isEnterPress';
  14. import LocaleConsumer from '../locale/localeConsumer';
  15. import type { Locale } from '../locale/interface';
  16. import type { Ellipsis, EllipsisPos, ShowTooltip, TypographyBaseSize, TypographyBaseType } from './interface';
  17. import { CopyableConfig, LinkType } from './title';
  18. import { BaseProps } from '../_base/baseComponent';
  19. import { isSemiIcon, runAfterTicks } from '../_utils';
  20. import SizeContext from './context';
  21. import ResizeObserver, { ObserverProperty, ResizeEntry } 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. static contextType = SizeContext;
  163. context: TypographyBaseSize;
  164. wrapperRef: React.RefObject<any>;
  165. expandRef: React.RefObject<any>;
  166. copyRef: React.RefObject<any>;
  167. rafId: ReturnType<typeof requestAnimationFrame>;
  168. expandStr: string;
  169. collapseStr: string;
  170. observerTakingEffect: boolean = false
  171. constructor(props: BaseTypographyProps) {
  172. super(props);
  173. this.state = {
  174. editable: false,
  175. copied: false,
  176. // ellipsis
  177. // if text is overflow in container
  178. isOverflowed: false,
  179. ellipsisContent: props.children,
  180. expanded: false,
  181. // if text is truncated with js
  182. isTruncated: false,
  183. prevChildren: null,
  184. };
  185. this.wrapperRef = React.createRef();
  186. this.expandRef = React.createRef();
  187. this.copyRef = React.createRef();
  188. }
  189. componentDidMount() {
  190. if (this.props.ellipsis) {
  191. // runAfterTicks: make sure start observer on the next tick
  192. this.onResize().then(()=>runAfterTicks(()=>this.observerTakingEffect = true, 1));
  193. }
  194. }
  195. static getDerivedStateFromProps(props: BaseTypographyProps, prevState: BaseTypographyState) {
  196. const { prevChildren } = prevState;
  197. const newState: Partial<BaseTypographyState> = {};
  198. newState.prevChildren = props.children;
  199. if (props.ellipsis && prevChildren !== props.children) {
  200. // reset ellipsis state if children update
  201. newState.isOverflowed = false;
  202. newState.ellipsisContent = props.children;
  203. newState.expanded = false;
  204. newState.isTruncated = true;
  205. }
  206. return newState;
  207. }
  208. componentDidUpdate(prevProps: BaseTypographyProps) {
  209. // Render was based on outdated refs and needs to be rerun
  210. if (this.props.children !== prevProps.children) {
  211. this.forceUpdate();
  212. if (this.props.ellipsis) {
  213. this.onResize();
  214. }
  215. }
  216. }
  217. componentWillUnmount() {
  218. if (this.rafId) {
  219. window.cancelAnimationFrame(this.rafId);
  220. }
  221. }
  222. onResize = async (entries?: ResizeEntry[]) => {
  223. if (this.rafId) {
  224. window.cancelAnimationFrame(this.rafId);
  225. }
  226. return new Promise<void>(resolve => {
  227. this.rafId = window.requestAnimationFrame(async ()=>{
  228. await this.getEllipsisState();
  229. resolve();
  230. });
  231. });
  232. };
  233. // if it needs to use js overflowed:
  234. // 1. text is expandable 2. expandText need to be shown 3. has extra operation 4. text need to ellipse from mid
  235. canUseCSSEllipsis = () => {
  236. const { copyable } = this.props;
  237. const { expandable, expandText, pos, suffix } = this.getEllipsisOpt();
  238. return !expandable && isUndefined(expandText) && !copyable && pos === 'end' && !suffix.length;
  239. };
  240. /**
  241. * whether truncated
  242. * rows < = 1 if there is overflow content, return true
  243. * rows > 1 if there is overflow height, return true
  244. * @param {Number} rows
  245. * @returns {Boolean}
  246. */
  247. shouldTruncated = (rows: number) => {
  248. if (!rows || rows < 1) {
  249. return false;
  250. }
  251. const updateOverflow =
  252. rows <= 1 ?
  253. this.compareSingleRow() :
  254. this.wrapperRef.current.scrollHeight > this.wrapperRef.current.offsetHeight;
  255. return updateOverflow;
  256. };
  257. /**
  258. * 通过将 content 给到 Range 对象,借助 Range 的 getBoundingClientRect 拿到 content 的准确 width
  259. * 不受 css ellipsis 与否的影响
  260. * By giving the content to the Range object, get the exact width of the content with the help of Range's getBoundingClientRect
  261. * Not affected by css ellipsis or not
  262. * https://github.com/DouyinFE/semi-design/issues/1731
  263. */
  264. compareSingleRow = () => {
  265. if (!(document && document.createRange)) {
  266. return false;
  267. }
  268. const containerNode = this.wrapperRef.current;
  269. const containerWidth = containerNode.getBoundingClientRect().width;
  270. const childNodes = Array.from(containerNode.childNodes) as Node[];
  271. const range = document.createRange();
  272. const contentWidth = childNodes.reduce((acc: number, node: Node) => {
  273. range.selectNodeContents(node as Node);
  274. return acc + (range.getBoundingClientRect().width ?? 0);
  275. }, 0);
  276. range.detach();
  277. return contentWidth > containerWidth;
  278. }
  279. showTooltip = () => {
  280. const { isOverflowed, isTruncated, expanded } = this.state;
  281. const { showTooltip, expandable, expandText } = this.getEllipsisOpt();
  282. const canUseCSSEllipsis = this.canUseCSSEllipsis();
  283. // If the css is truncated, use isOverflowed to judge. If the css is truncated, use isTruncated to judge.
  284. const overflowed = !expanded && (canUseCSSEllipsis ? isOverflowed : isTruncated);
  285. const noExpandText = !expandable && isUndefined(expandText);
  286. const show = noExpandText && overflowed && showTooltip;
  287. if (!show) {
  288. return show;
  289. }
  290. const defaultOpts = {
  291. type: 'tooltip',
  292. };
  293. if (typeof showTooltip === 'object') {
  294. if (showTooltip.type && showTooltip.type.toLowerCase() === 'popover') {
  295. return merge(
  296. {
  297. opts: {
  298. // style: { width: '240px' },
  299. showArrow: true,
  300. },
  301. },
  302. showTooltip,
  303. {
  304. opts: {
  305. className: cls({
  306. [`${prefixCls}-ellipsis-popover`]: true,
  307. [showTooltip?.opts?.className]: Boolean(showTooltip?.opts?.className)
  308. }),
  309. }
  310. }
  311. );
  312. }
  313. return { ...defaultOpts, ...showTooltip };
  314. }
  315. return defaultOpts;
  316. };
  317. onHover = ()=>{
  318. const canUseCSSEllipsis = this.canUseCSSEllipsis();
  319. if (canUseCSSEllipsis) {
  320. const { rows, suffix, pos } = this.getEllipsisOpt();
  321. const updateOverflow = this.shouldTruncated(rows);
  322. // isOverflowed needs to be updated to show tooltip when using css ellipsis
  323. this.setState({
  324. isOverflowed: updateOverflow,
  325. isTruncated: false
  326. });
  327. return undefined;
  328. }
  329. }
  330. getEllipsisState = async ()=> {
  331. const { rows, suffix, pos } = this.getEllipsisOpt();
  332. const { children } = this.props;
  333. // wait until element mounted
  334. if (!this.wrapperRef || !this.wrapperRef.current) {
  335. await this.onResize();
  336. return;
  337. }
  338. const { expanded } = this.state;
  339. const canUseCSSEllipsis = this.canUseCSSEllipsis();
  340. if (canUseCSSEllipsis) {
  341. // const updateOverflow = this.shouldTruncated(rows);
  342. // // isOverflowed needs to be updated to show tooltip when using css ellipsis
  343. // this.setState({
  344. // isOverflowed: updateOverflow,
  345. // isTruncated: false
  346. // });
  347. return ;
  348. }
  349. // If children is null, css/js truncated flag isTruncate is false
  350. if (isNull(children)) {
  351. return new Promise<void>(resolve=>{
  352. this.setState({
  353. isTruncated: false,
  354. isOverflowed: false
  355. }, resolve);
  356. });
  357. }
  358. // Currently only text truncation is supported, if there is non-text,
  359. // both css truncation and js truncation should throw a warning
  360. warning(
  361. 'children' in this.props && typeof children !== 'string',
  362. "[Semi Typography] Only children with pure text could be used with ellipsis at this moment."
  363. );
  364. if (!rows || rows < 0 || expanded) {
  365. return;
  366. }
  367. const extraNode = { expand: this.expandRef.current, copy: this.copyRef && this.copyRef.current };
  368. // Perform type conversion on children to prevent component crash due to non-string type of children
  369. // https://github.com/DouyinFE/semi-design/issues/2167
  370. const realChildren = Array.isArray(children) ? children.join('') : String(children);
  371. const content = getRenderText(
  372. this.wrapperRef.current,
  373. rows,
  374. realChildren,
  375. extraNode,
  376. ELLIPSIS_STR,
  377. suffix,
  378. pos
  379. );
  380. return new Promise<void>(resolve=>{
  381. this.setState({
  382. isOverflowed: false,
  383. ellipsisContent: content,
  384. isTruncated: realChildren !== content,
  385. }, resolve);
  386. });
  387. }
  388. /**
  389. * Triggered when the fold button is clicked to save the latest expanded state
  390. * @param {Event} e
  391. */
  392. toggleOverflow = (e: React.MouseEvent<HTMLAnchorElement>) => {
  393. const { onExpand, expandable, collapsible } = this.getEllipsisOpt();
  394. const { expanded } = this.state;
  395. onExpand && onExpand(!expanded, e);
  396. if ((expandable && !expanded) || (collapsible && expanded)) {
  397. this.setState({ expanded: !expanded });
  398. }
  399. };
  400. getEllipsisOpt = (): Ellipsis => {
  401. const { ellipsis } = this.props;
  402. if (!ellipsis) {
  403. return {};
  404. }
  405. const opt = {
  406. rows: 1,
  407. expandable: false,
  408. pos: 'end' as EllipsisPos,
  409. suffix: '',
  410. showTooltip: false,
  411. collapsible: false,
  412. expandText: (ellipsis as Ellipsis).expandable ? this.expandStr : undefined,
  413. collapseText: (ellipsis as Ellipsis).collapsible ? this.collapseStr : undefined,
  414. ...(typeof ellipsis === 'object' ? ellipsis : null),
  415. };
  416. return opt;
  417. };
  418. renderExpandable = () => {
  419. const { expanded, isTruncated } = this.state;
  420. if (!isTruncated) return null;
  421. const { expandText, expandable, collapseText, collapsible } = this.getEllipsisOpt();
  422. const noExpandText = !expandable && isUndefined(expandText);
  423. const noCollapseText = !collapsible && isUndefined(collapseText);
  424. let text;
  425. if (!expanded && !noExpandText) {
  426. text = expandText;
  427. } else if (expanded && !noCollapseText) {
  428. text = collapseText;
  429. }
  430. if (!noExpandText || !noCollapseText) {
  431. return (
  432. // TODO: replace `a` tag with `span` in next major version
  433. // NOTE: may have effect on style
  434. // eslint-disable-next-line jsx-a11y/anchor-is-valid
  435. <a
  436. role="button"
  437. tabIndex={0}
  438. className={`${prefixCls}-ellipsis-expand`}
  439. key="expand"
  440. ref={this.expandRef}
  441. aria-label={text}
  442. onClick={this.toggleOverflow}
  443. onKeyPress={e => isEnterPress(e) && this.toggleOverflow(e as any)}
  444. >
  445. {text}
  446. </a>
  447. );
  448. }
  449. return null;
  450. };
  451. /**
  452. * 获取文本的缩略class和style
  453. *
  454. * 截断类型:
  455. * - 当设置中间截断(pos='middle')、可展开(expandable)、有后缀(suffix 非空)、可复制(copyable),启用 JS 截断策略
  456. * - 非以上场景,启用 CSS 截断策略
  457. * 相关变量
  458. * props:
  459. * - ellipsis:
  460. * - rows
  461. * - expandable
  462. * - pos
  463. * - suffix
  464. * state:
  465. * - isOverflowed,文本是否处于overflow状态
  466. * - expanded,文本是否处于折叠状态
  467. * - isTruncated,文本是否被js截断
  468. *
  469. * Get the abbreviated class and style of the text
  470. *
  471. * Truncation type:
  472. * -When setting middle ellipsis (pos='middle')、expandable、suffix is not empty、copyable, the JS ellipsis strategy is enabled
  473. * -Otherwise, enable the CSS ellipsis strategy
  474. * related variables
  475. * props:
  476. * -ellipsis:
  477. * -rows
  478. * -expandable
  479. * -pos
  480. * -suffix
  481. * state:
  482. * -isOverflowed, whether the text is in an overflow state
  483. * -expanded, whether the text is in a collapsed state
  484. * -isTruncated, whether the text is truncated by js
  485. * @returns {Object}
  486. */
  487. getEllipsisStyle = () => {
  488. const { ellipsis, component } = this.props;
  489. if (!ellipsis) {
  490. return {
  491. ellipsisCls: '',
  492. ellipsisStyle: {},
  493. // ellipsisAttr: {}
  494. };
  495. }
  496. const { rows } = this.getEllipsisOpt();
  497. const { expanded } = this.state;
  498. const useCSS = !expanded && this.canUseCSSEllipsis();
  499. const ellipsisCls = cls({
  500. [`${prefixCls}-ellipsis`]: true,
  501. [`${prefixCls}-ellipsis-single-line`]: rows === 1,
  502. [`${prefixCls}-ellipsis-multiple-line`]: rows > 1,
  503. // component === 'span', Text component, It should be externally displayed inline
  504. [`${prefixCls}-ellipsis-multiple-line-text`]: rows > 1 && component === 'span',
  505. [`${prefixCls}-ellipsis-overflow-ellipsis`]: rows === 1 && useCSS,
  506. // component === 'span', Text component, It should be externally displayed inline
  507. [`${prefixCls}-ellipsis-overflow-ellipsis-text`]: rows === 1 && useCSS && component === 'span',
  508. });
  509. const ellipsisStyle = useCSS && rows > 1 ? { WebkitLineClamp: rows } : {};
  510. return {
  511. ellipsisCls,
  512. ellipsisStyle,
  513. };
  514. };
  515. renderEllipsisText = (opt: Ellipsis) => {
  516. const { suffix } = opt;
  517. const { children } = this.props;
  518. const { isTruncated, expanded, ellipsisContent } = this.state;
  519. if (expanded || !isTruncated) {
  520. return (
  521. <span onMouseEnter={this.onHover}>
  522. {children}
  523. {suffix && suffix.length ? suffix : null}
  524. </span>
  525. );
  526. }
  527. return (
  528. <span onMouseEnter={this.onHover}>
  529. {ellipsisContent}
  530. {/* {ELLIPSIS_STR} */}
  531. {suffix}
  532. </span>
  533. );
  534. };
  535. renderOperations() {
  536. return (
  537. <>
  538. {this.renderExpandable()}
  539. {this.renderCopy()}
  540. </>
  541. );
  542. }
  543. renderCopy() {
  544. const { copyable, children } = this.props;
  545. if (!copyable) {
  546. return null;
  547. }
  548. // If it is configured in the content of copyable, the copied content will be the content in copyable
  549. const willCopyContent = (copyable as CopyableConfig)?.content ?? children;
  550. let copyContent: string;
  551. let hasObject = false;
  552. if (Array.isArray(willCopyContent)) {
  553. copyContent = '';
  554. willCopyContent.forEach(value => {
  555. if (typeof value === 'object') {
  556. hasObject = true;
  557. }
  558. copyContent += String(value);
  559. });
  560. } else if (typeof willCopyContent !== 'object') {
  561. copyContent = String(willCopyContent);
  562. } else {
  563. hasObject = true;
  564. copyContent = String(willCopyContent);
  565. }
  566. warning(
  567. hasObject,
  568. 'Content to be copied in Typography is a object, it will case a [object Object] mistake when copy to clipboard.'
  569. );
  570. const copyConfig = {
  571. content: copyContent,
  572. duration: 3,
  573. ...(typeof copyable === 'object' ? copyable : null),
  574. };
  575. return <Copyable {...copyConfig} forwardRef={this.copyRef}/>;
  576. }
  577. renderIcon() {
  578. const { icon, size } = this.props;
  579. const realSize = size === 'inherit' ? this.context : size;
  580. if (!icon) {
  581. return null;
  582. }
  583. const iconSize: Size = realSize === 'small' ? 'small' : 'default';
  584. return (
  585. <span className={`${prefixCls}-icon`} x-semi-prop="icon">
  586. {isSemiIcon(icon) ? React.cloneElement((icon as React.ReactElement), { size: iconSize }) : icon}
  587. </span>
  588. );
  589. }
  590. renderContent() {
  591. const {
  592. component,
  593. children,
  594. className,
  595. type,
  596. spacing,
  597. disabled,
  598. style,
  599. ellipsis,
  600. icon,
  601. size,
  602. link,
  603. heading,
  604. weight,
  605. ...rest
  606. } = this.props;
  607. const textProps = omit(rest, [
  608. 'strong',
  609. 'editable',
  610. 'mark',
  611. 'copyable',
  612. 'underline',
  613. 'code',
  614. // 'link',
  615. 'delete',
  616. ]);
  617. const realSize = size === 'inherit' ? this.context : size;
  618. const iconNode = this.renderIcon();
  619. const ellipsisOpt = this.getEllipsisOpt();
  620. const { ellipsisCls, ellipsisStyle } = this.getEllipsisStyle();
  621. let textNode = ellipsis ? this.renderEllipsisText(ellipsisOpt) : children;
  622. const linkCls = cls({
  623. [`${prefixCls}-link-text`]: link,
  624. [`${prefixCls}-link-underline`]: this.props.underline && link,
  625. });
  626. textNode = wrapperDecorations(
  627. this.props,
  628. <>
  629. {iconNode}
  630. {this.props.link ? <span className={linkCls}>{textNode}</span> : textNode}
  631. </>
  632. );
  633. const hTagReg = /^h[1-6]$/;
  634. const isHeader = isString(heading) && hTagReg.test(heading);
  635. const wrapperCls = cls(className, ellipsisCls, {
  636. // [`${prefixCls}-primary`]: !type || type === 'primary',
  637. [`${prefixCls}-${type}`]: type && !link,
  638. [`${prefixCls}-${realSize}`]: realSize,
  639. [`${prefixCls}-link`]: link,
  640. [`${prefixCls}-disabled`]: disabled,
  641. [`${prefixCls}-${spacing}`]: spacing,
  642. [`${prefixCls}-${heading}`]: isHeader,
  643. [`${prefixCls}-${heading}-weight-${weight}`]: isHeader && weight && isNaN(Number(weight)),
  644. });
  645. const textStyle: CSSProperties = {
  646. ...(
  647. isNaN(Number(weight)) ? {} : { fontWeight: weight }
  648. ),
  649. ...style
  650. };
  651. return (
  652. <Typography
  653. className={wrapperCls}
  654. style={{ ...textStyle, ...ellipsisStyle }}
  655. component={component}
  656. forwardRef={this.wrapperRef}
  657. {...textProps}
  658. >
  659. {textNode}
  660. {this.renderOperations()}
  661. </Typography>
  662. );
  663. }
  664. renderTipWrapper() {
  665. const { children } = this.props;
  666. const showTooltip = this.showTooltip();
  667. const content = this.renderContent();
  668. if (showTooltip) {
  669. const { type, opts, renderTooltip } = showTooltip as ShowTooltip;
  670. if (isFunction(renderTooltip)) {
  671. return renderTooltip(children, content);
  672. } else if (type.toLowerCase() === 'popover') {
  673. return (
  674. <Popover content={children} position="top" {...opts}>
  675. {content}
  676. </Popover>
  677. );
  678. }
  679. return (
  680. <Tooltip content={children} position="top" {...opts}>
  681. {content}
  682. </Tooltip>
  683. );
  684. } else {
  685. return content;
  686. }
  687. }
  688. render() {
  689. const { size } = this.props;
  690. const realSize = size === 'inherit' ? this.context : size;
  691. const content = (
  692. <SizeContext.Provider value={realSize}>
  693. <LocaleConsumer componentName="Typography">
  694. {(locale: Locale['Typography']) => {
  695. this.expandStr = locale.expand;
  696. this.collapseStr = locale.collapse;
  697. return this.renderTipWrapper();
  698. }}
  699. </LocaleConsumer>
  700. </SizeContext.Provider>
  701. );
  702. if (this.props.ellipsis) {
  703. return (
  704. <ResizeObserver onResize={(...args)=>{
  705. if (this.observerTakingEffect) {
  706. this.onResize(...args);
  707. }
  708. }} observeParent observerProperty={ObserverProperty.Width}>
  709. {content}
  710. </ResizeObserver>
  711. );
  712. }
  713. return content;
  714. }
  715. }