From fd73fb427f69c3ea4cc2a3e86447a3f060cb74ed Mon Sep 17 00:00:00 2001 From: Jarrian Date: Mon, 19 Dec 2022 14:13:23 +0800 Subject: [PATCH] ADD: Search | Search RegEx [FIX] --- screens/Search.js | 96 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 26 deletions(-) diff --git a/screens/Search.js b/screens/Search.js index e49ae57..6f1ad3b 100644 --- a/screens/Search.js +++ b/screens/Search.js @@ -18,8 +18,8 @@ function compareStrings(a, b) { return (a < b) ? -1 : (a > b) ? 1 : 0; } -function findMatches(wordToMatch, PinoyFoods) { - return PinoyFoods.filter(food => { +function findMatches(wordToMatch, foods) { + return foods.filter(food => { const regex = new RegExp(wordToMatch, 'gi'); return food.name.match(regex) || food.tagalog.match(regex) || matchType(wordToMatch, food.type); }); @@ -38,8 +38,8 @@ export default function Search({ navigation, route }) { const [foods, setFoods] = useState(PinoyFoods); const [searchText, setSearchText] = useState(null); - const onChange = (text) => { - let word = text.replace(/[^a-zA-Z\s\-]/g, ''); + const onChange = (string) => { + let word = string.replace(/[^a-zA-Z -]/g, ''); setSearchText(word); setFoods(findMatches(word, PinoyFoods)); }; @@ -58,6 +58,7 @@ export default function Search({ navigation, route }) { /> - ( - - )} - ListFooterComponent={() => ( - - )} - renderItem={({ item }) => ( - - )} - /> + { foods.length >= 1 ? ( + ( + + )} + ListFooterComponent={() => ( + + )} + renderItem={({ item }) => ( + + )} + /> + ) : ( + + 240 ? 240 : window.width/3 } + color="#bbb" + /> + + No Search Results! + + Please search a food name or category! + + + + )} ); } @@ -113,5 +131,31 @@ const styles = StyleSheet.create({ fontSize: 16, flex: 1, color: '#444', - } + }, + emptyContainer: { + padding: 32, + height: window.height/3 + 16, + maxHeight: 480, + width: '100%', + alignItems: 'center', + justifyContent: 'center', + }, + emptyLabelContainer: { + marginVertical: 4, + alignItems: 'center', + justifyContent: 'center', + }, + emptyLabel: { + fontSize: window.width/20 > 32 ? 32 : window.width/20, + fontWeight: 'bold', + color: '#888' + }, + emptyLabelDetails: { + textAlign: 'center', + marginVertical: 8, + color: '#aaa', + fontSize: window.width/32 > 24 ? 24 : window.width/32, + width: window.width/1.5, + maxWidth: 480, + }, }); \ No newline at end of file