Created
January 20, 2021 11:37
-
-
Save GalloDaSballo/2d72ede9edaeec06a33cc1c02b7f11f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
import CopyToClipboard from "react-copy-to-clipboard"; | |
import CopySuccess from "../static/images/icons/copy-success.svg"; | |
import styles from "./TextInputWithCopy.module.scss"; | |
const TextInputWithCopy = ({ | |
label, | |
text, | |
}: { | |
label: string; | |
text: string; | |
}) => { | |
const [copied, setCopied] = useState(false); | |
useEffect(() => { | |
let timeout = setTimeout(() => null, 5000); | |
if (copied) { | |
timeout = setTimeout(() => { | |
setCopied(false); | |
}, 5000); | |
} | |
return () => clearTimeout(timeout); | |
}, [copied]); | |
return ( | |
<div className={styles.input}> | |
<label>{label}</label> | |
<div className={styles.wrapper}> | |
<input type="text" value={text} spellCheck={false} readOnly /> | |
<CopyToClipboard text={text} onCopy={() => setCopied(true)}> | |
<button className={copied ? styles.success : ""}> | |
{copied ? ( | |
<> | |
Copied <CopySuccess /> | |
</> | |
) : ( | |
"Copy" | |
)} | |
</button> | |
</CopyToClipboard> | |
</div> | |
</div> | |
); | |
}; | |
export default TextInputWithCopy; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment