forked from wonsikin/react-native-barcode-builder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
191 lines (166 loc) · 4.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React, { PureComponent } from 'react';
import { View, StyleSheet, Text } from 'react-native';
import PropTypes from 'prop-types';
import barcodes from 'jsbarcode/src/barcodes';
import {Surface, Shape} from '@react-native-community/art';
export default class Barcode extends PureComponent {
static propTypes = {
/* what the barCode stands for */
value: PropTypes.string,
/* Select which barcode type to use */
format: PropTypes.oneOf(Object.keys(barcodes)),
/* Overide the text that is diplayed */
text: PropTypes.string,
/* The width option is the width of a single bar. */
width: PropTypes.number,
/* The height of the barcode. */
height: PropTypes.number,
/* Set the color of the bars */
lineColor: PropTypes.string,
/* Set the color of the text. */
textColor: PropTypes.string,
/* Set the background of the barcode. */
background: PropTypes.string,
/* Handle error for invalid barcode of selected format */
onError: PropTypes.func
};
static defaultProps = {
value: undefined,
format: 'CODE128',
text: undefined,
width: 2,
height: 100,
lineColor: '#000000',
textColor: '#000000',
background: '#ffffff',
onError: undefined
};
constructor(props) {
super(props);
this.state = {
bars: [],
barCodeWidth: 0
};
}
componentDidUpdate(prevProps) {
if (prevProps.value !== this.props.value) {
this.update(this.props);
}
}
componentDidMount() {
this.update();
}
componentDidUpdate() {
this.update();
}
update() {
const encoder = barcodes[this.props.format];
const encoded = this.encode(this.props.value, encoder, this.props);
if (encoded) {
this.state.bars = this.drawSvgBarCode(encoded, this.props);
this.state.barCodeWidth = encoded.data.length * this.props.width;
}
}
drawSvgBarCode(encoding, options = {}) {
const rects = [];
// binary data of barcode
const binary = encoding.data;
let barWidth = 0;
let x = 0;
let yFrom = 0;
// alert(JSON.stringify(options));
for (let b = 0; b < binary.length; b++) {
x = b * options.width;
if (binary[b] === '1') {
barWidth++;
} else if (barWidth > 0) {
rects[rects.length] = this.drawRect(
x - options.width * barWidth,
yFrom,
options.width * barWidth,
options.height
);
barWidth = 0;
}
}
// Last draw is needed since the barcode ends with 1
if (barWidth > 0) {
rects[rects.length] = this.drawRect(
x - options.width * (barWidth - 1),
yFrom,
options.width * barWidth,
options.height
);
}
return rects;
}
drawRect(x, y, width, height) {
return `M${x},${y}h${width}v${height}h-${width}z`;
}
getTotalWidthOfEncodings(encodings) {
let totalWidth = 0;
for (let i = 0; i < encodings.length; i++) {
totalWidth += encodings[i].width;
}
return totalWidth;
}
// encode() handles the Encoder call and builds the binary string to be rendered
encode(text, Encoder, options) {
// If text is not a non-empty string, throw error.
if (typeof text !== "string" || text.length === 0) {
if (this.props.onError) {
this.props.onError(new Error('Barcode value must be a non-empty string'));
return;
}
throw new Error('Barcode value must be a non-empty string');
}
var encoder;
try {
encoder = new Encoder(text, options);
} catch (error) {
// If the encoder could not be instantiated, throw error.
if (this.props.onError) {
this.props.onError(new Error('Invalid barcode format.'));
return;
}
throw new Error('Invalid barcode format.');
}
// If the input is not valid for the encoder, throw error.
if (!encoder.valid()) {
if (this.props.onError) {
this.props.onError(new Error('Invalid barcode for selected format.'));
return;
}
throw new Error('Invalid barcode for selected format.');
}
// Make a request for the binary data (and other infromation) that should be rendered
// encoded stucture is {
// text: 'xxxxx',
// data: '110100100001....'
// }
var encoded = encoder.encode();
return encoded;
}
render() {
this.update();
const backgroundStyle = {
backgroundColor: this.props.background
};
return (
<View style={[styles.svgContainer, backgroundStyle]}>
<Surface height={this.props.height} width={this.state.barCodeWidth}>
<Shape d={this.state.bars} fill={this.props.lineColor} />
</Surface>
{ typeof(this.props.text) != 'undefined' &&
<Text style={{color: this.props.textColor, width: this.state.barCodeWidth, textAlign: 'center'}} >{this.props.text}</Text>
}
</View>
);
}
}
const styles = StyleSheet.create({
svgContainer: {
alignItems: 'center',
padding: 10
}
});