@terreno/ui 58.0.5 → 58.1.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 (59) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/dist/AiSuggestionBox.js +3 -3
  3. package/dist/AiSuggestionBox.js.map +1 -1
  4. package/dist/Common.d.ts +38 -0
  5. package/dist/Common.d.ts.map +1 -1
  6. package/dist/Common.js.map +1 -1
  7. package/dist/DataTable.d.ts.map +1 -1
  8. package/dist/DataTable.js +3 -1
  9. package/dist/DataTable.js.map +1 -1
  10. package/dist/DraggableList.d.ts +7 -4
  11. package/dist/DraggableList.d.ts.map +1 -1
  12. package/dist/DraggableList.js +206 -4
  13. package/dist/DraggableList.js.map +1 -1
  14. package/dist/FeedbackCommentField.d.ts +8 -0
  15. package/dist/FeedbackCommentField.d.ts.map +1 -0
  16. package/dist/FeedbackCommentField.js +39 -0
  17. package/dist/FeedbackCommentField.js.map +1 -0
  18. package/dist/GPTChat.d.ts +15 -1
  19. package/dist/GPTChat.d.ts.map +1 -1
  20. package/dist/GPTChat.js +148 -43
  21. package/dist/GPTChat.js.map +1 -1
  22. package/dist/LabeledCheckBox.d.ts +3 -2
  23. package/dist/LabeledCheckBox.d.ts.map +1 -1
  24. package/dist/LabeledCheckBox.js +3 -2
  25. package/dist/LabeledCheckBox.js.map +1 -1
  26. package/dist/TextField.d.ts.map +1 -1
  27. package/dist/TextField.js +10 -7
  28. package/dist/TextField.js.map +1 -1
  29. package/dist/ThumbsUpDownButtons.d.ts +9 -0
  30. package/dist/ThumbsUpDownButtons.d.ts.map +1 -0
  31. package/dist/ThumbsUpDownButtons.js +60 -0
  32. package/dist/ThumbsUpDownButtons.js.map +1 -0
  33. package/dist/ThumbsUpDownFeedback.d.ts +3 -2
  34. package/dist/ThumbsUpDownFeedback.d.ts.map +1 -1
  35. package/dist/ThumbsUpDownFeedback.js +10 -52
  36. package/dist/ThumbsUpDownFeedback.js.map +1 -1
  37. package/dist/index.d.ts +1 -0
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +1 -0
  40. package/dist/index.js.map +1 -1
  41. package/package.json +2 -2
  42. package/src/AiSuggestionBox.tsx +3 -3
  43. package/src/Common.ts +39 -0
  44. package/src/DataTable.test.tsx +31 -0
  45. package/src/DataTable.tsx +3 -1
  46. package/src/DraggableList.test.tsx +141 -1
  47. package/src/DraggableList.tsx +359 -6
  48. package/src/FeedbackCommentField.test.tsx +107 -0
  49. package/src/FeedbackCommentField.tsx +74 -0
  50. package/src/GPTChat.test.tsx +327 -14
  51. package/src/GPTChat.tsx +463 -259
  52. package/src/LabeledCheckBox.tsx +3 -2
  53. package/src/TextField.test.tsx +126 -1
  54. package/src/TextField.tsx +13 -7
  55. package/src/ThumbsUpDownButtons.tsx +120 -0
  56. package/src/ThumbsUpDownFeedback.test.tsx +154 -1
  57. package/src/ThumbsUpDownFeedback.tsx +26 -101
  58. package/src/__snapshots__/DataTable.test.tsx.snap +28 -28
  59. package/src/index.tsx +1 -0
@@ -28,8 +28,9 @@ interface LabeledCheckBoxProps {
28
28
  }
29
29
 
30
30
  /**
31
- * A checkbox with its label as one pressable `role="checkbox"` control. Internal to the auth
32
- * screens and cards (trust this device, sign out other devices, I saved these codes).
31
+ * A checkbox with its label as one pressable `role="checkbox"` control. Used by the auth
32
+ * screens and the GPT chat tools list. Space toggles it, because a checkbox `Pressable` does
33
+ * not receive Space as a press on web.
33
34
  */
