@@ -33,7 +33,7 @@ type Props = {
3333 fgColor : string ,
3434} ;
3535
36- class QRCode extends React . Component {
36+ class QRCode extends React . Component < Props > {
3737 props : Props ;
3838 _canvas : ?HTMLCanvasElement ;
3939
@@ -42,6 +42,8 @@ class QRCode extends React.Component {
4242 level : 'L' ,
4343 bgColor : '#FFFFFF' ,
4444 fgColor : '#000000' ,
45+ didGeneratePNGData : null ,
46+ value : ""
4547 } ;
4648
4749 static propTypes = {
@@ -50,11 +52,12 @@ class QRCode extends React.Component {
5052 level : PropTypes . oneOf ( [ 'L' , 'M' , 'Q' , 'H' ] ) ,
5153 bgColor : PropTypes . string ,
5254 fgColor : PropTypes . string ,
55+ didGeneratePNGData : PropTypes . func
5356 } ;
5457
5558 shouldComponentUpdate ( nextProps : Props ) {
5659 return Object . keys ( QRCode . propTypes ) . some (
57- k => this . props [ k ] !== nextProps [ k ]
60+ k => this . props [ k ] !== nextProps [ k ] && k !== "didGeneratePNGData"
5861 ) ;
5962 }
6063
@@ -67,7 +70,7 @@ class QRCode extends React.Component {
6770 }
6871
6972 update ( ) {
70- var { value, size, level, bgColor, fgColor} = this . props ;
73+ var { value, size, level, bgColor, fgColor } = this . props ;
7174
7275 // We'll use type===-1 to force QRCode to automatically pick the best type
7376 var qrcode = new QRCodeImpl ( - 1 , ErrorCorrectLevel [ level ] ) ;
@@ -92,8 +95,8 @@ class QRCode extends React.Component {
9295 canvas . height = canvas . width = size * scale ;
9396 ctx . scale ( scale , scale ) ;
9497
95- cells . forEach ( function ( row , rdx ) {
96- row . forEach ( function ( cell , cdx ) {
98+ cells . forEach ( function ( row , rdx ) {
99+ row . forEach ( function ( cell , cdx ) {
97100 ctx && ( ctx . fillStyle = cell ? fgColor : bgColor ) ;
98101 var w = Math . ceil ( ( cdx + 1 ) * tileW ) - Math . floor ( cdx * tileW ) ;
99102 var h = Math . ceil ( ( rdx + 1 ) * tileH ) - Math . floor ( rdx * tileH ) ;
@@ -106,13 +109,17 @@ class QRCode extends React.Component {
106109 ) ;
107110 } ) ;
108111 } ) ;
112+
113+ if ( this . props . didGeneratePNGData != null ) {
114+ this . props . didGeneratePNGData ( canvas . toDataURL ( "image/png" ) ) ;
115+ }
109116 }
110117 }
111118
112119 render ( ) {
113120 return (
114121 < canvas
115- style = { { height : this . props . size , width : this . props . size } }
122+ style = { { height : this . props . size , width : this . props . size } }
116123 height = { this . props . size }
117124 width = { this . props . size }
118125 ref = { ( ref : ?HTMLCanvasElement ) : ?HTMLCanvasElement =>
0 commit comments