copyable.tsx 4.7 KB

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