Skip to content

Commit

Permalink
Video-46-Create-Search-Box
Browse files Browse the repository at this point in the history
  • Loading branch information
basir committed Jul 25, 2021
1 parent 3a8b9f2 commit 89f1ced
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,9 @@ $ Open http://localhost:3000
1. add drawer
2. list categories
3. redirect to search screen
46. Create Search Box
1. add form
2. handle form submit
3. redirect to search screen


31 changes: 30 additions & 1 deletion components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import {
ListItem,
Divider,
ListItemText,
InputBase,
} from '@material-ui/core';
import MenuIcon from '@material-ui/icons/Menu';
import CancelIcon from '@material-ui/icons/Cancel';
import SearchIcon from '@material-ui/icons/Search';
import useStyles from '../utils/styles';
import { Store } from '../utils/Store';
import { getError } from '../utils/error';
Expand Down Expand Up @@ -83,6 +85,16 @@ export default function Layout({ title, description, children }) {
enqueueSnackbar(getError(err), { variant: 'error' });
}
};

const [query, setQuery] = useState('');
const queryChangeHandler = (e) => {
setQuery(e.target.value);
};
const submitHandler = (e) => {
e.preventDefault();
router.push(`/search?query=${query}`);
};

useEffect(() => {
fetchCategories();
}, []);
Expand Down Expand Up @@ -124,6 +136,7 @@ export default function Layout({ title, description, children }) {
edge="start"
aria-label="open drawer"
onClick={sidebarOpenHandler}
className={classes.menuButton}
>
<MenuIcon className={classes.navbarButton} />
</IconButton>
Expand Down Expand Up @@ -173,7 +186,23 @@ export default function Layout({ title, description, children }) {
</List>
</Drawer>

<div className={classes.grow}></div>
<div className={classes.searchSection}>
<form onSubmit={submitHandler} className={classes.searchForm}>
<InputBase
name="query"
className={classes.searchInput}
placeholder="Search products"
onChange={queryChangeHandler}
/>
<IconButton
type="submit"
className={classes.iconButton}
aria-label="search"
>
<SearchIcon />
</IconButton>
</form>
</div>
<div>
<Switch
checked={darkMode}
Expand Down
32 changes: 30 additions & 2 deletions utils/styles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { makeStyles } from '@material-ui/core';

const useStyles = makeStyles({
const useStyles = makeStyles((theme) => ({
navbar: {
backgroundColor: '#203040',
'& a': {
Expand Down Expand Up @@ -56,5 +56,33 @@ const useStyles = makeStyles({
toolbar: {
justifyContent: 'space-between',
},
});
menuButton: { padding: 0 },
// search
searchSection: {
display: 'none',
[theme.breakpoints.up('md')]: {
display: 'flex',
},
},
searchForm: {
border: '1px solid #ffffff',
backgroundColor: '#ffffff',
borderRadius: 5,
},
searchInput: {
paddingLeft: 5,
color: '#000000',
'& ::placeholder': {
color: '#606060',
},
},
iconButton: {
backgroundColor: '#f8c040',
padding: 5,
borderRadius: '0 5px 5px 0',
'& span': {
color: '#000000',
},
},
}));
export default useStyles;

0 comments on commit 89f1ced

Please sign in to comment.