mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 18:13:25 +00:00
feat: always focus input when search tab is pressed
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { useRef } from "react";
|
||||||
import { Platform, View } from "react-native";
|
import { Platform, View } from "react-native";
|
||||||
import { Tabs } from "expo-router";
|
import { Tabs } from "expo-router";
|
||||||
|
|
||||||
@@ -6,13 +7,27 @@ import Colors from "@movie-web/tailwind-config/colors";
|
|||||||
import { MovieWebSvg } from "~/components/Icon";
|
import { MovieWebSvg } from "~/components/Icon";
|
||||||
import SvgTabBarIcon from "~/components/SvgTabBarIcon";
|
import SvgTabBarIcon from "~/components/SvgTabBarIcon";
|
||||||
import TabBarIcon from "~/components/TabBarIcon";
|
import TabBarIcon from "~/components/TabBarIcon";
|
||||||
|
import SearchTabContext from "./search/SearchTabContext";
|
||||||
|
|
||||||
export default function TabLayout() {
|
export default function TabLayout() {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||||
|
const focusSearchInputRef = useRef(() => {});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<SearchTabContext.Provider value={{ focusSearchInputRef }}>
|
||||||
<Tabs
|
<Tabs
|
||||||
sceneContainerStyle={{
|
sceneContainerStyle={{
|
||||||
backgroundColor: Colors.background,
|
backgroundColor: Colors.background,
|
||||||
}}
|
}}
|
||||||
|
screenListeners={({ route }) => ({
|
||||||
|
tabPress: () => {
|
||||||
|
switch (route.name) {
|
||||||
|
case "search":
|
||||||
|
focusSearchInputRef.current();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})}
|
||||||
screenOptions={{
|
screenOptions={{
|
||||||
headerShown: false,
|
headerShown: false,
|
||||||
tabBarActiveTintColor: Colors.primary[100],
|
tabBarActiveTintColor: Colors.primary[100],
|
||||||
@@ -88,5 +103,6 @@ export default function TabLayout() {
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Tabs>
|
</Tabs>
|
||||||
|
</SearchTabContext.Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
8
apps/expo/src/app/(tabs)/search/SearchTabContext.tsx
Normal file
8
apps/expo/src/app/(tabs)/search/SearchTabContext.tsx
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
const SearchTabContext = React.createContext({
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||||
|
focusSearchInputRef: { current: () => {} },
|
||||||
|
});
|
||||||
|
|
||||||
|
export default SearchTabContext;
|
@@ -1,10 +1,12 @@
|
|||||||
import { useCallback, useRef, useState } from "react";
|
import { useCallback, useContext, useEffect, useRef, useState } from "react";
|
||||||
import { TextInput, View } from "react-native";
|
import { TextInput, View } from "react-native";
|
||||||
import { useFocusEffect } from "expo-router";
|
import { useFocusEffect } from "expo-router";
|
||||||
import { FontAwesome5 } from "@expo/vector-icons";
|
import { FontAwesome5 } from "@expo/vector-icons";
|
||||||
|
|
||||||
import Colors from "@movie-web/tailwind-config/colors";
|
import Colors from "@movie-web/tailwind-config/colors";
|
||||||
|
|
||||||
|
import SearchTabContext from "./SearchTabContext";
|
||||||
|
|
||||||
export default function Searchbar({
|
export default function Searchbar({
|
||||||
onSearchChange,
|
onSearchChange,
|
||||||
}: {
|
}: {
|
||||||
@@ -13,6 +15,14 @@ export default function Searchbar({
|
|||||||
const [keyword, setKeyword] = useState("");
|
const [keyword, setKeyword] = useState("");
|
||||||
const inputRef = useRef<TextInput>(null);
|
const inputRef = useRef<TextInput>(null);
|
||||||
|
|
||||||
|
const { focusSearchInputRef } = useContext(SearchTabContext);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
focusSearchInputRef.current = () => {
|
||||||
|
inputRef.current?.focus();
|
||||||
|
};
|
||||||
|
}, [focusSearchInputRef]);
|
||||||
|
|
||||||
useFocusEffect(
|
useFocusEffect(
|
||||||
useCallback(() => {
|
useCallback(() => {
|
||||||
// When the screen is focused
|
// When the screen is focused
|
||||||
|
Reference in New Issue
Block a user