Skip to content

Commit 0e51466

Browse files
committed
Merge tag '5.1.2' into develop
Release 5.1.2
2 parents 8857b88 + 58c611f commit 0e51466

4 files changed

Lines changed: 56 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ logoMargin | 2 | logo's distance to its wrapper
104104
logoBorderRadius | 0 | the border-radius of logo image (Android is not supported)
105105
getRef | null | Get SVG ref for further usage
106106
ecl | 'M' | Error correction level
107+
onError(error) | undefined | Callback fired when exception happened during the code generating process
107108

108109

109110
## Saving generated code to gallery

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-qrcode-svg",
3-
"version": "5.1.1",
3+
"version": "5.1.2",
44
"description": "A QR Code generator for React Native based on react-native-svg and javascript-qrcode.",
55
"main": "index.js",
66
"types": "index.d.ts",

src/__tests__/QRCode-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,30 @@ describe('QRCode', () => {
1818
).toJSON()
1919
expect(tree).toMatchSnapshot()
2020
})
21+
22+
// Let's simulate too big data passed to QRCode and check if onError Callback
23+
// Called properly
24+
it('calls onError in case of issue with code generating', () => {
25+
const onErrorMock = jest.fn()
26+
// Let's try to render with too big amount of data that should
27+
// throw an exception
28+
renderer.create(
29+
<QRCode
30+
value={(new Array(1000000)).join('123')}
31+
onError={onErrorMock}
32+
/>
33+
)
34+
expect(onErrorMock.mock.calls.length).toBe(1)
35+
})
36+
37+
it('does not call onError in case if value is fine', () => {
38+
const onErrorMock = jest.fn()
39+
renderer.create(
40+
<QRCode
41+
value='123'
42+
onError={onErrorMock}
43+
/>
44+
)
45+
expect(onErrorMock.mock.calls.length).toBe(0)
46+
})
2147
})

src/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ export default class QRCode extends PureComponent {
3434
/* get svg ref for further usage */
3535
getRef: PropTypes.func,
3636
/* error correction level */
37-
ecl: PropTypes.oneOf(['L', 'M', 'Q', 'H'])
37+
ecl: PropTypes.oneOf(['L', 'M', 'Q', 'H']),
38+
/* Callback function that's called in case if any errors
39+
* appeared during the process of code generating.
40+
* Error object is passed to the callback.
41+
*/
42+
onError: PropTypes.func
3843
};
3944
static defaultProps = {
4045
value: 'This is a QR Code.',
@@ -45,7 +50,8 @@ export default class QRCode extends PureComponent {
4550
logoBackgroundColor: DEFAULT_BG_COLOR,
4651
logoMargin: 2,
4752
logoBorderRadius: 0,
48-
ecl: 'M'
53+
ecl: 'M',
54+
onError: undefined
4955
};
5056
constructor (props) {
5157
super(props)
@@ -62,10 +68,19 @@ export default class QRCode extends PureComponent {
6268
}
6369
/* calculate the size of the cell and draw the path */
6470
setMatrix (props) {
65-
const { value, size, ecl } = props
66-
this._matrix = genMatrix(value, ecl)
67-
this._cellSize = size / this._matrix.length
68-
this._path = this.transformMatrixIntoPath()
71+
const { value, size, ecl, onError } = props
72+
try {
73+
this._matrix = genMatrix(value, ecl)
74+
this._cellSize = size / this._matrix.length
75+
this._path = this.transformMatrixIntoPath()
76+
} catch (error) {
77+
if (onError && typeof onError === 'function') {
78+
onError(error)
79+
} else {
80+
// Pass the error when no handler presented
81+
throw error
82+
}
83+
}
6984
}
7085
/* project the matrix into path draw */
7186
transformMatrixIntoPath () {
@@ -129,11 +144,13 @@ export default class QRCode extends PureComponent {
129144
height={size}
130145
fill={backgroundColor}
131146
/>
132-
<Path
133-
d={this._path}
134-
stroke={color}
135-
strokeWidth={this._cellSize}
136-
/>
147+
{ this._path && this._cellSize && (
148+
<Path
149+
d={this._path}
150+
stroke={color}
151+
strokeWidth={this._cellSize}
152+
/>
153+
)}
137154
{logo && (
138155
<G x={logoPosition} y={logoPosition}>
139156
<Rect

0 commit comments

Comments
 (0)