Skip to content

Commit 5c03aa5

Browse files
authored
Merge pull request #7 from rishichawda/development
Development
2 parents 25ff1d1 + 81e42fd commit 5c03aa5

File tree

6 files changed

+253
-25
lines changed

6 files changed

+253
-25
lines changed

README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
# THIS PROJECT IS NO LONGER MAINTAINED
1+
#### This is a fork of [react-native-qrcode](https://github.com/cssivision/react-native-qrcode) with fixes and other enhancements since the original is not maintained anymore.
22

33
# react-native-qrcode
4-
A react-native component to generate [QRcode](http://en.wikipedia.org/wiki/QR_code), not only support English.
4+
A react-native component to generate [QRcode](http://en.wikipedia.org/wiki/QR_code).
55

66
## Installation
7+
78
```sh
8-
npm install react-native-qrcode --save
9+
npm install react-native-qrcode-generator --save
910
```
11+
12+
*(**Note** : If you want to use the original unmaintained package, [visit this link](https://github.com/cssivision/react-native-qrcode)).*
13+
1014
## Usage
1115
```jsx
1216
'use strict';
1317

1418
import React, { Component } from 'react'
15-
import QRCode from 'react-native-qrcode';
19+
import QRCode from 'react-native-qrcode-generator';
1620

1721
import {
1822
AppRegistry,
@@ -37,7 +41,7 @@ class HelloWorld extends Component {
3741
<QRCode
3842
value={this.state.text}
3943
size={200}
40-
bgColor='purple'
44+
bgColor='black'
4145
fgColor='white'/>
4246
</View>
4347
);
@@ -66,17 +70,24 @@ AppRegistry.registerComponent('HelloWorld', () => HelloWorld);
6670

6771
module.exports = HelloWorld;
6872
```
73+
74+
#### Output :
75+
76+
<img src='qrcode.png' height = '256' width = '256'/>
77+
78+
##
79+
6980
## Available Props
7081

71-
prop | type | default value
82+
prop | type | Description
7283
----------|----------------------|--------------
73-
`value` | `string` | `http://facebook.github.io/react-native/`
74-
`size` | `number` | `128`
75-
`bgColor` | `string` (CSS color) | `"#000"`
76-
`fgColor` | `string` (CSS color) | `"#FFF"`
84+
`value` | `string` ( **Default** : `http://facebook.github.io/react-native/` ) | Value of the QRCode.
85+
`size` | `number` ( **Default** : `128` ) | Size of the qrcode / image.
86+
`bgColor` | `string` ( **Default** : `white` ) | Background Color for the qrcode / image.
87+
`fgColor` | `string` ( **Default** : `black` ) | Foreground Color for the qrcode / image.
88+
`getImageOnLoad` | `function` | Returns the base64 png image data *( string )*.
7789

78-
<img src='qrcode.png' height = '256' width = '256'/>
7990

80-
# Licenses
91+
## Licenses
8192

82-
All source code is licensed under the [MIT License](https://github.com/cssivision/react-native-qrcode/blob/master/LICENSE).
93+
All source code is licensed under the [MIT License](https://github.com/rishichawda/react-native-qrcode/blob/master/LICENSE).

lib/Canvas.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,40 @@ var createReactClass = require('create-react-class');
77
var {
88
View,
99
WebView,
10-
Platform
10+
Platform,
1111
} = require('react-native');
1212

13+
function getRenderHTML(context, render, generator) {
14+
return `
15+
<style>
16+
*{margin:0;padding:0;}canvas{transform:translateZ(0);}
17+
</style>
18+
<canvas></canvas>
19+
<script>
20+
var canvas = document.querySelector('canvas');
21+
(${render}).call(${context}, canvas);
22+
(${generator}).call('', ${context} , canvas);
23+
</script>"
24+
`;
25+
}
26+
27+
function generateImage(context, canvas) {
28+
if (window.postMessage.length !== 1) {
29+
setTimeout(function() {
30+
(generateImage).call(this, context, canvas);
31+
}, 100);
32+
} else {
33+
const image = document.createElement('canvas');
34+
const imageContext = image.getContext('2d');
35+
image.width = context.size;
36+
image.height = context.size;
37+
imageContext.fillStyle = context.bgColor;
38+
imageContext.fillRect(0, 0, context.size, context.size);
39+
imageContext.drawImage(canvas, 0, 0);
40+
window.postMessage(image.toDataURL());
41+
}
42+
}
43+
1344
var Canvas = createReactClass({
1445
propTypes: {
1546
style: PropTypes.object,
@@ -19,16 +50,23 @@ var Canvas = createReactClass({
1950
onLoadEnd: PropTypes.func,
2051
},
2152

53+
getImage: function(event) {
54+
if(this.props.generateImage) {
55+
this.props.generateImage(event.nativeEvent.data);
56+
}
57+
},
58+
2259
render() {
2360
var contextString = JSON.stringify(this.props.context);
2461
var renderString = this.props.render.toString();
62+
var generate = generateImage.toString();
2563
return (
2664
<View style={this.props.style}>
2765
<WebView
2866
automaticallyAdjustContentInsets={false}
2967
scalesPageToFit={Platform.OS === 'android'}
3068
contentInset={{top: 0, right: 0, bottom: 0, left: 0}}
31-
source={{html: "<style>*{margin:0;padding:0;}canvas{transform:translateZ(0);}</style><canvas></canvas><script>var canvas = document.querySelector('canvas');(" + renderString + ").call(" + contextString + ", canvas);</script>"}}
69+
source={{ html: getRenderHTML(contextString, renderString, generate), baseUrl: '' }}
3270
opaque={false}
3371
underlayColor={'transparent'}
3472
style={this.props.style}
@@ -37,6 +75,7 @@ var Canvas = createReactClass({
3775
onLoad={this.props.onLoad}
3876
onLoadEnd={this.props.onLoadEnd}
3977
originWhitelist={['*']}
78+
onMessage={this.getImage}
4079
/>
4180
</View>
4281
);

lib/QRCode.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Canvas = require('./Canvas.js');
77
var qr = require('qr.js');
88
var {
99
View,
10+
ActivityIndicator,
1011
} = require('react-native');
1112

1213
function renderCanvas(canvas) {
@@ -47,6 +48,10 @@ function renderCanvas(canvas) {
4748
});
4849
}
4950

51+
function getDiff(x, y) {
52+
return Object.keys(x).some(value => x[value] !== y[value]);
53+
}
54+
5055
var QRCode = createReactClass({
5156
PropTypes: {
5257
value: PropTypes.string,
@@ -55,19 +60,40 @@ var QRCode = createReactClass({
5560
fgColor: PropTypes.string,
5661
onLoad: PropTypes.func,
5762
onLoadEnd: PropTypes.func,
63+
getImageOnLoad: PropTypes.func,
5864
},
5965

6066
getDefaultProps: function() {
6167
return {
62-
value: 'https://github.com/cssivision',
68+
value: 'https://github.com/rishichawda/react-native-qrcode-generator',
6369
fgColor: 'white',
6470
bgColor: 'black',
6571
size: 128,
6672
onLoad: () => {},
6773
onLoadEnd: () => {},
74+
getImageOnLoad: () => {},
75+
}
76+
},
77+
78+
componentDidUpdate: function(prev) {
79+
const hasChanged = getDiff(prev, this.props);
80+
if(hasChanged) {
81+
this.setState({ isLoading: true });
6882
}
6983
},
7084

85+
getInitialState: function() {
86+
return { isLoading: true };
87+
},
88+
89+
onLoadEnd: function() {
90+
this.setState({
91+
isLoading: false,
92+
}, () => {
93+
this.props.onLoadEnd();
94+
});
95+
},
96+
7197
utf16to8: function(str) {
7298
var out, i, len, c;
7399
out = "";
@@ -91,8 +117,11 @@ var QRCode = createReactClass({
91117
render: function() {
92118
var size = this.props.size;
93119
var value = this.utf16to8(this.props.value);
120+
var styles = this.state.isLoading ? { width: 0, height: 0, overflow: 'hidden' } : undefined;
94121
return (
95-
<View>
122+
<>
123+
{ this.state.isLoading && <ActivityIndicator /> }
124+
<View style={styles}>
96125
<Canvas
97126
context={{
98127
size: size,
@@ -101,12 +130,14 @@ var QRCode = createReactClass({
101130
fgColor: this.props.fgColor,
102131
cells: qr(value).modules,
103132
}}
133+
generateImage={this.props.getImageOnLoad}
104134
render={renderCanvas}
105135
onLoad={this.props.onLoad}
106-
onLoadEnd={this.props.onLoadEnd}
136+
onLoadEnd={this.onLoadEnd}
107137
style={{height: size, width: size}}
108138
/>
109139
</View>
140+
</>
110141
);
111142
}
112143
});

package-lock.json

Lines changed: 147 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)