Refactor/barcode1d typed locale#31
Merged
Merged
Conversation
The PropertiesPanel cast (t.registry as unknown as Record<string, BarcodeLocale>)[config.localeKey] stripped type safety from every 1D barcode registry: a typo or renamed key compiled cleanly and showed empty labels at runtime. Replace localeKey: string with locale: (t) => BarcodeLocale at the config level. Each registry call site picks its block as '(t) => t.registry.<key>', which TS verifies both for existence and for shape against the BarcodeLocale interface. Drop the cast in the panel; loc is now plainly BarcodeLocale. 14 1D barcode registries migrated (code128 / code39 / ean13 keep their inline panels and aren't affected).
Previously barcode1d.tsx derived the translation type via ReturnType<typeof useT>, coupling a pure data type to a React hook. The hook is one consumer of the type, not its source. Move the type to locales/index.ts where the locale data lives. useT now consumes Translations rather than producing it; non-React modules (future SSR, tests, lib code) can pick up the type without pulling the React-bound hook.
These three were the last 1D barcodes carrying their own inline PropertiesPanel and toZPL — virtually identical to what createBarcode1D emits. The duplication was an old smell waiting on type-safe locale handling (which the previous commit landed). - code128 / code39: direct migration with hasCheckDigit: true. code39 keeps its content-spec (charset filter) via the helper's contentSpec parameter. - ean13: adopts Barcode1DProps (gains an unused checkDigit: boolean). hasCheckDigit: false hides the checkbox in the panel; toZPL ignores the field. zplParser and testModels updated to set checkDigit: false explicitly. The data-shape change is forward-only — runtime never reads p.checkDigit on EAN-13. Net: ~240 lines of duplicated panel/toZPL code deleted.
Make the type contract explicit at the hook boundary. Inferred return already equals Translations, but the annotation locks the shape and fails fast if locales[code] ever drifts.
There was a problem hiding this comment.
Code Review
This pull request refactors the 1D barcode registry by introducing a createBarcode1D helper to reduce boilerplate and replacing string-based localeKey references with a type-safe locale selector function. It also updates EAN-13 barcode properties to include a default checkDigit value. I have no feedback to provide.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.