copyable.tsx 5.0 KB

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