saleor-dashboard/src/components/Debounce.tsx

29 lines
652 B
TypeScript
Raw Normal View History

2019-08-09 10:26:22 +00:00
import React from "react";
2019-06-19 14:40:52 +00:00
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;