saleor-dashboard/src/components/Debounce.tsx
2019-08-09 12:26:22 +02:00

28 lines
652 B
TypeScript

import React from "react";
export interface DebounceProps<T> {
children: ((props: (...args: T[]) => void) => React.ReactNode);
debounceFn: (...args: T[]) => void;
time?: number;
}
export class Debounce<T> extends React.Component<DebounceProps<T>> {
timer = null;
handleDebounce = (...args: T[]) => {
const { debounceFn, time } = this.props;
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => debounceFn(...args), time || 200);
};
componentWillUnmount() {
clearTimeout(this.timer);
}
render() {
return this.props.children(this.handleDebounce);
}
}
export default Debounce;