title.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, TypographyBaseType, OmitTypographyProps } from './interface';
  6. import { ArrayElement } from '@douyinfe/semi-foundation/utils/type';
  7. type OmitTitleProps = OmitTypographyProps;
  8. export interface CopyableConfig {
  9. content?: string;
  10. copyTip?: React.ReactNode;
  11. successTip?: React.ReactNode;
  12. icon?: React.ReactNode;
  13. onCopy?(e: React.MouseEvent, content: string, res: boolean): void
  14. }
  15. export type LinkType = React.AnchorHTMLAttributes<HTMLAnchorElement> | boolean;
  16. export interface TitleProps extends Omit<React.HTMLAttributes<HTMLHeadingElement>, OmitTitleProps> {
  17. className?: string;
  18. component?: React.ElementType;
  19. copyable?: CopyableConfig | boolean;
  20. delete?: boolean;
  21. disabled?: boolean;
  22. /**
  23. * ellipsis 用于设置截断相关参数.
  24. * Ellipsis is used to set ellipsis related parameters.
  25. * ellipsis 仅支持纯文本的截断,不支持 reactNode 等复杂类型,请确保 children 传入内容类型为 string.
  26. * Ellipsis only supports ellipsis of plain text, and does not support complex types such as reactNode.
  27. * Please ensure that the content type of children is string.
  28. * Semi 截断有两种策略, CSS 截断和 JS 截断。
  29. * Semi ellipsis has two strategies, CSS ellipsis and JS ellipsis.
  30. * - 当设置中间截断(pos='middle')、可展开(expandable)、有后缀(suffix 非空)、可复制(copyable),启用 JS 截断策略
  31. * - When setting middle ellipsis (pos='middle')、expandable、suffix is not empty string、copyable,
  32. * the JS ellipsis strategy is enabled
  33. * - 非以上场景,启用 CSS 截断策略
  34. * - Otherwise, enable the CSS ellipsis strategy
  35. *
  36. * 通常来说 CSS 截断的性能优于 JS 截断。在 children 不变, 容器尺寸不变的情况下,CSS 截断只涉及 1-2 次计算,js 截断基于二分法,可能涉及多次计算。
  37. * In general CSS ellipsis performs better than JS ellipsis. when the children and container size remain unchanged,
  38. * CSS ellipsis only involves 1-2 calculations, while JS ellipsis is based on dichotomy and may require multiple calculations.
  39. * 同时使用大量带有截断功能的 Typography 需注意性能消耗,如在 Table 中,可通过设置合理的页容量进行分页减少性能损耗
  40. * Pay attention to performance consumption when using a large number of Typography with ellipsis. For example, in Table,
  41. * you can reduce performance loss by setting a reasonable pageSize for paging
  42. */
  43. ellipsis?: Ellipsis | boolean;
  44. heading?: ArrayElement<typeof strings.HEADING>;
  45. link?: LinkType;
  46. mark?: boolean;
  47. strong?: boolean;
  48. style?: React.CSSProperties;
  49. type?: TypographyBaseType;
  50. underline?: boolean;
  51. weight?: ArrayElement<typeof strings.WEIGHT> | number
  52. }
  53. export default class Title extends PureComponent<TitleProps> {
  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. heading: PropTypes.oneOf(strings.HEADING),
  66. style: PropTypes.object,
  67. className: PropTypes.string,
  68. component: PropTypes.string,
  69. weight: PropTypes.oneOfType([PropTypes.oneOf(strings.WEIGHT), PropTypes.number]),
  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. heading: 1,
  83. style: {},
  84. className: '',
  85. };
  86. render() {
  87. const { heading, ...rest } = this.props;
  88. const component = strings.HEADING.indexOf(heading) !== -1 ? `h${heading}` : 'h1';
  89. // Passing headings to support custom components
  90. return <Base component={component as React.ElementType} heading={component} {...rest} />;
  91. }
  92. }