Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Split generic debounce component and form debounce component

dominik-zeglenbb6a4dd5b3e2
diff --git a/saleor/static/dashboard-next/components/Debounce.tsx b/saleor/static/dashboard-next/components/Debounce.tsx
index 228a2e3c6e..522c4b20cd 100644
--- a/saleor/static/dashboard-next/components/Debounce.tsx
+++ b/saleor/static/dashboard-next/components/Debounce.tsx
@@ -1,41 +1,24 @@
import * as React from "react";
export interface DebounceProps {
- change: (event: React.ChangeEvent<any>) => void;
- children:
- | ((
- props: (event: React.ChangeEvent<any>) => void
- ) => React.ReactElement<any>)
- | React.ReactNode;
- submit: (event: React.FormEvent<any>) => void;
+ children: ((props: () => void) => React.ReactNode);
+ debounceFn: (event: React.FormEvent<any>) => void;
time?: number;
}
-export interface DebounceState {
- timer: any | null;
-}
-export class Debounce extends React.Component<DebounceProps, DebounceState> {
- state: DebounceState = {
- timer: null
- };
+export class Debounce extends React.Component<DebounceProps> {
+ timer = null;
- handleChange = (event: React.ChangeEvent<any>) => {
- const { timer } = this.state;
- if (timer) {
- clearTimeout(timer);
+ handleDebounce = () => {
+ const { debounceFn, time } = this.props;
+ if (this.timer) {
+ clearTimeout(this.timer);
}
- this.setState({
- timer: setTimeout(this.props.submit, this.props.time || 200)
- });
- this.props.change(event);
+ this.timer = setTimeout(debounceFn, time || 200);
};
render() {
- const { children } = this.props;
- if (typeof children === "function") {
- return children(this.handleChange);
- }
- return children;
+ return this.props.children(this.handleDebounce);
}
}
export default Debounce;
diff --git a/saleor/static/dashboard-next/components/DebounceForm.tsx b/saleor/static/dashboard-next/components/DebounceForm.tsx
new file mode 100644
index 0000000000..0fac679100
--- /dev/null
+++ b/saleor/static/dashboard-next/components/DebounceForm.tsx
@@ -0,0 +1,27 @@
+import * as React from "react";
+import Debounce from "./Debounce";
+
+export interface DebounceFormProps {
+ change: (event: React.ChangeEvent<any>) => void;
+ children: ((
+ props: (event: React.ChangeEvent<any>) => void
+ ) => React.ReactNode);
+ submit: (event: React.FormEvent<any>) => void;
+ time?: number;
+}
+
+export const DebounceForm: React.StatelessComponent<DebounceFormProps> = ({
+ change,
+ children,
+ submit,
+ time
+}) => (
+ <Debounce debounceFn={submit} time={time}>
+ {debounceFn =>
+ children(event => {
+ change(event);
+ debounceFn();
+ })
+ }
+ </Debounce>
+);
diff --git a/saleor/static/dashboard-next/orders/components/OrderDraftDetailsProducts/OrderDraftDetailsProducts.tsx b/saleor/static/dashboard-next/orders/components/OrderDraftDetailsProducts/OrderDraftDetailsProducts.tsx
index fed362d4da..57c690e1e0 100644
--- a/saleor/static/dashboard-next/orders/components/OrderDraftDetailsProducts/OrderDraftDetailsProducts.tsx
+++ b/saleor/static/dashboard-next/orders/components/OrderDraftDetailsProducts/OrderDraftDetailsProducts.tsx
@@ -10,7 +10,7 @@ import Typography from "@material-ui/core/Typography";
import DeleteIcon from "@material-ui/icons/Delete";
import * as React from "react";
-import Debounce from "../../../components/Debounce";
+import { DebounceForm } from "../../../components/DebounceForm";
import Form from "../../../components/Form";
import Money from "../../../components/Money";
import Skeleton from "../../../components/Skeleton";
@@ -96,7 +96,7 @@ const OrderDraftDetailsProducts = decorate<OrderDraftDetailsProductsProps>(
onSubmit={data => onOrderLineChange(line.id, data)}
>
{({ change, data, hasChanged, submit }) => (
- <Debounce
+ <DebounceForm
change={change}
submit={hasChanged ? submit : undefined}
time={200}
@@ -111,7 +111,7 @@ const OrderDraftDetailsProducts = decorate<OrderDraftDetailsProductsProps>(
onChange={debounce}
/>
)}
- </Debounce>
+ </DebounceForm>
)}
</Form>
) : (