@terreno/ui 0.28.0 → 0.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Common.d.ts +89 -0
- package/dist/Common.js.map +1 -1
- package/dist/Signature.d.ts +3 -1
- package/dist/Signature.js +4 -3
- package/dist/Signature.js.map +1 -1
- package/dist/Signature.native.d.ts +2 -0
- package/dist/Signature.native.js +2 -2
- package/dist/Signature.native.js.map +1 -1
- package/dist/SignatureCaptureField.d.ts +13 -0
- package/dist/SignatureCaptureField.js +69 -0
- package/dist/SignatureCaptureField.js.map +1 -0
- package/dist/TypedSignatureField.d.ts +18 -0
- package/dist/TypedSignatureField.js +102 -0
- package/dist/TypedSignatureField.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +5 -1
- package/src/Common.ts +97 -0
- package/src/Signature.native.tsx +10 -2
- package/src/Signature.tsx +5 -0
- package/src/SignatureCaptureField.test.tsx +135 -0
- package/src/SignatureCaptureField.tsx +172 -0
- package/src/TypedSignatureField.test.tsx +113 -0
- package/src/TypedSignatureField.tsx +181 -0
- package/src/__snapshots__/SignatureCaptureField.test.tsx.snap +51 -0
- package/src/__snapshots__/TypedSignatureField.test.tsx.snap +372 -0
- package/src/bunSetup.ts +28 -0
- package/src/index.tsx +2 -0
package/src/Common.ts
CHANGED
|
@@ -2145,6 +2145,103 @@ export interface SignatureFieldProps {
|
|
|
2145
2145
|
fullWidth?: boolean;
|
|
2146
2146
|
}
|
|
2147
2147
|
|
|
2148
|
+
/**
|
|
2149
|
+
* A selectable signature typeface. `key` is the stable identifier consumers persist
|
|
2150
|
+
* (so a stored signature keeps rendering in the same style even if `fontFamily` or
|
|
2151
|
+
* `label` are later renamed); `fontFamily` is the loaded font family used for rendering;
|
|
2152
|
+
* `label` is the human-readable name shown in the font picker.
|
|
2153
|
+
*/
|
|
2154
|
+
export interface SignatureFont {
|
|
2155
|
+
key: string;
|
|
2156
|
+
label: string;
|
|
2157
|
+
fontFamily: string;
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
/**
|
|
2161
|
+
* The persisted value of a typed signature: the name the signer typed and the `key` of
|
|
2162
|
+
* the font they chose. Kept minimal so consumers can store it directly and re-render the
|
|
2163
|
+
* signature deterministically.
|
|
2164
|
+
*/
|
|
2165
|
+
export interface TypedSignatureValue {
|
|
2166
|
+
typedName: string;
|
|
2167
|
+
fontKey: string;
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
export interface TypedSignatureFieldProps extends WithTestID {
|
|
2171
|
+
testIDs?: FieldTestIDs;
|
|
2172
|
+
/** Section title rendered above the field. Defaults to "Signature". */
|
|
2173
|
+
title?: string;
|
|
2174
|
+
/** Controlled value. When omitted the field starts empty with the first font selected. */
|
|
2175
|
+
value?: TypedSignatureValue;
|
|
2176
|
+
onChange: (value: TypedSignatureValue) => void;
|
|
2177
|
+
/**
|
|
2178
|
+
* Selectable fonts. Defaults to the library's bundled signature fonts. When supplying a
|
|
2179
|
+
* custom list, the consumer is responsible for loading those font families (e.g. via
|
|
2180
|
+
* expo-font) so the preview renders correctly.
|
|
2181
|
+
*/
|
|
2182
|
+
fonts?: SignatureFont[];
|
|
2183
|
+
/** Label for the name input. Defaults to "Full name". */
|
|
2184
|
+
nameLabel?: string;
|
|
2185
|
+
/** Placeholder for the name input. Defaults to "Type your full name". */
|
|
2186
|
+
placeholder?: string;
|
|
2187
|
+
helperText?: string;
|
|
2188
|
+
errorText?: string;
|
|
2189
|
+
disabled?: boolean; // default false
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
/** Which capture method the signer is using in a {@link SignatureCaptureFieldProps} field. */
|
|
2193
|
+
export type SignatureMode = "draw" | "type";
|
|
2194
|
+
|
|
2195
|
+
/** A signature captured by drawing: a base64 PNG data URL. */
|
|
2196
|
+
export interface DrawnSignatureValue {
|
|
2197
|
+
mode: "draw";
|
|
2198
|
+
image: string;
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
/** A signature captured by typing: a name plus the chosen font key. */
|
|
2202
|
+
export interface TypedSignatureCaptureValue extends TypedSignatureValue {
|
|
2203
|
+
mode: "type";
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2206
|
+
/**
|
|
2207
|
+
* The persisted value of a signature capture field. The `mode` discriminant tells consumers
|
|
2208
|
+
* how to render it: `draw` carries a base64 PNG `image`; `type` carries a `typedName` and
|
|
2209
|
+
* `fontKey`.
|
|
2210
|
+
*/
|
|
2211
|
+
export type SignatureCaptureValue = DrawnSignatureValue | TypedSignatureCaptureValue;
|
|
2212
|
+
|
|
2213
|
+
export interface SignatureCaptureFieldProps extends WithTestID {
|
|
2214
|
+
testIDs?: FieldTestIDs;
|
|
2215
|
+
/** Section title rendered above the field. Defaults to "Signature". */
|
|
2216
|
+
title?: string;
|
|
2217
|
+
/** Controlled value. When omitted the field starts empty in `defaultMode`. */
|
|
2218
|
+
value?: SignatureCaptureValue;
|
|
2219
|
+
onChange: (value: SignatureCaptureValue) => void;
|
|
2220
|
+
/**
|
|
2221
|
+
* Which capture method is shown first when the field is empty. Ignored once `value` has a
|
|
2222
|
+
* mode. Defaults to "type".
|
|
2223
|
+
*/
|
|
2224
|
+
defaultMode?: SignatureMode;
|
|
2225
|
+
/**
|
|
2226
|
+
* Fonts offered in "type" mode. Defaults to the library's bundled signature fonts. When
|
|
2227
|
+
* supplying a custom list, the consumer must load those font families themselves.
|
|
2228
|
+
*/
|
|
2229
|
+
fonts?: SignatureFont[];
|
|
2230
|
+
/** Label for the typed name input. Defaults to "Full name". */
|
|
2231
|
+
nameLabel?: string;
|
|
2232
|
+
/** Placeholder for the typed name input. Defaults to "Type your full name". */
|
|
2233
|
+
placeholder?: string;
|
|
2234
|
+
/** Stretches the draw pad to the full available width. */
|
|
2235
|
+
fullWidth?: boolean;
|
|
2236
|
+
/** Fired when the signer starts drawing — use to disable a parent ScrollView. */
|
|
2237
|
+
onStart?: () => void;
|
|
2238
|
+
/** Fired when the signer stops drawing — use to re-enable a parent ScrollView. */
|
|
2239
|
+
onEnd?: () => void;
|
|
2240
|
+
helperText?: string;
|
|
2241
|
+
errorText?: string;
|
|
2242
|
+
disabled?: boolean; // default false
|
|
2243
|
+
}
|
|
2244
|
+
|
|
2148
2245
|
export interface SideDrawerProps {
|
|
2149
2246
|
// Position of the drawer relative to the child
|
|
2150
2247
|
position?: "right" | "left";
|
package/src/Signature.native.tsx
CHANGED
|
@@ -11,6 +11,8 @@ interface Props {
|
|
|
11
11
|
onStart?: () => void;
|
|
12
12
|
onEnd?: () => void;
|
|
13
13
|
fullWidth?: boolean;
|
|
14
|
+
/** Test id applied to the drawing surface. */
|
|
15
|
+
testID?: string;
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
const STROKE_WIDTH_PX = 2.5;
|
|
@@ -32,7 +34,13 @@ const SNAPSHOT_DELAY_MS = 60;
|
|
|
32
34
|
* Reports the signature to the parent as a base64 PNG data URL via onChange,
|
|
33
35
|
* and pushes "" on clear so "signature required" gating resets immediately.
|
|
34
36
|
*/
|
|
35
|
-
export const Signature: FC<Props> = ({
|
|
37
|
+
export const Signature: FC<Props> = ({
|
|
38
|
+
fullWidth = false,
|
|
39
|
+
onChange,
|
|
40
|
+
onStart,
|
|
41
|
+
onEnd,
|
|
42
|
+
testID,
|
|
43
|
+
}: Props) => {
|
|
36
44
|
const {theme} = useTheme();
|
|
37
45
|
const canvasRef = useCanvasRef();
|
|
38
46
|
const signaturePadHeight = getSignaturePadHeight(Platform.OS);
|
|
@@ -140,7 +148,7 @@ export const Signature: FC<Props> = ({fullWidth = false, onChange, onStart, onEn
|
|
|
140
148
|
}, [clearSnapshotTimer, onChange]);
|
|
141
149
|
|
|
142
150
|
return (
|
|
143
|
-
<View style={{minWidth: 220, width: fullWidth ? "100%" : undefined}}>
|
|
151
|
+
<View style={{minWidth: 220, width: fullWidth ? "100%" : undefined}} testID={testID}>
|
|
144
152
|
<GestureDetector gesture={panGesture}>
|
|
145
153
|
<View
|
|
146
154
|
style={{
|
package/src/Signature.tsx
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
import {Text, View} from "react-native";
|
|
9
9
|
|
|
10
10
|
import {useTheme} from "./Theme";
|
|
11
|
+
import {toDomTestProps} from "./testing/resolveTestId";
|
|
11
12
|
|
|
12
13
|
export interface SignatureProps {
|
|
13
14
|
onChange: (signature: string) => void;
|
|
@@ -15,6 +16,8 @@ export interface SignatureProps {
|
|
|
15
16
|
onEnd?: () => void;
|
|
16
17
|
fullWidth?: boolean;
|
|
17
18
|
value?: string; // note this
|
|
19
|
+
/** Test id applied to the drawing surface (the canvas on web). */
|
|
20
|
+
testID?: string;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
const SIGNATURE_WIDTH_PX = 300;
|
|
@@ -40,6 +43,7 @@ export const Signature = ({
|
|
|
40
43
|
onChange,
|
|
41
44
|
onStart,
|
|
42
45
|
onEnd,
|
|
46
|
+
testID,
|
|
43
47
|
}: SignatureProps): ReactElement => {
|
|
44
48
|
const {theme} = useTheme();
|
|
45
49
|
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
|
@@ -168,6 +172,7 @@ export const Signature = ({
|
|
|
168
172
|
}}
|
|
169
173
|
>
|
|
170
174
|
<canvas
|
|
175
|
+
{...toDomTestProps(testID)}
|
|
171
176
|
height={SIGNATURE_HEIGHT_PX}
|
|
172
177
|
onPointerDown={handlePointerDown}
|
|
173
178
|
onPointerLeave={handlePointerUp}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {fireEvent} from "@testing-library/react-native";
|
|
3
|
+
|
|
4
|
+
import type {SignatureCaptureValue} from "./Common";
|
|
5
|
+
import {SignatureCaptureField} from "./SignatureCaptureField";
|
|
6
|
+
import {renderWithTheme} from "./test-utils";
|
|
7
|
+
|
|
8
|
+
describe("SignatureCaptureField", () => {
|
|
9
|
+
const defaultProps = {
|
|
10
|
+
onChange: () => {},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
it("component is defined", () => {
|
|
14
|
+
expect(SignatureCaptureField).toBeDefined();
|
|
15
|
+
expect(typeof SignatureCaptureField).toBe("function");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("renders both Draw and Type mode options", () => {
|
|
19
|
+
const {getByText} = renderWithTheme(<SignatureCaptureField {...defaultProps} />);
|
|
20
|
+
expect(getByText("Draw")).toBeTruthy();
|
|
21
|
+
expect(getByText("Type")).toBeTruthy();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("defaults to type mode and shows the typed name input", () => {
|
|
25
|
+
const {getByText} = renderWithTheme(<SignatureCaptureField {...defaultProps} />);
|
|
26
|
+
// The typed field renders its name input label.
|
|
27
|
+
expect(getByText("Full name")).toBeTruthy();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("respects defaultMode=draw and shows the draw hint", () => {
|
|
31
|
+
const {getByText} = renderWithTheme(
|
|
32
|
+
<SignatureCaptureField {...defaultProps} defaultMode="draw" />
|
|
33
|
+
);
|
|
34
|
+
expect(
|
|
35
|
+
getByText("Draw your signature above, then it will be saved automatically.")
|
|
36
|
+
).toBeTruthy();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("emits a typed value when the user types", () => {
|
|
40
|
+
const onChange = mock((_v: SignatureCaptureValue) => {});
|
|
41
|
+
const {UNSAFE_getAllByProps} = renderWithTheme(<SignatureCaptureField onChange={onChange} />);
|
|
42
|
+
|
|
43
|
+
const inputs = UNSAFE_getAllByProps({}).filter(
|
|
44
|
+
(el) => typeof el.props?.onChangeText === "function"
|
|
45
|
+
);
|
|
46
|
+
expect(inputs.length).toBeGreaterThan(0);
|
|
47
|
+
inputs[0].props.onChangeText("Jane Doe");
|
|
48
|
+
|
|
49
|
+
const [emitted] = onChange.mock.calls[0];
|
|
50
|
+
expect(emitted.mode).toBe("type");
|
|
51
|
+
expect(emitted).toMatchObject({mode: "type", typedName: "Jane Doe"});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("emits a drawn value when the pad reports an image in draw mode", () => {
|
|
55
|
+
const onChange = mock((_v: SignatureCaptureValue) => {});
|
|
56
|
+
const {UNSAFE_getAllByProps} = renderWithTheme(
|
|
57
|
+
<SignatureCaptureField defaultMode="draw" onChange={onChange} />
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const drawPads = UNSAFE_getAllByProps({}).filter(
|
|
61
|
+
(el) => typeof el.props?.onChange === "function" && "fullWidth" in (el.props ?? {})
|
|
62
|
+
);
|
|
63
|
+
expect(drawPads.length).toBeGreaterThan(0);
|
|
64
|
+
drawPads[0].props.onChange("data:image/png;base64,abc");
|
|
65
|
+
|
|
66
|
+
expect(onChange).toHaveBeenCalledWith({image: "data:image/png;base64,abc", mode: "draw"});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("switches to draw mode when the Draw toggle is pressed", () => {
|
|
70
|
+
const {getByText, queryByText} = renderWithTheme(<SignatureCaptureField {...defaultProps} />);
|
|
71
|
+
// Starts in type mode: draw hint absent.
|
|
72
|
+
expect(
|
|
73
|
+
queryByText("Draw your signature above, then it will be saved automatically.")
|
|
74
|
+
).toBeNull();
|
|
75
|
+
|
|
76
|
+
fireEvent.press(getByText("Draw"));
|
|
77
|
+
|
|
78
|
+
expect(
|
|
79
|
+
getByText("Draw your signature above, then it will be saved automatically.")
|
|
80
|
+
).toBeTruthy();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("renders a read-only drawn image when disabled with a drawn value", () => {
|
|
84
|
+
const {toJSON} = renderWithTheme(
|
|
85
|
+
<SignatureCaptureField
|
|
86
|
+
disabled
|
|
87
|
+
onChange={() => {}}
|
|
88
|
+
value={{image: "data:image/png;base64,xyz", mode: "draw"}}
|
|
89
|
+
/>
|
|
90
|
+
);
|
|
91
|
+
expect(toJSON()).toMatchSnapshot();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("forwards the resolved input test id to the typed name input in type mode", () => {
|
|
95
|
+
const {UNSAFE_getAllByProps} = renderWithTheme(
|
|
96
|
+
<SignatureCaptureField {...defaultProps} testID="sig" />
|
|
97
|
+
);
|
|
98
|
+
// testID "sig" resolves to input id "sig" and is forwarded to the typed name input.
|
|
99
|
+
const inputs = UNSAFE_getAllByProps({}).filter(
|
|
100
|
+
(el) => typeof el.props?.onChangeText === "function"
|
|
101
|
+
);
|
|
102
|
+
expect(inputs.length).toBeGreaterThan(0);
|
|
103
|
+
expect(inputs[0].props.testID).toBe("sig");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("forwards the resolved input test id to the draw pad in draw mode", () => {
|
|
107
|
+
const {UNSAFE_getAllByProps} = renderWithTheme(
|
|
108
|
+
<SignatureCaptureField {...defaultProps} defaultMode="draw" testID="sig" />
|
|
109
|
+
);
|
|
110
|
+
// The draw pad is the node exposing both onChange and fullWidth; it should carry the id.
|
|
111
|
+
const drawPads = UNSAFE_getAllByProps({}).filter(
|
|
112
|
+
(el) => typeof el.props?.onChange === "function" && "fullWidth" in (el.props ?? {})
|
|
113
|
+
);
|
|
114
|
+
expect(drawPads.length).toBeGreaterThan(0);
|
|
115
|
+
expect(drawPads[0].props.testID).toBe("sig");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("forwards an explicit testIDs.input to the active control", () => {
|
|
119
|
+
const {UNSAFE_getAllByProps} = renderWithTheme(
|
|
120
|
+
<SignatureCaptureField {...defaultProps} testIDs={{input: "custom-input"}} />
|
|
121
|
+
);
|
|
122
|
+
const inputs = UNSAFE_getAllByProps({}).filter(
|
|
123
|
+
(el) => typeof el.props?.onChangeText === "function"
|
|
124
|
+
);
|
|
125
|
+
expect(inputs.length).toBeGreaterThan(0);
|
|
126
|
+
expect(inputs[0].props.testID).toBe("custom-input");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("renders an error message when errorText is provided", () => {
|
|
130
|
+
const {getByText} = renderWithTheme(
|
|
131
|
+
<SignatureCaptureField {...defaultProps} errorText="A signature is required." />
|
|
132
|
+
);
|
|
133
|
+
expect(getByText("A signature is required.")).toBeTruthy();
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import {type ReactElement, useState} from "react";
|
|
2
|
+
import {Image, Text as NativeText, View} from "react-native";
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
SignatureCaptureFieldProps,
|
|
6
|
+
SignatureCaptureValue,
|
|
7
|
+
SignatureMode,
|
|
8
|
+
TypedSignatureValue,
|
|
9
|
+
} from "./Common";
|
|
10
|
+
import {FieldError} from "./fieldElements/FieldError";
|
|
11
|
+
import {FieldHelperText} from "./fieldElements/FieldHelperText";
|
|
12
|
+
import {FieldTitle} from "./fieldElements/FieldTitle";
|
|
13
|
+
import {SegmentedControl} from "./SegmentedControl";
|
|
14
|
+
import {Signature} from "./Signature";
|
|
15
|
+
import {useTheme} from "./Theme";
|
|
16
|
+
import {TypedSignatureField} from "./TypedSignatureField";
|
|
17
|
+
import {resolveFieldTestIDsFromProps} from "./testing/resolveTestId";
|
|
18
|
+
|
|
19
|
+
const MODE_ITEMS: {label: string; mode: SignatureMode}[] = [
|
|
20
|
+
{label: "Draw", mode: "draw"},
|
|
21
|
+
{label: "Type", mode: "type"},
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Read-only preview of a drawn signature, mirroring SignatureField's disabled rendering.
|
|
26
|
+
* Shows the captured image, or a grayed placeholder box when there is nothing to show.
|
|
27
|
+
*/
|
|
28
|
+
const DrawnSignaturePreview = ({image}: {image?: string}): ReactElement => {
|
|
29
|
+
const {theme} = useTheme();
|
|
30
|
+
if (!image) {
|
|
31
|
+
return (
|
|
32
|
+
<View
|
|
33
|
+
style={{
|
|
34
|
+
backgroundColor: theme.surface.neutralLight,
|
|
35
|
+
height: 90,
|
|
36
|
+
marginVertical: 8,
|
|
37
|
+
width: 300,
|
|
38
|
+
}}
|
|
39
|
+
/>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return (
|
|
43
|
+
<Image
|
|
44
|
+
accessibilityIgnoresInvertColors={false}
|
|
45
|
+
resizeMode="contain"
|
|
46
|
+
source={{uri: image}}
|
|
47
|
+
style={{
|
|
48
|
+
borderColor: theme.border.dark,
|
|
49
|
+
borderWidth: 1,
|
|
50
|
+
height: 80,
|
|
51
|
+
marginVertical: 8,
|
|
52
|
+
width: 300,
|
|
53
|
+
}}
|
|
54
|
+
/>
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A cross-platform signature field that lets the signer either draw their signature or type
|
|
60
|
+
* their name and pick a font, chosen via a Draw/Type toggle. Draw mode reuses the platform
|
|
61
|
+
* signature pad (Skia on native, canvas on web); type mode reuses {@link TypedSignatureField}.
|
|
62
|
+
*
|
|
63
|
+
* Emits a discriminated {@link SignatureCaptureValue} on every change so a parent can gate
|
|
64
|
+
* "signature required" and persist whichever representation the signer produced. The active
|
|
65
|
+
* mode is tracked internally (initialized from `value.mode` or `defaultMode`); switching modes
|
|
66
|
+
* does not emit a value on its own — a value is emitted only once the signer draws or types.
|
|
67
|
+
*/
|
|
68
|
+
export const SignatureCaptureField = ({
|
|
69
|
+
title = "Signature",
|
|
70
|
+
value,
|
|
71
|
+
onChange,
|
|
72
|
+
defaultMode = "type",
|
|
73
|
+
fonts,
|
|
74
|
+
nameLabel,
|
|
75
|
+
placeholder,
|
|
76
|
+
fullWidth = false,
|
|
77
|
+
onStart,
|
|
78
|
+
onEnd,
|
|
79
|
+
helperText,
|
|
80
|
+
errorText,
|
|
81
|
+
disabled = false,
|
|
82
|
+
testID,
|
|
83
|
+
testIDs,
|
|
84
|
+
}: SignatureCaptureFieldProps): ReactElement => {
|
|
85
|
+
const {theme} = useTheme();
|
|
86
|
+
const fieldTestIDs = resolveFieldTestIDsFromProps({testID, testIDs});
|
|
87
|
+
const [mode, setMode] = useState<SignatureMode>(value?.mode ?? defaultMode);
|
|
88
|
+
|
|
89
|
+
const drawnImage = value?.mode === "draw" ? value.image : undefined;
|
|
90
|
+
const typedValue: TypedSignatureValue | undefined =
|
|
91
|
+
value?.mode === "type" ? {fontKey: value.fontKey, typedName: value.typedName} : undefined;
|
|
92
|
+
|
|
93
|
+
const emitDrawn = (image: string): void => {
|
|
94
|
+
const next: SignatureCaptureValue = {image, mode: "draw"};
|
|
95
|
+
onChange(next);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const emitTyped = (typed: TypedSignatureValue): void => {
|
|
99
|
+
const next: SignatureCaptureValue = {mode: "type", ...typed};
|
|
100
|
+
onChange(next);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// When disabled, render a read-only view of whichever representation was captured — no toggle.
|
|
104
|
+
if (disabled) {
|
|
105
|
+
return (
|
|
106
|
+
<View style={{flexDirection: "column", width: "100%"}} {...(testID ? {testID} : {})}>
|
|
107
|
+
{Boolean(title) && <FieldTitle testID={fieldTestIDs.label} text={title} />}
|
|
108
|
+
{value?.mode === "draw" ? (
|
|
109
|
+
<DrawnSignaturePreview image={drawnImage} />
|
|
110
|
+
) : (
|
|
111
|
+
<TypedSignatureField
|
|
112
|
+
disabled
|
|
113
|
+
fonts={fonts}
|
|
114
|
+
nameLabel={nameLabel}
|
|
115
|
+
onChange={emitTyped}
|
|
116
|
+
placeholder={placeholder}
|
|
117
|
+
testIDs={{input: fieldTestIDs.input}}
|
|
118
|
+
title=""
|
|
119
|
+
value={typedValue}
|
|
120
|
+
/>
|
|
121
|
+
)}
|
|
122
|
+
{Boolean(helperText) && <FieldHelperText testID={fieldTestIDs.helper} text={helperText!} />}
|
|
123
|
+
</View>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return (
|
|
128
|
+
<View style={{flexDirection: "column", width: "100%"}} {...(testID ? {testID} : {})}>
|
|
129
|
+
{Boolean(title) && <FieldTitle testID={fieldTestIDs.label} text={title} />}
|
|
130
|
+
{Boolean(errorText) && <FieldError testID={fieldTestIDs.error} text={errorText!} />}
|
|
131
|
+
|
|
132
|
+
<View style={{marginTop: 8}}>
|
|
133
|
+
<SegmentedControl
|
|
134
|
+
items={MODE_ITEMS.map((item) => item.label)}
|
|
135
|
+
onChange={(index) => setMode(MODE_ITEMS[index].mode)}
|
|
136
|
+
selectedIndex={MODE_ITEMS.findIndex((item) => item.mode === mode)}
|
|
137
|
+
/>
|
|
138
|
+
</View>
|
|
139
|
+
|
|
140
|
+
<View style={{marginTop: 12}}>
|
|
141
|
+
{mode === "draw" ? (
|
|
142
|
+
<View>
|
|
143
|
+
<Signature
|
|
144
|
+
fullWidth={fullWidth}
|
|
145
|
+
onChange={emitDrawn}
|
|
146
|
+
onEnd={onEnd}
|
|
147
|
+
onStart={onStart}
|
|
148
|
+
testID={fieldTestIDs.input}
|
|
149
|
+
/>
|
|
150
|
+
{!drawnImage && (
|
|
151
|
+
<NativeText style={{color: theme.text.secondaryLight, fontSize: 12, marginTop: 4}}>
|
|
152
|
+
Draw your signature above, then it will be saved automatically.
|
|
153
|
+
</NativeText>
|
|
154
|
+
)}
|
|
155
|
+
</View>
|
|
156
|
+
) : (
|
|
157
|
+
<TypedSignatureField
|
|
158
|
+
fonts={fonts}
|
|
159
|
+
nameLabel={nameLabel}
|
|
160
|
+
onChange={emitTyped}
|
|
161
|
+
placeholder={placeholder}
|
|
162
|
+
testIDs={{input: fieldTestIDs.input}}
|
|
163
|
+
title=""
|
|
164
|
+
value={typedValue}
|
|
165
|
+
/>
|
|
166
|
+
)}
|
|
167
|
+
</View>
|
|
168
|
+
|
|
169
|
+
{Boolean(helperText) && <FieldHelperText testID={fieldTestIDs.helper} text={helperText!} />}
|
|
170
|
+
</View>
|
|
171
|
+
);
|
|
172
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {fireEvent} from "@testing-library/react-native";
|
|
3
|
+
|
|
4
|
+
import type {TypedSignatureValue} from "./Common";
|
|
5
|
+
import {DEFAULT_SIGNATURE_FONTS, TypedSignatureField} from "./TypedSignatureField";
|
|
6
|
+
import {renderWithTheme} from "./test-utils";
|
|
7
|
+
|
|
8
|
+
describe("TypedSignatureField", () => {
|
|
9
|
+
const defaultProps = {
|
|
10
|
+
onChange: () => {},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
it("component is defined", () => {
|
|
14
|
+
expect(TypedSignatureField).toBeDefined();
|
|
15
|
+
expect(typeof TypedSignatureField).toBe("function");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("exposes a non-empty set of default fonts", () => {
|
|
19
|
+
expect(DEFAULT_SIGNATURE_FONTS.length).toBeGreaterThan(0);
|
|
20
|
+
for (const font of DEFAULT_SIGNATURE_FONTS) {
|
|
21
|
+
expect(font.key).toBeTruthy();
|
|
22
|
+
expect(font.label).toBeTruthy();
|
|
23
|
+
expect(font.fontFamily).toBeTruthy();
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("renders correctly with default props", () => {
|
|
28
|
+
const {toJSON} = renderWithTheme(<TypedSignatureField {...defaultProps} />);
|
|
29
|
+
expect(toJSON()).toMatchSnapshot();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("renders the default title and a preview of the typed name", () => {
|
|
33
|
+
const {getByText, getAllByText} = renderWithTheme(
|
|
34
|
+
<TypedSignatureField {...defaultProps} value={{fontKey: "caveat", typedName: "Jane Doe"}} />
|
|
35
|
+
);
|
|
36
|
+
expect(getByText("Signature")).toBeTruthy();
|
|
37
|
+
// Name appears in both the input value and the live preview.
|
|
38
|
+
expect(getAllByText("Jane Doe").length).toBeGreaterThan(0);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("renders each font option as a pressable label", () => {
|
|
42
|
+
const {getByText} = renderWithTheme(<TypedSignatureField {...defaultProps} />);
|
|
43
|
+
for (const font of DEFAULT_SIGNATURE_FONTS) {
|
|
44
|
+
expect(getByText(font.label)).toBeTruthy();
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("emits the typed name with the current font key when the name changes", () => {
|
|
49
|
+
const onChange = mock((_v: TypedSignatureValue) => {});
|
|
50
|
+
const {UNSAFE_getAllByProps} = renderWithTheme(
|
|
51
|
+
<TypedSignatureField onChange={onChange} value={{fontKey: "great-vibes", typedName: ""}} />
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const inputs = UNSAFE_getAllByProps({}).filter(
|
|
55
|
+
(el) => typeof el.props?.onChangeText === "function"
|
|
56
|
+
);
|
|
57
|
+
expect(inputs.length).toBeGreaterThan(0);
|
|
58
|
+
inputs[0].props.onChangeText("Sam Smith");
|
|
59
|
+
|
|
60
|
+
expect(onChange).toHaveBeenCalledWith({fontKey: "great-vibes", typedName: "Sam Smith"});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("emits the selected font key while preserving the typed name", () => {
|
|
64
|
+
const onChange = mock((_v: TypedSignatureValue) => {});
|
|
65
|
+
const {getByText} = renderWithTheme(
|
|
66
|
+
<TypedSignatureField onChange={onChange} value={{fontKey: "caveat", typedName: "Jane Doe"}} />
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
fireEvent.press(getByText("Great Vibes"));
|
|
70
|
+
|
|
71
|
+
expect(onChange).toHaveBeenCalledWith({fontKey: "great-vibes", typedName: "Jane Doe"});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("falls back to the first font when the stored font key is unknown", () => {
|
|
75
|
+
const onChange = mock((_v: TypedSignatureValue) => {});
|
|
76
|
+
const {UNSAFE_getAllByProps} = renderWithTheme(
|
|
77
|
+
<TypedSignatureField onChange={onChange} value={{fontKey: "does-not-exist", typedName: ""}} />
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const inputs = UNSAFE_getAllByProps({}).filter(
|
|
81
|
+
(el) => typeof el.props?.onChangeText === "function"
|
|
82
|
+
);
|
|
83
|
+
inputs[0].props.onChangeText("Ann Lee");
|
|
84
|
+
|
|
85
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
86
|
+
fontKey: DEFAULT_SIGNATURE_FONTS[0].key,
|
|
87
|
+
typedName: "Ann Lee",
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("falls back to the bundled fonts when an empty list is supplied", () => {
|
|
92
|
+
const onChange = mock((_v: TypedSignatureValue) => {});
|
|
93
|
+
// An empty `fonts` list must not crash — the field falls back to the bundled defaults.
|
|
94
|
+
const {getByText} = renderWithTheme(<TypedSignatureField fonts={[]} onChange={onChange} />);
|
|
95
|
+
|
|
96
|
+
for (const font of DEFAULT_SIGNATURE_FONTS) {
|
|
97
|
+
expect(getByText(font.label)).toBeTruthy();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
fireEvent.press(getByText(DEFAULT_SIGNATURE_FONTS[0].label));
|
|
101
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
102
|
+
fontKey: DEFAULT_SIGNATURE_FONTS[0].key,
|
|
103
|
+
typedName: "",
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("renders an error message when errorText is provided", () => {
|
|
108
|
+
const {getByText} = renderWithTheme(
|
|
109
|
+
<TypedSignatureField {...defaultProps} errorText="Signature is required" />
|
|
110
|
+
);
|
|
111
|
+
expect(getByText("Signature is required")).toBeTruthy();
|
|
112
|
+
});
|
|
113
|
+
});
|