Skip to content

Commit 1b48634

Browse files
committed
refactor: improve architecture and rendering logic
1 parent da884ee commit 1b48634

File tree

4 files changed

+155
-85
lines changed

4 files changed

+155
-85
lines changed

packages/pluggableWidgets/barcode-generator-web/src/BarcodeGenerator.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<caption>Flat</caption>
4949
<description>Enable flat barcode, skip guard bars</description>
5050
</property>
51-
<property key="lastChar" type="string" required="true">
51+
<property key="lastChar" type="string" required="false">
5252
<caption>Last character</caption>
5353
<description>Character after the barcode</description>
5454
</property>
@@ -71,7 +71,7 @@
7171
<enumerationValue key="EAN2">EAN-2</enumerationValue>
7272
</enumerationValues>
7373
</property>
74-
<property key="addonValue" type="attribute" required="false">
74+
<property key="addonValue" type="attribute" required="true">
7575
<caption>Addon value</caption>
7676
<description>Value for the addon barcode (5 digits for EAN-5, 2 digits for EAN-2)</description>
7777
<attributeTypes>

packages/pluggableWidgets/barcode-generator-web/src/components/BarcodeRenderer.tsx

Lines changed: 7 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
import JsBarcode from "jsbarcode";
2-
import { forwardRef, useEffect } from "react";
3-
4-
interface BarcodeOptions {
5-
format: string;
6-
width: number;
7-
height: number;
8-
margin: number;
9-
displayValue: boolean;
10-
ean128?: boolean;
11-
flat?: boolean;
12-
lastChar?: string;
13-
mod43?: boolean;
14-
}
1+
import { forwardRef } from "react";
2+
import { useRenderBarcode } from "../hooks/useRenderBarcode";
153

