Skip to content
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
15,853 changes: 15,853 additions & 0 deletions Example/package-lock.json

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ const colorfulQRCODe = (
</Description>
);

const versionedQRCode = (
<Description text="QR code with version and additional props">
<QRCode
value="Versioned QR code"
size={qrCodeSize}
version={25}
/>
</Description>
);

const urlPngQRCode = (
<Description text="QR code with PNG from url avatar">
<QRCode
Expand Down Expand Up @@ -117,6 +127,7 @@ const App = () => {
<Text style={styles.title}>Example usages of QRCode component</Text>
{defaultQRCode}
{colorfulQRCODe}
{versionedQRCode}
{urlPngQRCode}
{localPngQRCode}
{localPngBackgroundColorQRCode}
Expand Down
2,750 changes: 1,443 additions & 1,307 deletions Example/yarn.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

[![NPM](https://nodei.co/npm/react-native-qrcode-svg.png)](https://npmjs.org/package/react-native-qrcode-svg)

# react-native-qrcode-svg
Expand Down Expand Up @@ -123,7 +122,8 @@ quietZone | 0 | quiet zone around the qr in pixels (useful when saving image to
getRef | null | Get SVG ref for further usage
ecl | 'M' | Error correction level
onError(error) | undefined | Callback fired when exception happened during the code generating process

version | undefined | QR Code version (1-40). Higher version = bigger QR code, more data.
additionalProps | undefined | Additional properties to pass directly to the underlying qrcode library.

## Saving generated code to gallery
_Note: Experimental only ( not tested on iOS) , uses getRef() and needs [RNFS module](https://github.com/itinance/react-native-fs)_
Expand Down
10 changes: 7 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from "react";
import type {ImageSourcePropType} from "react-native";
import type {SvgProps} from "react-native-svg";
import type { ImageSourcePropType } from "react-native";
import type { SvgProps } from "react-native-svg";

declare class QRCode extends React.PureComponent<QRCodeProps, any> {}
declare class QRCode extends React.PureComponent<QRCodeProps, any> { }

export interface QRCodeProps {
/* what the qr code stands for */
Expand Down Expand Up @@ -45,6 +45,10 @@ export interface QRCodeProps {
ecl?: "L" | "M" | "Q" | "H";
/* error handler called when matrix fails to generate */
onError?: Function;
/* QR Code version (1-40). Higher version = bigger QR code, more data. It automatically adjusts for best version size based on data entered. */
version?: number;
/* Additional properties to pass directly to the underlying qrcode library. */
additionalProps?: Record<string, any>;
/** testID for testing */
testID?: string;
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/__tests__/Matrix-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ it('generates with ecl:Q correctly', () => {
const matrix = genMatrix('test', 'Q')
expect(matrix).toMatchSnapshot()
})

it('generates with version and additionalProps correctly', () => {
const matrix = genMatrix('test', 'M', 5, { maskPattern: 2 })
expect(matrix).toMatchSnapshot()
})
7 changes: 5 additions & 2 deletions src/genMatrix.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import QRCode from 'qrcode'

export default (value, errorCorrectionLevel) => {
const arr = Array.prototype.slice.call(QRCode.create(value, { errorCorrectionLevel }).modules.data, 0)
export default (value, errorCorrectionLevel, version, additionalProps = {}) => {
const arr = Array.prototype.slice.call(
QRCode.create(value, { errorCorrectionLevel, version, ...additionalProps }).modules.data,
0
)
const sqrt = Math.sqrt(arr.length)
return arr.reduce((rows, key, index) => (index % sqrt === 0 ? rows.push([key]) : rows[rows.length - 1].push(key)) && rows, [])
}
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,15 @@ const QRCode = ({
getRef,
onError,
testID,
// version = '' any number from 1 to 40, if left empty, auto selected based on data length
additionalProps,
}) => {
const result = useMemo(() => {
try {
return transformMatrixIntoPath(genMatrix(value, ecl), size);
return transformMatrixIntoPath(
genMatrix(value, ecl, version, additionalProps),
size
);
} catch (error) {
if (onError && typeof onError === "function") {
onError(error);
Expand All @@ -111,7 +116,7 @@ const QRCode = ({
throw error;
}
}
}, [value, size, ecl]);
}, [value, size, ecl, version, additionalProps]);

if (!result) {
return null;
Expand Down