Skip to content

Commit 300ada2

Browse files
committed
feat: add boostLevel prop and support value segmentation
1 parent 59e4c1b commit 300ada2

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

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[]>((accum, v) => {
32+
accum.push(...QrSegment.makeSegments(v));
33+
return accum;
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)