@@ -11,7 +11,12 @@ import classNames from 'classnames'
1111class SearchBar extends Component {
1212 constructor ( props ) {
1313 super ( props )
14- this . state = { searchState : 'empty' , suggestions : [ ] }
14+ const searchValue = this . getQueryStringValue ( props . searchTermKey )
15+ this . state = {
16+ searchState : searchValue . length > 0 ? 'filled' : 'empty' ,
17+ suggestions : [ ] ,
18+ searchValue : searchValue
19+ }
1520 this . onFocus = this . onFocus . bind ( this )
1621 this . onChange = this . onChange . bind ( this )
1722 this . onKeyUp = this . onKeyUp . bind ( this )
@@ -22,6 +27,10 @@ class SearchBar extends Component {
2227 this . handleSuggestionsUpdate = this . handleSuggestionsUpdate . bind ( this )
2328 }
2429
30+ getQueryStringValue ( key ) {
31+ return unescape ( window . location . href . replace ( new RegExp ( "^(?:.*[&\\?]" + escape ( key ) . replace ( / [ \. \+ \* ] / g, "\\$&" ) + "(?:\\=([^&]*))?)?.*$" , "i" ) , "$1" ) ) ;
32+ }
33+
2534 componentDidMount ( ) {
2635 window . addEventListener ( 'click' , this . handleOutsideClick )
2736 }
@@ -203,11 +212,13 @@ class SearchBar extends Component {
203212SearchBar . propTypes = {
204213 onSearch : PropTypes . func . isRequired ,
205214 onTermChange : PropTypes . func . isRequired ,
206- recentTerms : PropTypes . array
215+ recentTerms : PropTypes . array ,
216+ searchTermKey : PropTypes . string
207217}
208218
209219SearchBar . defaultProps = {
210- recentTerms : [ ]
220+ recentTerms : [ ] ,
221+ searchTermKey : 'q'
211222}
212223
213224export default SearchBar
0 commit comments