forked from WikiEducationFoundation/WikiEduDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into refactor-1
- Loading branch information
Showing
71 changed files
with
1,463 additions
and
892 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/assets/javascripts/components/article_finder/article_finder_search_bar.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import { debounce } from 'lodash'; | ||
import { fetchArticleAutocompleteResults } from '../../utils/article_finder_utils'; | ||
|
||
// Controls Search bar and autocomplete functionality | ||
|
||
function ArticleFinderSearchBar({ value, onChange, onSearch, disabled, wiki }) { | ||
const [suggestions, setSuggestions] = useState([]); | ||
const [isAutocompleteLoading, setAutocompleteLoading] = useState(false); | ||
let searchClass = 'search-bar'; | ||
|
||
if (suggestions.length > 0) { | ||
searchClass += ' autocomplete-on'; | ||
} | ||
|
||
const _getSuggestionsApi = useCallback(debounce(async (q) => { | ||
setAutocompleteLoading(true); | ||
const results = await fetchArticleAutocompleteResults(q, wiki).catch(() => []); | ||
setAutocompleteLoading(false); | ||
setSuggestions(results); | ||
}, 500), []); | ||
|
||
const inputChangeHandler = (e) => { | ||
onChange(e.target.value); | ||
// autocomplete disabled while disabled (during loading etc.) | ||
if (!disabled) { | ||
if (e.target.value === '') { | ||
setSuggestions([]); | ||
} else { | ||
_getSuggestionsApi(e.target.value); | ||
} | ||
} | ||
}; | ||
|
||
const searchHandler = () => { | ||
if (disabled || value.trim() === '') return; | ||
setSuggestions([]); | ||
onSearch(value); | ||
}; | ||
|
||
const onKeyDownHandler = (e) => { | ||
// Search on Enter | ||
if (e.key === 'Enter') { | ||
searchHandler(); | ||
} | ||
}; | ||
|
||
const autoCompleteClickHandler = (suggestion) => { | ||
onChange(suggestion); | ||
// call onSearch directly instead of searchHandler to use the latest suggestion value and not the old state value | ||
onSearch(suggestion); | ||
setSuggestions([]); | ||
}; | ||
|
||
return ( | ||
<div className={searchClass}> | ||
<input | ||
type="text" | ||
id="article-searchbar" | ||
placeholder={I18n.t('article_finder.search_placeholder')} | ||
onChange={inputChangeHandler} | ||
value={value} | ||
onKeyDown={onKeyDownHandler} | ||
/> | ||
|
||
{ | ||
isAutocompleteLoading && <div className="loader"><div className="loading__spinner"/></div> | ||
} | ||
|
||
<button onClick={searchHandler} disabled={disabled}> | ||
{I18n.t('article_finder.search')} | ||
</button> | ||
<div className="autocomplete"> | ||
{ | ||
suggestions.map((sug) => { | ||
return <div key={sug} className="autocomplete-item" onClick={() => autoCompleteClickHandler(sug)}> {sug} </div>; | ||
}) | ||
} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default ArticleFinderSearchBar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.