Skip to content

Commit b6fae00

Browse files
SDrinkwaterzpao
andcommitted
Create ErrorCorrectionLevel type and wire through
Closes #291 Note: updated from original to remove wiring through. Not relevant with removal of generic component. Encourage using React utility types instead. Co-authored-by: Paul O’Shannessy <[email protected]>
1 parent 5debc5c commit b6fae00

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

examples/full.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {QRCodeSVG, QRCodeCanvas} from '..';
22
import React, {useState} from 'react';
3+
import type {ComponentProps} from 'react';
4+
5+
type ErrorCorrectionLevel = ComponentProps<typeof QRCodeSVG>['level'];
36

47
function FullDemo() {
58
const [value, setValue] = useState(
@@ -8,7 +11,7 @@ function FullDemo() {
811
const [size, setSize] = useState(128);
912
const [fgColor, setFgColor] = useState('#000000');
1013
const [bgColor, setBgColor] = useState('#ffffff');
11-
const [level, setLevel] = useState('L');
14+
const [level, setLevel] = useState<ErrorCorrectionLevel>('L');
1215
const [minVersion, setMinVersion] = useState(1);
1316
const [marginSize, setMarginSize] = useState(0);
1417
const [title, setTitle] = useState('Title for my QR Code');
@@ -118,7 +121,9 @@ function FullDemo() {
118121
<label>
119122
Error Level:
120123
<br />
121-
<select onChange={(e) => setLevel(e.target.value)} value={level}>
124+
<select
125+
onChange={(e) => setLevel(e.target.value as ErrorCorrectionLevel)}
126+
value={level}>
122127
<option value="L">L</option>
123128
<option value="M">M</option>
124129
<option value="Q">Q</option>

src/__test__/index.test.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,27 @@ import renderer from 'react-test-renderer';
33
import {QRCodeSVG, QRCodeCanvas} from '..';
44
import {describe, expect, test} from '@jest/globals';
55

6-
const BASIC_PROPS = {
6+
import type {ComponentPropsWithoutRef} from 'react';
7+
8+
// We're only using the explicit (non-DOM, non-ref) props for testing, but TS
9+
// doesn't love us passing our test data as props without actually typing the
10+
// object. So we'll make sure we give types that work. It's a little messy but
11+
// gets the job done without being too fancy.
12+
// Specifically, get the DOM-specific keys and omit them from the also-de-ref'ed
13+
// component props. Then union together the two types since they may not always
14+
// be the same.
15+
type QRCodeCanvasProps = Omit<
16+
ComponentPropsWithoutRef<typeof QRCodeCanvas>,
17+
keyof React.CanvasHTMLAttributes<HTMLCanvasElement>
18+
>;
19+
type QRCodeSVGProps = Omit<
20+
ComponentPropsWithoutRef<typeof QRCodeSVG>,
21+
keyof React.SVGProps<SVGSVGElement>
22+
>;
23+
type QRProps = QRCodeCanvasProps | QRCodeSVGProps;
24+
type PartialQRProps = Partial<QRCodeCanvasProps> | Partial<QRCodeSVGProps>;
25+
26+
const BASIC_PROPS: QRProps = {
727
value: 'http://picturesofpeoplescanningqrcodes.tumblr.com/',
828
size: 128,
929
bgColor: '#ffffff',
@@ -12,7 +32,7 @@ const BASIC_PROPS = {
1232
includeMargin: false,
1333
};
1434

15-
const TEST_CONFIGS = [
35+
const TEST_CONFIGS: PartialQRProps[] = [
1636
{includeMargin: true},
1737
{includeMargin: false},
1838
{level: 'L'},

src/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import qrcodegen from './third-party/qrcodegen';
1010

1111
type Modules = ReturnType<qrcodegen.QrCode['getModules']>;
1212
type Excavation = {x: number; y: number; w: number; h: number};
13+
type ErrorCorrectionLevel = 'L' | 'M' | 'Q' | 'H';
1314

1415
const ERROR_LEVEL_MAP: {[index: string]: qrcodegen.QrCode.Ecc} = {
1516
L: qrcodegen.QrCode.Ecc.LOW,
@@ -31,8 +32,7 @@ type ImageSettings = {
3132
type QRProps = {
3233
value: string;
3334
size?: number;
34-
// Should be a real enum, but doesn't seem to be compatible with real code.
35-
level?: string;
35+
level?: ErrorCorrectionLevel;
3636
bgColor?: string;
3737
fgColor?: string;
3838
style?: CSSProperties;
@@ -46,7 +46,7 @@ type QRPropsCanvas = QRProps & React.CanvasHTMLAttributes<HTMLCanvasElement>;
4646
type QRPropsSVG = QRProps & React.SVGAttributes<SVGSVGElement>;
4747

4848
const DEFAULT_SIZE = 128;
49-
const DEFAULT_LEVEL = 'L';
49+
const DEFAULT_LEVEL: ErrorCorrectionLevel = 'L';
5050
const DEFAULT_BGCOLOR = '#FFFFFF';
5151
const DEFAULT_FGCOLOR = '#000000';
5252
const DEFAULT_INCLUDEMARGIN = false;

0 commit comments

Comments
 (0)