import { Input, Column, Row, Text, Pressable, HStack, VStack, Image, Icon, Square, } from "native-base"; import React from "react"; import { Ionicons } from "@expo/vector-icons"; export enum SuggestionType { TEXT, ILLUSTRATED, } export type SuggestionList = { type: SuggestionType; data: SuggestionProps | IllustratedSuggestionProps; }[]; export interface SearchBarProps { onTextChange: (text: string) => void; onTextSubmit: (text: string) => void; suggestions: SuggestionList; } export interface IllustratedSuggestionProps { text: string; subtext: string; imageSrc: string; onPress: () => void; } export interface SuggestionProps { text: string; onPress: () => void; } // debounce function const debounce = (func: any, delay: number) => { let inDebounce: any; return function (this: any) { const context = this; const args = arguments; clearTimeout(inDebounce); inDebounce = setTimeout(() => func.apply(context, args), delay); }; }; const IllustratedSuggestion = ({ text, subtext, imageSrc, onPress, }: IllustratedSuggestionProps) => { return ( Alternate Text {text} {subtext} ); }; const TextSuggestion = ({ text, onPress }: SuggestionProps) => { return ( {text} ); }; // render the suggestions based on the type const SuggestionRenderer = (suggestions: SuggestionList) => { const suggestionRenderers = { [SuggestionType.TEXT]: TextSuggestion, [SuggestionType.ILLUSTRATED]: IllustratedSuggestion, }; return suggestions.map((suggestion, index) => { const SuggestionComponent = suggestionRenderers[suggestion.type]; return ; }); }; const SearchBar = ({ onTextChange, onTextSubmit, suggestions, }: SearchBarProps) => { const debouncedOnTextChange = React.useRef( debounce((t: string) => onTextChange(t), 70) ).current; return ( <> onTextSubmit(event.nativeEvent.text)} /> {SuggestionRenderer(suggestions)} ); }; export default SearchBar;