2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2017-01-20 19:38:18 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-07-03 09:48:17 +02:00
|
|
|
import qrcode from 'qrcode-generator';
|
2017-07-17 18:37:33 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-01-20 19:38:18 +01:00
|
|
|
|
2017-03-31 23:36:24 +02:00
|
|
|
import { calculateType } from './qrSize';
|
|
|
|
|
|
|
|
import styles from './qrCode.css';
|
|
|
|
|
2017-01-20 19:38:18 +01:00
|
|
|
const QROPTS = {
|
2017-03-31 23:36:24 +02:00
|
|
|
ERROR_LEVEL: 'M',
|
|
|
|
MAX_SIZE: 40,
|
|
|
|
MIN_SIZE: 5
|
2017-01-20 19:38:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class QrCode extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
margin: PropTypes.number,
|
|
|
|
size: PropTypes.number,
|
|
|
|
value: PropTypes.string.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
margin: 2,
|
2017-01-27 15:33:02 +01:00
|
|
|
size: 4
|
2017-01-20 19:38:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
image: null
|
|
|
|
};
|
|
|
|
|
|
|
|
componentWillMount () {
|
|
|
|
this.generateCode(this.props);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
const hasChanged = nextProps.value !== this.props.value ||
|
|
|
|
nextProps.size !== this.props.size ||
|
|
|
|
nextProps.margin !== this.props.margin;
|
|
|
|
|
|
|
|
if (hasChanged) {
|
|
|
|
this.generateCode(nextProps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { className } = this.props;
|
|
|
|
const { image } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2017-03-31 23:36:24 +02:00
|
|
|
className={ [styles.qr, className].join(' ') }
|
2017-01-20 19:38:18 +01:00
|
|
|
dangerouslySetInnerHTML={ {
|
|
|
|
__html: image
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
generateCode (props) {
|
|
|
|
const { margin, size, value } = props;
|
2017-03-31 23:36:24 +02:00
|
|
|
const type = calculateType(value.length, QROPTS.ERROR_LEVEL);
|
|
|
|
const qr = qrcode(type, QROPTS.ERROR_LEVEL);
|
2017-01-20 19:38:18 +01:00
|
|
|
|
2017-03-31 23:36:24 +02:00
|
|
|
qr.addData(value, 'Byte');
|
2017-01-20 19:38:18 +01:00
|
|
|
qr.make();
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
image: qr.createImgTag(size, margin)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|