@terreno/ui 0.23.1 → 0.25.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 (80) hide show
  1. package/dist/AIRequestExplorer.js +6 -1
  2. package/dist/AIRequestExplorer.js.map +1 -1
  3. package/dist/ActionSheet.d.ts +1 -0
  4. package/dist/ActionSheet.js +3 -0
  5. package/dist/ActionSheet.js.map +1 -1
  6. package/dist/AiSuggestionBox.d.ts +11 -0
  7. package/dist/AiSuggestionBox.js +50 -23
  8. package/dist/AiSuggestionBox.js.map +1 -1
  9. package/dist/Common.d.ts +25 -1
  10. package/dist/Common.js.map +1 -1
  11. package/dist/CustomSelectField.js +2 -2
  12. package/dist/CustomSelectField.js.map +1 -1
  13. package/dist/FilePickerButton.js +2 -1
  14. package/dist/FilePickerButton.js.map +1 -1
  15. package/dist/PickerSelect.d.ts +10 -2
  16. package/dist/PickerSelect.js +91 -17
  17. package/dist/PickerSelect.js.map +1 -1
  18. package/dist/SelectBadge.js +1 -1
  19. package/dist/SelectBadge.js.map +1 -1
  20. package/dist/SelectField.js +9 -9
  21. package/dist/SelectField.js.map +1 -1
  22. package/dist/WebDropdownMenu.d.ts +36 -5
  23. package/dist/WebDropdownMenu.js +211 -29
  24. package/dist/WebDropdownMenu.js.map +1 -1
  25. package/dist/createWebPortal.d.ts +7 -0
  26. package/dist/createWebPortal.js +3 -0
  27. package/dist/createWebPortal.js.map +1 -0
  28. package/dist/createWebPortal.native.d.ts +7 -0
  29. package/dist/createWebPortal.native.js +2 -0
  30. package/dist/createWebPortal.native.js.map +1 -0
  31. package/dist/fieldElements/FieldHelperText.js +1 -1
  32. package/dist/fieldElements/FieldHelperText.js.map +1 -1
  33. package/dist/icons/SparklesIcon.d.ts +7 -0
  34. package/dist/icons/SparklesIcon.js +32 -0
  35. package/dist/icons/SparklesIcon.js.map +1 -0
  36. package/dist/icons/index.d.ts +1 -0
  37. package/dist/icons/index.js +1 -0
  38. package/dist/icons/index.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/AIRequestExplorer.tsx +6 -1
  41. package/src/ActionSheet.test.tsx +198 -2
  42. package/src/ActionSheet.tsx +4 -0
  43. package/src/AiSuggestionBox.test.tsx +184 -15
  44. package/src/AiSuggestionBox.tsx +72 -55
  45. package/src/Common.ts +28 -1
  46. package/src/CustomSelectField.test.tsx +13 -3
  47. package/src/CustomSelectField.tsx +2 -0
  48. package/src/EmojiSelector.test.tsx +153 -0
  49. package/src/FilePickerButton.tsx +2 -1
  50. package/src/Modal.test.tsx +47 -0
  51. package/src/PickerSelect.test.tsx +253 -14
  52. package/src/PickerSelect.tsx +206 -49
  53. package/src/SelectBadge.tsx +1 -0
  54. package/src/SelectBadge.web.test.tsx +10 -0
  55. package/src/SelectField.tsx +19 -15
  56. package/src/WebDropdownMenu.test.tsx +449 -0
  57. package/src/WebDropdownMenu.tsx +405 -60
  58. package/src/__snapshots__/AddressField.test.tsx.snap +2088 -665
  59. package/src/__snapshots__/AiSuggestionBox.test.tsx.snap +489 -102
  60. package/src/__snapshots__/CustomSelectField.test.tsx.snap +2118 -1369
  61. package/src/__snapshots__/Field.test.tsx.snap +3787 -1806
  62. package/src/__snapshots__/HeightField.test.tsx.snap +3 -0
  63. package/src/__snapshots__/PickerSelect.test.tsx.snap +7 -7
  64. package/src/__snapshots__/SelectField.test.tsx.snap +1548 -1102
  65. package/src/__snapshots__/Slider.test.tsx.snap +3 -0
  66. package/src/__snapshots__/TextArea.test.tsx.snap +3 -0
  67. package/src/__snapshots__/TextField.test.tsx.snap +3 -0
  68. package/src/__snapshots__/TimezonePicker.test.tsx.snap +2499 -1353
  69. package/src/createWebPortal.native.ts +8 -0
  70. package/src/createWebPortal.test.tsx +13 -0
  71. package/src/createWebPortal.ts +10 -0
  72. package/src/fieldElements/FieldHelperText.tsx +4 -2
  73. package/src/fieldElements/__snapshots__/FieldHelperText.test.tsx.snap +3 -0
  74. package/src/icons/SparklesIcon.test.tsx +16 -0
  75. package/src/icons/SparklesIcon.tsx +38 -0
  76. package/src/icons/__snapshots__/SparklesIcon.test.tsx.snap +47 -0
  77. package/src/icons/index.ts +1 -0
  78. package/src/table/TableBadge.test.tsx +19 -11
  79. package/src/table/__snapshots__/TableBadge.test.tsx.snap +226 -175
  80. package/src/types/react-dom.d.ts +5 -0
