Implement searching

This commit is contained in:
Mosh Hamedani 2023-03-03 10:55:02 -08:00
parent 7c572a4560
commit f002e86214
5 changed files with 30 additions and 9 deletions

View file

@ -13,6 +13,7 @@ export interface GameQuery {
genre: Genre | null; genre: Genre | null;
platform: Platform | null; platform: Platform | null;
sortOrder: string; sortOrder: string;
searchText: string;
} }
function App() { function App() {
@ -30,7 +31,7 @@ function App() {
}} }}
> >
<GridItem area="nav"> <GridItem area="nav">
<NavBar /> <NavBar onSearch={(searchText) => setGameQuery({ ...gameQuery, searchText })} />
</GridItem> </GridItem>
<Show above="lg"> <Show above="lg">
<GridItem area="aside" paddingX={5}> <GridItem area="aside" paddingX={5}>

View file

@ -3,11 +3,15 @@ import logo from '../assets/logo.webp';
import ColorModeSwitch from './ColorModeSwitch'; import ColorModeSwitch from './ColorModeSwitch';
import SearchInput from './SearchInput'; import SearchInput from './SearchInput';
const NavBar = () => { interface Props {
onSearch: (searchText: string) => void;
}
const NavBar = ({ onSearch }: Props) => {
return ( return (
<HStack padding='10px'> <HStack padding='10px'>
<Image src={logo} boxSize='60px' /> <Image src={logo} boxSize='60px' />
<SearchInput /> <SearchInput onSearch={onSearch} />
<ColorModeSwitch /> <ColorModeSwitch />
</HStack> </HStack>
) )

View file

@ -1,12 +1,24 @@
import { Input, InputGroup, InputLeftElement } from "@chakra-ui/react"; import { Input, InputGroup, InputLeftElement } from "@chakra-ui/react";
import { useRef } from "react";
import { BsSearch } from "react-icons/bs"; import { BsSearch } from "react-icons/bs";
const SearchInput = () => { interface Props {
onSearch: (searchText: string) => void;
}
const SearchInput = ({ onSearch }: Props) => {
const ref = useRef<HTMLInputElement>(null);
return ( return (
<InputGroup> <form onSubmit={(event) => {
<InputLeftElement children={<BsSearch />} /> event.preventDefault();
<Input borderRadius={20} placeholder="Search games..." variant="filled" /> if (ref.current) onSearch(ref.current.value);
</InputGroup> }}>
<InputGroup>
<InputLeftElement children={<BsSearch />} />
<Input ref={ref} borderRadius={20} placeholder="Search games..." variant="filled" />
</InputGroup>
</form>
); );
}; };

View file

@ -23,7 +23,8 @@ const useGames = (gameQuery: GameQuery) =>
params: { params: {
genres: gameQuery.genre?.id, genres: gameQuery.genre?.id,
platforms: gameQuery.platform?.id, platforms: gameQuery.platform?.id,
ordering: gameQuery.sortOrder ordering: gameQuery.sortOrder,
search: gameQuery.searchText
}, },
}, },
[gameQuery] [gameQuery]

View file

@ -0,0 +1,3 @@
form {
width: 100%;
}