copyable.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. import isEnterPress from '@douyinfe/semi-foundation/utils/isEnterPress';
  13. const prefixCls = cssClasses.PREFIX;
  14. export interface CopyableProps extends BaseProps {
  15. content?: string;
  16. copyTip?: React.ReactNode;
  17. duration?: number;
  18. forwardRef?: React.RefObject<any>;
  19. successTip?: React.ReactNode;
  20. onCopy?: (e: React.MouseEvent, content: string, res: boolean) => void;
  21. }
  22. interface CopyableState {
  23. copied: boolean;
  24. item: string;
  25. }
  26. export class Copyable extends React.PureComponent<CopyableProps, CopyableState> {
  27. static propTypes = {
  28. content: PropTypes.string,
  29. onCopy: PropTypes.func,
  30. successTip: PropTypes.node,
  31. copyTip: PropTypes.node,
  32. duration: PropTypes.number,
  33. style: PropTypes.object,
  34. className: PropTypes.string,
  35. };
  36. static defaultProps = {
  37. content: '',
  38. onCopy: noop,
  39. duration: 3,
  40. style: {},
  41. className: '',
  42. };
  43. _timeId: ReturnType<typeof setTimeout>;
  44. constructor(props: CopyableProps) {
  45. super(props);
  46. this.state = {
  47. copied: false,
  48. item: '',
  49. };
  50. }
  51. componentWillUnmount() {
  52. if (this._timeId) {
  53. clearTimeout(this._timeId);
  54. this._timeId = null;
  55. }
  56. }
  57. copy = (e: React.MouseEvent) => {
  58. const { content, duration, onCopy } = this.props;
  59. const res = copy(content);
  60. onCopy && onCopy(e, content, res);
  61. this.setCopied(content, duration);
  62. };
  63. setCopied = (item: string, timer: number) => {
  64. this.setState({
  65. copied: true,
  66. item,
  67. });
  68. this._timeId = setTimeout(() => {
  69. this.resetCopied();
  70. }, timer * 1000);
  71. };
  72. resetCopied = () => {
  73. if (this._timeId) {
  74. clearTimeout(this._timeId);
  75. this._timeId = null;
  76. this.setState({
  77. copied: false,
  78. item: '',
  79. });
  80. }
  81. };
  82. renderSuccessTip = () => {
  83. const { successTip } = this.props;
  84. if (typeof successTip !== 'undefined') {
  85. return successTip;
  86. }
  87. return (
  88. <LocaleConsumer componentName="Typography">
  89. {(locale: Locale['Typography']) => (
  90. <span>
  91. <IconTick />
  92. {locale.copied}
  93. </span>
  94. )}
  95. </LocaleConsumer>
  96. );
  97. };
  98. render() {
  99. const { style, className, forwardRef, copyTip } = this.props;
  100. const { copied } = this.state;
  101. const finalCls = cls(className, {
  102. [`${prefixCls}-action-copy`]: !copied,
  103. [`${prefixCls}-action-copied`]: copied,
  104. });
  105. return (
  106. <LocaleConsumer componentName="Typography">
  107. {(locale: Locale['Typography']) => (
  108. <span style={{ marginLeft: '4px', ...style }} className={finalCls} ref={forwardRef}>
  109. {copied ? (
  110. this.renderSuccessTip()
  111. ) : (
  112. <Tooltip content={typeof copyTip !== 'undefined' ? copyTip : locale.copy}>
  113. {/* TODO: replace `a` tag with `span` in next major version
  114. NOTE: may have effect on style */}
  115. {/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
  116. <a className={`${prefixCls}-action-copy-icon`}>
  117. <IconCopy
  118. role="button"
  119. tabIndex={0}
  120. onClick={this.copy}
  121. onKeyPress={e => isEnterPress(e) && this.copy(e as any)}
  122. />
  123. </a>
  124. </Tooltip>
  125. )}
  126. </span>
  127. )}
  128. </LocaleConsumer>
  129. );
  130. }
  131. }
  132. export default Copyable;