Skip to content

Commit 8d9ff6e

Browse files
committed
feat: code cleanup & enhance barcode config and subconfig
1 parent d28344f commit 8d9ff6e

File tree

11 files changed

+326
-45
lines changed

11 files changed

+326
-45
lines changed

packages/pluggableWidgets/barcode-generator-web/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@mendix/widget-plugin-component-kit": "workspace:*",
5757
"@mendix/widget-plugin-platform": "workspace:*",
5858
"@mendix/widget-plugin-test-utils": "workspace:*",
59-
"cross-env": "^7.0.3"
59+
"cross-env": "^7.0.3",
60+
"eslint": "^9.37.0"
6061
}
6162
}

packages/pluggableWidgets/barcode-generator-web/src/BarcodeGenerator.editorConfig.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hidePropertiesIn, Properties } from "@mendix/pluggable-widgets-tools";
1+
import { hidePropertiesIn, hidePropertyIn, Properties } from "@mendix/pluggable-widgets-tools";
22
import { BarcodeGeneratorPreviewProps } from "../typings/BarcodeGeneratorProps";
33

44
export type Problem = {
@@ -30,13 +30,41 @@ export function getProperties(values: BarcodeGeneratorPreviewProps, defaultPrope
3030
]);
3131
}
3232

33+
if (values.codeFormat !== "CODE128" && values.customCodeFormat !== "CODE128") {
34+
hidePropertyIn(defaultProperties, values, "enableEan128");
35+
}
36+
37+
if (
38+
values.customCodeFormat !== "EAN13" &&
39+
values.customCodeFormat !== "EAN8" &&
40+
values.customCodeFormat !== "UPC"
41+
) {
42+
hidePropertyIn(defaultProperties, values, "enableFlat");
43+
}
44+
45+
if (values.customCodeFormat !== "EAN13") {
46+
hidePropertyIn(defaultProperties, values, "lastChar");
47+
}
48+
49+
if (values.customCodeFormat !== "EAN13" && values.customCodeFormat !== "EAN8") {
50+
hidePropertiesIn(defaultProperties, values, ["addonFormat", "addonValue", "addonSpacing"]);
51+
}
52+
if (values.addonFormat !== "EAN5" && values.addonFormat !== "EAN2") {
53+
hidePropertiesIn(defaultProperties, values, ["addonValue", "addonSpacing"]);
54+
}
55+
56+
if (values.customCodeFormat !== "CODE39") {
57+
hidePropertyIn(defaultProperties, values, "enableMod43");
58+
}
59+
3360
if (values.qrImageCenter) {
3461
hidePropertiesIn(defaultProperties, values, ["qrImageX", "qrImageY"]);
3562
}
3663

