title.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. ellipsis?: Ellipsis | boolean;
  23. heading?: ArrayElement<typeof strings.HEADING>;
  24. link?: LinkType;
  25. mark?: boolean;
  26. strong?: boolean;
  27. style?: React.CSSProperties;
  28. type?: TypographyBaseType;
  29. underline?: boolean
  30. }
  31. export default class Title extends PureComponent<TitleProps> {
  32. static propTypes = {
  33. copyable: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  34. delete: PropTypes.bool,
  35. disabled: PropTypes.bool,
  36. // editable: PropTypes.bool,
  37. ellipsis: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  38. mark: PropTypes.bool,
  39. link: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  40. underline: PropTypes.bool,
  41. strong: PropTypes.bool,
  42. type: PropTypes.oneOf(strings.TYPE),
  43. heading: PropTypes.oneOf(strings.HEADING),
  44. style: PropTypes.object,
  45. className: PropTypes.string,
  46. component: PropTypes.string,
  47. };
  48. static defaultProps = {
  49. copyable: false,
  50. delete: false,
  51. disabled: false,
  52. // editable: false,
  53. ellipsis: false,
  54. mark: false,
  55. underline: false,
  56. strong: false,
  57. link: false,
  58. type: 'primary',
  59. heading: 1,
  60. style: {},
  61. className: '',
  62. };
  63. render() {
  64. const { heading, ...rest } = this.props;
  65. const component = strings.HEADING.indexOf(heading) !== -1 ? `h${heading}` : 'h1';
  66. // Passing headings to support custom components
  67. return <Base component={component as React.ElementType} heading={component} {...rest} />;
  68. }
  69. }