1import React, {useState} from 'react'; 2import {StyleSheet} from 'react-native'; 3import rpx from '@/utils/rpx'; 4import {useNavigation} from '@react-navigation/native'; 5import MusicBar from '@/components/musicBar'; 6import SearchResult from './searchResult'; 7import StatusBar from '@/components/base/statusBar'; 8import {Appbar, Searchbar} from 'react-native-paper'; 9import useColors from '@/hooks/useColors'; 10import {SafeAreaView} from 'react-native-safe-area-context'; 11import {fontSizeConst} from '@/constants/uiConst'; 12import {useParams} from '@/entry/router'; 13 14function filterMusic(query: string, musicList: IMusic.IMusicItem[]) { 15 if (query?.length === 0) { 16 return musicList; 17 } 18 return musicList.filter(_ => 19 `${_.title} ${_.artist} ${_.album} ${_.platform}`.includes(query), 20 ); 21} 22 23export default function SearchMusicList() { 24 const {musicList} = useParams<'search-music-list'>(); 25 const navigation = useNavigation(); 26 const [result, setResult] = useState<IMusic.IMusicItem[]>(musicList ?? []); 27 const [query, setQuery] = useState(''); 28 29 const colors = useColors(); 30 31 const onChangeSearch = (_: string) => { 32 setQuery(_); 33 setResult(filterMusic(_.trim(), musicList ?? [])); 34 }; 35 36 return ( 37 <SafeAreaView style={style.wrapper}> 38 <StatusBar /> 39 <Appbar.Header 40 style={[style.appbar, {backgroundColor: colors.primary}]}> 41 <Appbar.BackAction 42 onPress={() => { 43 navigation.goBack(); 44 }} 45 /> 46 <Searchbar 47 style={style.searchBar} 48 inputStyle={style.input} 49 placeholder="在列表中搜索歌曲" 50 value={query} 51 onChangeText={onChangeSearch} 52 /> 53 </Appbar.Header> 54 <SearchResult result={result} /> 55 <MusicBar /> 56 </SafeAreaView> 57 ); 58} 59 60const style = StyleSheet.create({ 61 wrapper: { 62 width: rpx(750), 63 flex: 1, 64 }, 65 appbar: { 66 shadowColor: 'transparent', 67 backgroundColor: '#2b333eaa', 68 }, 69 searchBar: { 70 minWidth: rpx(375), 71 flex: 1, 72 borderRadius: rpx(64), 73 height: rpx(64), 74 color: '#666666', 75 }, 76 input: { 77 padding: 0, 78 color: '#666666', 79 height: rpx(64), 80 fontSize: fontSizeConst.subTitle, 81 textAlignVertical: 'center', 82 includeFontPadding: false, 83 }, 84}); 85