Skip to content

Commit 37661f5

Browse files
author
vikasrohit
committed
AS#113534192025965, Autofill the search term, from query string, in SearchBar
-- Done
1 parent dc29bed commit 37661f5

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

components/SearchBar/SearchBar.jsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import classNames from 'classnames'
1111
class 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 {
203212
SearchBar.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

209219
SearchBar.defaultProps = {
210-
recentTerms: []
220+
recentTerms : [],
221+
searchTermKey : 'q'
211222
}
212223

213224
export default SearchBar

0 commit comments

Comments
 (0)