paragraph.tsx 4.1 KB

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