-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.tsx
57 lines (50 loc) · 1.37 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import React, { CSSProperties, ReactNode } from 'react';
import { CopyOutlined } from '@ant-design/icons';
import { message, Tooltip } from 'antd';
import classNames from 'classnames';
import useClippy from 'use-clippy';
import useLocale from '../locale/useLocale';
import './style.scss';
export interface ICopyProps {
text: string;
title?: ReactNode;
button?: ReactNode;
hideTooltip?: boolean;
style?: CSSProperties;
className?: string;
onCopy?: (text: string) => void;
}
const Copy: React.FC<ICopyProps> = (props) => {
const locale = useLocale('Copy');
const {
button = <CopyOutlined className="dtc-copy__default-icon" />,
text,
title = locale.copy,
hideTooltip,
style,
className,
onCopy = () => message.success(locale.copied),
} = props;
const [_, setClipboard] = useClippy();
const handleCopy = () => {
setClipboard(text);
onCopy(text);
};
const renderCopyButton = () => (
<span
className={classNames(['dtc-copy', className])}
style={style}
onClick={() => handleCopy()}
>
{button}
</span>
);
return !hideTooltip ? (
<Tooltip placement="right" title={title}>
{renderCopyButton()}
</Tooltip>
) : (
renderCopyButton()
);
};
export default Copy;