Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestSearchSheet.tsx

95 lines3.5 KB

1import { useNavigation } from "@react-navigation/native";
2import { useRef, type ReactNode } from "react";
3import { Platform, View, type TextInputInstance } from "react-native";
4​
5import { AppBar } from "../../components/ui/AppBar";
6import { SearchField } from "../../components/ui/SearchField";
7import { SheetHeader } from "../../components/ui/SheetHeader";
8import { Toolbar } from "../../components/ui/Toolbar";
9import { NativeStackScreenOptions } from "../../native/StackHeader";
10import { useNativeMailSearchToolbar } from "../../native/use-native-mail-search-toolbar";
11import {
12 createNativeMailSearchToolbarItem,
13 NATIVE_MAIL_SEARCH_TOOLBAR_CONTENT_INSET,
14} from "../layout/native-mail-search-toolbar";
15​
16/**
17 * A searchable picker over a pull request list or a pull request: a pushed card with a search
18 * field on Android, a sheet with the native search toolbar on iOS.
19 */
20export function PullRequestSearchSheet(props: {
21 readonly title: string;
22 readonly placeholder: string;
23 readonly closeAccessibilityLabel: string;
24 readonly clearAccessibilityLabel: string;
25 readonly searchTextChangeId: string;
26 readonly query: string;
27 readonly onQueryChange: (query: string) => void;
28 /** Renders the list, given the bottom padding that keeps its end clear of the search bar. */
29 readonly children: (list: { readonly paddingBottom: number }) => ReactNode;
30}) {
31 const navigation = useNavigation();
32 const integratedSearch = useNativeMailSearchToolbar();
33 const searchRef = useRef<TextInputInstance>(null);
34 const list = props.children({
35 paddingBottom: integratedSearch ? NATIVE_MAIL_SEARCH_TOOLBAR_CONTENT_INSET + 16 : 16,
36 });
37 if (Platform.OS === "android") {
38 return (
39 <View className="flex-1 bg-sheet" collapsable={false}>
40 <AppBar embedded title={props.title} hideBottomBorder onBack={() => navigation.goBack()} />
41 <View className="bg-header px-4 pb-3 pt-1">
42 <SearchField
43 inputRef={searchRef}
44 accessibilityLabel={props.placeholder}
45 clearAccessibilityLabel={props.clearAccessibilityLabel}
46 onChangeText={props.onQueryChange}
47 placeholder={props.placeholder}
48 value={props.query}
49 />
50 </View>
51 {list}
52 </View>
53 );
54 }
55 return (
56 <View className="flex-1 bg-sheet">
57 <SheetHeader
58 title={props.title}
59 onClose={() => navigation.goBack()}
60 accessibilityLabel={props.closeAccessibilityLabel}
61 />
62 <NativeStackScreenOptions
63 options={{
64 unstable_headerToolbarItems: integratedSearch
65 ? () => [
66 createNativeMailSearchToolbarItem({
67 placeholder: props.placeholder,
68 onSearchTextChange: props.onQueryChange,
69 showsSearchDismissButton: true,
70 searchTextChangeId: props.searchTextChangeId,
71 }),
72 ]
73 : undefined,
74 headerSearchBarOptions: integratedSearch
75 ? undefined
76 : {
77 placeholder: props.placeholder,
78 allowToolbarIntegration: true,
79 autoCapitalize: "none",
80 hideNavigationBar: false,
81 obscureBackground: false,
82 onChangeText: (event) => props.onQueryChange(event.nativeEvent.text),
83 onCancelButtonPress: () => props.onQueryChange(""),
84 },
85 }}
86 />
87 {integratedSearch ? null : (
88 <Toolbar placement="bottom">
89 <Toolbar.SearchBarSlot />
90 </Toolbar>
91 )}
92 {list}
93 </View>
94 );