164
interface BarcodeRendererProps {
175
value: string;
@@ -48,70 +36,8 @@ export const BarcodeRenderer = forwardRef<SVGSVGElement, BarcodeRendererProps>(
4836
},
4937
ref
5038
) => {
51-
useEffect(() => {
52-
if (ref && typeof ref !== "function" && ref.current && value) {
53-
try {
54-
// Check if format supports addons and addon data is provided
55-
const supportsAddon =
56-
addonValue &&
57-
(format === "EAN13" || format === "EAN8") &&
58-
(addonFormat === "EAN5" || addonFormat === "EAN2");
59-
60-
if (supportsAddon) {
61-
const barcodeInstance = JsBarcode(ref.current);
62-
63-
// Generate main barcode
64-
if (format === "EAN13") {
65-
barcodeInstance.EAN13(value, {
66-
width,
67-
height,
68-
margin,
69-
displayValue
70-
});
71-
} else if (format === "EAN8") {
72-
barcodeInstance.EAN8(value, {
73-
width,
74-
height,
75-
margin,
76-
displayValue
77-
});
78-
}
79-
80-
// Add spacing
81-
barcodeInstance.blank(addonSpacing || 20);
82-
83-
// Add addon
84-
if (addonFormat === "EAN5") {
85-
barcodeInstance.EAN5(addonValue, {
86-
width: 1
87-
});
88-
} else if (addonFormat === "EAN2") {
89-
barcodeInstance.EAN2(addonValue, {
90-
width: 1
91-
});
92-
}
93-
barcodeInstance.render();
94-
} else {
95-
// Standard single barcode generation
96-
const options: BarcodeOptions = {
97-
format,
98-
width,
99-
height,
100-
margin,
101-
displayValue,
102-
ean128: enableEan128,
103-
flat: enableFlat,
104-
lastChar,
105-
mod43: enableMod43
106-
};
107-
108-
JsBarcode(ref.current, value, options);
109-
}
110-
} catch (error) {
111-
console.error("Error generating barcode:", error);
112-
}
113-
}
114-
}, [
39+
useRenderBarcode(
40+
ref,
11541
value,
11642
width,
11743
height,
@@ -120,13 +46,12 @@ export const BarcodeRenderer = forwardRef<SVGSVGElement, BarcodeRendererProps>(
12046
displayValue,
12147
enableEan128,
12248
enableFlat,
123-
enableMod43,
12449
lastChar,
50+
enableMod43,
12551
addonValue,
12652
addonFormat,
127-
addonSpacing,
128-
ref
129-
]);
53+
addonSpacing
54+
);
13055

13156
return <svg ref={ref} />;
13257
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import JsBarcode from "jsbarcode";
2+
import { type ForwardedRef, useEffect } from "react";
3+
4+
interface BarcodeMethodOptions {
5+
width?: number;
6+
height?: number;
7+
margin?: number;
8+
displayValue?: boolean;
9+
}
10+
11+
interface BarcodeInstance {
12+
EAN13: (value: string, options: BarcodeMethodOptions) => BarcodeInstance;
13+
EAN8: (value: string, options: BarcodeMethodOptions) => BarcodeInstance;
14+
EAN5: (value: string, options: BarcodeMethodOptions) => BarcodeInstance;
15+
EAN2: (value: string, options: BarcodeMethodOptions) => BarcodeInstance;
16+
blank: (spacing: number) => BarcodeInstance;
17+
render: () => void;
18+
[key: string]: any;
19+
}
20+
21+
interface BarcodeOptions {
22+
format: string;
23+
width: number;
24+
height: number;
25+
margin: number;
26+
displayValue: boolean;
27+
ean128?: boolean;
28+
flat?: boolean;
29+
lastChar?: string;
30+
mod43?: boolean;
31+
}
32+
33+
const createBarcodeWithAddon = (
34+
ref: ForwardedRef<SVGSVGElement>,
35+
value: string,
36+
mainFormat: string,
37+
addonValue: string,
38+
addonFormat: string,
39+
options: BarcodeOptions,
40+
addonSpacing: number
41+
): void => {
42+
if (ref && typeof ref !== "function" && ref.current) {
43+
const barcodeInstance = JsBarcode(ref.current) as BarcodeInstance;
44+
45+
// Generate main barcode dynamically
46+
barcodeInstance[mainFormat](value, {
47+
width: options.width,
48+
height: options.height,
49+
margin: options.margin,
50+
displayValue: options.displayValue
51+
});
52+
53+
// Add spacing
54+
barcodeInstance.blank(addonSpacing);
55+
56+
// Add addon dynamically
57+
barcodeInstance[addonFormat](addonValue, { width: 1 });
58+
59+
barcodeInstance.render();
60+
}
61+
};
62+
63+
const createStandardBarcode = (ref: ForwardedRef<SVGSVGElement>, value: string, options: BarcodeOptions): void => {
64+
if (ref && typeof ref !== "function" && ref.current) {
65+
JsBarcode(ref.current, value, options);
66+
}
67+
};
68+
69+
const renderBarcode = (
70+
ref: ForwardedRef<SVGSVGElement>,
71+
value: string,
72+
format: string,
73+
addonValue: string | undefined,
74+
addonFormat: string | undefined,
75+
addonSpacing: number,
76+
options: BarcodeOptions
77+
): void => {
78+
switch (addonFormat) {
79+
case "EAN5":
80+
createBarcodeWithAddon(ref, value, format, addonValue!, addonFormat, options, addonSpacing);
81+
break;
82+
83+
case "EAN2":
84+
createBarcodeWithAddon(ref, value, format, addonValue!, addonFormat, options, addonSpacing);
85+
break;
86+
87+
default:
88+
createStandardBarcode(ref, value, options);
89+
break;
90+
}
91+
};
92+
93+
export const useRenderBarcode = (
94+
ref: ForwardedRef<SVGSVGElement>,
95+
value: string,
96+
width: number,
97+
height: number,
98+
format: string,
99+
margin: number,
100+
displayValue: boolean,
101+
enableEan128: boolean,
102+
enableFlat: boolean,
103+
lastChar: string,
104+
enableMod43: boolean,
105+
addonValue?: string,
106+
addonFormat?: string,
107+
addonSpacing?: number
108+
): void => {
109+
useEffect(() => {
110+
if (ref && typeof ref !== "function" && ref.current && value) {
111+
try {
112+
const options: BarcodeOptions = {
113+
format,
114+
width,
115+
height,
116+
margin,
117+
displayValue,
118+
ean128: enableEan128,
119+
flat: enableFlat,
120+
lastChar,
121+
mod43: enableMod43
122+
};
123+
124+
renderBarcode(ref, value, format, addonValue, addonFormat, addonSpacing || 20, options);
125+
} catch (error) {
126+
console.error("Error generating barcode:", error);
127+
}
128+
}
129+
}, [
130+
value,
131+
width,
132+
height,
133+
format,
134+
margin,
135+
displayValue,
136+
enableEan128,
137+
enableFlat,
138+
enableMod43,
139+
lastChar,
140+
addonValue,
141+
addonFormat,
142+
addonSpacing,
143+
ref
144+
]);
145+
};

packages/pluggableWidgets/barcode-generator-web/typings/BarcodeGeneratorProps.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface BarcodeGeneratorContainerProps {
2929
enableMod43: boolean;
3030
allowDownload: boolean;
3131
addonFormat: AddonFormatEnum;
32-
addonValue?: EditableValue<string>;
32+
addonValue: EditableValue<string>;
3333
addonSpacing: number;
3434
displayValue: boolean;
3535
codeWidth: number;

0 commit comments

Comments
 (0)