@terreno/ui 0.27.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.
Files changed (49) hide show
  1. package/dist/Common.d.ts +97 -7
  2. package/dist/Common.js.map +1 -1
  3. package/dist/CustomSelectField.js +2 -2
  4. package/dist/CustomSelectField.js.map +1 -1
  5. package/dist/PickerSelect.d.ts +4 -5
  6. package/dist/PickerSelect.js +4 -3
  7. package/dist/PickerSelect.js.map +1 -1
  8. package/dist/SelectBadge.js +1 -1
  9. package/dist/SelectBadge.js.map +1 -1
  10. package/dist/SelectField.js +3 -3
  11. package/dist/SelectField.js.map +1 -1
  12. package/dist/Signature.d.ts +3 -1
  13. package/dist/Signature.js +4 -3
  14. package/dist/Signature.js.map +1 -1
  15. package/dist/Signature.native.d.ts +2 -0
  16. package/dist/Signature.native.js +2 -2
  17. package/dist/Signature.native.js.map +1 -1
  18. package/dist/SignatureCaptureField.d.ts +13 -0
  19. package/dist/SignatureCaptureField.js +69 -0
  20. package/dist/SignatureCaptureField.js.map +1 -0
  21. package/dist/TypedSignatureField.d.ts +18 -0
  22. package/dist/TypedSignatureField.js +102 -0
  23. package/dist/TypedSignatureField.js.map +1 -0
  24. package/dist/WebDropdownMenu.d.ts +7 -8
  25. package/dist/WebDropdownMenu.js +4 -3
  26. package/dist/WebDropdownMenu.js.map +1 -1
  27. package/dist/index.d.ts +2 -0
  28. package/dist/index.js +2 -0
  29. package/dist/index.js.map +1 -1
  30. package/package.json +5 -1
  31. package/src/Common.ts +105 -7
  32. package/src/CustomSelectField.test.tsx +3 -8
  33. package/src/CustomSelectField.tsx +2 -2
  34. package/src/PickerSelect.test.tsx +22 -18
  35. package/src/PickerSelect.tsx +6 -6
  36. package/src/SelectBadge.tsx +1 -1
  37. package/src/SelectField.tsx +2 -2
  38. package/src/Signature.native.tsx +10 -2
  39. package/src/Signature.tsx +5 -0
  40. package/src/SignatureCaptureField.test.tsx +135 -0
  41. package/src/SignatureCaptureField.tsx +172 -0
  42. package/src/TypedSignatureField.test.tsx +113 -0
  43. package/src/TypedSignatureField.tsx +181 -0
  44. package/src/WebDropdownMenu.test.tsx +14 -19
  45. package/src/WebDropdownMenu.tsx +8 -8
  46. package/src/__snapshots__/SignatureCaptureField.test.tsx.snap +51 -0
  47. package/src/__snapshots__/TypedSignatureField.test.tsx.snap +372 -0
  48. package/src/bunSetup.ts +28 -0
  49. package/src/index.tsx +2 -0
@@ -11,6 +11,7 @@ import {renderWithTheme} from "./test-utils";
11
11
 
