449dc55aaa
* chore: Upgrade Prettier 1.8 -> 2.0 * chore: Upgrade Babel 6 -> 7 * chore: Upgrade eslint plugins * chore: Add eslint import/order rules * chore: Update flow-typed deps
35 lines
854 B
JavaScript
35 lines
854 B
JavaScript
// @flow
|
|
import copy from "copy-to-clipboard";
|
|
import * as React from "react";
|
|
|
|
type Props = {
|
|
text: string,
|
|
children?: React.Node,
|
|
onClick?: () => void,
|
|
onCopy: () => void,
|
|
};
|
|
|
|
class CopyToClipboard extends React.PureComponent<Props> {
|
|
onClick = (ev: SyntheticEvent<>) => {
|
|
const { text, onCopy, children } = this.props;
|
|
const elem = React.Children.only(children);
|
|
copy(text, {
|
|
debug: process.env.NODE_ENV !== "production",
|
|
});
|
|
|
|
if (onCopy) onCopy();
|
|
|
|
if (elem && elem.props && typeof elem.props.onClick === "function") {
|
|
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;
|