Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: value support string[] type #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/QRCodeCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
marginSize,
style,
imageSettings,
boostLevel,
...otherProps
} = props;
const imgSrc = imageSettings?.src;
Expand Down Expand Up @@ -54,6 +55,7 @@
marginSize,
imageSettings,
size,
boostLevel,
});

React.useEffect(() => {
Expand Down Expand Up @@ -137,9 +139,7 @@
src={imgSrc}
key={imgSrc}
style={{ display: 'none' }}
onLoad={() => {
setIsImageLoaded(true);
}}
onLoad={() => setIsImageLoaded(true)}

Check warning on line 142 in src/QRCodeCanvas.tsx

View check run for this annotation

Codecov / codecov/patch

src/QRCodeCanvas.tsx#L142

Added line #L142 was not covered by tests
ref={_image}
// when crossOrigin is not set, the image will be tainted
// and the canvas cannot be exported to an image
Expand Down
2 changes: 2 additions & 0 deletions src/QRCodeSVG.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const QRCodeSVG = React.forwardRef<SVGSVGElement, QRPropsSVG>((props, ref) => {
title,
marginSize,
imageSettings,
boostLevel,
...otherProps
} = props;

Expand All @@ -35,6 +36,7 @@ const QRCodeSVG = React.forwardRef<SVGSVGElement, QRPropsSVG>((props, ref) => {
marginSize,
imageSettings,
size,
boostLevel,
});

let cellsToDraw = cells;
Expand Down
21 changes: 17 additions & 4 deletions src/hooks/useQRCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { ERROR_LEVEL_MAP, getImageSettings, getMarginSize } from '../utils';
import React from 'react';

interface Options {
value: string;
value: string | string[];
level: ErrorCorrectionLevel;
minVersion: number;
includeMargin: boolean;
marginSize?: number;
imageSettings?: ImageSettings;
size: number;
boostLevel?: boolean;
}

export const useQRCode = (opt: Options) => {
Expand All @@ -22,12 +23,24 @@ export const useQRCode = (opt: Options) => {
marginSize,
imageSettings,
size,
boostLevel,
} = opt;

const memoizedQrcode = React.useMemo(() => {
const segments = QrSegment.makeSegments(value);
return QrCode.encodeSegments(segments, ERROR_LEVEL_MAP[level], minVersion);
}, [value, level, minVersion]);
const values = Array.isArray(value) ? value : [value];
const segments = values.reduce<QrSegment[]>((acc, v) => {
acc.push(...QrSegment.makeSegments(v));
return acc;
}, []);
return QrCode.encodeSegments(
segments,
ERROR_LEVEL_MAP[level],
minVersion,
undefined,
undefined,
boostLevel,
);
}, [value, level, minVersion, boostLevel]);

return React.useMemo(() => {
const cs = memoizedQrcode.getModules();
Expand Down
10 changes: 9 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type QRProps = {
* The value to encode into the QR Code. An array of strings can be passed in
* to represent multiple segments to further optimize the QR Code.
*/
value: string;
value: string | string[];
/**
* The size, in pixels, to render the QR Code.
* @defaultValue 128
Expand Down Expand Up @@ -118,6 +118,14 @@ export type QRProps = {
* @defaultValue 1
*/
minVersion?: number;

/**
* If enabled, the Error Correction Level of the result may be higher than
* the specified Error Correction Level option if it can be done without
* increasing the version.
* @defaultValue true
*/
boostLevel?: boolean;
};

export type QRPropsCanvas = QRProps &
Expand Down
Loading