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
getSearchResults = () => { | |
const {searchTerm} = this.state; | |
return searchTerm ? this.searchEngine.search(searchTerm) : [] | |
// searchEngine.search is the expensive search algorithm | |
}; |
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
render () { | |
return ( | |
<Autocomplete | |
value={this.state.searchTerm} | |
items={this.getSearchResults()} | |
renderMenu={this.reactVirtualizedList} | |
renderItem={this.renderItem} | |
getItemValue={ item => item.name } | |
onChange={(e, value) => this.setState({searchTerm: value})} | |
/> |
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
app.route('/prep-staging/testSearch') | |
.post(bodyParser.json(), (req, res) => { | |
const {data, searchTerm} = req.body | |
const engine = new SearchEngine(data) | |
const searchResults = engine.search(searchTerm) | |
res.send({searchResult}) |
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
//searchResults.js | |
export default class SearchResults extends React.Component { | |
constructor (props) { | |
super(); | |
this.state = { | |
searchResults: [], | |
} | |
//initiate the worker array: | |
this.workerArray = new WorkerArrayController({ |
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
//searchResults.js | |
componentDidUpdate(prevProps) { | |
const {searchTerm, searchEngine} = this.props; | |
if(searchTerm && searchTerm !== prevProps.searchTerm) { | |
this.setState({ | |
searchResults: searchEngine.search(searchTerm) | |
}) | |
} | |
} |
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
//autocomplete.js | |
render () { | |
return ( | |
<div> | |
<input | |
onChange={ e => this.setState({searchTerm: e.target.value})} | |
value={this.state.searchTerm}/> | |
<SearchResults |
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
//autocomplete.js | |
render () { | |
return ( | |
<div> | |
<input | |
onChange={ e => this.setState({searchTerm: e.target.value})} | |
value={this.state.searchTerm}/> | |
<SearchResults | |
searchEngine={this.props.searchEngine} |
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
export function useClickOutside ({container, handler, dependencies, addListenerConditional = true}) { | |
useEffect(() => { | |
if(addListenerConditional) { | |
document.addEventListener('mousedown', handleClickOutside); | |
} | |
return () => document.removeEventListener('mousedown', handleClickOutside) | |
}, dependencies); | |
function handleClickOutside (e) { | |
if(!container.current.contains(e.target)) { |
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
//searchResults.js | |
export default class SearchResults extends React.Component { | |
constructor (props) { | |
super(); | |
this.state = { | |
searchResults: [], | |
} | |
//initiate the webworker: | |
this.webWorker = new Worker('...path to webWorker.js') |
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
//webWorker.js | |
self.importScripts('...the search engine script, provides the SearchEngine constructor'); | |
let searchEngine; | |
let cache = {} | |
function initiateSearchEngine (data) { | |
searchEngine = new SearchEngine(data); | |
cache = {}; | |
} |
OlderNewer