17
17
18
18
import { useLoad } from '../../hooks/api' ;
19
19
import { getQRCode } from '../../api/backend' ;
20
+ import { Check } from '../icon' ;
21
+ import { useEffect , useRef , useState } from 'react' ;
22
+ import { useTranslation } from 'react-i18next' ;
20
23
import style from './qrcode.module.css' ;
21
24
22
25
type TProps = {
23
26
data ?: string ;
24
27
size ?: number ;
28
+ tapToCopy ?: boolean ;
25
29
} ;
26
30
27
31
export const QRCode = ( {
28
32
data,
29
33
size = 256 ,
34
+ tapToCopy = true
30
35
} : TProps ) => {
31
36
const qrCode = useLoad ( data !== undefined ? getQRCode ( data ) : null , [ data ] ) ;
37
+
32
38
if ( ! qrCode ) {
33
39
if ( data !== undefined ) {
34
40
return < div className = { style . empty } > </ div > ;
@@ -45,6 +51,49 @@ export const QRCode = ({
45
51
}
46
52
47
53
return (
48
- < img width = { size } height = { size } src = { qrCode . data } />
54
+ tapToCopy ?
55
+ < TapToCopyQRCode data = { data } qrCodeData = { qrCode . data } size = { size } /> :
56
+ < img width = { size } height = { size } src = { qrCode . data } />
57
+ ) ;
58
+ } ;
59
+
60
+ type TTapToCopyQRCodeProps = {
61
+ data ?: string ;
62
+ qrCodeData : string
63
+ size : number
64
+ }
65
+
66
+ const TapToCopyQRCode = ( { data, qrCodeData, size } : TTapToCopyQRCodeProps ) => {
67
+ const inputRef = useRef < HTMLInputElement > ( null ) ;
68
+ const [ success , setSuccess ] = useState ( false ) ;
69
+
70
+ const { t } = useTranslation ( ) ;
71
+
72
+ useEffect ( ( ) => {
73
+ if ( success ) {
74
+ setTimeout ( ( ) => setSuccess ( false ) , 1500 ) ;
75
+ }
76
+ } , [ success ] ) ;
77
+
78
+
79
+ const handleCopy = ( ) => {
80
+ inputRef . current ?. select ( ) ;
81
+ if ( document . execCommand ( 'copy' ) ) {
82
+ setSuccess ( true ) ;
83
+ }
84
+ } ;
85
+
86
+
87
+ return (
88
+ < div onClick = { handleCopy } >
89
+ < input className = { style . hiddenInput } ref = { inputRef } value = { data } readOnly />
90
+ < div style = { { width : size , height : size } } className = { style . outerContainer } >
91
+ < img className = { `${ style . qrCodeContainer } ${ success ? style . hide : style . show } ` } width = { size } height = { size } src = { qrCodeData } />
92
+ < div className = { `${ style . checkContainer } ${ style . show } ` } >
93
+ < Check width = { size / 2 } height = { size / 2 } />
94
+ </ div >
95
+ </ div >
96
+ < p className = { `${ style . copiedText } ${ success ? style . show : style . hide } ` } > { t ( 'receive.qrCodeCopiedMessage' ) } </ p >
97
+ </ div >
49
98
) ;
50
99
} ;
0 commit comments