12
12
  describe("PickerSelect", () => {
13
13
  const defaultProps = {
14
+ disableSearch: true,
14
15
  items: [
15
16
  {label: "Option 1", value: "1"},
16
17
  {label: "Option 2", value: "2"},
@@ -18,7 +19,6 @@ describe("PickerSelect", () => {
18
19
  ],
19
20
  onValueChange: () => {},
20
21
  placeholder: {label: "Select an option", value: ""},
21
- searchable: false,
22
22
  };
23
23
 
24
24
  it("renders correctly with default props", () => {
@@ -171,7 +171,7 @@ describe("PickerSelect", () => {
171
171
  let hadDocument = false;
172
172
  let savedDocument: any;
173
173
 
174
- const searchableWebProps = {...defaultProps, searchable: true};
174
+ const searchEnabledWebProps = {...defaultProps, disableSearch: false};
175
175
 
176
176
  const ensureDocument = () => {
177
177
  hadDocument = "document" in globalThis;
@@ -228,7 +228,7 @@ describe("PickerSelect", () => {
228
228
  PlatformModule.OS = "web";
229
229
  const onOpen = mock(() => {});
230
230
  const {getByTestId} = renderWithTheme(
231
- <RNPickerSelect {...searchableWebProps} onOpen={onOpen} value="1" />
231
+ <RNPickerSelect {...searchEnabledWebProps} onOpen={onOpen} value="1" />
232
232
  );
233
233
  await openSearchableWebPicker(getByTestId);
234
234
  expect(onOpen).toHaveBeenCalled();
@@ -238,14 +238,14 @@ describe("PickerSelect", () => {
238
238
  }
239
239
  });
240
240
 
241
- it("opens web menu on press when searchable is false", async () => {
241
+ it("opens web menu on press when disableSearch is true", async () => {
242
242
  ensureDocument();
243
243
  savedOS = PlatformModule.OS;
244
244
  try {
245
245
  PlatformModule.OS = "web";
246
246
  const onOpen = mock(() => {});
247
247
  const {getByTestId} = renderWithTheme(
248
- <RNPickerSelect {...defaultProps} onOpen={onOpen} searchable={false} value="1" />
248
+ <RNPickerSelect {...defaultProps} disableSearch onOpen={onOpen} value="1" />
249
249
  );
250
250
  await act(async () => {
251
251
  fireEvent.press(getByTestId("web_picker"));
@@ -264,7 +264,7 @@ describe("PickerSelect", () => {
264
264
  PlatformModule.OS = "web";
265
265
  const onOpen = mock(() => {});
266
266
  const {getByTestId} = renderWithTheme(
267
- <RNPickerSelect {...searchableWebProps} disabled onOpen={onOpen} />
267
+ <RNPickerSelect {...searchEnabledWebProps} disabled onOpen={onOpen} />
268
268
  );
269
269
  await act(async () => {
270
270
  fireEvent(getByTestId("text_input"), "focus");
@@ -284,7 +284,7 @@ describe("PickerSelect", () => {
284
284
  const onClose = mock(() => {});
285
285
  const onOpen = mock(() => {});
286
286
  const {getByTestId} = renderWithTheme(
287
- <RNPickerSelect {...searchableWebProps} onClose={onClose} onOpen={onOpen} value="1" />
287
+ <RNPickerSelect {...searchEnabledWebProps} onClose={onClose} onOpen={onOpen} value="1" />
288
288
  );
289
289
  await openSearchableWebPicker(getByTestId);
290
290
  expect(onOpen).toHaveBeenCalled();
@@ -304,7 +304,7 @@ describe("PickerSelect", () => {
304
304
  try {
305
305
  PlatformModule.OS = "web";
306
306
  const {getByTestId, queryByTestId} = renderWithTheme(
307
- <RNPickerSelect {...searchableWebProps} value="1" />
307
+ <RNPickerSelect {...searchEnabledWebProps} value="1" />
308
308
  );
309
309
  await openSearchableWebPicker(getByTestId);
310
310
  const input = getByTestId("text_input");
@@ -327,7 +327,9 @@ describe("PickerSelect", () => {
327
327
  savedOS = PlatformModule.OS;
328
328
  try {
329
329
  PlatformModule.OS = "web";
330
- const {getByTestId} = renderWithTheme(<RNPickerSelect {...searchableWebProps} value="1" />);
330
+ const {getByTestId} = renderWithTheme(
331
+ <RNPickerSelect {...searchEnabledWebProps} value="1" />
332
+ );
331
333
  await openSearchableWebPicker(getByTestId);
332
334
  const input = getByTestId("text_input");
333
335
  await act(async () => {
@@ -354,7 +356,7 @@ describe("PickerSelect", () => {
354
356
  try {
355
357
  PlatformModule.OS = "web";
356
358
  const {getByTestId, queryByTestId} = renderWithTheme(
357
- <RNPickerSelect {...searchableWebProps} value="1" />
359
+ <RNPickerSelect {...searchEnabledWebProps} value="1" />
358
360
  );
359
361
  const input = getByTestId("text_input");
360
362
  await act(async () => {
@@ -379,7 +381,7 @@ describe("PickerSelect", () => {
379
381
  {label: "Melon", value: "m"},
380
382
  ];
381
383
  const props = {
382
- ...searchableWebProps,
384
+ ...searchEnabledWebProps,
383
385
  items: itemsWithHelper,
384
386
  placeholder: {label: "Select", value: ""},
385
387
  };
@@ -403,7 +405,9 @@ describe("PickerSelect", () => {
403
405
  savedOS = PlatformModule.OS;
404
406
  try {
405
407
  PlatformModule.OS = "web";
406
- const {getByTestId} = renderWithTheme(<RNPickerSelect {...searchableWebProps} value="1" />);
408
+ const {getByTestId} = renderWithTheme(
409
+ <RNPickerSelect {...searchEnabledWebProps} value="1" />
410
+ );
407
411
  await openSearchableWebPicker(getByTestId);
408
412
  await act(async () => {
409
413
  fireEvent.changeText(getByTestId("text_input"), "zzz");
@@ -469,7 +473,7 @@ describe("PickerSelect", () => {
469
473
  PlatformModule.OS = "web";
470
474
  const mockOnValueChange = mock(() => {});
471
475
  const {getByTestId, queryByTestId, rerender} = renderWithTheme(
472
- <RNPickerSelect {...searchableWebProps} onValueChange={mockOnValueChange} value="1" />
476
+ <RNPickerSelect {...searchEnabledWebProps} onValueChange={mockOnValueChange} value="1" />
473
477
  );
474
478
  await openSearchableWebPicker(getByTestId);
475
479
  await act(async () => {
@@ -478,7 +482,7 @@ describe("PickerSelect", () => {
478
482
  expect(mockOnValueChange).toHaveBeenCalledWith("2", 2);
479
483
  expect(queryByTestId("web_dropdown_backdrop")).toBeNull();
480
484
  rerender(
481
- <RNPickerSelect {...searchableWebProps} onValueChange={mockOnValueChange} value="2" />
485
+ <RNPickerSelect {...searchEnabledWebProps} onValueChange={mockOnValueChange} value="2" />
482
486
  );
483
487
  expect(getByTestId("text_input").props.value).toBe("Option 2");
484
488
  } finally {
@@ -494,7 +498,7 @@ describe("PickerSelect", () => {
494
498
  PlatformModule.OS = "web";
495
499
  const mockOnValueChange = mock(() => {});
496
500
  const {getByTestId} = renderWithTheme(
497
- <RNPickerSelect {...searchableWebProps} onValueChange={mockOnValueChange} value="1" />
501
+ <RNPickerSelect {...searchEnabledWebProps} onValueChange={mockOnValueChange} value="1" />
498
502
  );
499
503
  await openSearchableWebPicker(getByTestId);
500
504
  await act(async () => {
@@ -593,8 +597,8 @@ describe("PickerSelect", () => {
593
597
  const {getByTestId} = renderWithTheme(
594
598
  <RNPickerSelect
595
599
  {...defaultProps}
600
+ disableSearch
596
601
  onValueChange={mockOnValueChange}
597
- searchable={false}
598
602
  value="1"
599
603
  />
600
604
  );
@@ -619,7 +623,7 @@ describe("PickerSelect", () => {
619
623
  PlatformModule.OS = "android";
620
624
  const onOpen = mock(() => {});
621
625
  const {getByTestId, queryByTestId} = renderWithTheme(
622
- <RNPickerSelect {...defaultProps} onOpen={onOpen} searchable value="2" />
626
+ <RNPickerSelect {...defaultProps} disableSearch={false} onOpen={onOpen} value="2" />
623
627
  );
624
628
  expect(queryByTestId("android_picker")).toBeNull();
625
629
  await act(async () => {
@@ -647,8 +651,8 @@ describe("PickerSelect", () => {
647
651
  const {getByTestId} = renderWithTheme(
648
652
  <RNPickerSelect
649
653
  {...defaultProps}
654
+ disableSearch={false}
650
655
  onValueChange={mockOnValueChange}
651
- searchable
652
656
  value="1"
653
657
  />
654
658
  );
@@ -129,11 +129,10 @@ export interface RNPickerSelectProps {
129
129
  InputAccessoryView?: ComponentType<{testID?: string}>;
130
130
 
131
131
  /**
132
- * When true, options can be filtered as the user types. On web, search happens
133
- * in the trigger field; on native, search appears in the dropdown menu.
134
- * @default true
132
+ * When true, hides the type-to-filter search UI. Search is enabled by default.
133
+ * @default false
135
134
  */
136
- searchable?: boolean;
135
+ disableSearch?: boolean;
137
136
  }
138
137
 
139
138
  export const RNPickerSelect = ({
@@ -158,8 +157,9 @@ export const RNPickerSelect = ({
158
157
  touchableWrapperProps,
159
158
 
160
159
  InputAccessoryView,
161
- searchable = true,
160
+ disableSearch = false,
162
161
  }: RNPickerSelectProps) => {
162
+ const searchable = !disableSearch;
163
163
  const [showPicker, setShowPicker] = useState<boolean>(false);
164
164
  const [webSearchQuery, setWebSearchQuery] = useState("");
165
165
  const webSearchInputRef = useRef<TextInput>(null);
@@ -810,6 +810,7 @@ export const RNPickerSelect = ({
810
810
  )}
811
811
  <WebDropdownMenu
812
812
  anchor={webAnchor}
813
+ disableSearch={disableSearch || searchInTrigger}
813
814
  keepTriggerFocus={searchInTrigger}
814
815
  onClose={closeWebMenu}
815
816
  onSelect={(_val, idx) => {
@@ -823,7 +824,6 @@ export const RNPickerSelect = ({
823
824
  }}
824
825
  options={menuOptions}
825
826
  presentation={Platform.OS === "android" ? "centered" : "anchored"}
826
- searchable={searchable && !searchInTrigger}
827
827
  selectedIndex={menuSelectedIndex >= 0 ? menuSelectedIndex : undefined}
828
828
  showEmptyStateWhenNoOptions={searchInTrigger && webSearchQuery.trim().length > 0}
829
829
  testIDPrefix="web_dropdown"
@@ -227,12 +227,12 @@ export const SelectBadge = ({
227
227
  return (
228
228
  <WebDropdownMenu
229
229
  anchor={webAnchor}
230
+ disableSearch
230
231
  minWidth={160}
231
232
  onClose={() => setShowPicker(false)}
232
233
  onSelect={(val) => handleOnChange(val)}
233
234
  options={webMenuOptions}
234
235
  optionTextStyle={{fontFamily: "text", fontSize: 12}}
235
- searchable={false}
236
236
  selectedValue={value}
237
237
  testIDPrefix="web_badge"
238
238
  visible={showPicker}
@@ -15,7 +15,7 @@ export const SelectField: FC<SelectFieldProps> = ({
15
15
  options,
16
16
  requireValue = false,
17
17
  placeholder = "Please select an option.",
18
- searchable = true,
18
+ disableSearch = false,
19
19
  title,
20
20
  value,
21
21
  onChange,
@@ -32,6 +32,7 @@ export const SelectField: FC<SelectFieldProps> = ({
32
32
  <View style={{alignSelf: "stretch", minWidth: 0, width: "100%"}}>
33
33
  <RNPickerSelect
34
34
  disabled={disabled}
35
+ disableSearch={disableSearch}
35
36
  items={options}
36
37
  onValueChange={(v) => {
37
38
  if (v === undefined || v === null || v === "") {
@@ -41,7 +42,6 @@ export const SelectField: FC<SelectFieldProps> = ({
41
42
  }
42
43
  }}
43
44
  placeholder={!requireValue ? clearOption : {}}
44
- searchable={searchable}
45
45
  textInputProps={{testID: fieldTestIDs.input}}
46
46
  value={value ?? ""}
47
47
  />
@@ -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> = ({fullWidth = false, onChange, onStart, onEnd}: 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
+ };