@@ -1,21 +1,36 @@
1
- import {type ReactElement, useRef, useState} from "react";
1
+ import {type ReactElement, useCallback, useEffect, useMemo, useRef, useState} from "react";
2
2
  import {
3
+ Dimensions,
3
4
  type DimensionValue,
4
5
  Modal,
6
+ Platform,
5
7
  Pressable,
6
8
  ScrollView,
7
9
  Text,
10
+ TextInput,
8
11
  type TextStyle,
9
12
  View,
13
+ type ViewStyle,
10
14
  } from "react-native";
11
15
 
16
+ import {createWebPortal} from "./createWebPortal";
12
17
  import {useTheme} from "./Theme";
13
18
 
19
+ export const scheduleAfterPaint = (callback: () => void): void => {
20
+ if (typeof requestAnimationFrame === "function") {
21
+ requestAnimationFrame(callback);
22
+ } else {
23
+ setTimeout(callback, 0);
24
+ }
25
+ };
26
+
14
27
  export interface WebDropdownMenuOption {
15
28
  key?: string | number;
16
29
  label: string;
17
30
  value: string;
18
31
  color?: string;
32
+ /** Secondary line under the label (web dropdown only). */
33
+ helperText?: string;
19
34
  }
20
35
 
21
36
  export interface WebDropdownAnchor {
@@ -53,6 +68,29 @@ export interface WebDropdownMenuProps {
53
68
  optionTextStyle?: TextStyle;
54
69
  /** Prefix for the testIDs on the menu / backdrop / option nodes. */
55
70
  testIDPrefix?: string;
71
+ /**
72
+ * When true, renders a search input at the top of the dropdown that
73
+ * filters options by label as the user types. The filter resets each
74
+ * time the menu opens.
75
+ * @default true
76
+ */
77
+ searchable?: boolean;
78
+ /**
79
+ * When true and `options` is empty, shows a "No matching options" message.
80
+ * Used when the parent filters options externally (e.g. search in the trigger).
81
+ */
82
+ showEmptyStateWhenNoOptions?: boolean;
83
+ /**
84
+ * When true, renders the menu in a lightweight portal overlay instead of
85
+ * React Native's Modal so focus can remain on an external trigger input.
86
+ * Web only.
87
+ */
88
+ keepTriggerFocus?: boolean;
89
+ /**
90
+ * `anchored` positions the menu below/above the trigger (web-style).
91
+ * `centered` shows a centered dialog, matching Android's native picker modal.
92
+ */
93
+ presentation?: "anchored" | "centered";
56
94
  }
57
95
 
58
96
  interface PressableWebState {
@@ -62,10 +100,15 @@ interface PressableWebState {
62
100
  }
63
101
 
64
102
  /**
65
- * Shared web-only popup used by `RNPickerSelect` and `SelectBadge` so every
66
- * browser renders the same styled dropdown instead of falling back to the
67
- * platform-native `<select>` UI. Must be anchored to a trigger element via
68
- * `useWebDropdownAnchor` (or an equivalent measurement).
103
+ * Shared popup used by `RNPickerSelect` and `SelectBadge` for a consistent
104
+ * styled dropdown across web and native. Must be anchored to a trigger element
105
+ * via `useWebDropdownAnchor` (or an equivalent measurement) when using anchored
106
+ * presentation.
107
+ *
108
+ * When `searchable` is true a text input appears at the top of the menu so
109
+ * the user can type to filter options by label. `RNPickerSelect` handles
110
+ * search in the trigger field on web instead and passes pre-filtered options
111
+ * with `searchable={false}`.
69
112
  */
70
113
  export const WebDropdownMenu = ({
71
114
  visible,
@@ -79,62 +122,217 @@ export const WebDropdownMenu = ({
79
122
  minWidth,
80
123
  optionTextStyle,
81
124
  testIDPrefix = "web_dropdown",
125
+ searchable = true,
126
+ showEmptyStateWhenNoOptions = false,
127
+ keepTriggerFocus = false,
128
+ presentation = "anchored",
82
129
  }: WebDropdownMenuProps): ReactElement => {
83
130
  const {theme} = useTheme();
131
+ const [searchQuery, setSearchQuery] = useState("");
132
+ const searchInputRef = useRef<TextInput>(null);
133
+ const listScrollRef = useRef<ScrollView>(null);
134
+ const listViewportHeightRef = useRef(0);
135
+ const optionLayoutsRef = useRef<Array<{height: number; offset: number}>>([]);
84
136
 
85
- return (
86
- <Modal
87
- animationType="none"
88
- onRequestClose={onClose}
89
- testID={`${testIDPrefix}_modal`}
90
- transparent
91
- visible={visible}
92
- >
93
- <Pressable
94
- aria-role="button"
95
- onPress={onClose}
96
- style={{flex: 1}}
97
- testID={`${testIDPrefix}_backdrop`}
98
- />
99
- <View
100
- style={{
101
- backgroundColor: theme.surface.base,
102
- borderColor: theme.border.dark,
103
- borderRadius: 4,
104
- borderWidth: 1,
105
- left: anchor.x,
106
- maxHeight: 300,
107
- minWidth,
108
- overflow: "hidden",
109
- position: "absolute",
110
- shadowColor: "#000",
111
- shadowOffset: {height: 2, width: 0},
112
- shadowOpacity: 0.15,
113
- shadowRadius: 8,
114
- top: anchor.y + anchor.height + 4,
115
- width: width ?? anchor.width,
137
+ const normalizedQuery = searchQuery.toLowerCase();
138
+ const filteredOptions = useMemo(() => {
139
+ if (!searchable || normalizedQuery.length === 0) {
140
+ return options;
141
+ }
142
+ return options.filter((item) => {
143
+ if (item.label.toLowerCase().includes(normalizedQuery)) {
144
+ return true;
145
+ }
146
+ const helper = item.helperText?.toLowerCase();
147
+ if (helper?.includes(normalizedQuery)) {
148
+ return true;
149
+ }
150
+ return false;
151
+ });
152
+ }, [normalizedQuery, options, searchable]);
153
+
154
+ const selectedFilteredIndex = useMemo(() => {
155
+ if (selectedIndex === undefined) {
156
+ return -1;
157
+ }
158
+ return filteredOptions.findIndex((item) => options.indexOf(item) === selectedIndex);
159
+ }, [filteredOptions, options, selectedIndex]);
160
+
161
+ const scrollSelectedIntoView = useCallback((): void => {
162
+ if (selectedFilteredIndex < 0 || !listScrollRef.current) {
163
+ return;
164
+ }
165
+ const layout = optionLayoutsRef.current[selectedFilteredIndex];
166
+ const viewportHeight = listViewportHeightRef.current;
167
+ if (!layout || viewportHeight <= 0) {
168
+ return;
169
+ }
170
+ const centeredOffset = layout.offset - viewportHeight / 2 + layout.height / 2;
171
+ listScrollRef.current.scrollTo({
172
+ animated: false,
173
+ y: Math.max(0, centeredOffset),
174
+ });
175
+ }, [selectedFilteredIndex]);
176
+
177
+ // Reset the search query each time the menu opens and auto-focus the
178
+ // search input so the user can immediately start typing.
179
+ useEffect(() => {
180
+ if (!visible) {
181
+ return;
182
+ }
183
+ setSearchQuery("");
184
+ optionLayoutsRef.current = [];
185
+ if (searchable) {
186
+ scheduleAfterPaint(() => {
187
+ searchInputRef.current?.focus();
188
+ });
189
+ }
190
+ }, [searchable, visible]);
191
+
192
+ // Center the selected row in the list when the menu opens (native picker parity).
193
+ useEffect(() => {
194
+ if (!visible || selectedFilteredIndex < 0) {
195
+ return;
196
+ }
197
+ scheduleAfterPaint(() => {
198
+ scrollSelectedIntoView();
199
+ });
200
+ }, [scrollSelectedIntoView, selectedFilteredIndex, visible]);
201
+
202
+ const handleOptionLayout = useCallback(
203
+ (index: number, offset: number, height: number): void => {
204
+ optionLayoutsRef.current[index] = {height, offset};
205
+ if (visible && index === selectedFilteredIndex) {
206
+ scheduleAfterPaint(() => {
207
+ scrollSelectedIntoView();
208
+ });
209
+ }
210
+ },
211
+ [scrollSelectedIntoView, selectedFilteredIndex, visible]
212
+ );
213
+
214
+ const windowHeight = Dimensions.get("window").height;
215
+ const windowWidth = Dimensions.get("window").width;
216
+ const isCenteredPresentation = presentation === "centered";
217
+
218
+ const menuMaxHeight = isCenteredPresentation ? Math.round(windowHeight * 0.55) : 300;
219
+ const gap = 4;
220
+ const spaceBelow = windowHeight - (anchor.y + anchor.height + gap);
221
+ // If not enough room below the trigger, open the menu above it instead.
222
+ const isOpenAbove =
223
+ !isCenteredPresentation && spaceBelow < menuMaxHeight && anchor.y > spaceBelow;
224
+ const menuTop = anchor.y + anchor.height + gap;
225
+ const menuBottom = windowHeight - anchor.y + gap;
226
+ const clampedMaxHeight = isCenteredPresentation
227
+ ? menuMaxHeight
228
+ : isOpenAbove
229
+ ? Math.min(menuMaxHeight, anchor.y - gap)
230
+ : Math.min(menuMaxHeight, spaceBelow);
231
+
232
+ const anchoredMenuLayoutStyle = {
233
+ backgroundColor: theme.surface.base,
234
+ borderColor: theme.border.dark,
235
+ borderRadius: 4,
236
+ borderWidth: 1,
237
+ left: anchor.x,
238
+ maxHeight: clampedMaxHeight,
239
+ minWidth,
240
+ overflow: "hidden" as const,
241
+ shadowColor: "#000",
242
+ shadowOffset: {height: 2, width: 0},
243
+ shadowOpacity: 0.15,
244
+ shadowRadius: 8,
245
+ ...(isOpenAbove ? {bottom: menuBottom} : {top: menuTop}),
246
+ width: width ?? anchor.width,
247
+ zIndex: 2,
248
+ };
249
+
250
+ const centeredMenuLayoutStyle = {
251
+ backgroundColor: theme.surface.base,
252
+ borderRadius: 8,
253
+ elevation: 8,
254
+ maxHeight: clampedMaxHeight,
255
+ maxWidth: Math.min(400, windowWidth - 48),
256
+ overflow: "hidden" as const,
257
+ shadowColor: "#000",
258
+ shadowOffset: {height: 4, width: 0},
259
+ shadowOpacity: 0.25,
260
+ shadowRadius: 12,
261
+ width: Math.min(400, windowWidth - 48),
262
+ };
263
+
264
+ const menuLayoutStyle = isCenteredPresentation
265
+ ? centeredMenuLayoutStyle
266
+ : anchoredMenuLayoutStyle;
267
+
268
+ const menuContent = (
269
+ <>
270
+ {searchable && (
271
+ <View
272
+ style={{
273
+ borderBottomColor: theme.border.default,
274
+ borderBottomWidth: 1,
275
+ paddingHorizontal: 8,
276
+ paddingVertical: 6,
277
+ }}
278
+ >
279
+ <TextInput
280
+ autoFocus
281
+ onChangeText={setSearchQuery}
282
+ placeholder="Search..."
283
+ placeholderTextColor={theme.text.secondaryLight}
284
+ ref={searchInputRef}
285
+ style={{
286
+ borderColor: theme.border.dark,
287
+ borderRadius: 4,
288
+ borderWidth: 1,
289
+ color: theme.text.primary,
290
+ fontSize: 14,
291
+ paddingHorizontal: 8,
292
+ paddingVertical: 4,
293
+ ...(Platform.OS === "web" ? {outline: "none"} : {}),
294
+ }}
295
+ testID={`${testIDPrefix}_search`}
296
+ value={searchQuery}
297
+ />
298
+ </View>
299
+ )}
300
+ <ScrollView
301
+ keyboardShouldPersistTaps="handled"
302
+ onLayout={(event) => {
303
+ listViewportHeightRef.current = event.nativeEvent.layout.height;
304
+ scheduleAfterPaint(() => {
305
+ scrollSelectedIntoView();
306
+ });
116
307
  }}
117
- testID={`${testIDPrefix}_menu`}
308
+ ref={listScrollRef}
118
309
  >
119
- <ScrollView>
120
- {options.map((item, idx) => {
121
- const isSelected =
122
- selectedIndex !== undefined ? idx === selectedIndex : item.value === selectedValue;
123
- return (
124
- <Pressable
125
- aria-role="button"
126
- key={item.key ?? idx}
127
- onPress={() => onSelect(item.value, idx)}
128
- style={(state: PressableWebState) => ({
129
- backgroundColor:
130
- isSelected || state.hovered || state.pressed
131
- ? theme.surface.neutralLight
132
- : theme.surface.base,
133
- paddingHorizontal: 12,
134
- paddingVertical: 10,
135
- })}
136
- testID={`${testIDPrefix}_option_${item.value}`}
137
- >
310
+ {filteredOptions.map((item, filteredIndex) => {
311
+ const originalIdx = options.indexOf(item);
312
+ const isSelected =
313
+ selectedIndex !== undefined
314
+ ? originalIdx === selectedIndex
315
+ : item.value === selectedValue;
316
+ return (
317
+ <Pressable
318
+ aria-role="button"
319
+ key={item.key ?? originalIdx}
320
+ onLayout={(event) => {
321
+ const {height, y} = event.nativeEvent.layout;
322
+ handleOptionLayout(filteredIndex, y, height);
323
+ }}
324
+ onPress={() => onSelect(item.value, originalIdx)}
325
+ style={(state: PressableWebState) => ({
326
+ backgroundColor:
327
+ isSelected || state.hovered || state.pressed
328
+ ? theme.surface.neutralLight
329
+ : theme.surface.base,
330
+ paddingHorizontal: 12,
331
+ paddingVertical: 10,
332
+ })}
333
+ testID={`${testIDPrefix}_option_${item.value}`}
334
+ >
335
+ <View style={{alignSelf: "stretch"}}>
138
336
  <Text
139
337
  style={{
140
338
  color: item.color ?? theme.text.primary,
@@ -144,11 +342,158 @@ export const WebDropdownMenu = ({
144
342
  >
145
343
  {item.label}
146
344
  </Text>
147
- </Pressable>
148
- );
149
- })}
150
- </ScrollView>
345
+ {Boolean(item.helperText) && (
346
+ <Text
347
+ style={{
348
+ color: theme.text.secondaryDark,
349
+ fontSize: 12,
350
+ fontWeight: "400",
351
+ lineHeight: 16,
352
+ marginTop: 2,
353
+ }}
354
+ >
355
+ {item.helperText}
356
+ </Text>
357
+ )}
358
+ </View>
359
+ </Pressable>
360
+ );
361
+ })}
362
+ {(searchable || showEmptyStateWhenNoOptions) && filteredOptions.length === 0 && (
363
+ <View style={{paddingHorizontal: 12, paddingVertical: 10}}>
364
+ <Text
365
+ style={{color: theme.text.secondaryLight, fontStyle: "italic"}}
366
+ testID={`${testIDPrefix}_no_results`}
367
+ >
368
+ No matching options
369
+ </Text>
370
+ </View>
371
+ )}
372
+ </ScrollView>
373
+ </>
374
+ );
375
+
376
+ // Close on Escape when using the portal overlay (Modal handles this itself).
377
+ useEffect(() => {
378
+ if (!visible || !keepTriggerFocus || Platform.OS !== "web" || typeof document === "undefined") {
379
+ return;
380
+ }
381
+ const closeOnEscape = (event: KeyboardEvent): void => {
382
+ if (event.key === "Escape") {
383
+ event.stopPropagation();
384
+ onClose();
385
+ }
386
+ };
387
+ document.addEventListener("keyup", closeOnEscape, false);
388
+ return () => {
389
+ if (typeof document !== "undefined") {
390
+ document.removeEventListener("keyup", closeOnEscape, false);
391
+ }
392
+ };
393
+ }, [keepTriggerFocus, onClose, visible]);
394
+
395
+ if (Platform.OS === "web" && keepTriggerFocus) {
396
+ if (!visible) {
397
+ return <View testID={`${testIDPrefix}_modal`} />;
398
+ }
399
+
400
+ const webFixedOverlayStyle = {
401
+ inset: 0,
402
+ position: "fixed",
403
+ zIndex: 9999,
404
+ } as unknown as ViewStyle;
405
+
406
+ const webFixedBackdropStyle = {
407
+ inset: 0,
408
+ position: "fixed",
409
+ zIndex: 1,
410
+ } as unknown as ViewStyle;
411
+
412
+ const webFixedMenuStyle = {
413
+ ...menuLayoutStyle,
414
+ position: "fixed",
415
+ } as unknown as ViewStyle;
416
+
417
+ const overlay = (
418
+ <View pointerEvents="box-none" style={webFixedOverlayStyle}>
419
+ <Pressable
420
+ aria-role="button"
421
+ onPress={onClose}
422
+ style={webFixedBackdropStyle}
423
+ testID={`${testIDPrefix}_backdrop`}
424
+ />
425
+ <View style={webFixedMenuStyle} testID={`${testIDPrefix}_menu`}>
426
+ {menuContent}
427
+ </View>
151
428
  </View>
429
+ );
430
+
431
+ const portalTarget =
432
+ typeof document !== "undefined" && document.body instanceof HTMLElement
433
+ ? document.body
434
+ : null;
435
+
436
+ if (portalTarget) {
437
+ return createWebPortal({children: overlay, container: portalTarget});
438
+ }
439
+
440
+ return overlay;
441
+ }
442
+
443
+ return (
444
+ <Modal
445
+ animationType="fade"
446
+ onRequestClose={onClose}
447
+ testID={`${testIDPrefix}_modal`}
448
+ transparent
449
+ visible={visible}
450
+ >
451
+ {isCenteredPresentation ? (
452
+ <View
453
+ style={{
454
+ alignItems: "center",
455
+ backgroundColor: "rgba(0, 0, 0, 0.45)",
456
+ flex: 1,
457
+ justifyContent: "center",
458
+ paddingHorizontal: 24,
459
+ }}
460
+ >
461
+ <Pressable
462
+ aria-role="button"
463
+ onPress={onClose}
464
+ style={{
465
+ bottom: 0,
466
+ left: 0,
467
+ position: "absolute",
468
+ right: 0,
469
+ top: 0,
470
+ }}
471
+ testID={`${testIDPrefix}_backdrop`}
472
+ />
473
+ <View style={menuLayoutStyle} testID={`${testIDPrefix}_menu`}>
474
+ {menuContent}
475
+ </View>
476
+ </View>
477
+ ) : (
478
+ <>
479
+ <Pressable
480
+ aria-role="button"
481
+ onPress={onClose}
482
+ style={{flex: 1, zIndex: 0}}
483
+ testID={`${testIDPrefix}_backdrop`}
484
+ />
485
+ <View
486
+ style={{
487
+ ...anchoredMenuLayoutStyle,
488
+ position: "absolute",
489
+ zIndex: 1,
490
+ }}
491
+ testID={`${testIDPrefix}_menu`}
492
+ >
493
+ {menuContent}
494
+ </View>
495
+ </>
496
+ )}
152
497
  </Modal>
153
498
  );
154
499
  };