Skip to content

Commit cabca2e

Browse files
committed
Add debounce
1 parent 79448b0 commit cabca2e

6 files changed

Lines changed: 42 additions & 23 deletions

File tree

package-lock.json

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"ts-loader": "^5.3.1",
9797
"tslint": "^5.11.0",
9898
"tslint-eslint-rules": "^5.4.0",
99-
"typescript": "^3.2.1",
99+
"typescript": "^3.2.2",
100100
"wdio-dot-reporter": "0.0.10",
101101
"wdio-jasmine-framework": "^0.3.8",
102102
"wdio-selenium-standalone-service": "0.0.11",

src/DropdownReference/components/DropdownReference.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Select, { Async } from "react-select";
44

55
import { Alert } from "../../SharedResources/components/Alert";
66
import { Label } from "../../SharedResources/components/Label";
7-
import { DropdownReferenceProps, hideDropDown } from "../../SharedResources/utils/ContainerUtils";
7+
import { DropdownReferenceProps, debounce, hideDropDown } from "../../SharedResources/utils/ContainerUtils";
88

99
import "react-select/dist/react-select.css";
1010
import "../../SharedResources/ui/Dropdown.scss";
@@ -58,7 +58,7 @@ export class DropdownReference extends Component<DropdownReferenceProps> {
5858
})
5959
: createElement(Async, {
6060
searchPromptText: this.props.searchPromptText,
61-
loadOptions: this.props.asyncData,
61+
loadOptions: debounce(this.props.asyncData, 200),
6262
...commonProps
6363
}),
6464
createElement(Alert, { className: "widget-dropdown-reference-alert" }, this.props.alertMessage)

src/DropdownReferenceSet/components/DropdownReferenceSet.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Component, createElement } from "react";
2-
import Select, { Async, LoadOptionsHandler } from "react-select";
2+
import Select, { Async } from "react-select";
33
import * as classNames from "classnames";
44

55
import { Alert } from "../../SharedResources/components/Alert";
66
import { Label } from "../../SharedResources/components/Label";
7-
import { hideDropDown } from "../../SharedResources/utils/ContainerUtils";
7+
import { debounce, hideDropDown } from "../../SharedResources/utils/ContainerUtils";
88

99
import "react-select/dist/react-select.css";
1010
import "../../SharedResources/ui/Dropdown.scss";
@@ -13,7 +13,7 @@ export interface DropdownReferenceSetProps {
1313
styleObject?: object;
1414
labelWidth: number;
1515
data?: ReferenceOption[];
16-
asyncData: LoadOptionsHandler<{}>;
16+
asyncData: any;
1717
labelCaption: string;
1818
showLabel: boolean;
1919
emptyOptionCaption: string;
@@ -98,7 +98,7 @@ export class DropdownReferenceSet extends Component<DropdownReferenceSetProps> {
9898
: createElement(Async, {
9999
autoload: false,
100100
autoFocus: true,
101-
loadOptions: this.props.asyncData,
101+
loadOptions: debounce(this.props.asyncData, 200),
102102
searchPromptText: this.props.searchPromptText,
103103
...commonProps
104104
}),

src/SharedResources/ui/Dropdown.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
.Select-arrow {
44
display: none;
55
}
6+
7+
.Select-clear {
8+
color: $gray-dark;
9+
}
10+
611
.Select.is-focused:not(.is-open) > .Select-control,
712
select.has-value.is-focused.is-open > .Select-control {
813
border-color: $brand-primary;

src/SharedResources/utils/ContainerUtils.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ContainerProps } from "../../DropdownReference/components/DropdownReferenceContainer";
22
import { ContainerProps as ReferenceSetProps } from "../../DropdownReferenceSet/components/DropdownReferenceSetContainer";
3-
import { LoadOptionsHandler } from "react-select";
43

54
export interface AttributeType {
65
name: string;
@@ -16,7 +15,7 @@ export interface DropdownReferenceProps {
1615
styleObject?: object;
1716
labelWidth: number;
1817
data: ReferenceOption[];
19-
asyncData: LoadOptionsHandler<{}>;
18+
asyncData: any;
2019
value?: string;
2120
labelCaption: string;
2221
showLabel: boolean;
@@ -48,6 +47,21 @@ export function hideDropDown() {
4847
}
4948
}
5049

50+
export function debounce(input: any, wait = 0) {
51+
let timeout: any = null;
52+
let resolves: any = null;
53+
54+
return (...args: []) => {
55+
clearTimeout(timeout);
56+
timeout = setTimeout(() => {
57+
resolves(input(...args));
58+
resolves = null;
59+
}, wait);
60+
61+
return new Promise(r => resolves = r);
62+
};
63+
}
64+
5165
export const parseStyle = (style = ""): { [key: string]: string } => {
5266
try {
5367
return style.split(";").reduce<{ [key: string]: string }>((styleObject, line) => {

0 commit comments

Comments
 (0)