Skip to content

Commit

Permalink
fix: Add debounce for 300 ms when searching (#1089)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman Hotsiy <gotsijroman@gmail.com>
  • Loading branch information
faheem00 and RomanHotsiy authored Mar 16, 2020
1 parent af415e8 commit 373f018
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 2 additions & 0 deletions e2e/integration/search.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('Search', () => {
it('should support arrow navigation', () => {
getSearchInput().type('int', { force: true });

cy.wait(500);

getSearchInput().type('{downarrow}', { force: true });
getResult(0).should('have.class', 'active');

Expand Down
23 changes: 15 additions & 8 deletions src/components/SearchBox/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MenuItem } from '../SideMenu/MenuItem';
import { MarkerService } from '../../services/MarkerService';
import { SearchResult } from '../../services/SearchWorker.worker';

import { bind, debounce } from 'decko';
import { PerfectScrollbarWrap } from '../../common-elements/perfect-scrollbar';
import {
ClearIcon,
Expand Down Expand Up @@ -94,25 +95,31 @@ export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxStat
setResults(results: SearchResult[], term: string) {
this.setState({
results,
term,
});
this.props.marker.mark(term);
}

@bind
@debounce(400)
searchCallback(searchTerm: string) {
this.props.search.search(searchTerm).then(res => {
this.setResults(res, searchTerm);
});
}

search = (event: React.ChangeEvent<HTMLInputElement>) => {
const q = event.target.value;
if (q.length < 3) {
this.clearResults(q);
return;
}

this.setState({
term: q,
});

this.props.search.search(event.target.value).then(res => {
this.setResults(res, q);
});
this.setState(
{
term: q,
},
() => this.searchCallback(this.state.term),
);
};

render() {
Expand Down

0 comments on commit 373f018

Please sign in to comment.