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

PullRequestSearchSheet.tsx

99 lines3.6 KB

1import { useNavigation } from "@react-navigation/native";
2import { useRef, type ReactNode } from "react";
3import { Platform, View, type TextInputInstance } from "react-native";
4​
5import { AndroidScreenHeader } from "../../components/ui/AndroidScreenHeader";
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 <AndroidScreenHeader
41 title={props.title}
42 hideBottomBorder
43 onBack={() => navigation.goBack()}
44 />
45 <View className="bg-header px-4 pb-3 pt-1">
46 <SearchField
47 inputRef={searchRef}
48 accessibilityLabel={props.placeholder}
49 clearAccessibilityLabel={props.clearAccessibilityLabel}
50 onChangeText={props.onQueryChange}
51 placeholder={props.placeholder}
52 value={props.query}
53 />
54 </View>
55 {list}
56 </View>
57 );
58 }
59 return (
60 <View className="flex-1 bg-sheet">
61 <SheetHeader
62 title={props.title}
63 onClose={() => navigation.goBack()}
64 accessibilityLabel={props.closeAccessibilityLabel}
65 />
66 <NativeStackScreenOptions
67 options={{
68 unstable_headerToolbarItems: integratedSearch
69 ? () => [
70 createNativeMailSearchToolbarItem({
71 placeholder: props.placeholder,
72 onSearchTextChange: props.onQueryChange,
73 showsSearchDismissButton: true,
74 searchTextChangeId: props.searchTextChangeId,
75 }),
76 ]
77 : undefined,
78 headerSearchBarOptions: integratedSearch
79 ? undefined
80 : {
81 placeholder: props.placeholder,
82 allowToolbarIntegration: true,
83 autoCapitalize: "none",
84 hideNavigationBar: false,
85 obscureBackground: false,
86 onChangeText: (event) => props.onQueryChange(event.nativeEvent.text),
87 onCancelButtonPress: () => props.onQueryChange(""),
88 },
89 }}
90 />
91 {integratedSearch ? null : (
92 <Toolbar placement="bottom">
93 <Toolbar.SearchBarSlot />
94 </Toolbar>
95 )}
96 {list}
97 </View>
98 );