-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCopyToClipboard.tsx
50 lines (42 loc) · 1.2 KB
/
CopyToClipboard.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
import Tooltip, { TooltipProps } from '@material-ui/core/Tooltip';
import copy from 'clipboard-copy';
import React from 'react';
//import * as React from "react";
interface ChildProps {
copy: (content: any) => void;
}
interface Props {
TooltipProps?: Partial<TooltipProps>;
children: (props: ChildProps) => React.ReactElement<any>;
}
interface OwnState {
showTooltip: boolean;
}
/**
* Render prop component that wraps element in a Tooltip that shows "Copied to clipboard!" when the
* copy function is invoked
*/
class CopyToClipboard extends React.Component<Props, OwnState> {
public state: OwnState = { showTooltip: false };
public render() {
return (
<Tooltip
open={this.state.showTooltip}
title={'Copied to clipboard!'}
leaveDelay={1500}
onClose={this.handleOnTooltipClose}
{...(this.props.TooltipProps || {})}
>
{this.props.children({ copy: this.onCopy }) as React.ReactElement<any>}
</Tooltip>
);
}
private onCopy = (content: any) => {
copy(content);
this.setState({ showTooltip: true });
};
private handleOnTooltipClose = () => {
this.setState({ showTooltip: false });
};
}
export default CopyToClipboard;