Skip to content

Commit 4d2e1e5

Browse files
committed
add didGeneratePNGData callback prop
1 parent d42027a commit 4d2e1e5

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ React.render(
2222

2323
## Available Props
2424

25-
prop | type | default value
26-
----------|----------------------|--------------
27-
`value` | `string` |
28-
`size` | `number` | `128`
29-
`bgColor` | `string` (CSS color) | `"#FFFFFF"`
30-
`fgColor` | `string` (CSS color) | `"#000000"`
31-
`level` | `string` (`'L' 'M' 'Q' 'H'`) | `'L'`
25+
prop | type | default value
26+
-----------------------|-------------------------------|--------------
27+
`value` | `string` |
28+
`size` | `number` | `128`
29+
`bgColor` | `string` (CSS color) | `"#FFFFFF"`
30+
`fgColor` | `string` (CSS color) | `"#000000"`
31+
`level` | `string` (`'L' 'M' 'Q' 'H'`) | `'L'`
32+
`didGeneratePNGData` | `(string) => void` |
3233

3334
<img src="qrcode.png" height="256" width="256">
3435

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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ type Props = {
3131
level: $Keys<typeof ErrorCorrectLevel>,
3232
bgColor: string,
3333
fgColor: string,
34+
didGeneratePNGData: (string) => void
3435
};
3536

36-
class QRCode extends React.Component {
37+
class QRCode extends React.Component<Props> {
3738
props: Props;
3839
_canvas: ?HTMLCanvasElement;
3940

@@ -42,6 +43,8 @@ class QRCode extends React.Component {
4243
level: 'L',
4344
bgColor: '#FFFFFF',
4445
fgColor: '#000000',
46+
didGeneratePNGData: null,
47+
value: ""
4548
};
4649

4750
static propTypes = {
@@ -50,11 +53,12 @@ class QRCode extends React.Component {
5053
level: PropTypes.oneOf(['L', 'M', 'Q', 'H']),
5154
bgColor: PropTypes.string,
5255
fgColor: PropTypes.string,
56+
didGeneratePNGData: PropTypes.func
5357
};
5458

5559
shouldComponentUpdate(nextProps: Props) {
5660
return Object.keys(QRCode.propTypes).some(
57-
k => this.props[k] !== nextProps[k]
61+
k => this.props[k] !== nextProps[k] && k !== "didGeneratePNGData"
5862
);
5963
}
6064

@@ -106,6 +110,10 @@ class QRCode extends React.Component {
106110
);
107111
});
108112
});
113+
114+
if (this.props.didGeneratePNGData != null) {
115+
this.props.didGeneratePNGData(canvas.toDataURL("image/png"));
116+
}
109117
}
110118
}
111119

0 commit comments

Comments
 (0)