-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Searching bogs down while typing a search term #1027
Comments
I snooped around to see if there was an existing dependency that provided a debounce implementation. Just a quick note: |
I'm having the same issue and I don't have any clue how do debounce searches. There must be a way as there don't seem to have issue. Any help will be appreciated. Thanks. |
If I have some time this week I can give it a shot. Lodash has a debounce implementation, and it is a subdependency already... |
Hi strbean, Is there anyone who has done this? |
Unfortunately, I wasn't able to get tests passing on a fresh clone here. My dev environments atm is strictly python, haven't gotten to touch JS at work yet :'(, so I don't think I can dig into it right now. This seemed to work though well, if you/anyone wants to apply it and make a PR: diff --git a/package.json b/package.json
index 5b199dc..26c83f4 100644
--- a/package.json
+++ b/package.json
@@ -98,7 +98,6 @@
"html-webpack-plugin": "^3.1.0",
"jest": "^24.8.0",
"license-checker": "^25.0.1",
- "lodash": "^4.17.15",
"mobx": "^4.3.1",
"prettier": "^1.18.2",
"prettier-eslint": "^9.0.0",
@@ -144,6 +143,7 @@
"marked": "^0.7.0",
"memoize-one": "^5.0.5",
"mobx-react": "^6.1.1",
+ "lodash": "^4.17.15",
"openapi-sampler": "1.0.0-beta.15",
"perfect-scrollbar": "^1.4.0",
"polished": "^3.4.1",
diff --git a/src/components/SearchBox/SearchBox.tsx b/src/components/SearchBox/SearchBox.tsx
index e6ade2c..4517809 100644
--- a/src/components/SearchBox/SearchBox.tsx
+++ b/src/components/SearchBox/SearchBox.tsx
@@ -1,3 +1,4 @@
+import { debounce } from 'lodash';
import * as React from 'react';
import { IMenuItem } from '../../services/MenuStore';
@@ -41,6 +42,7 @@ export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxStat
term: '',
activeItemIdx: -1,
};
+ this.search = debounce(this.search, 500);
}
clearResults(term: string) {
@@ -99,8 +101,9 @@ export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxStat
this.props.marker.mark(term);
}
- search = (event: React.ChangeEvent<HTMLInputElement>) => {
+ handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const q = event.target.value;
+
if (q.length < 3) {
this.clearResults(q);
return;
@@ -110,10 +113,14 @@ export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxStat
term: q,
});
- this.props.search.search(event.target.value).then(res => {
+ this.search(q);
+ };
+
+ search(q) {
+ this.props.search.search(q).then(res => {
this.setResults(res, q);
});
- };
+ }
render() {
const { activeItemIdx } = this.state;
@@ -133,7 +140,7 @@ export class SearchBox extends React.PureComponent<SearchBoxProps, SearchBoxStat
onKeyDown={this.handleKeyDown}
placeholder="Search..."
type="text"
- onChange={this.search}
+ onChange={this.handleChange}
/>
{results.length > 0 && (
<PerfectScrollbarWrap |
Thanks, strbean. |
Reproducing:
Use redoc on a large spec.
Quickly type a phrase into the search bar
Result:
The UI hangs, a search is performed for each change event.
Fix:
Debounce searches, so a search is only performed when a certain amount of time has passed since the last change event.
The text was updated successfully, but these errors were encountered: