Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions components/SearchBar/SearchBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SearchBar extends Component {

handleSuggestionsUpdate(requestNo, data) {
if (requestNo === this.state.maxRequestNo) {
this.setState({loading: false, suggestions: data})
this.setState({loading: false, suggestions: data, selectedSuggestionIdx: null})
}
}

Expand All @@ -71,7 +71,8 @@ class SearchBar extends Component {
searchValue: this.refs.searchValue.value,
requestNo: rc,
maxRequestNo: rc,
loading: true
loading: true,
searchState: 'focused'
}
},
function() {
Expand All @@ -93,11 +94,45 @@ class SearchBar extends Component {

onKeyUp(evt) {
const eventKey = evt.keyCode
evt.stopPropagation()
evt.preventDefault()
// if return is pressed
if (eventKey === 13) {
this.setState({ searchState: 'filled' }, function() {
this.search()
})
} else if (eventKey === 39) { // right arrow key is pressed
const suggestion = this.state.suggestions.length > 0 ? this.state.suggestions[0] : null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think const suggestion = this.state.suggestions[0] would work too

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess yes because that would assign undefined if suggestions array does not have any element in it and if (suggestion) would not evaluate to true in both cases, undefined and null

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to close the thread, change is done in dev.

if (suggestion) {
this.refs.searchValue.value = suggestion
// trigger the change event handler
this.onChange()
}
} else if (eventKey === 38) { // up arrow key
const currSelectedIdx = this.state.selectedSuggestionIdx
if (currSelectedIdx) { // index is none of (undefined, null, 0)
const suggestionIdx = currSelectedIdx - 1
const suggestion = this.state.suggestions[suggestionIdx]
this.refs.searchValue.value = suggestion
this.setState({
selectedSuggestionIdx : suggestionIdx,
searchValue: suggestion
})
}
} else if (eventKey === 40) { // down arrow key
const currSelectedIdx = this.state.selectedSuggestionIdx
// index is none of (undefined, null, 0)
if (typeof currSelectedIdx === 'undefined'
|| currSelectedIdx === null
|| this.state.suggestions.length > currSelectedIdx + 1) {
const suggestionIdx = typeof currSelectedIdx === 'number' ? currSelectedIdx + 1 : 0
const suggestion = this.state.suggestions[suggestionIdx]
this.refs.searchValue.value = suggestion
this.setState({
selectedSuggestionIdx: suggestionIdx,
searchValue: suggestion
})
}
}
}

Expand Down Expand Up @@ -140,7 +175,7 @@ class SearchBar extends Component {

const results = this.state.loading === true
? <div className="loading-suggestions"><Loader /></div>
: <SearchSuggestions recentSearch={ recentList } popularSearch={ popularList } onSuggestionSelect={ this.handleSuggestionSelect } />
: <SearchSuggestions recentSearch={ recentList } searchTerm={ this.state.searchValue } popularSearch={ popularList } onSuggestionSelect={ this.handleSuggestionSelect } />
return (
<div className={ sbClasses }>
<input className="search-bar__text" onFocus={ this.onFocus } onChange={ this.onChange } onKeyUp={ this.onKeyUp } ref="searchValue" value={this.state.searchValue} />
Expand Down
19 changes: 13 additions & 6 deletions components/SearchSuggestions/SearchSuggestions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,32 @@ class SearchSuggestions extends Component {
render() {
const recentList = this.props.recentSearch
const popularList = this.props.popularSearch

const suggestionItem = (term, i) => {
let labelDOM = term
const searchTerm = this.props.searchTerm
let exactMatch = false
if (searchTerm.length > 0) {
const idx = term.toLowerCase().indexOf(searchTerm.toLowerCase())
if (idx !== -1) {
// check if exact match
exactMatch = idx === 0 && term.length === searchTerm.length
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it ever be the case that term.length === searchTerm.length, but they aren't the same word?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nopes, because we are checking for index of searchTerm in term to be 0. If you think we can have that case, please let me know.

// prepare DOM for the content to be rendered under StandardListItem
labelDOM = (
<span>
<span className={ itemClasses }>
{ term.substring(0, idx) }
<strong>{ searchTerm }</strong>
{ term.substring(idx + searchTerm.length) }
</span>
)
}
}

// prepares css class for li
const itemClasses = classNames(
{ selected : exactMatch }
)
// prepares and returns the DOM for each popular/recent search item
return (
<li key={ i } data-term={term} onClick={ this.handleClick }>
<li key={ i } data-term={term} onClick={ this.handleClick } className={ itemClasses }>
<StandardListItem labelText={ labelDOM } showIcon={ false } />
</li>
)
Expand Down Expand Up @@ -105,13 +112,13 @@ class SearchSuggestions extends Component {
SearchSuggestions.propTypes = {
onSuggestionSelect : PropTypes.func.isRequired,
recentSearch : PropTypes.array,
popularSearch : PropTypes.array,
popularSearch : PropTypes.array,
searchTerm : PropTypes.string
}

SearchSuggestions.defaultProps = {
recentSearch : [],
popularSearch : [],
popularSearch : [],
searchTerm : ''
}

Expand Down
4 changes: 4 additions & 0 deletions components/SearchSuggestions/SearchSuggestions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
padding: 0;
padding-bottom: 5px;

li.selected {
background-color: $gray-light;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine Vic will want the nav when it's completely finished to include the new UI Kit colors / fonts, etc. We can check with him on the details.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okies. But managing the separate branch till we have UI kit complete, can be bit difficult because we have continuous updates in dev branch. Anyway, it sounds good to have finished nav out instead of unfinished one.

}

.StandardListItem {
align-items: initial;
text-align: left;
Expand Down