3764
if (values.codeFormat !== "Custom") {
3865
hidePropertiesIn(defaultProperties, values, ["customCodeFormat"]);
3966
}
67+
4068
return defaultProperties;
4169
}
4270

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export default function BarcodeGenerator({
1313
customCodeFormat,
1414
codeMargin,
1515
displayValue,
16+
enableEan128,
17+
enableFlat,
18+
lastChar,
19+
enableMod43,
20+
addonValue,
21+
addonFormat,
22+
addonSpacing,
1623
qrSize,
1724
qrMargin,
1825
qrTitle,
@@ -48,6 +55,13 @@ export default function BarcodeGenerator({
4855
const qrimageWidth = qrImageWidth ?? 24;
4956
const qrimageOpacity = qrImageOpacity?.toNumber() ?? 1;
5057
const qrimageExcavate = qrImageExcavate ?? true;
58+
const supportsEan128 = enableEan128 ?? false;
59+
const supportsFlat = enableFlat ?? false;
60+
const lastCharacter = lastChar ?? "";
61+
const supportsMod43 = enableMod43 ?? false;
62+
const processedAddonValue = addonValue?.status === "available" ? addonValue.value : "";
63+
const processedAddonFormat = addonFormat ?? "EAN5";
64+
const processedAddonSpacing = addonSpacing ?? 20;
5165

5266
const { downloadSVG } = useDownload({ format, svgRef, qrContainerRef });
5367

@@ -64,6 +78,13 @@ export default function BarcodeGenerator({
6478
height={height}
6579
margin={margin}
6680
displayValue={showValue}
81+
enableEan128={supportsEan128}
82+
enableFlat={supportsFlat}
83+
lastChar={lastCharacter}
84+
enableMod43={supportsMod43}
85+
addonValue={processedAddonValue}
86+
addonFormat={processedAddonFormat}
87+
addonSpacing={processedAddonSpacing}
6788
qrsize={qrsize}
6889
qrmargin={qrmargin}
6990
qrlevel={qrlevel}

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
<enumerationValue key="CODE128">CODE128</enumerationValue>
3232
<enumerationValue key="EAN13">EAN-13</enumerationValue>
3333
<enumerationValue key="EAN8">EAN-8</enumerationValue>
34-
<enumerationValue key="EAN5">EAN-5</enumerationValue>
35-
<enumerationValue key="EAN2">EAN-2</enumerationValue>
3634
<enumerationValue key="UPC">UPC</enumerationValue>
3735
<enumerationValue key="CODE39">CODE39</enumerationValue>
3836
<enumerationValue key="ITF14">ITF-14</enumerationValue>
@@ -42,23 +40,61 @@
4240
<enumerationValue key="CODE93">CODE93</enumerationValue>
4341
</enumerationValues>
4442
</property>
43+
<property key="enableEan128" type="boolean" defaultValue="false">
44+
<caption>EAN-128</caption>
45+
<description>Enable encoding CODE128 as GS1-128/EAN-128</description>
46+
</property>
47+
<property key="enableFlat" type="boolean" defaultValue="false">
48+
<caption>Flat</caption>
49+
<description>Enable flat barcode, skip guard bars</description>
50+
</property>
51+
<property key="lastChar" type="string" required="true">
52+
<caption>Last character</caption>
53+
<description>Character after the barcode</description>
54+
</property>
55+
<property key="enableMod43" type="boolean" required="true" defaultValue="false">
56+
<caption>Mod43</caption>
57+
<description>For code 39 if used with modulo 43 check digit</description>
58+
</property>
4559
<property key="allowDownload" type="boolean" defaultValue="false">
4660
<caption>Allow download</caption>
4761
<description>Adds a download button</description>
4862
</property>
4963
</propertyGroup>
64+
<propertyGroup caption="EAN Addons">
65+
<property key="addonFormat" type="enumeration" required="true" defaultValue="None">
66+
<caption>Addon format</caption>
67+
<description>Choose between EAN-5 or EAN-2 addon format</description>
68+
<enumerationValues>
69+
<enumerationValue key="None">None</enumerationValue>
70+
<enumerationValue key="EAN5">EAN-5</enumerationValue>
71+
<enumerationValue key="EAN2">EAN-2</enumerationValue>
72+
</enumerationValues>
73+
</property>
74+
<property key="addonValue" type="attribute" required="false">
75+
<caption>Addon value</caption>
76+
<description>Value for the addon barcode (5 digits for EAN-5, 2 digits for EAN-2)</description>
77+
<attributeTypes>
78+
<attributeType name="String" />
79+
</attributeTypes>
80+
</property>
81+
<property key="addonSpacing" type="integer" required="true" defaultValue="20">
82+
<caption>Addon spacing</caption>
83+
<description>Space between main barcode and addon (in pixels)</description>
84+
</property>
85+
</propertyGroup>
5086
<propertyGroup caption="Display">
5187
<property key="displayValue" type="boolean" defaultValue="false">
5288
<caption>Display value</caption>
5389
<description>Display the value below the code</description>
5490
</property>
5591
<property key="codeWidth" type="integer" required="true" defaultValue="2">
5692
<caption>Bar width</caption>
57-
<description>Width of the barcode bars</description>
93+
<description>Width of a single bar</description>
5894
</property>
5995
<property key="codeHeight" type="integer" required="true" defaultValue="200">
6096
<caption>Code height</caption>
61-
<description>In pixels</description>
97+
<description>Height of the barcode</description>
6298
</property>
6399
<property key="codeMargin" type="integer" required="true" defaultValue="2">
64100
<caption>Margin size</caption>
@@ -74,7 +110,7 @@
74110
</property>
75111
<property key="qrTitle" type="string" required="true">
76112
<caption>Title</caption>
77-
<description>Used for accessibility reasons.</description>
113+
<description>Used for accessibility reasons</description>
78114
</property>
79115
<property key="qrLevel" type="enumeration" required="true" defaultValue="L">
80116
<caption>Level</caption>

packages/pluggableWidgets/barcode-generator-web/src/__tests__/BarcodeGenerator.spec.tsx

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,32 @@ describe("BarcodeGenerator", () => {
2525
class: "mx-barcode-generator",
2626
tabIndex: -1,
2727
codeFormat: "QRCode" as CodeFormatEnum,
28+
customCodeFormat: "CODE128" as CustomCodeFormatEnum,
29+
enableEan128: false,
30+
enableFlat: false,
31+
lastChar: "",
32+
enableMod43: false,
33+
allowDownload: false,
2834
displayValue: false,
2935
codeWidth: 2,
3036
codeHeight: 200,
31-
qrSize: 128,
3237
codeMargin: 4,
33-
customCodeFormat: "CODE128" as CustomCodeFormatEnum,
38+
qrSize: 128,
39+
qrMargin: 2,
40+
qrTitle: "",
41+
qrLevel: "L" as any,
42+
qrImage: false,
43+
qrImageSrc: { status: "unavailable" } as any,
44+
qrImageCenter: true,
45+
qrImageX: 0,
46+
qrImageY: 0,
47+
qrImageHeight: 24,
48+
qrImageWidth: 24,
49+
qrImageOpacity: { toNumber: () => 1 } as any,
50+
qrImageExcavate: true,
51+
addonFormat: "None" as any,
52+
addonValue: { status: "unavailable" } as any,
53+
addonSpacing: 20,
3454
codeValue: new EditableValueBuilder<string>().withValue(barcodeDefaultValue).build()
3555
};
3656

@@ -87,7 +107,7 @@ describe("BarcodeGenerator", () => {
87107
it("renders CODE128 barcode when format is not QR", () => {
88108
const props = {
89109
...defaultProps,
90-
codeFormat: "CODE128" as const,
110+
codeFormat: "CODE128" as CodeFormatEnum,
91111
codeValue: {
92112
value: "123456789",
93113
status: "available"
@@ -108,7 +128,11 @@ describe("BarcodeGenerator", () => {
108128
width: 2,
109129
height: 200,
110130
margin: 4,
111-
displayValue: false
131+
displayValue: false,
132+
ean128: false,
133+
flat: false,
134+
lastChar: "",
135+
mod43: false
112136
}
113137
);
114138
});
@@ -207,7 +231,46 @@ describe("BarcodeGenerator", () => {
207231
width: 2, // from defaultProps
208232
height: 200, // from defaultProps
209233
margin: 4, // from defaultProps
210-
displayValue: false
234+
displayValue: false,
235+
ean128: false,
236+
flat: false,
237+
lastChar: "",
238+
mod43: false
211239
});
212240
});
241+
242+
it("supports EAN addon functionality", () => {
243+
const mockBarcodeInstance = {
244+
EAN13: jest.fn().mockReturnThis(),
245+
blank: jest.fn().mockReturnThis(),
246+
EAN5: jest.fn().mockReturnThis(),
247+
render: jest.fn()
248+
};
249+
250+
mockJsBarcode.mockReturnValue(mockBarcodeInstance);
251+
252+
const props = {
253+
...defaultProps,
254+
codeFormat: "Custom" as CodeFormatEnum,
255+
customCodeFormat: "EAN13" as any,
256+
addonValue: {
257+
value: "12345",
258+
status: "available"
259+
} as any,
260+
addonFormat: "EAN5" as any,
261+
addonSpacing: 25,
262+
codeValue: {
263+
value: "1234567890128",
264+
status: "available"
265+
} as any
266+
};
267+
268+
render(<BarcodeGenerator {...props} />);
269+
270+
expect(mockJsBarcode).toHaveBeenCalled();
271+
expect(mockBarcodeInstance.EAN13).toHaveBeenCalledWith("1234567890128", expect.any(Object));
272+
expect(mockBarcodeInstance.blank).toHaveBeenCalledWith(25);
273+
expect(mockBarcodeInstance.EAN5).toHaveBeenCalledWith("12345", expect.any(Object));
274+
expect(mockBarcodeInstance.render).toHaveBeenCalled();
275+
});
213276
});

0 commit comments

Comments
 (0)