This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
137 lines (126 loc) · 3.28 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
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import { View, Text } from 'react-native';
import Svg, { Path } from 'react-native-svg';
import barcodes from 'jsbarcode/src/barcodes';
const Barcode = ({
value = '',
width = 2,
height = 100,
format = 'CODE128',
lineColor = '#000000',
background = '#ffffff',
text,
textStyle,
style,
onError,
maxWidth,
}) => {
const drawRect = (x, y, width, height) => {
return `M${x},${y}h${width}v${height}h-${width}z`;
};
const drawSvgBarCode = (encoded) => {
const rects = [];
const { data: binary } = encoded;
const barCodeWidth = binary.length * width;
const singleBarWidth =
typeof maxWidth === 'number' && barCodeWidth > maxWidth
? maxWidth / binary.length
: width;
let barWidth = 0;
let x = 0;
let yFrom = 0;
for (let b = 0; b < binary.length; b++) {
x = b * singleBarWidth;
if (binary[b] === '1') {
barWidth++;
} else if (barWidth > 0) {
rects[rects.length] = drawRect(
x - singleBarWidth * barWidth,
yFrom,
singleBarWidth * barWidth,
height,
);
barWidth = 0;
}
}
if (barWidth > 0) {
rects[rects.length] = drawRect(
x - singleBarWidth * (barWidth - 1),
yFrom,
singleBarWidth * barWidth,
height,
);
}
return rects;
};
const encode = (text, Encoder) => {
if (typeof text !== 'string' || text.length === 0) {
throw new Error('Barcode value must be a non-empty string');
}
const encoder = new Encoder(text, {
width,
format,
height,
lineColor,
background,
flat: true,
});
if (!encoder.valid()) {
throw new Error('Invalid barcode for selected format.');
}
return encoder.encode();
};
const { bars, barCodeWidth } = useMemo(() => {
try {
const encoder = barcodes[format];
if (!encoder) {
throw new Error('Invalid barcode format.');
}
const encoded = encode(value, encoder);
const barCodeWidth = encoded.data.length * width;
return {
bars: drawSvgBarCode(encoded),
barCodeWidth:
typeof maxWidth === 'number' && barCodeWidth > maxWidth
? maxWidth
: barCodeWidth,
};
} catch (error) {
if (__DEV__) {
console.error(error.message);
}
if (onError) {
onError(error);
}
}
return {
bars: [],
barCodeWidth: 0,
};
}, [value, width, height, format, lineColor, background, maxWidth]);
return (
<View
style={[{ backgroundColor: background, alignItems: 'center' }, style]}
>
<Svg height={height} width={barCodeWidth} fill={lineColor}>
<Path d={bars.join(' ')} />
</Svg>
{text && <Text style={[{ textAlign: 'center' }, textStyle]}>{text}</Text>}
</View>
);
};
Barcode.propTypes = {
value: PropTypes.string.isRequired,
format: PropTypes.oneOf(Object.keys(barcodes)),
width: PropTypes.number,
maxWidth: PropTypes.number,
height: PropTypes.number,
lineColor: PropTypes.string,
background: PropTypes.string,
text: PropTypes.node,
textStyle: PropTypes.object,
style: PropTypes.object,
onError: PropTypes.func,
};
export default Barcode;