Skip to content

Commit ef97801

Browse files
committed
add didGeneratePNGData callback prop
1 parent d42027a commit ef97801

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"homepage": "http://zpao.github.io/qrcode.react",
1111
"main": "lib/index.js",
1212
"scripts": {
13-
"flow": "flow",
13+
"flow": "./node_modules/.bin/flow",
1414
"lint": "eslint src/ examples/demo.js",
1515
"pretty": "prettier --write --single-quote --bracket-spacing=false --trailing-comma=es5 {.eslintrc.js,src/*.js,examples/demo.js}",
16-
"prepublish": "flow && make clean && make all",
16+
"prepublish": "./node_modules/.bin/flow && make clean && make all",
1717
"prepublish-docs": "make clean && make all",
1818
"publish-docs": "gh-pages --dist=examples --src='{index.html,bundle.js}'",
1919
"test": "echo \"Error: no test specified\" && exit 1"
@@ -45,10 +45,10 @@
4545
"eslint": "^3.19.0",
4646
"eslint-config-prettier": "^1.6.0",
4747
"eslint-plugin-react": "^3.16.1",
48-
"flow-bin": "^0.45.0",
48+
"flow-bin": "^0.57.3",
4949
"gh-pages": "^0.12.0",
5050
"prettier": "^1.1.0",
5151
"react": "^16.0.0",
5252
"react-dom": "^16.0.0"
5353
}
54-
}
54+
}

src/index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)