34
35
  export const LabeledCheckBox: FC<LabeledCheckBoxProps> = ({
35
36
  label,
@@ -904,7 +904,7 @@ describe("TextField", () => {
904
904
  }
905
905
  });
906
906
 
907
- it("disables browser autocorrect and spellcheck on web", () => {
907
+ it("enables spellcheck but keeps autocorrect off on web prose fields", () => {
908
908
  const savedOS = PlatformModule.OS;
909
909
  try {
910
910
  PlatformModule.OS = "web";
@@ -913,12 +913,137 @@ describe("TextField", () => {
913
913
  );
914
914
  const input = getByDisplayValue("controlled title");
915
915
  expect(input.props.autoCorrect).toBe(false);
916
+ expect(input.props.spellCheck).toBe(true);
917
+ } finally {
918
+ PlatformModule.OS = savedOS;
919
+ }
920
+ });
921
+
922
+ it("turns spellcheck off on web when spellCheck is false", () => {
923
+ const savedOS = PlatformModule.OS;
924
+ try {
925
+ PlatformModule.OS = "web";
926
+ const {getByDisplayValue} = renderWithTheme(
927
+ <TextField onChange={mockOnChange} spellCheck={false} type="text" value="no spell" />
928
+ );
929
+ expect(getByDisplayValue("no spell").props.spellCheck).toBe(false);
930
+ } finally {
931
+ PlatformModule.OS = savedOS;
932
+ }
933
+ });
934
+
935
+ it("does not enable spellcheck on web for specialized field types by default", () => {
936
+ const savedOS = PlatformModule.OS;
937
+ try {
938
+ PlatformModule.OS = "web";
939
+ const {getByDisplayValue} = renderWithTheme(
940
+ <TextField onChange={mockOnChange} type="email" value="user@example.com" />
941
+ );
942
+ const input = getByDisplayValue("user@example.com");
916
943
  expect(input.props.spellCheck).toBe(false);
944
+ expect(input.props.autoCorrect).toBe(false);
917
945
  } finally {
918
946
  PlatformModule.OS = savedOS;
919
947
  }
920
948
  });
921
949
 
950
+ it("applies spellCheck and autoCorrect overrides on native", () => {
951
+ const savedOS = PlatformModule.OS;
952
+ try {
953
+ PlatformModule.OS = "ios";
954
+ const {getByDisplayValue, rerender} = renderWithTheme(
955
+ <TextField onChange={mockOnChange} type="text" value="prose" />
956
+ );
957
+ const prose = getByDisplayValue("prose");
958
+ expect(prose.props.spellCheck).toBeUndefined();
959
+ expect(prose.props.autoCorrect).toBe(true);
960
+
961
+ rerender(
962
+ <TextField
963
+ autoCorrect={false}
964
+ onChange={mockOnChange}
965
+ spellCheck={true}
966
+ type="text"
967
+ value="prose"
968
+ />
969
+ );
970
+ const overridden = getByDisplayValue("prose");
971
+ expect(overridden.props.spellCheck).toBe(true);
972
+ expect(overridden.props.autoCorrect).toBe(false);
973
+ } finally {
974
+ PlatformModule.OS = savedOS;
975
+ }
976
+ });
977
+
978
+ it("forwards a spellcheck replacement on web when spellcheck is enabled", async () => {
979
+ const savedOS = PlatformModule.OS;
980
+ try {
981
+ PlatformModule.OS = "web";
982
+ let value = "teh";
983
+ const onChange = mock((next: string) => {
984
+ value = next;
985
+ });
986
+ const renderField = (): ReactElement => (
987
+ <TextField onChange={onChange} spellCheck type="text" value={value} />
988
+ );
989
+ const view = renderWithTheme(renderField());
990
+ const input = view.getByDisplayValue(value);
991
+ await act(async () => {
992
+ fireEvent(input, "change", {
993
+ nativeEvent: {inputType: "insertReplacementText", text: "the"},
994
+ });
995
+ fireEvent.changeText(input, "the");
996
+ });
997
+ expect(onChange).toHaveBeenCalledWith("the");
998
+ } finally {
999
+ PlatformModule.OS = savedOS;
1000
+ }
1001
+ });
1002
+
1003
+ it("drops a spellcheck replacement that flips a prose field back and forth", async () => {
1004
+ const savedOS = PlatformModule.OS;
1005
+ const originalNow = performance.now.bind(performance);
1006
+ let fakeNow = 30_000;
1007
+ const withoutSpace = "reachthreshold";
1008
+ const withSpace = "reach threshold";
1009
+
1010
+ try {
1011
+ PlatformModule.OS = "web";
1012
+ performance.now = () => fakeNow;
1013
+
1014
+ let value = withoutSpace;
1015
+ const onChange = mock((next: string) => {
1016
+ value = next;
1017
+ });
1018
+ const renderField = (): ReactElement => (
1019
+ <TextField onChange={onChange} type="text" value={value} />
1020
+ );
1021
+ const view = renderWithTheme(renderField());
1022
+
1023
+ const replace = async (text: string): Promise<void> => {
1024
+ const input = view.getByDisplayValue(value);
1025
+ await act(async () => {
1026
+ fireEvent(input, "change", {
1027
+ nativeEvent: {inputType: "insertReplacementText", text},
1028
+ });
1029
+ fireEvent.changeText(input, text);
1030
+ });
1031
+ view.rerender(renderField());
1032
+ fakeNow += 10;
1033
+ };
1034
+
1035
+ await replace(withSpace);
1036
+ await replace(withoutSpace);
1037
+ await replace(withSpace);
1038
+
1039
+ expect(onChange.mock.calls.map((call) => call[0])).toEqual([withSpace]);
1040
+ expect(value).toBe(withSpace);
1041
+ } finally {
1042
+ performance.now = originalNow;
1043
+ PlatformModule.OS = savedOS;
1044
+ }
1045
+ });
1046
+
922
1047
  it("does not recurse when synthetic onChange alternates by a single-space autocorrect delta", async () => {
923
1048
  const savedOS = PlatformModule.OS;
924
1049
  const withoutSpace =
package/src/TextField.tsx CHANGED
@@ -134,6 +134,8 @@ export const TextField: FC<TextFieldProps> = ({
134
134
  type = "text",
135
135
  showVisibilityToggle = true,
136
136
  autoComplete,
137
+ spellCheck,
138
+ autoCorrect,
137
139
  inputRef,
138
140
  multiline,
139
141
  rows = 1,
@@ -259,12 +261,10 @@ export const TextField: FC<TextFieldProps> = ({
259
261
  console.warn(`${type} is not yet supported`);
260
262
  }
261
263
 
262
- // RN Web browser autocorrect/spellcheck fights controlled `value` and can oscillate
263
- // between variants (e.g. "reachthreshold" vs "reach threshold"), firing onChangeText forever.
264
- const shouldAutocorrect =
265
- Platform.OS !== "web" &&
266
- ["text", "textarea"].includes(type) &&
267
- (!autoComplete || autoComplete === "on");
264
+ const isProseField =
265
+ ["text", "textarea"].includes(type) && (!autoComplete || autoComplete === "on");
266
+ const shouldSpellCheck = spellCheck ?? isProseField;
267
+ const shouldAutocorrect = autoCorrect ?? (Platform.OS !== "web" && isProseField);
268
268
 
269
269
  const valueRef = useRef(value);
270
270
  const onChangeRef = useRef(onChange);
@@ -314,6 +314,12 @@ export const TextField: FC<TextFieldProps> = ({
314
314
 
315
315
  const autofillProps = getAutofillProps({autoComplete, type: type || "text"});
316
316
  const keyboardType = keyboardMap[type];
317
+ const spellCheckProps =
318
+ Platform.OS === "web"
319
+ ? {spellCheck: shouldSpellCheck}
320
+ : spellCheck !== undefined
321
+ ? {spellCheck}
322
+ : {};
317
323
 
318
324
  return (
319
325
  <View
@@ -352,7 +358,7 @@ export const TextField: FC<TextFieldProps> = ({
352
358
  }}
353
359
  >
354
360
  <TextInput
355
- {...(Platform.OS === "web" ? {spellCheck: false} : {})}
361
+ {...spellCheckProps}
356
362
  {...autofillProps}
357
363
  accessibilityHint="Enter text here"
358
364
  accessibilityState={{disabled}}
@@ -0,0 +1,120 @@
1
+ import MaterialIcons from "@expo/vector-icons/MaterialIcons";
2
+ import {type ComponentProps, 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
+ * The thumbs up / thumbs down pair without the optional comment field. Kept apart from
39
+ * `ThumbsUpDownFeedback` so components that `TextField` renders (e.g. `AiSuggestionBox`) can use
40
+ * it without importing `TextField` back (an import cycle).
41
+ */
42
+ export const ThumbsUpDownButtons: FC<
43
+ Omit<ThumbsUpDownFeedbackProps, "comment" | "commentPlaceholder" | "onCommentSubmit">
44
+ > = ({
45
+ disabled = false,
46
+ negativeAccessibilityLabel = "Thumbs down",
47
+ onChange,
48
+ positiveAccessibilityLabel = "Thumbs up",
49
+ testID,
50
+ value,
51
+ }) => {
52
+ const {theme} = useTheme();
53
+
54
+ const handlePress = useCallback(
55
+ async (pressedValue: ThumbsUpDownFeedbackValue) => {
56
+ if (disabled) {
57
+ return;
58
+ }
59
+ await onChange(value === pressedValue ? undefined : pressedValue);
60
+ },
61
+ [disabled, onChange, value]
62
+ );
63
+
64
+ const handleKeyDown = useCallback(
65
+ async (optionValue: ThumbsUpDownFeedbackValue, event: WebKeyDownEvent) => {
66
+ if (event.key !== " " && event.key !== "Spacebar") {
67
+ return;
68
+ }
69
+ event.preventDefault();
70
+ // A native button activates once per press, so ignore auto-repeat from a held spacebar.
71
+ if (event.repeat) {
72
+ return;
73
+ }
74
+ await handlePress(optionValue);
75
+ },
76
+ [handlePress]
77
+ );
78
+
79
+ const renderOption = (optionValue: ThumbsUpDownFeedbackValue) => {
80
+ const isSelected = value === optionValue;
81
+ const isPositive = optionValue === "positive";
82
+ const iconColor = disabled ? theme.surface.disabled : theme.surface.secondaryDark;
83
+ const iconName =
84
+ ICON_NAME_MAP[isPositive ? "positive" : "negative"][isSelected ? "selected" : "unselected"];
85
+ const webKeyDownProps: WebKeyDownProps = {
86
+ onKeyDown: async (event) => handleKeyDown(optionValue, event),
87
+ };
88
+
89
+ return (
90
+ <Pressable
91
+ {...(webKeyDownProps as Pick<ComponentProps<typeof Pressable>, "onKeyDown">)}
92
+ accessibilityLabel={isPositive ? positiveAccessibilityLabel : negativeAccessibilityLabel}
93
+ accessibilityRole="checkbox"
94
+ accessibilityState={{checked: isSelected, disabled, selected: isSelected}}
95
+ aria-checked={isSelected}
96
+ aria-disabled={disabled}
97
+ disabled={disabled}
98
+ onPress={async () => handlePress(optionValue)}
99
+ style={{
100
+ alignItems: "center",
101
+ backgroundColor: "transparent",
102
+ borderRadius: TAP_TARGET_SIZE / 2,
103
+ height: TAP_TARGET_SIZE,
104
+ justifyContent: "center",
105
+ width: TAP_TARGET_SIZE,
106
+ }}
107
+ testID={testID ? `${testID}-${isPositive ? "positive" : "negative"}` : undefined}
108
+ >
109
+ <MaterialIcons color={iconColor} name={iconName} selectable={undefined} size={ICON_SIZE} />
110
+ </Pressable>
111
+ );
112
+ };
113
+
114
+ return (
115
+ <Box alignItems="center" direction="row" testID={testID}>
116
+ {renderOption("positive")}
117
+ {renderOption("negative")}
118
+ </Box>
119
+ );
120
+ };
@@ -1,5 +1,5 @@
1
1
  import {describe, expect, it, jest} from "bun:test";
2
- import {fireEvent} from "@testing-library/react-native";
2
+ import {fireEvent, waitFor} from "@testing-library/react-native";
3
3
 
4
4
  import {ThumbsUpDownFeedback} from "./ThumbsUpDownFeedback";
5
5
  import {renderWithTheme} from "./test-utils";
@@ -97,4 +97,157 @@ describe("ThumbsUpDownFeedback", () => {
97
97
  fireEvent.press(getByTestId("feedback-positive"));
98
98
  expect(onChange).not.toHaveBeenCalled();
99
99
  });
100
+ describe("comment field", () => {
101
+ it("does not render a comment input without onCommentSubmit", () => {
102
+ const {getByTestId, queryByTestId} = renderWithTheme(
103
+ <ThumbsUpDownFeedback onChange={jest.fn()} testID="feedback" value="negative" />
104
+ );
105
+ expect(getByTestId("feedback-negative")).toBeTruthy();
106
+ expect(queryByTestId("feedback-comment-input")).toBeNull();
107
+ expect(queryByTestId("feedback-comment-send")).toBeNull();
108
+ });
109
+
110
+ it("hides the input with no value and shows it once negative is selected", () => {
111
+ const onCommentSubmit = jest.fn();
112
+ const {getByTestId, queryByTestId, rerender} = renderWithTheme(
113
+ <ThumbsUpDownFeedback
114
+ onChange={jest.fn()}
115
+ onCommentSubmit={onCommentSubmit}
116
+ testID="feedback"
117
+ />
118
+ );
119
+ expect(queryByTestId("feedback-comment-input")).toBeNull();
120
+ rerender(
121
+ <ThumbsUpDownFeedback
122
+ onChange={jest.fn()}
123
+ onCommentSubmit={onCommentSubmit}
124
+ testID="feedback"
125
+ value="negative"
126
+ />
127
+ );
128
+ expect(getByTestId("feedback-comment-input")).toBeTruthy();
129
+ expect(getByTestId("feedback-comment-send")).toBeTruthy();
130
+ });
131
+
132
+ it("shows the input for positive too", () => {
133
+ const {getByTestId} = renderWithTheme(
134
+ <ThumbsUpDownFeedback
135
+ onChange={jest.fn()}
136
+ onCommentSubmit={jest.fn()}
137
+ testID="feedback"
138
+ value="positive"
139
+ />
140
+ );
141
+ expect(getByTestId("feedback-comment-input")).toBeTruthy();
142
+ });
143
+
144
+ it("uses the default and custom placeholder", () => {
145
+ const a = renderWithTheme(
146
+ <ThumbsUpDownFeedback
147
+ onChange={jest.fn()}
148
+ onCommentSubmit={jest.fn()}
149
+ testID="feedback"
150
+ value="negative"
151
+ />
152
+ );
153
+ expect(a.getByPlaceholderText("Tell us more (optional)")).toBeTruthy();
154
+ const b = renderWithTheme(
155
+ <ThumbsUpDownFeedback
156
+ commentPlaceholder="What went wrong?"
157
+ onChange={jest.fn()}
158
+ onCommentSubmit={jest.fn()}
159
+ testID="feedback"
160
+ value="negative"
161
+ />
162
+ );
163
+ expect(b.getByPlaceholderText("What went wrong?")).toBeTruthy();
164
+ });
165
+
166
+ it("sends the trimmed comment once, then shows the sent state", async () => {
167
+ const onCommentSubmit = jest.fn();
168
+ const {getByTestId, queryByTestId, getByText} = renderWithTheme(
169
+ <ThumbsUpDownFeedback
170
+ onChange={jest.fn()}
171
+ onCommentSubmit={onCommentSubmit}
172
+ testID="feedback"
173
+ value="negative"
174
+ />
175
+ );
176
+ fireEvent.changeText(getByTestId("feedback-comment-input"), " Too long ");
177
+ fireEvent.press(getByTestId("feedback-comment-send"));
178
+ await waitFor(() => expect(getByTestId("feedback-comment-sent")).toBeTruthy());
179
+ expect(onCommentSubmit).toHaveBeenCalledTimes(1);
180
+ expect(onCommentSubmit).toHaveBeenCalledWith("Too long");
181
+ expect(getByText("Thanks for the feedback")).toBeTruthy();
182
+ expect(queryByTestId("feedback-comment-input")).toBeNull();
183
+ });
184
+
185
+ it("does not send empty or whitespace text", () => {
186
+ const onCommentSubmit = jest.fn();
187
+ const {getByTestId} = renderWithTheme(
188
+ <ThumbsUpDownFeedback
189
+ onChange={jest.fn()}
190
+ onCommentSubmit={onCommentSubmit}
191
+ testID="feedback"
192
+ value="negative"
193
+ />
194
+ );
195
+ fireEvent.press(getByTestId("feedback-comment-send"));
196
+ fireEvent.changeText(getByTestId("feedback-comment-input"), " ");
197
+ fireEvent.press(getByTestId("feedback-comment-send"));
198
+ expect(onCommentSubmit).not.toHaveBeenCalled();
199
+ });
200
+
201
+ it("keeps the input and text when onCommentSubmit rejects", async () => {
202
+ const onCommentSubmit = jest.fn().mockRejectedValue(new Error("nope"));
203
+ const {getByTestId, queryByTestId} = renderWithTheme(
204
+ <ThumbsUpDownFeedback
205
+ onChange={jest.fn()}
206
+ onCommentSubmit={onCommentSubmit}
207
+ testID="feedback"
208
+ value="negative"
209
+ />
210
+ );
211
+ fireEvent.changeText(getByTestId("feedback-comment-input"), "keep me");
212
+ fireEvent.press(getByTestId("feedback-comment-send"));
213
+ await waitFor(() => expect(onCommentSubmit).toHaveBeenCalledTimes(1));
214
+ expect(queryByTestId("feedback-comment-sent")).toBeNull();
215
+ expect(getByTestId("feedback-comment-input").props.value).toBe("keep me");
216
+ });
217
+
218
+ it("hides the input when the selection is cleared", () => {
219
+ const onCommentSubmit = jest.fn();
220
+ const {queryByTestId, getByTestId, rerender} = renderWithTheme(
221
+ <ThumbsUpDownFeedback
222
+ onChange={jest.fn()}
223
+ onCommentSubmit={onCommentSubmit}
224
+ testID="feedback"
225
+ value="negative"
226
+ />
227
+ );
228
+ expect(getByTestId("feedback-comment-input")).toBeTruthy();
229
+ rerender(
230
+ <ThumbsUpDownFeedback
231
+ onChange={jest.fn()}
232
+ onCommentSubmit={onCommentSubmit}
233
+ testID="feedback"
234
+ />
235
+ );
236
+ expect(queryByTestId("feedback-comment-input")).toBeNull();
237
+ });
238
+
239
+ it("shows the sent state, not the input, when comment is already sent", () => {
240
+ const {getByTestId, queryByTestId} = renderWithTheme(
241
+ <ThumbsUpDownFeedback
242
+ comment="Too long"
243
+ onChange={jest.fn()}
244
+ onCommentSubmit={jest.fn()}
245
+ testID="feedback"
246
+ value="negative"
247
+ />
248
+ );
249
+ expect(getByTestId("feedback-comment-sent")).toBeTruthy();
250
+ expect(queryByTestId("feedback-comment-input")).toBeNull();
251
+ });
252
+ });
100
253
  });
@@ -1,118 +1,43 @@
1
- import MaterialIcons from "@expo/vector-icons/MaterialIcons";
2
- import {type ComponentProps, type FC, useCallback} from "react";
3
- import {Pressable} from "react-native";
1
+ import type {FC} from "react";
4
2
 
5
3
  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
- }
4
+ import type {ThumbsUpDownFeedbackProps} from "./Common";
5
+ import {FeedbackCommentField} from "./FeedbackCommentField";
6
+ import {ThumbsUpDownButtons} from "./ThumbsUpDownButtons";
36
7
 
37
8
  /**
38
9
  * A pair of thumbs up / thumbs down options for collecting binary feedback, e.g. on an AI
39
10
  * 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.
11
+ * again clears the selection and calls `onChange` with undefined. With `onCommentSubmit`, a
12
+ * comment field appears under the thumbs once a value is selected.
41
13
  */
42
14
  export const ThumbsUpDownFeedback: FC<ThumbsUpDownFeedbackProps> = ({
43
- disabled = false,
44
- negativeAccessibilityLabel = "Thumbs down",
45
- onChange,
46
- positiveAccessibilityLabel = "Thumbs up",
15
+ comment,
16
+ commentPlaceholder,
17
+ onCommentSubmit,
47
18
  testID,
48
19
  value,
20
+ ...buttonProps
49
21
  }) => {
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
- };
22
+ const thumbs = <ThumbsUpDownButtons {...buttonProps} testID={testID} value={value} />;
86
23
 
87
- return (
88
- <Pressable
89
- {...(webKeyDownProps as Pick<ComponentProps<typeof Pressable>, "onKeyDown">)}
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
- };
24
+ if (!onCommentSubmit) {
25
+ return thumbs;
26
+ }
111
27
 
112
28
  return (
113
- <Box alignItems="center" direction="row" testID={testID}>
114
- {renderOption("positive")}
115
- {renderOption("negative")}
29
+ <Box gap={2}>
30
+ {thumbs}
31
+ {value ? (
32
+ // Keyed by value so changing the vote starts a fresh comment.
33
+ <FeedbackCommentField
34
+ key={value}
35
+ onSubmit={onCommentSubmit}
36
+ placeholder={commentPlaceholder}
37
+ sentComment={comment}
38
+ testID={testID ? `${testID}-comment` : undefined}
39
+ />
40
+ ) : undefined}
116
41
  </Box>
117
42
  );
118
43
  };