Skip to content

Commit 4dbb7a9

Browse files
authored
Dashboard: debounce search input (codesandbox#4021)
1 parent 296111f commit 4dbb7a9

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

packages/app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
"svg-react-loader": "^0.4.4",
224224
"tern": "^0.21.0",
225225
"textextensions": "^3.3.0",
226+
"use-debounce": "^3.4.2",
226227
"use-interval": "^1.2.1",
227228
"util": "0.11.1",
228229
"vue": "^2.5.2",

packages/app/src/app/pages/NewDashboard/Header/index.tsx

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState } from 'react';
2+
import { useDebouncedCallback } from 'use-debounce';
23
import { useOvermind } from 'app/overmind';
34
import { UserMenu } from 'app/pages/common/UserMenu';
45
import {
@@ -12,22 +13,21 @@ import css from '@styled-system/css';
1213
import { withRouter } from 'react-router-dom';
1314

1415
export const HeaderComponent = ({ history }) => {
15-
const [value, setValue] = useState();
16+
const [value, setValue] = useState('');
1617
const {
1718
actions: { modalOpened },
1819
} = useOvermind();
1920

2021
const searchQuery = new URLSearchParams(window.location.search).get('query');
2122

22-
const onChange = e => {
23+
const search = query => history.push(`/new-dashboard/search?query=${query}`);
24+
const [debouncedSearch] = useDebouncedCallback(search, 250);
25+
26+
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
2327
setValue(e.target.value);
24-
if (!e.target.value) {
25-
history.push(`/new-dashboard/all`);
26-
}
2728

28-
if (e.target.value.length >= 2) {
29-
history.push(`/new-dashboard/search?query=${e.target.value}`);
30-
}
29+
if (!e.target.value) history.push(`/new-dashboard/all`);
30+
if (e.target.value.length >= 2) debouncedSearch(e.target.value);
3131
};
3232

3333
return (
@@ -48,14 +48,9 @@ export const HeaderComponent = ({ history }) => {
4848
>
4949
<IconButton name="menu" size={18} title="Menu" />
5050
<Input
51+
type="text"
5152
value={value || searchQuery || ''}
5253
onChange={onChange}
53-
onKeyDown={e => {
54-
if (e.key === 'Enter' && value) {
55-
history.push(`/new-dashboard/search?query=${e.target.value}`);
56-
}
57-
}}
58-
type="text"
5954
placeholder="Search all sandboxes"
6055
css={css({ maxWidth: 480, display: ['none', 'none', 'block'] })}
6156
/>

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30758,6 +30758,11 @@ use-callback-ref@^1.2.1:
3075830758
resolved "https://registry.yarnpkg.com/use-callback-ref/-/use-callback-ref-1.2.3.tgz#9f939dfb5740807bbf9dd79cdd4e99d27e827756"
3075930759
integrity sha512-DPBPh1i2adCZoIArRlTuKRy7yue7QogtEnfv0AKrWsY+GA+4EKe37zhRDouNnyWMoNQFYZZRF+2dLHsWE4YvJA==
3076030760

30761+
use-debounce@^3.4.2:
30762+
version "3.4.2"
30763+
resolved "https://registry.yarnpkg.com/use-debounce/-/use-debounce-3.4.2.tgz#f4333cca59504244b916159600427bf977ec4c46"
30764+
integrity sha512-rW44wZaFPh3XiwUzGBA0JRuklpbfKO/szU/5CYD2Q/erLmCem63lJ650p3a+NJE6S+g0rulKtBOfa/3rw/GN+Q==
30765+
3076130766
use-interval@^1.2.1:
3076230767
version "1.2.1"
3076330768
resolved "https://registry.yarnpkg.com/use-interval/-/use-interval-1.2.1.tgz#ec4aaa869888bcf491edcc1f001eb631db908597"

0 commit comments

Comments
 (0)