copyable.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Tooltip from '../tooltip/index';
  4. import { cssClasses } from '@douyinfe/semi-foundation/typography/constants';
  5. import copy from 'copy-text-to-clipboard';
  6. import cls from 'classnames';
  7. import { noop } from '@douyinfe/semi-foundation/utils/function';
  8. import LocaleConsumer from '../locale/localeConsumer';
  9. import { IconCopy, IconTick } from '@douyinfe/semi-icons';
  10. import { BaseProps } from '../_base/baseComponent';
  11. import { Locale } from '../locale/interface';
  12. const prefixCls = cssClasses.PREFIX;
  13. export interface CopyableProps extends BaseProps {
  14. content?: string;
  15. copyTip?: React.ReactNode;
  16. duration?: number;
  17. forwardRef?: React.RefObject<any>;
  18. successTip?: React.ReactNode;
  19. onCopy?: (e: React.MouseEvent, content: string, res: boolean) => void;
  20. }
  21. interface CopyableState {
  22. copied: boolean;
  23. item: string;
  24. }
  25. export class Copyable extends React.PureComponent<CopyableProps, CopyableState> {
  26. static propTypes = {
  27. content: PropTypes.string,
  28. onCopy: PropTypes.func,
  29. successTip: PropTypes.node,
  30. copyTip: PropTypes.node,
  31. duration: PropTypes.number,
  32. style: PropTypes.object,
  33. className: PropTypes.string,
  34. };
  35. static defaultProps = {
  36. content: '',
  37. onCopy: noop,
  38. duration: 3,
  39. style: {},
  40. className: '',
  41. };
  42. _timeId: ReturnType<typeof setTimeout>;
  43. constructor(props: CopyableProps) {
  44. super(props);
  45. this.state = {
  46. copied: false,
  47. item: '',
  48. };
  49. }
  50. componentWillUnmount() {
  51. if (this._timeId) {
  52. clearTimeout(this._timeId);
  53. this._timeId = null;
  54. }
  55. }
  56. copy = (e: React.MouseEvent) => {
  57. const { content, duration, onCopy } = this.props;
  58. const res = copy(content);
  59. onCopy && onCopy(e, content, res);
  60. this.setCopied(content, duration);
  61. };
  62. setCopied = (item: string, timer: number) => {
  63. this.setState({
  64. copied: true,
  65. item,
  66. });
  67. this._timeId = setTimeout(() => {
  68. this.resetCopied();
  69. }, timer * 1000);
  70. };
  71. resetCopied = () => {
  72. if (this._timeId) {
  73. clearTimeout(this._timeId);
  74. this._timeId = null;
  75. this.setState({
  76. copied: false,
  77. item: '',
  78. });
  79. }
  80. };
  81. renderSuccessTip = () => {
  82. const { successTip } = this.props;
  83. if (typeof successTip !== 'undefined') {
  84. return successTip;
  85. }
  86. return (
  87. <LocaleConsumer componentName="Typography">
  88. {(locale: Locale['Typography']) => (
  89. <span>
  90. <IconTick />
  91. {locale.copied}
  92. </span>
  93. )}
  94. </LocaleConsumer>
  95. );
  96. };
  97. render() {
  98. const { style, className, forwardRef, copyTip } = this.props;
  99. const { copied } = this.state;
  100. const finalCls = cls(className, {
  101. [`${prefixCls}-action-copy`]: !copied,
  102. [`${prefixCls}-action-copied`]: copied,
  103. });
  104. return (
  105. <LocaleConsumer componentName="Typography">
  106. {(locale: Locale['Typography']) => (
  107. <span style={{ marginLeft: '4px', ...style }} className={finalCls} ref={forwardRef}>
  108. {copied ? (
  109. this.renderSuccessTip()
  110. ) : (
  111. <Tooltip content={typeof copyTip !== 'undefined' ? copyTip : locale.copy}>
  112. <a className={`${prefixCls}-action-copy-icon`}>
  113. <IconCopy onClick={this.copy} />
  114. </a>
  115. </Tooltip>
  116. )}
  117. </span>
  118. )}
  119. </LocaleConsumer>
  120. );
  121. }
  122. }
  123. export default Copyable;