text.tsx 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React, { PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { strings } from '@douyinfe/semi-foundation/typography/constants';
  4. import Base from './base';
  5. import { Ellipsis, TypographyBaseSize, TypographyBaseType, OmitTypographyProps } from './interface';
  6. import { CopyableConfig, LinkType } from './title';
  7. type OmitTextProps = OmitTypographyProps;
  8. export interface TextProps extends Omit<React.HTMLAttributes<HTMLSpanElement>, OmitTextProps> {
  9. children?: React.ReactNode;
  10. className?: string;
  11. code?: boolean;
  12. component?: React.ElementType;
  13. copyable?: CopyableConfig | boolean;
  14. delete?: boolean;
  15. disabled?: boolean;
  16. /**
  17. * ellipsis 用于设置截断相关参数.
  18. * Ellipsis is used to set ellipsis related parameters.
  19. * ellipsis 仅支持纯文本的截断,不支持 reactNode 等复杂类型,请确保 children 传入内容类型为 string.
  20. * Ellipsis only supports ellipsis of plain text, and does not support complex types such as reactNode.
  21. * Please ensure that the content type of children is string.
  22. * Semi 截断有两种策略, CSS 截断和 JS 截断。
  23. * Semi ellipsis has two strategies, CSS ellipsis and JS ellipsis.
  24. * - 当设置中间截断(pos='middle')、可展开(expandable)、有后缀(suffix 非空)、可复制(copyable),启用 JS 截断策略
  25. * - When setting middle ellipsis (pos='middle')、expandable、suffix is not empty string、copyable,
  26. * the JS ellipsis strategy is enabled
  27. * - 非以上场景,启用 CSS 截断策略
  28. * - Otherwise, enable the CSS ellipsis strategy
  29. *
  30. * 通常来说 CSS 截断的性能优于 JS 截断。在 children 不变, 容器尺寸不变的情况下,CSS 截断只涉及 1-2 次计算,js 截断基于二分法,可能涉及多次计算。
  31. * In general CSS ellipsis performs better than JS ellipsis. when the children and container size remain unchanged,
  32. * CSS ellipsis only involves 1-2 calculations, while JS ellipsis is based on dichotomy and may require multiple calculations.
  33. * 同时使用大量带有截断功能的 Typography 需注意性能消耗,如在 Table 中,可通过设置合理的页容量进行分页减少性能损耗
  34. * Pay attention to performance consumption when using a large number of Typography with ellipsis. For example, in Table,
  35. * you can reduce performance loss by setting a reasonable pageSize for paging
  36. */
  37. ellipsis?: Ellipsis | boolean;
  38. icon?: React.ReactNode | string;
  39. link?: LinkType;
  40. mark?: boolean;
  41. size?: TypographyBaseSize;
  42. strong?: boolean;
  43. style?: React.CSSProperties;
  44. type?: TypographyBaseType;
  45. underline?: boolean
  46. }
  47. export default class Text extends PureComponent<TextProps> {
  48. static propTypes = {
  49. copyable: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  50. delete: PropTypes.bool,
  51. disabled: PropTypes.bool,
  52. icon: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
  53. ellipsis: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  54. mark: PropTypes.bool,
  55. underline: PropTypes.bool,
  56. link: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  57. strong: PropTypes.bool,
  58. type: PropTypes.oneOf(strings.TYPE),
  59. size: PropTypes.oneOf(strings.SIZE),
  60. style: PropTypes.object,
  61. className: PropTypes.string,
  62. code: PropTypes.bool,
  63. component: PropTypes.string,
  64. };
  65. static defaultProps = {
  66. copyable: false,
  67. delete: false,
  68. disabled: false,
  69. icon: '',
  70. // editable: false,
  71. ellipsis: false,
  72. mark: false,
  73. underline: false,
  74. strong: false,
  75. link: false,
  76. type: 'primary',
  77. style: {},
  78. size: 'normal',
  79. className: '',
  80. };
  81. render() {
  82. return <Base component={'span'} {...this.props} />;
  83. }
  84. }