@@ -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