1
0

text.tsx 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. weight?: number
  47. }
  48. export default class Text extends PureComponent<TextProps> {
  49. static propTypes = {
  50. copyable: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  51. delete: PropTypes.bool,
  52. disabled: PropTypes.bool,
  53. icon: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
  54. ellipsis: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  55. mark: PropTypes.bool,
  56. underline: PropTypes.bool,
  57. link: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  58. strong: PropTypes.bool,
  59. type: PropTypes.oneOf(strings.TYPE),
  60. size: PropTypes.oneOf(strings.SIZE),
  61. style: PropTypes.object,
  62. className: PropTypes.string,
  63. code: PropTypes.bool,
  64. component: PropTypes.string,
  65. weight: PropTypes.number,
  66. };
  67. static defaultProps = {
  68. copyable: false,
  69. delete: false,
  70. disabled: false,
  71. icon: '',
  72. // editable: false,
  73. ellipsis: false,
  74. mark: false,
  75. underline: false,
  76. strong: false,
  77. link: false,
  78. type: 'primary',
  79. style: {},
  80. size: 'normal',
  81. className: '',
  82. };
  83. render() {
  84. return <Base component={'span'} {...this.props} />;
  85. }
  86. }