@himanshu-sorathiya/react-kit 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +11 -0
- package/dist/index.js +36 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -146,5 +146,16 @@ export interface UseSortReturn<T> {
|
|
|
146
146
|
getSortIndex: (id: string) => number | undefined;
|
|
147
147
|
}
|
|
148
148
|
export declare function useSort<T>(data: T[], initialSorts?: SortState): UseSortReturn<T>;
|
|
149
|
+
export interface ThrottleOptions {
|
|
150
|
+
leading?: boolean;
|
|
151
|
+
trailing?: boolean;
|
|
152
|
+
}
|
|
153
|
+
export interface UseThrottleReturn<Args extends any[]> {
|
|
154
|
+
throttledFunc: (...args: Args) => void;
|
|
155
|
+
cancel: () => void;
|
|
156
|
+
flush: () => void;
|
|
157
|
+
isPending: boolean;
|
|
158
|
+
}
|
|
159
|
+
export declare function useThrottle<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: ThrottleOptions): UseThrottleReturn<Args>;
|
|
149
160
|
|
|
150
161
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -420,4 +420,39 @@ function O(e, n = []) {
|
|
|
420
420
|
};
|
|
421
421
|
}
|
|
422
422
|
//#endregion
|
|
423
|
-
|
|
423
|
+
//#region src/hooks/useThrottle.ts
|
|
424
|
+
function k(e, n, i = {}) {
|
|
425
|
+
let [s, c] = o(!1), l = i.leading ?? !0, u = i.trailing ?? !0, d = a(e), f = a(null), p = a(-1), m = a(null);
|
|
426
|
+
r(() => {
|
|
427
|
+
d.current = e;
|
|
428
|
+
}, [e]);
|
|
429
|
+
let h = t(() => {
|
|
430
|
+
f.current &&= (clearTimeout(f.current), null), p.current = -1, m.current = null, c(!1);
|
|
431
|
+
}, []), g = t(() => {
|
|
432
|
+
m.current && (f.current &&= (clearTimeout(f.current), null), d.current(...m.current), p.current = l ? Date.now() : -1, m.current = null, c(!1));
|
|
433
|
+
}, [l]);
|
|
434
|
+
return r(() => () => {
|
|
435
|
+
h();
|
|
436
|
+
}, [h]), {
|
|
437
|
+
throttledFunc: t((...e) => {
|
|
438
|
+
c(!0);
|
|
439
|
+
let t = Date.now();
|
|
440
|
+
if (m.current = e, p.current === -1 ? l : t - p.current >= n) f.current &&= (clearTimeout(f.current), null), d.current(...e), p.current = t, m.current = null, c(!1);
|
|
441
|
+
else if (!f.current) {
|
|
442
|
+
let e = n - (p.current === -1 ? 0 : t - p.current);
|
|
443
|
+
f.current = setTimeout(() => {
|
|
444
|
+
u && m.current ? (d.current(...m.current), p.current = l ? Date.now() : -1) : p.current = -1, m.current = null, f.current = null, c(!1);
|
|
445
|
+
}, e);
|
|
446
|
+
}
|
|
447
|
+
}, [
|
|
448
|
+
n,
|
|
449
|
+
l,
|
|
450
|
+
u
|
|
451
|
+
]),
|
|
452
|
+
cancel: h,
|
|
453
|
+
flush: g,
|
|
454
|
+
isPending: s
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
//#endregion
|
|
458
|
+
export { d as ModalLayout, f as useDebounce, p as useDebouncedValue, g as useFilter, v as useKey, u as useModal, y as usePagination, O as useSort, k as useThrottle };
|