title.tsx 2.6 KB

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