1*bf6e62f2S猫头猫import React, {useEffect, useState} from 'react'; 2*bf6e62f2S猫头猫import { 3*bf6e62f2S猫头猫 Keyboard, 4*bf6e62f2S猫头猫 StatusBar, 5*bf6e62f2S猫头猫 StyleSheet, 6*bf6e62f2S猫头猫 Text, 7*bf6e62f2S猫头猫 TextInput, 8*bf6e62f2S猫头猫 ToastAndroid, 9*bf6e62f2S猫头猫 View, 10*bf6e62f2S猫头猫} from 'react-native'; 11*bf6e62f2S猫头猫import rpx from '@/utils/rpx'; 12*bf6e62f2S猫头猫import {useNavigation, useTheme} from '@react-navigation/native'; 13*bf6e62f2S猫头猫import {useAtom, useSetAtom} from 'jotai'; 14*bf6e62f2S猫头猫import {pageStatusAtom, PageStatus, queryAtom} from '../store/atoms'; 15*bf6e62f2S猫头猫import useSearch from '../hooks/useSearch'; 16*bf6e62f2S猫头猫import {Appbar, Button, Searchbar} from 'react-native-paper'; 17*bf6e62f2S猫头猫import getStatusBarHeight from '@/utils/getStatusBarHeight'; 18*bf6e62f2S猫头猫import { addHistory } from '../common/historySearch'; 19*bf6e62f2S猫头猫import { fontSizeConst } from '@/constants/uiConst'; 20*bf6e62f2S猫头猫import useTextColor from '@/hooks/useTextColor'; 21*bf6e62f2S猫头猫 22*bf6e62f2S猫头猫interface INavBarProps {} 23*bf6e62f2S猫头猫export default function NavBar(props: INavBarProps) { 24*bf6e62f2S猫头猫 const navigation = useNavigation(); 25*bf6e62f2S猫头猫 const search = useSearch(); 26*bf6e62f2S猫头猫 const [query, setQuery] = useAtom(queryAtom); 27*bf6e62f2S猫头猫 const setPageStatus = useSetAtom(pageStatusAtom); 28*bf6e62f2S猫头猫 const textColor = useTextColor(); 29*bf6e62f2S猫头猫 const {colors} = useTheme(); 30*bf6e62f2S猫头猫 31*bf6e62f2S猫头猫 const onSearchSubmit = async () => { 32*bf6e62f2S猫头猫 if (query === '') { 33*bf6e62f2S猫头猫 return; 34*bf6e62f2S猫头猫 } 35*bf6e62f2S猫头猫 setPageStatus(prev => 36*bf6e62f2S猫头猫 prev === PageStatus.EDITING ? PageStatus.SEARCHING : prev, 37*bf6e62f2S猫头猫 ); 38*bf6e62f2S猫头猫 await search(query, 'all'); 39*bf6e62f2S猫头猫 await addHistory(query); 40*bf6e62f2S猫头猫 }; 41*bf6e62f2S猫头猫 42*bf6e62f2S猫头猫 return ( 43*bf6e62f2S猫头猫 <View style={[style.wrapper, {backgroundColor: colors.primary}]}> 44*bf6e62f2S猫头猫 <Appbar style={[style.appbar, {backgroundColor: colors.primary}]}> 45*bf6e62f2S猫头猫 <Appbar.BackAction 46*bf6e62f2S猫头猫 onPress={() => { 47*bf6e62f2S猫头猫 // !!这个组件有bug,输入法拉起的时候返回会默认打开panel 48*bf6e62f2S猫头猫 Keyboard.dismiss(); 49*bf6e62f2S猫头猫 navigation.goBack(); 50*bf6e62f2S猫头猫 }}></Appbar.BackAction> 51*bf6e62f2S猫头猫 <Searchbar 52*bf6e62f2S猫头猫 autoFocus 53*bf6e62f2S猫头猫 inputStyle={style.input} 54*bf6e62f2S猫头猫 style={style.searchBar} 55*bf6e62f2S猫头猫 onFocus={() => { 56*bf6e62f2S猫头猫 setPageStatus(PageStatus.EDITING); 57*bf6e62f2S猫头猫 }} 58*bf6e62f2S猫头猫 placeholder="输入要搜索的歌曲" 59*bf6e62f2S猫头猫 onSubmitEditing={onSearchSubmit} 60*bf6e62f2S猫头猫 onChangeText={_ => setQuery(_)} 61*bf6e62f2S猫头猫 value={query}></Searchbar> 62*bf6e62f2S猫头猫 <Button color={textColor} onPress={onSearchSubmit}> 63*bf6e62f2S猫头猫 搜索 64*bf6e62f2S猫头猫 </Button> 65*bf6e62f2S猫头猫 </Appbar> 66*bf6e62f2S猫头猫 </View> 67*bf6e62f2S猫头猫 ); 68*bf6e62f2S猫头猫} 69*bf6e62f2S猫头猫 70*bf6e62f2S猫头猫const style = StyleSheet.create({ 71*bf6e62f2S猫头猫 wrapper: { 72*bf6e62f2S猫头猫 paddingTop: getStatusBarHeight(), 73*bf6e62f2S猫头猫 }, 74*bf6e62f2S猫头猫 appbar: { 75*bf6e62f2S猫头猫 shadowColor: 'transparent', 76*bf6e62f2S猫头猫 }, 77*bf6e62f2S猫头猫 searchBar: { 78*bf6e62f2S猫头猫 minWidth: rpx(375), 79*bf6e62f2S猫头猫 flex: 1, 80*bf6e62f2S猫头猫 borderRadius: rpx(64), 81*bf6e62f2S猫头猫 height: rpx(64), 82*bf6e62f2S猫头猫 color: '#666666' 83*bf6e62f2S猫头猫 }, 84*bf6e62f2S猫头猫 input: { 85*bf6e62f2S猫头猫 padding: 0, 86*bf6e62f2S猫头猫 color: '#666666', 87*bf6e62f2S猫头猫 height: rpx(64), 88*bf6e62f2S猫头猫 fontSize: fontSizeConst.small, 89*bf6e62f2S猫头猫 textAlignVertical: 'center', 90*bf6e62f2S猫头猫 includeFontPadding: false, 91*bf6e62f2S猫头猫 } 92*bf6e62f2S猫头猫}); 93