|
|
@@ -3,27 +3,24 @@ import ClipboardJS from "clipboard";
|
|
|
import React, { Component, RefObject } from "react";
|
|
|
import styled from "styled-components";
|
|
|
|
|
|
-
|
|
|
-
|
|
|
type PropsType = {
|
|
|
- text: string,
|
|
|
- onSuccess?: (e: ClipboardJS.Event) => void
|
|
|
- onError?: (e: ClipboardJS.Event) => void
|
|
|
+ text: string;
|
|
|
+ onSuccess?: (e: ClipboardJS.Event) => void;
|
|
|
+ onError?: (e: ClipboardJS.Event) => void;
|
|
|
};
|
|
|
|
|
|
type StateType = {
|
|
|
- clipboard: ClipboardJS | undefined,
|
|
|
+ clipboard: ClipboardJS | undefined;
|
|
|
};
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* Enables parent onClick to copy the text provided to the CopyToClipboard element.
|
|
|
- *
|
|
|
- *
|
|
|
- * Example of usage:
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * Example of usage:
|
|
|
* <MyCustomComponent>
|
|
|
- * <CopyToClipboard
|
|
|
- * text={`some usefull text ${var}`}
|
|
|
+ * <CopyToClipboard
|
|
|
+ * text={`some usefull text ${var}`}
|
|
|
* onSuccess={(e) => console.log("Success event:", e)}
|
|
|
* onError={(e) => console.log("Error event:", e)}
|
|
|
* />
|
|
|
@@ -31,33 +28,35 @@ type StateType = {
|
|
|
*/
|
|
|
export default class CopyToClipboard extends Component<PropsType, StateType> {
|
|
|
triggerRef: RefObject<HTMLSpanElement>;
|
|
|
-
|
|
|
+
|
|
|
state: StateType = {
|
|
|
clipboard: undefined,
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
constructor(props: PropsType) {
|
|
|
super(props);
|
|
|
this.triggerRef = React.createRef();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
componentDidMount() {
|
|
|
const trigger = this.triggerRef.current.parentElement;
|
|
|
if (!trigger) {
|
|
|
- console.error('Couldn\'t find a parent element. The CopyToClipboard component should be inside the trigger component, for example a button')
|
|
|
+ console.error(
|
|
|
+ "Couldn't find a parent element. The CopyToClipboard component should be inside the trigger component, for example a button"
|
|
|
+ );
|
|
|
return;
|
|
|
}
|
|
|
const clipboard = new ClipboardJS(trigger, {
|
|
|
text: () => {
|
|
|
return this.props.text;
|
|
|
- }
|
|
|
+ },
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
this.props.onSuccess && clipboard.on("success", this.props.onSuccess);
|
|
|
-
|
|
|
+
|
|
|
this.props.onError && clipboard.on("error", this.props.onError);
|
|
|
-
|
|
|
- this.setState({clipboard})
|
|
|
+
|
|
|
+ this.setState({ clipboard });
|
|
|
}
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
@@ -67,14 +66,10 @@ export default class CopyToClipboard extends Component<PropsType, StateType> {
|
|
|
}
|
|
|
|
|
|
render() {
|
|
|
- return (
|
|
|
- <NonVisibleSpan ref={this.triggerRef}>
|
|
|
- </NonVisibleSpan>
|
|
|
- )
|
|
|
+ return <NonVisibleSpan ref={this.triggerRef}></NonVisibleSpan>;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
const NonVisibleSpan = styled.span`
|
|
|
display: none;
|
|
|
-`
|
|
|
-
|
|
|
+`;
|