Skip to content

Commit f5afe2b

Browse files
authored
feat: add boostLevel prop and support value segmentation (#144)
* feat: add boostLevel prop and support value segmentation * update * up
1 parent 7aafb60 commit f5afe2b

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/QRCodeCanvas.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
2626
marginSize,
2727
style,
2828
imageSettings,
29+
boostLevel,
2930
...otherProps
3031
} = props;
3132
const imgSrc = imageSettings?.src;
@@ -54,6 +55,7 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
5455
marginSize,
5556
imageSettings,
5657
size,
58+
boostLevel,
5759
});
5860

5961
React.useEffect(() => {
@@ -134,6 +136,7 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
134136
if (imgSrc != null) {
135137
img = (
136138
<img
139+
alt="QR-Code"
137140
src={imgSrc}
138141
key={imgSrc}
139142
style={{ display: 'none' }}

src/QRCodeSVG.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const QRCodeSVG = React.forwardRef<SVGSVGElement, QRPropsSVG>((props, ref) => {
2424
title,
2525
marginSize,
2626
imageSettings,
27+
boostLevel,
2728
...otherProps
2829
} = props;
2930

@@ -35,6 +36,7 @@ const QRCodeSVG = React.forwardRef<SVGSVGElement, QRPropsSVG>((props, ref) => {
3536
marginSize,
3637
imageSettings,
3738
size,
39+
boostLevel,
3840
});
3941

4042
let cellsToDraw = cells;

src/hooks/useQRCode.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { ERROR_LEVEL_MAP, getImageSettings, getMarginSize } from '../utils';
44
import React from 'react';
55

66
interface Options {
7-
value: string;
7+
value: string | string[];
88
level: ErrorCorrectionLevel;
99
minVersion: number;
1010
includeMargin: boolean;
1111
marginSize?: number;
1212
imageSettings?: ImageSettings;
1313
size: number;
14+
boostLevel?: boolean;
1415
}
1516

1617
export const useQRCode = (opt: Options) => {
@@ -22,12 +23,24 @@ export const useQRCode = (opt: Options) => {
2223
marginSize,
2324
imageSettings,
2425
size,
26+
boostLevel,
2527
} = opt;
2628

27-
const memoizedQrcode = React.useMemo(() => {
28-
const segments = QrSegment.makeSegments(value);
29-
return QrCode.encodeSegments(segments, ERROR_LEVEL_MAP[level], minVersion);
30-
}, [value, level, minVersion]);
29+
const memoizedQrcode = React.useMemo<QrCode>(() => {
30+
const values = Array.isArray(value) ? value : [value];
31+
const segments = values.reduce<QrSegment[]>((acc, val) => {
32+
acc.push(...QrSegment.makeSegments(val));
33+
return acc;
34+
}, []);
35+
return QrCode.encodeSegments(
36+
segments,
37+
ERROR_LEVEL_MAP[level],
38+
minVersion,
39+
undefined,
40+
undefined,
41+
boostLevel,
42+
);
43+
}, [value, level, minVersion, boostLevel]);
3144

3245
return React.useMemo(() => {
3346
const cs = memoizedQrcode.getModules();

src/interface.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ export type QRProps = {
6060
* The value to encode into the QR Code. An array of strings can be passed in
6161
* to represent multiple segments to further optimize the QR Code.
6262
*/
63-
value: string;
63+
value: string | string[];
64+
/**
65+
* If enabled, the Error Correction Level of the result may be higher than
66+
* the specified Error Correction Level option if it can be done without
67+
* increasing the version.
68+
* @defaultValue true
69+
*/
70+
boostLevel?: boolean;
6471
/**
6572
* The size, in pixels, to render the QR Code.
6673
* @defaultValue 128

0 commit comments

Comments
 (0)