-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathQRCode.tsx
More file actions
64 lines (58 loc) · 1.97 KB
/
QRCode.tsx
File metadata and controls
64 lines (58 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { QRCodeSVG } from "qrcode.react";
import { forwardRef, ReactElement, useRef } from "react";
import { DownloadButton } from "./DownloadButton";
import { QRCodeTypeConfig } from "../config/Barcode.config";
import { downloadCode } from "../utils/download-code";
interface QRCodeRendererProps {
config: QRCodeTypeConfig;
}
interface QRCodeSVGWrapperProps {
value: string;
size?: number;
level?: string;
marginSize?: number;
title?: string;
imageSettings?: any;
}
const QRCodeSVGWrapper = forwardRef<SVGSVGElement, QRCodeSVGWrapperProps>(
({ value, size, level, marginSize, title, imageSettings }, ref) => (
<QRCodeSVG
ref={ref as any}
value={value}
size={size}
level={level as any}
marginSize={marginSize}
title={title}
imageSettings={imageSettings}
/>
)
);
QRCodeSVGWrapper.displayName = "QRCodeSVGWrapper";
export function QRCodeRenderer({ config }: QRCodeRendererProps): ReactElement {
const ref = useRef<SVGSVGElement>(null);
const { codeValue, downloadButton, size, margin, title, level, overlay } = config;
const buttonPosition = downloadButton?.buttonPosition ?? "bottom";
const button = downloadButton && (
<DownloadButton
onClick={() => downloadCode(ref, config, downloadButton.fileName)}
ariaLabel={downloadButton.label}
caption={downloadButton.caption}
/>
);
return (
<div className="qrcode-renderer">
{config.showTitle && <h3 className="qrcode-renderer-title">{title}</h3>}
{buttonPosition === "top" && button}
<QRCodeSVGWrapper
ref={ref}
value={codeValue}
size={size}
level={level}
marginSize={margin}
title={title}
imageSettings={overlay}
/>
{buttonPosition === "bottom" && button}
</div>
);
}