@terreno/ui 0.28.0 → 0.30.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/AiSuggestionBox.js +7 -12
- package/dist/AiSuggestionBox.js.map +1 -1
- package/dist/Common.d.ts +115 -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/ThumbsUpDownFeedback.d.ts +8 -0
- package/dist/ThumbsUpDownFeedback.js +60 -0
- package/dist/ThumbsUpDownFeedback.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 +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/package.json +5 -1
- package/src/AiSuggestionBox.test.tsx +2 -2
- package/src/AiSuggestionBox.tsx +17 -48
- package/src/Common.ts +125 -0
- package/src/ConsentHistory.test.tsx +161 -0
- package/src/Signature.native.tsx +10 -2
- package/src/Signature.tsx +5 -0
- package/src/SignatureCaptureField.test.tsx +165 -0
- package/src/SignatureCaptureField.tsx +172 -0
- package/src/ThumbsUpDownFeedback.test.tsx +100 -0
- package/src/ThumbsUpDownFeedback.tsx +118 -0
- package/src/TypedSignatureField.test.tsx +113 -0
- package/src/TypedSignatureField.tsx +181 -0
- package/src/__snapshots__/AiSuggestionBox.test.tsx.snap +108 -30
- package/src/__snapshots__/SignatureCaptureField.test.tsx.snap +96 -0
- package/src/__snapshots__/TypedSignatureField.test.tsx.snap +372 -0
- package/src/bunSetup.ts +33 -0
- package/src/index.tsx +3 -0
|
@@ -0,0 +1,165 @@
|
|
|
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("renders a placeholder box when disabled with a drawn value that has no image", () => {
|
|
95
|
+
const {toJSON} = renderWithTheme(
|
|
96
|
+
<SignatureCaptureField disabled onChange={() => {}} value={{image: "", mode: "draw"}} />
|
|
97
|
+
);
|
|
98
|
+
expect(toJSON()).toMatchSnapshot();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("renders a read-only typed signature when disabled with a typed value", () => {
|
|
102
|
+
const {getByDisplayValue, queryByText} = renderWithTheme(
|
|
103
|
+
<SignatureCaptureField
|
|
104
|
+
disabled
|
|
105
|
+
helperText="Signed on Jan 1"
|
|
106
|
+
onChange={() => {}}
|
|
107
|
+
title="Signature"
|
|
108
|
+
value={{fontKey: "dancing-script", mode: "type", typedName: "Jane Doe"}}
|
|
109
|
+
/>
|
|
110
|
+
);
|
|
111
|
+
expect(getByDisplayValue("Jane Doe")).toBeTruthy();
|
|
112
|
+
expect(queryByText("Draw")).toBeNull();
|
|
113
|
+
expect(queryByText("Type")).toBeNull();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("renders the disabled typed field when disabled without a value", () => {
|
|
117
|
+
const {getByText, queryByText} = renderWithTheme(
|
|
118
|
+
<SignatureCaptureField disabled helperText="No signature captured" onChange={() => {}} />
|
|
119
|
+
);
|
|
120
|
+
expect(getByText("No signature captured")).toBeTruthy();
|
|
121
|
+
expect(queryByText("Draw")).toBeNull();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("forwards the resolved input test id to the typed name input in type mode", () => {
|
|
125
|
+
const {UNSAFE_getAllByProps} = renderWithTheme(
|
|
126
|
+
<SignatureCaptureField {...defaultProps} testID="sig" />
|
|
127
|
+
);
|
|
128
|
+
// testID "sig" resolves to input id "sig" and is forwarded to the typed name input.
|
|
129
|
+
const inputs = UNSAFE_getAllByProps({}).filter(
|
|
130
|
+
(el) => typeof el.props?.onChangeText === "function"
|
|
131
|
+
);
|
|
132
|
+
expect(inputs.length).toBeGreaterThan(0);
|
|
133
|
+
expect(inputs[0].props.testID).toBe("sig");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("forwards the resolved input test id to the draw pad in draw mode", () => {
|
|
137
|
+
const {UNSAFE_getAllByProps} = renderWithTheme(
|
|
138
|
+
<SignatureCaptureField {...defaultProps} defaultMode="draw" testID="sig" />
|
|
139
|
+
);
|
|
140
|
+
// The draw pad is the node exposing both onChange and fullWidth; it should carry the id.
|
|
141
|
+
const drawPads = UNSAFE_getAllByProps({}).filter(
|
|
142
|
+
(el) => typeof el.props?.onChange === "function" && "fullWidth" in (el.props ?? {})
|
|
143
|
+
);
|
|
144
|
+
expect(drawPads.length).toBeGreaterThan(0);
|
|
145
|
+
expect(drawPads[0].props.testID).toBe("sig");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("forwards an explicit testIDs.input to the active control", () => {
|
|
149
|
+
const {UNSAFE_getAllByProps} = renderWithTheme(
|
|
150
|
+
<SignatureCaptureField {...defaultProps} testIDs={{input: "custom-input"}} />
|
|
151
|
+
);
|
|
152
|
+
const inputs = UNSAFE_getAllByProps({}).filter(
|
|
153
|
+
(el) => typeof el.props?.onChangeText === "function"
|
|
154
|
+
);
|
|
155
|
+
expect(inputs.length).toBeGreaterThan(0);
|
|
156
|
+
expect(inputs[0].props.testID).toBe("custom-input");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("renders an error message when errorText is provided", () => {
|
|
160
|
+
const {getByText} = renderWithTheme(
|
|
161
|
+
<SignatureCaptureField {...defaultProps} errorText="A signature is required." />
|
|
162
|
+
);
|
|
163
|
+
expect(getByText("A signature is required.")).toBeTruthy();
|
|
164
|
+
});
|
|
165
|
+
});
|
|
@@ -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,100 @@
|
|
|
1
|
+
import {describe, expect, it, jest} from "bun:test";
|
|
2
|
+
import {fireEvent} from "@testing-library/react-native";
|
|
3
|
+
|
|
4
|
+
import {ThumbsUpDownFeedback} from "./ThumbsUpDownFeedback";
|
|
5
|
+
import {renderWithTheme} from "./test-utils";
|
|
6
|
+
|
|
7
|
+
describe("ThumbsUpDownFeedback", () => {
|
|
8
|
+
it("renders both options", () => {
|
|
9
|
+
const {getByTestId} = renderWithTheme(
|
|
10
|
+
<ThumbsUpDownFeedback onChange={jest.fn()} testID="feedback" />
|
|
11
|
+
);
|
|
12
|
+
expect(getByTestId("feedback-positive")).toBeTruthy();
|
|
13
|
+
expect(getByTestId("feedback-negative")).toBeTruthy();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("calls onChange with the pressed value", () => {
|
|
17
|
+
const onChange = jest.fn();
|
|
18
|
+
const {getByTestId} = renderWithTheme(
|
|
19
|
+
<ThumbsUpDownFeedback onChange={onChange} testID="feedback" />
|
|
20
|
+
);
|
|
21
|
+
fireEvent.press(getByTestId("feedback-negative"));
|
|
22
|
+
expect(onChange).toHaveBeenCalledWith("negative");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("clears the selection when pressing the selected option", () => {
|
|
26
|
+
const onChange = jest.fn();
|
|
27
|
+
const {getByTestId} = renderWithTheme(
|
|
28
|
+
<ThumbsUpDownFeedback onChange={onChange} testID="feedback" value="positive" />
|
|
29
|
+
);
|
|
30
|
+
fireEvent.press(getByTestId("feedback-positive"));
|
|
31
|
+
expect(onChange).toHaveBeenCalledWith(undefined);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("exposes the selection and custom labels to assistive technology", () => {
|
|
35
|
+
const {getByTestId} = renderWithTheme(
|
|
36
|
+
<ThumbsUpDownFeedback
|
|
37
|
+
negativeAccessibilityLabel="Not helpful"
|
|
38
|
+
onChange={jest.fn()}
|
|
39
|
+
positiveAccessibilityLabel="Helpful"
|
|
40
|
+
testID="feedback"
|
|
41
|
+
value="negative"
|
|
42
|
+
/>
|
|
43
|
+
);
|
|
44
|
+
const positive = getByTestId("feedback-positive");
|
|
45
|
+
const negative = getByTestId("feedback-negative");
|
|
46
|
+
expect(positive.props.accessibilityLabel).toBe("Helpful");
|
|
47
|
+
expect(negative.props.accessibilityLabel).toBe("Not helpful");
|
|
48
|
+
expect(positive.props.accessibilityState.selected).toBe(false);
|
|
49
|
+
expect(negative.props.accessibilityState.selected).toBe(true);
|
|
50
|
+
// react-native-web ignores accessibilityState, so the flat aria prop is what reaches the DOM.
|
|
51
|
+
expect(positive.props["aria-checked"]).toBe(false);
|
|
52
|
+
expect(negative.props["aria-checked"]).toBe(true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("activates on the spacebar, which react-native-web only handles for buttons", async () => {
|
|
56
|
+
const onChange = jest.fn();
|
|
57
|
+
const preventDefault = jest.fn();
|
|
58
|
+
const {getByTestId} = renderWithTheme(
|
|
59
|
+
<ThumbsUpDownFeedback onChange={onChange} testID="feedback" />
|
|
60
|
+
);
|
|
61
|
+
await getByTestId("feedback-positive").props.onKeyDown({key: " ", preventDefault});
|
|
62
|
+
expect(preventDefault).toHaveBeenCalled();
|
|
63
|
+
expect(onChange).toHaveBeenCalledWith("positive");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("ignores auto-repeat from a held spacebar", async () => {
|
|
67
|
+
const onChange = jest.fn();
|
|
68
|
+
const preventDefault = jest.fn();
|
|
69
|
+
const {getByTestId} = renderWithTheme(
|
|
70
|
+
<ThumbsUpDownFeedback onChange={onChange} testID="feedback" />
|
|
71
|
+
);
|
|
72
|
+
await getByTestId("feedback-positive").props.onKeyDown({
|
|
73
|
+
key: " ",
|
|
74
|
+
preventDefault,
|
|
75
|
+
repeat: true,
|
|
76
|
+
});
|
|
77
|
+
expect(preventDefault).toHaveBeenCalled();
|
|
78
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("ignores other keys so react-native-web keeps handling enter", async () => {
|
|
82
|
+
const onChange = jest.fn();
|
|
83
|
+
const preventDefault = jest.fn();
|
|
84
|
+
const {getByTestId} = renderWithTheme(
|
|
85
|
+
<ThumbsUpDownFeedback onChange={onChange} testID="feedback" />
|
|
86
|
+
);
|
|
87
|
+
await getByTestId("feedback-positive").props.onKeyDown({key: "Enter", preventDefault});
|
|
88
|
+
expect(preventDefault).not.toHaveBeenCalled();
|
|
89
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("does not call onChange when disabled", () => {
|
|
93
|
+
const onChange = jest.fn();
|
|
94
|
+
const {getByTestId} = renderWithTheme(
|
|
95
|
+
<ThumbsUpDownFeedback disabled onChange={onChange} testID="feedback" />
|
|
96
|
+
);
|
|
97
|
+
fireEvent.press(getByTestId("feedback-positive"));
|
|
98
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import MaterialIcons from "@expo/vector-icons/MaterialIcons";
|
|
2
|
+
import {type FC, useCallback} from "react";
|
|
3
|
+
import {Pressable} from "react-native";
|
|
4
|
+
|
|
5
|
+
import {Box} from "./Box";
|
|
6
|
+
import type {ThumbsUpDownFeedbackProps, ThumbsUpDownFeedbackValue} from "./Common";
|
|
7
|
+
import {useTheme} from "./Theme";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* MaterialIcons pads its artwork inside the em box and the thumbs are wider than they are tall, so
|
|
11
|
+
* this font size is the one that paints a 16px wide thumb — the Figma bounding box.
|
|
12
|
+
*/
|
|
13
|
+
const ICON_SIZE = 19.2;
|
|
14
|
+
|
|
15
|
+
const ICON_NAME_MAP = {
|
|
16
|
+
negative: {selected: "thumb-down-alt", unselected: "thumb-down-off-alt"},
|
|
17
|
+
positive: {selected: "thumb-up-alt", unselected: "thumb-up-off-alt"},
|
|
18
|
+
} as const;
|
|
19
|
+
|
|
20
|
+
const TAP_TARGET_SIZE = 32;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* react-native-web only activates a Pressable on the spacebar when the element is a native button or
|
|
24
|
+
* has role="button", so a checkbox needs its own spacebar handler (per ARIA, spacebar is the
|
|
25
|
+
* activation key for a checkbox). Not typed by react-native, so it is spread in separately.
|
|
26
|
+
*/
|
|
27
|
+
interface WebKeyDownEvent {
|
|
28
|
+
key: string;
|
|
29
|
+
preventDefault: () => void;
|
|
30
|
+
repeat?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface WebKeyDownProps {
|
|
34
|
+
onKeyDown?: (event: WebKeyDownEvent) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A pair of thumbs up / thumbs down options for collecting binary feedback, e.g. on an AI
|
|
39
|
+
* generated response. The selected option switches from an outlined to a filled thumb; pressing it
|
|
40
|
+
* again clears the selection and calls `onChange` with undefined.
|
|
41
|
+
*/
|
|
42
|
+
export const ThumbsUpDownFeedback: FC<ThumbsUpDownFeedbackProps> = ({
|
|
43
|
+
disabled = false,
|
|
44
|
+
negativeAccessibilityLabel = "Thumbs down",
|
|
45
|
+
onChange,
|
|
46
|
+
positiveAccessibilityLabel = "Thumbs up",
|
|
47
|
+
testID,
|
|
48
|
+
value,
|
|
49
|
+
}) => {
|
|
50
|
+
const {theme} = useTheme();
|
|
51
|
+
|
|
52
|
+
const handlePress = useCallback(
|
|
53
|
+
async (pressedValue: ThumbsUpDownFeedbackValue) => {
|
|
54
|
+
if (disabled) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
await onChange(value === pressedValue ? undefined : pressedValue);
|
|
58
|
+
},
|
|
59
|
+
[disabled, onChange, value]
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const handleKeyDown = useCallback(
|
|
63
|
+
async (optionValue: ThumbsUpDownFeedbackValue, event: WebKeyDownEvent) => {
|
|
64
|
+
if (event.key !== " " && event.key !== "Spacebar") {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
event.preventDefault();
|
|
68
|
+
// A native button activates once per press, so ignore auto-repeat from a held spacebar.
|
|
69
|
+
if (event.repeat) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
await handlePress(optionValue);
|
|
73
|
+
},
|
|
74
|
+
[handlePress]
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const renderOption = (optionValue: ThumbsUpDownFeedbackValue) => {
|
|
78
|
+
const isSelected = value === optionValue;
|
|
79
|
+
const isPositive = optionValue === "positive";
|
|
80
|
+
const iconColor = disabled ? theme.surface.disabled : theme.surface.secondaryDark;
|
|
81
|
+
const iconName =
|
|
82
|
+
ICON_NAME_MAP[isPositive ? "positive" : "negative"][isSelected ? "selected" : "unselected"];
|
|
83
|
+
const webKeyDownProps: WebKeyDownProps = {
|
|
84
|
+
onKeyDown: async (event) => handleKeyDown(optionValue, event),
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<Pressable
|
|
89
|
+
{...webKeyDownProps}
|
|
90
|
+
accessibilityLabel={isPositive ? positiveAccessibilityLabel : negativeAccessibilityLabel}
|
|
91
|
+
accessibilityRole="checkbox"
|
|
92
|
+
accessibilityState={{checked: isSelected, disabled, selected: isSelected}}
|
|
93
|
+
aria-checked={isSelected}
|
|
94
|
+
aria-disabled={disabled}
|
|
95
|
+
disabled={disabled}
|
|
96
|
+
onPress={async () => handlePress(optionValue)}
|
|
97
|
+
style={{
|
|
98
|
+
alignItems: "center",
|
|
99
|
+
backgroundColor: "transparent",
|
|
100
|
+
borderRadius: TAP_TARGET_SIZE / 2,
|
|
101
|
+
height: TAP_TARGET_SIZE,
|
|
102
|
+
justifyContent: "center",
|
|
103
|
+
width: TAP_TARGET_SIZE,
|
|
104
|
+
}}
|
|
105
|
+
testID={testID ? `${testID}-${isPositive ? "positive" : "negative"}` : undefined}
|
|
106
|
+
>
|
|
107
|
+
<MaterialIcons color={iconColor} name={iconName} selectable={undefined} size={ICON_SIZE} />
|
|
108
|
+
</Pressable>
|
|
109
|
+
);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
<Box alignItems="center" direction="row" testID={testID}>
|
|
114
|
+
{renderOption("positive")}
|
|
115
|
+
{renderOption("negative")}
|
|
116
|
+
</Box>
|
|
117
|
+
);
|
|
118
|
+
};
|
|
@@ -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
|
+
});
|