title.tsx 2.5 KB

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