diff options
author | eug-vs <eug-vs@keemail.me> | 2020-06-28 04:12:01 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-06-28 04:12:01 +0300 |
commit | ea9920d9ed0e7b3a3e565623e434e75bd3362f02 (patch) | |
tree | 3fdca2db655c80907cdef7ac3fdfabed6f78a5ec /src/components/Header | |
parent | b02ebbcb75faceb140d484c4a3a6779040ba0ca7 (diff) | |
download | which-ui-ea9920d9ed0e7b3a3e565623e434e75bd3362f02.tar.gz |
feat: close search on clickaway
Diffstat (limited to 'src/components/Header')
-rw-r--r-- | src/components/Header/SearchBar.tsx | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/src/components/Header/SearchBar.tsx b/src/components/Header/SearchBar.tsx index d240db9..2be8f6f 100644 --- a/src/components/Header/SearchBar.tsx +++ b/src/components/Header/SearchBar.tsx @@ -5,7 +5,8 @@ import { List, ListItem, Paper, - Divider + Divider, + ClickAwayListener } from '@material-ui/core'; import { makeStyles } from '@material-ui/core/styles'; import { User } from 'which-types'; @@ -67,27 +68,33 @@ const SearchBar: React.FC<PropTypes> = ({ navigate }) => { else setShouldRefetch(true); }; - const handleNavigate = (index: number) => () => { - navigate('profile', results[index]._id); + const handleClose = () => { setQuery(''); setResults([]); }; + const handleNavigate = (index: number) => () => { + navigate('profile', results[index]._id); + handleClose(); + }; + const SearchResults = ( - <Paper className={classes.results}> - <List> - { - results.map((result, index) => ( - <> - <ListItem button onClick={handleNavigate(index)}> - <UserStrip user={result} navigate={navigate} /> - </ListItem> - {(index < results.length - 1) && <Divider />} - </> - )) - } - </List> - </Paper> + <ClickAwayListener onClickAway={handleClose}> + <Paper className={classes.results}> + <List> + { + results.map((result, index) => ( + <> + <ListItem button onClick={handleNavigate(index)}> + <UserStrip user={result} navigate={navigate} /> + </ListItem> + {(index < results.length - 1) && <Divider />} + </> + )) + } + </List> + </Paper> + </ClickAwayListener> ); return ( |