Skip to content
TypeScript

refactor(mobile): share Android toolbar search fields (#12359)

Julius Marminge14337401bc80
diff --git a/apps/mobile/src/components/MaterialSearchField.tsx b/apps/mobile/src/components/MaterialSearchField.tsx
new file mode 100644
index 00000000000..5ed3945039d
--- /dev/null
+++ b/apps/mobile/src/components/MaterialSearchField.tsx
@@ -0,0 +1,59 @@
+import type { RefObject } from "react";
+import { Pressable, TextInput, View } from "react-native";
+
+import { SymbolView } from "./AppSymbol";
+
+export function MaterialSearchField({
+ inputRef,
+ accessibilityLabel,
+ clearAccessibilityLabel,
+ placeholder,
+ value,
+ onChangeText,
+}: {
+ readonly inputRef: RefObject<TextInput | null>;
+ readonly accessibilityLabel: string;
+ readonly clearAccessibilityLabel: string;
+ readonly placeholder: string;
+ readonly value: string;
+ readonly onChangeText: (value: string) => void;
+}) {
+ return (
+ <View className="h-12 min-w-0 flex-1 flex-row items-center gap-2 rounded-full border border-input-border bg-input px-3">
+ <SymbolView name="magnifyingglass" size={18} tintColorClassName="accent-foreground-muted" />
+ <TextInput
+ ref={inputRef}
+ accessibilityLabel={accessibilityLabel}
+ autoFocus
+ autoCapitalize="none"
+ autoCorrect={false}
+ returnKeyType="search"
+ placeholder={placeholder}
+ placeholderTextColorClassName="accent-placeholder"
+ selectionColorClassName="accent-primary/32"
+ cursorColorClassName="accent-primary"
+ selectionHandleColorClassName="accent-primary"
+ className="min-w-0 flex-1 py-2 font-sans text-base text-foreground"
+ value={value}
+ onChangeText={onChangeText}
+ />
+ {value.length > 0 ? (
+ <Pressable
+ accessibilityLabel={clearAccessibilityLabel}
+ accessibilityRole="button"
+ hitSlop={10}
+ onPress={() => {
+ onChangeText("");
+ inputRef.current?.focus();
+ }}
+ >
+ <SymbolView
+ name="xmark.circle.fill"
+ size={18}
+ tintColorClassName="accent-foreground-muted"
+ />
+ </Pressable>
+ ) : null}
+ </View>
+ );
+}
diff --git a/apps/mobile/src/features/files/MaterialFilesHeader.tsx b/apps/mobile/src/features/files/MaterialFilesHeader.tsx
index 2ce953569b6..8936ae9b37e 100644
--- a/apps/mobile/src/features/files/MaterialFilesHeader.tsx
+++ b/apps/mobile/src/features/files/MaterialFilesHeader.tsx
@@ -1,9 +1,9 @@
import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";
-import { BackHandler, Keyboard, Pressable, TextInput, View } from "react-native";
+import { BackHandler, Keyboard, type TextInput, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { AndroidHeaderIconButton, AndroidScreenHeader } from "../../components/AndroidScreenHeader";
-import { SymbolView } from "../../components/AppSymbol";
+import { MaterialSearchField } from "../../components/MaterialSearchField";
/** Keep Files search in the same header row on compact and expanded layouts. */
export function MaterialFilesHeader(props: {
@@ -74,46 +74,14 @@ export function MaterialFilesHeader(props: {
icon="arrow.left"
onPress={closeSearch}
/>
- <View className="h-12 min-w-0 flex-1 flex-row items-center gap-2 rounded-full border border-input-border bg-input px-3">
- <SymbolView
- name="magnifyingglass"
- size={18}
- tintColorClassName="accent-foreground-muted"
- />
- <TextInput
- ref={searchRef}
- accessibilityLabel="Search files"
- autoFocus
- autoCapitalize="none"
- autoCorrect={false}
- returnKeyType="search"
- placeholder="Search files"
- placeholderTextColorClassName="accent-placeholder"
- selectionColorClassName="accent-primary/32"
- cursorColorClassName="accent-primary"
- selectionHandleColorClassName="accent-primary"
- className="min-w-0 flex-1 py-2 font-sans text-base text-foreground"
- value={props.searchQuery}
- onChangeText={onSearchQueryChange}
- />
- {props.searchQuery.length > 0 ? (
- <Pressable
- accessibilityLabel="Clear file search"
- accessibilityRole="button"
- hitSlop={10}
- onPress={() => {
- onSearchQueryChange("");
- searchRef.current?.focus();
- }}
- >
- <SymbolView
- name="xmark.circle.fill"
- size={18}
- tintColorClassName="accent-foreground-muted"
- />
- </Pressable>
- ) : null}
- </View>
+ <MaterialSearchField
+ inputRef={searchRef}
+ accessibilityLabel="Search files"
+ clearAccessibilityLabel="Clear file search"
+ placeholder="Search files"
+ value={props.searchQuery}
+ onChangeText={onSearchQueryChange}
+ />
</View>
</View>
) : null}
diff --git a/apps/mobile/src/features/home/MaterialThreadListToolbar.tsx b/apps/mobile/src/features/home/MaterialThreadListToolbar.tsx
index 397e6984f3a..72671bc046d 100644
--- a/apps/mobile/src/features/home/MaterialThreadListToolbar.tsx
+++ b/apps/mobile/src/features/home/MaterialThreadListToolbar.tsx
@@ -1,12 +1,5 @@
import { useCallback, useEffect, useRef, useState, type ComponentProps } from "react";
-import {
- BackHandler,
- Keyboard,
- Pressable,
- TextInput,
- View,
- type LayoutChangeEvent,
-} from "react-native";
+import { BackHandler, Keyboard, type TextInput, View, type LayoutChangeEvent } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import type { MenuAction } from "@react-native-menu/menu";
@@ -15,7 +8,7 @@ import { CompactBrandTitle } from "../../components/CompactBrandTitle";
import { MaterialFloatingActionButton } from "../../components/MaterialFloatingActionButton";
import { AndroidAnchoredMenu } from "../../components/AndroidAnchoredMenu";
import { ControlPillMenu } from "../../components/ControlPill";
-import { SymbolView } from "../../components/AppSymbol";
+import { MaterialSearchField } from "../../components/MaterialSearchField";
import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
import { WorkspaceConnectionTitle } from "./WorkspaceConnectionTitle";
import { useWorkspaceState } from "../../state/workspace";
@@ -68,42 +61,14 @@ export function MaterialThreadListToolbar(props: {
? "line.3.horizontal.decrease.circle.fill"
: "line.3.horizontal.decrease.circle";
const searchField = (
- <View className="h-12 min-w-0 flex-1 flex-row items-center gap-2 rounded-full border border-input-border bg-input px-3">
- <SymbolView name="magnifyingglass" size={18} tintColorClassName="accent-foreground-muted" />
- <TextInput
- ref={searchRef}
- accessibilityLabel="Search threads"
- autoFocus={true}
- autoCapitalize="none"
- autoCorrect={false}
- returnKeyType="search"
- placeholder="Search"
- placeholderTextColorClassName="accent-placeholder"
- selectionColorClassName="accent-primary/32"
- cursorColorClassName="accent-primary"
- selectionHandleColorClassName="accent-primary"
- className="min-w-0 flex-1 py-2 font-sans text-base text-foreground"
- value={props.searchQuery}
- onChangeText={onSearchQueryChange}
- />
- {props.searchQuery.length > 0 ? (
- <Pressable
- accessibilityLabel="Clear search"
- accessibilityRole="button"
- hitSlop={10}
- onPress={() => {
- props.onSearchQueryChange("");
- searchRef.current?.focus();
- }}
- >
- <SymbolView
- name="xmark.circle.fill"
- size={18}
- tintColorClassName="accent-foreground-muted"
- />
- </Pressable>
- ) : null}
- </View>
+ <MaterialSearchField
+ inputRef={searchRef}
+ accessibilityLabel="Search threads"
+ clearAccessibilityLabel="Clear search"
+ placeholder="Search"
+ value={props.searchQuery}
+ onChangeText={onSearchQueryChange}
+ />
);
return (