2024-02-27 16:56:24 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import CheckOutlined from '@ant-design/icons/CheckOutlined';
|
|
|
|
import CopyOutlined from '@ant-design/icons/CopyOutlined';
|
2024-04-08 14:04:08 +08:00
|
|
|
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
|
2024-02-27 16:56:24 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import type { CopyConfig } from '.';
|
|
|
|
import TransButton from '../../_util/transButton';
|
2024-04-08 14:04:08 +08:00
|
|
|
import type { Locale } from '../../locale';
|
2024-02-27 16:56:24 +08:00
|
|
|
import Tooltip from '../../tooltip';
|
|
|
|
import { getNode, toList } from './util';
|
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
export interface CopyBtnProps extends Omit<CopyConfig, 'onCopy'> {
|
2024-02-27 16:56:24 +08:00
|
|
|
prefixCls: string;
|
|
|
|
copied: boolean;
|
|
|
|
locale: Locale['Text'];
|
2024-04-24 18:06:38 +08:00
|
|
|
onCopy: (e?: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
|
2024-02-27 16:56:24 +08:00
|
|
|
iconOnly: boolean;
|
2024-03-29 13:20:02 +08:00
|
|
|
loading: boolean;
|
2024-02-27 16:56:24 +08:00
|
|
|
}
|
|
|
|
|
2024-04-24 18:06:38 +08:00
|
|
|
const CopyBtn: React.FC<CopyBtnProps> = (props) => {
|
2024-04-25 11:08:55 +08:00
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
copied,
|
|
|
|
locale,
|
|
|
|
iconOnly,
|
|
|
|
tooltips,
|
|
|
|
icon,
|
|
|
|
loading: btnLoading,
|
|
|
|
tabIndex,
|
|
|
|
onCopy,
|
|
|
|
} = props;
|
2024-02-27 16:56:24 +08:00
|
|
|
|
|
|
|
const tooltipNodes = toList(tooltips);
|
|
|
|
const iconNodes = toList(icon);
|
|
|
|
|
2024-04-25 11:08:55 +08:00
|
|
|
const { copied: copiedText, copy: copyText } = locale ?? {};
|
2024-02-27 16:56:24 +08:00
|
|
|
|
|
|
|
const copyTitle = copied
|
|
|
|
? getNode(tooltipNodes[1], copiedText)
|
|
|
|
: getNode(tooltipNodes[0], copyText);
|
|
|
|
const systemStr = copied ? copiedText : copyText;
|
|
|
|
const ariaLabel = typeof copyTitle === 'string' ? copyTitle : systemStr;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Tooltip key="copy" title={copyTitle}>
|
|
|
|
<TransButton
|
|
|
|
className={classNames(`${prefixCls}-copy`, {
|
|
|
|
[`${prefixCls}-copy-success`]: copied,
|
|
|
|
[`${prefixCls}-copy-icon-only`]: iconOnly,
|
|
|
|
})}
|
2024-04-24 18:06:38 +08:00
|
|
|
onClick={onCopy}
|
2024-02-27 16:56:24 +08:00
|
|
|
aria-label={ariaLabel}
|
2024-04-25 11:08:55 +08:00
|
|
|
tabIndex={tabIndex}
|
2024-02-27 16:56:24 +08:00
|
|
|
>
|
|
|
|
{copied
|
|
|
|
? getNode(iconNodes[1], <CheckOutlined />, true)
|
2024-04-25 11:08:55 +08:00
|
|
|
: getNode(iconNodes[0], btnLoading ? <LoadingOutlined /> : <CopyOutlined />, true)}
|
2024-02-27 16:56:24 +08:00
|
|
|
</TransButton>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
2024-04-24 18:06:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default CopyBtn;
|