-
Notifications
You must be signed in to change notification settings - Fork 126
/
Component.js
60 lines (47 loc) · 1.17 KB
/
Component.js
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
58
59
60
import React from 'react';
import PropTypes from 'prop-types';
import copy from 'copy-to-clipboard';
export class CopyToClipboard extends React.PureComponent {
static propTypes = {
text: PropTypes.string.isRequired,
children: PropTypes.element.isRequired,
onCopy: PropTypes.func,
options: PropTypes.shape({
debug: PropTypes.bool,
message: PropTypes.string,
format: PropTypes.string
})
};
static defaultProps = {
onCopy: undefined,
options: undefined
};
onClick = event => {
const {
text,
onCopy,
children,
options
} = this.props;
const elem = React.Children.only(children);
const result = copy(text, options);
if (onCopy) {
onCopy(text, result);
}
// Bypass onClick if it was present
if (elem && elem.props && typeof elem.props.onClick === 'function') {
elem.props.onClick(event);
}
};
render() {
const {
text: _text,
onCopy: _onCopy,
options: _options,
children,
...props
} = this.props;
const elem = React.Children.only(children);
return React.cloneElement(elem, {...props, onClick: this.onClick});
}
}