2017-08-03 05:10:58 +00:00
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
import copy from "copy-to-clipboard";
|
2020-08-09 05:53:59 +00:00
|
|
|
import * as React from "react";
|
2017-08-03 05:10:58 +00:00
|
|
|
|
2017-08-23 03:26:05 +00:00
|
|
|
type Props = {
|
|
|
|
text: string,
|
2018-05-05 23:16:08 +00:00
|
|
|
children?: React.Node,
|
2019-08-09 06:09:09 +00:00
|
|
|
onClick?: () => void,
|
|
|
|
onCopy: () => void,
|
2017-08-23 03:26:05 +00:00
|
|
|
};
|
|
|
|
|
2018-05-05 23:16:08 +00:00
|
|
|
class CopyToClipboard extends React.PureComponent<Props> {
|
2019-08-09 06:09:09 +00:00
|
|
|
onClick = (ev: SyntheticEvent<>) => {
|
2017-08-03 05:10:58 +00:00
|
|
|
const { text, onCopy, children } = this.props;
|
|
|
|
const elem = React.Children.only(children);
|
2017-08-23 03:26:05 +00:00
|
|
|
copy(text, {
|
2020-07-18 18:02:40 +00:00
|
|
|
debug: process.env.NODE_ENV !== "production",
|
2017-08-03 05:10:58 +00:00
|
|
|
});
|
|
|
|
|
2017-08-23 03:26:05 +00:00
|
|
|
if (onCopy) onCopy();
|
2017-08-03 05:10:58 +00:00
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
if (elem && elem.props && typeof elem.props.onClick === "function") {
|
2017-08-03 05:10:58 +00:00
|
|
|
elem.props.onClick(ev);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { text: _text, onCopy: _onCopy, children, ...rest } = this.props;
|
|
|
|
const elem = React.Children.only(children);
|
|
|
|
return React.cloneElement(elem, { ...rest, onClick: this.onClick });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CopyToClipboard;
|