Skip to content

Commit 4a94560

Browse files
committed
Merge branch 'release/4.0.2'
2 parents b275a10 + ab2f1b2 commit 4a94560

9 files changed

Lines changed: 1064 additions & 131 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ render() {
6969
7070
```
7171

72+
```
73+
// get base64 string encode of the qrcode (currently logo is not included)
74+
getDataURL() {
75+
this.svg.toDataURL(this.callback);
76+
}
77+
callback(dataURL) {
78+
console.log(dataURL);
79+
}
80+
render() {
81+
return (
82+
<QRCode
83+
value="Just some string value"
84+
getRef={(c) => (this.svg = c)}
85+
/>
86+
);
87+
}
88+
```
89+
7290

7391
### Props
7492

@@ -80,6 +98,7 @@ color | 'black' | Color of the QR code
8098
logo | null | Image source object. Ex. {uri: 'base64string'} or {require('pathToImage')}
8199
logoSize | 20% of size | Size of the imprinted logo. Bigger logo = less error correction in QR code
82100
logoBackgroundColor | backgroundColor | The logo gets a filled quadratic background with this color. Use 'transparent' if your logo already has its own backdrop.
101+
getRef | null | Get SVG ref for further usage
83102

84103

85104
### Dependencies

package.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
{
22
"name": "react-native-qrcode-svg",
3-
"version": "4.0.1",
3+
"version": "4.0.2",
44
"description": "A QR Code generator for React Native based on react-native-svg and javascript-qrcode.",
55
"main": "index.js",
66
"scripts": {
77
"test": "jest",
88
"test-update": "npm run test -- --updateSnapshot",
9+
"lint-staged": "lint-staged",
10+
"lint": "standard 'src/**/*.js'",
911
"2npm": "publish"
1012
},
13+
"standard": {
14+
"env": [
15+
"jest"
16+
],
17+
"parser": "babel-eslint"
18+
},
19+
"pre-commit": "lint-staged",
20+
"lint-staged": {
21+
"*.js": "lint"
22+
},
1123
"repository": {
1224
"type": "git",
1325
"url": "git+ssh://git@github.com/awesomejerry/react-native-qrcode-svg.git"
@@ -32,11 +44,15 @@
3244
"javascript-qrcode": "git+https://git@github.com/awesomejerry/javascript-qrcode.git"
3345
},
3446
"devDependencies": {
47+
"babel-eslint": "^7.2.2",
3548
"babel-jest": "^19.0.0",
3649
"babel-preset-react-native": "^1.9.1",
3750
"jest": "^19.0.2",
51+
"lint-staged": "^3.4.0",
52+
"pre-commit": "^1.2.2",
3853
"publish": "^0.6.0",
3954
"react": "^15.4.2",
40-
"react-test-renderer": "^15.4.2"
55+
"react-test-renderer": "^15.4.2",
56+
"standard": "^10.0.2"
4157
}
4258
}

src/__mocks__/react-native-svg.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
import React from 'react';
1+
import React from 'react'
22

3-
export default function({ children }) {
3+
export default function ({ children }) {
44
return (
55
<div>
6-
{children}
6+
{children}
77
</div>
8-
);
8+
)
99
}
1010

1111
export const Rect = () => {
1212
return (
1313
<span>Rect</span>
14-
);
15-
};
14+
)
15+
}
1616

1717
export const Path = () => {
1818
return (
1919
<span>Path</span>
20-
);
21-
};
22-
20+
)
21+
}

src/__mocks__/react-native.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import React, { PropTypes } from 'react';
1+
import React, { PropTypes } from 'react'
22

33
export const View = ({ children }) => {
44
return (
55
<div>{children}</div>
6-
);
7-
};
6+
)
7+
}
88

99
export const Image = ({ source, resizeMode }) => {
1010
return (
1111
<div>
1212
<span>Source: {source.uri ? source.uri : source}</span>
1313
<span>ResizeMode: {resizeMode}</span>
1414
</div>
15-
);
16-
};
15+
)
16+
}
1717
Image.propTypes = {
1818
source: PropTypes.oneOfType([
1919
PropTypes.number,
2020
PropTypes.shape({
21-
uri: PropTypes.string,
22-
}),
23-
]),
24-
};
21+
uri: PropTypes.string
22+
})
23+
])
24+
}

src/__tests__/Matrix-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import genMatrix from '../genMatrix';
1+
import genMatrix from '../genMatrix'
22

33
const expected = [
44
[ 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 ],
@@ -22,9 +22,9 @@ const expected = [
2222
[ 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1 ],
2323
[ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 ],
2424
[ 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1 ]
25-
];
25+
]
2626

2727
it('generates correctly', () => {
28-
const matrix = genMatrix('test');
29-
expect(matrix).toEqual(expected);
30-
});
28+
const matrix = genMatrix('test')
29+
expect(matrix).toEqual(expected)
30+
})

src/__tests__/QRCode-test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import React from 'react';
2-
import QRCode from '../index';
3-
import renderer from 'react-test-renderer';
1+
import React from 'react'
2+
import QRCode from '../index'
3+
import renderer from 'react-test-renderer'
44

55
describe('QRCode', () => {
66
it('renders correctly', () => {
77
const tree = renderer.create(
88
<QRCode />
9-
).toJSON();
10-
expect(tree).toMatchSnapshot();
11-
});
9+
).toJSON()
10+
expect(tree).toMatchSnapshot()
11+
})
1212

1313
it('renders with logo correctly', () => {
1414
const tree = renderer.create(
1515
<QRCode
1616
logo={{ uri: 'fakeUri' }}
1717
/>
18-
).toJSON();
19-
expect(tree).toMatchSnapshot();
20-
});
21-
});
18+
).toJSON()
19+
expect(tree).toMatchSnapshot()
20+
})
21+
})

src/genMatrix.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { QrCode } from 'javascript-qrcode';
1+
import { QrCode } from 'javascript-qrcode'
22

33
export default (value) => {
4-
const qrcode = new QrCode(value);
5-
return qrcode.getData();
6-
};
4+
const qrcode = new QrCode(value)
5+
return qrcode.getData()
6+
}

src/index.js

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// core
2-
import React, { PropTypes, PureComponent } from 'react';
3-
import { View, Image } from 'react-native';
2+
import React, { PropTypes, PureComponent } from 'react'
3+
import { View, Image } from 'react-native'
44
// libs
5-
import Svg, { Rect, Path } from 'react-native-svg';
6-
import genMatrix from './genMatrix';
5+
import Svg, { Rect, Path } from 'react-native-svg'
6+
import genMatrix from './genMatrix'
77

88
/**
99
* A simple component for displaying QR Code using svg
@@ -27,84 +27,86 @@ export default class QRCode extends PureComponent {
2727
logoBackgroundColor: PropTypes.string,
2828
/* logo's distance to its wrapper */
2929
logoMargin: PropTypes.number,
30+
/* get svg ref for further usage */
31+
getRef: PropTypes.func
3032
};
3133
static defaultProps = {
3234
value: 'This is a QR Code.',
3335
size: 100,
3436
color: 'black',
3537
backgroundColor: 'white',
36-
logoMargin: 2,
38+
logoMargin: 2
3739
};
38-
constructor(props) {
39-
super(props);
40-
this._matrix = null;
41-
this._cellSize = null;
42-
this._path = null;
43-
this.setMatrix(props);
40+
constructor (props) {
41+
super(props)
42+
this._matrix = null
43+
this._cellSize = null
44+
this._path = null
45+
this.setMatrix(props)
4446
}
45-
componentWillUpdate(nextProps) {
47+
componentWillUpdate (nextProps) {
4648
// if value has changed, re-setMatrix
4749
if (nextProps.value !== this.props.value) {
48-
this.setMatrix(nextProps);
50+
this.setMatrix(nextProps)
4951
}
5052
}
5153
/* calculate the size of the cell and draw the path */
52-
setMatrix(props) {
53-
const { value, size } = props;
54-
this._matrix = genMatrix(value);
55-
this._cellSize = size / (this._matrix.length + 2);
56-
this._path = this.transformMatrixIntoPath();
54+
setMatrix (props) {
55+
const { value, size } = props
56+
this._matrix = genMatrix(value)
57+
this._cellSize = size / (this._matrix.length + 2)
58+
this._path = this.transformMatrixIntoPath()
5759
}
5860
/* project the matrix into path draw */
59-
transformMatrixIntoPath() {
60-
const matrix = this._matrix;
61-
const cellSize = this._cellSize;
61+
transformMatrixIntoPath () {
62+
const matrix = this._matrix
63+
const cellSize = this._cellSize
6264
// adjust origin
63-
const oY = cellSize * 1.5;
64-
const oX = cellSize;
65-
let d = '';
65+
const oY = cellSize * 1.5
66+
const oX = cellSize
67+
let d = ''
6668
matrix.forEach((row, i) => {
67-
let needDraw = false;
69+
let needDraw = false
6870
row.forEach((column, j) => {
6971
if (column) {
7072
if (!needDraw) {
71-
d += `M${oX + cellSize * j} ${oY + cellSize * i} `;
72-
needDraw = true;
73+
d += `M${oX + cellSize * j} ${oY + cellSize * i} `
74+
needDraw = true
7375
}
7476
if (needDraw && j === matrix.length - 1) {
75-
d += `L${oX + cellSize * (j + 1)} ${oY + cellSize * i} `;
77+
d += `L${oX + cellSize * (j + 1)} ${oY + cellSize * i} `
7678
}
7779
} else {
7880
if (needDraw) {
79-
d += `L${oX + cellSize * j} ${oY + cellSize * i} `;
80-
needDraw = false;
81+
d += `L${oX + cellSize * j} ${oY + cellSize * i} `
82+
needDraw = false
8183
}
8284
}
83-
});
84-
});
85-
return d;
85+
})
86+
})
87+
return d
8688
}
87-
renderLogo() {
89+
renderLogo () {
8890
const {
8991
size,
9092
backgroundColor,
9193
logo,
9294
logoBackgroundColor = backgroundColor,
9395
logoSize = size * 0.2,
94-
logoMargin,
95-
} = this.props;
96-
const wrapSize = logoSize + logoMargin * 2;
97-
const position = size / 2 - logoSize / 2 - logoMargin;
96+
logoMargin
97+
} = this.props
98+
const wrapSize = logoSize + logoMargin * 2
99+
const position = size / 2 - logoSize / 2 - logoMargin
98100

99101
return (
100102
<View
101103
style={{
102-
backgroundColor: logoBackgroundColor,
104+
backgroundColor: logoBackgroundColor,
103105
width: wrapSize,
104106
height: wrapSize,
105107
position: 'absolute',
106108
left: position,
107-
top: position,
109+
top: position
108110
}}
109111
>
110112
<Image
@@ -122,12 +124,12 @@ export default class QRCode extends PureComponent {
122124
)
123125
}
124126

125-
render() {
126-
const { size, color, backgroundColor, logo } = this.props;
127+
render () {
128+
const { size, color, backgroundColor, logo, getRef } = this.props
127129

128130
return (
129131
<View>
130-
<Svg width={size} height={size}>
132+
<Svg ref={getRef} width={size} height={size}>
131133
<Rect
132134
x={this._cellSize}
133135
y={this._cellSize}
@@ -143,6 +145,6 @@ export default class QRCode extends PureComponent {
143145
</Svg>
144146
{logo && this.renderLogo()}
145147
</View>
146-
);
148+
)
147149
}
148150
}

0 commit comments

Comments
 (0)