1
- import React from 'react' ;
1
+ /* eslint-disable react-hooks/exhaustive-deps */
2
+ import React , { useEffect } from 'react' ;
2
3
import { InputBase , makeStyles , fade , Popover , ClickAwayListener , ListItem , List , ListItemText } from '@material-ui/core' ;
3
4
import SearchIcon from '@material-ui/icons/Search' ;
5
+ import { useDispatch , useSelector } from 'react-redux' ;
6
+ import { search , searchRequest } from '../../reducers/items-reducer' ;
4
7
const useStyles = makeStyles ( ( theme ) => ( {
5
8
search : {
6
9
position : 'relative' ,
@@ -43,7 +46,7 @@ const useStyles = makeStyles((theme) => ({
43
46
typography : {
44
47
padding : theme . spacing ( 2 ) ,
45
48
} ,
46
- listRoot :{
49
+ listRoot : {
47
50
width : 220 ,
48
51
backgroundColor : theme . palette . background . paper ,
49
52
}
@@ -56,28 +59,39 @@ const SearchBox = () => {
56
59
let open = Boolean ( anchorEl ) ;
57
60
const id = open ? 'srch-popover' : undefined ;
58
61
const handleClick = ( event ) => {
59
- setAnchorEl ( event . currentTarget ) ;
62
+ if ( ! anchorEl ) {
63
+ setAnchorEl ( event . currentTarget ) ;
64
+ }
65
+ dispatch ( search ( event . target . value ) )
60
66
} ;
61
67
62
68
const handleClose = ( ) => {
63
69
setAnchorEl ( null ) ;
64
70
} ;
65
71
const [ selectedIndex , setSelectedIndex ] = React . useState ( 1 ) ;
66
-
72
+ const dispatch = useDispatch ( ) ;
73
+ const searchTxt = useSelector ( state => state . items . search )
74
+ const suggest = useSelector ( state => state . items . suggest )
67
75
const handleListItemClick = ( event , index ) => {
68
76
setSelectedIndex ( index ) ;
69
77
handleClose ( ) ;
70
78
} ;
79
+ useEffect ( ( ) => {
80
+ if ( searchTxt ) {
81
+ dispatch ( searchRequest ( searchTxt ) )
82
+ }
83
+ } , [ searchTxt ] ) ;
71
84
return (
72
85
< ClickAwayListener onClickAway = { handleClose } >
73
86
< div className = { classes . search } >
74
87
< div className = { classes . searchIcon } >
75
88
< SearchIcon />
76
89
</ div >
77
90
< InputBase
78
- onKeyPress = { handleClick }
79
91
placeholder = "Search…"
80
-
92
+ onChange = { handleClick }
93
+ value = { searchTxt }
94
+ autoFocus = { true }
81
95
classes = { {
82
96
root : classes . inputRoot ,
83
97
input : classes . inputInput ,
@@ -86,6 +100,8 @@ const SearchBox = () => {
86
100
/>
87
101
< Popover
88
102
id = { id }
103
+ disableAutoFocus
104
+ disableEnforceFocus
89
105
open = { open }
90
106
anchorEl = { anchorEl }
91
107
onClose = { handleClose }
@@ -100,27 +116,30 @@ const SearchBox = () => {
100
116
>
101
117
< div className = { classes . listRoot } >
102
118
103
- { /* <Typography className={classes.typography}>The content of the Popover.</Typography> */ }
104
- < List component = "nav" aria-label = "main mailbox folders" >
105
- < ListItem
106
- button
107
- selected = { selectedIndex === 0 }
108
- onClick = { ( event ) => handleListItemClick ( event , 0 ) }
109
- >
110
- { /* <ListItemIcon>
111
- <InboxIcon />
112
- </ListItemIcon>*/ }
113
- < ListItemText primary = "Inbox" />
114
- </ ListItem >
115
- < ListItem
116
- button
117
- selected = { selectedIndex === 1 }
118
- onClick = { ( event ) => handleListItemClick ( event , 1 ) }
119
- >
120
- < ListItemText primary = "dfgdfg" />
119
+ { /* <Typography className={classes.typography}>The content of the Popover.</Typography> */ }
120
+ < List component = "nav" aria-label = "main mailbox folders" >
121
+ { /* <ListItem
122
+ onClick={(event) => handleListItemClick(event, 0)}
123
+ >
124
+
125
+ <ListItemText primary="Inbox" />
126
+ </ListItem>
127
+ <ListItem
128
+ onClick={(event) => handleListItemClick(event, 1)}
129
+ >
130
+ <ListItemText primary="dfgdfg" />
121
131
122
- </ ListItem >
123
- </ List >
132
+ </ListItem> */ }
133
+ {
134
+ suggest . map (
135
+ ( sg ) => ( < ListItem
136
+ onClick = { ( event ) => handleListItemClick ( event , 0 ) }
137
+ >
138
+ < ListItemText primary = { sg . title } />
139
+ </ ListItem > )
140
+ )
141
+ }
142
+ </ List >
124
143
</ div >
125
144
</ Popover >
126
145
</ div >
0 commit comments