Skip to content

Commit b91d2e8

Browse files
feat(barcode-generator-web): add Data Matrix editor preview
Renders a representative Data Matrix glyph in the Studio Pro page editor when the Data Matrix format is selected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 356e8f1 commit b91d2e8

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { parseStyle } from "@mendix/widget-plugin-platform/preview/parse-style";
44
import { BarcodeGeneratorPreviewProps } from "../typings/BarcodeGeneratorProps";
55
import { DownloadIcon } from "./components/icons/DownloadIcon";
66
import { BarcodePreview } from "./components/preview/BarcodePreview";
7+
import { DataMatrixPreview } from "./components/preview/DataMatrixPreview";
78
import { QRCodePreview } from "./components/preview/QRCodePreview";
89

910
const defaultDownloadCaption = "Download";
@@ -23,6 +24,7 @@ function PreviewDownloadButton(props: BarcodeGeneratorPreviewProps): ReactElemen
2324
export function preview(props: BarcodeGeneratorPreviewProps): ReactElement {
2425
const styles = parseStyle(props.style);
2526
const isQrCode = props.codeFormat === "QRCode";
27+
const isDataMatrix = props.codeFormat === "DataMatrix";
2628
const downloadButton = <PreviewDownloadButton {...props} />;
2729

2830
return (
@@ -34,6 +36,8 @@ export function preview(props: BarcodeGeneratorPreviewProps): ReactElement {
3436
>
3537
{isQrCode ? (
3638
<QRCodePreview {...props} downloadButton={downloadButton} />
39+
) : isDataMatrix ? (
40+
<DataMatrixPreview {...props} downloadButton={downloadButton} />
3741
) : (
3842
<BarcodePreview {...props} downloadButton={downloadButton} />
3943
)}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { datamatrix, drawingSVG, gs1datamatrix } from "@bwip-js/browser";
2+
import { ReactElement, useMemo } from "react";
3+
import { BarcodeGeneratorPreviewProps } from "../../../typings/BarcodeGeneratorProps";
4+
5+
interface DataMatrixPreviewProps extends BarcodeGeneratorPreviewProps {
6+
downloadButton: ReactElement | null;
7+
}
8+
9+
const SAMPLE_PLAIN = "DATA MATRIX";
10+
const SAMPLE_GS1 = "(01)09501101020917(17)261231(10)ABC123";
11+
12+
export function DataMatrixPreview(props: DataMatrixPreviewProps): ReactElement {
13+
const { downloadButton, ...restProps } = props;
14+
const size = restProps.dmSize ?? 128;
15+
const displaySize = Math.min(size, 200); // Clamped to 200px for preview
16+
const gs1Mode = restProps.dmGs1Mode === true;
17+
18+
const svg = useMemo<string | null>(() => {
19+
try {
20+
return gs1Mode
21+
? gs1datamatrix({ bcid: "gs1datamatrix", text: SAMPLE_GS1, scale: 3, parse: true }, drawingSVG())
22+
: datamatrix({ bcid: "datamatrix", text: SAMPLE_PLAIN, scale: 3 }, drawingSVG());
23+
} catch {
24+
return null;
25+
}
26+
}, [gs1Mode]);
27+
28+
return (
29+
<div className="barcode-renderer datamatrix-renderer">
30+
{restProps.buttonPosition === "top" && downloadButton}
31+
{svg ? (
32+
<div
33+
className="datamatrix-preview-image"
34+
style={{ width: displaySize, height: displaySize }}
35+
dangerouslySetInnerHTML={{ __html: svg }}
36+
/>
37+
) : (
38+
<div className="alert alert-danger" role="alert">
39+
<strong>Data Matrix preview unavailable</strong>
40+
</div>
41+
)}
42+
{restProps.buttonPosition === "bottom" && downloadButton}
43+
</div>
44+
);
45+
}

0 commit comments

Comments
 (0)