@himanshu-sorathiya/react-kit 1.0.6 → 1.0.7

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 CHANGED
@@ -5,6 +5,25 @@ export interface ModalLayoutProps {
5
5
  className?: string;
6
6
  }
7
7
  export declare function ModalLayout({ children, className }: ModalLayoutProps): import("react").ReactPortal;
8
+ export interface DebounceOptions {
9
+ maxWait?: number;
10
+ leading?: boolean;
11
+ trailing?: boolean;
12
+ }
13
+ export interface UseDebounceReturn<Args extends any[]> {
14
+ debouncedFunc: (...args: Args) => void;
15
+ cancel: () => void;
16
+ flush: () => void;
17
+ isPending: boolean;
18
+ }
19
+ export declare function useDebounce<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: DebounceOptions): UseDebounceReturn<Args>;
20
+ export interface DebouncedValueOptions {
21
+ maxWait?: number;
22
+ leading?: boolean;
23
+ trailing?: boolean;
24
+ }
25
+ export type UseDebouncedValueReturn<T> = T;
26
+ export declare function useDebouncedValue<T>(value: T, delay: number, options?: DebouncedValueOptions): UseDebouncedValueReturn<T>;
8
27
  export type FilterType = "text" | "number" | "boolean" | "date" | "select" | "multiselect" | "custom";
9
28
  export type TextOperator = "contains" | "equals" | "startsWith" | "endsWith" | "notContains";
10
29
  export type NumberOperator = "equals" | "greaterThan" | "lessThan" | "greaterThanOrEqual" | "lessThanOrEqual" | "between";
package/dist/index.js CHANGED
@@ -48,8 +48,48 @@ function d({ children: e, className: n }) {
48
48
  }), document.body);
49
49
  }
50
50
  //#endregion
51
+ //#region src/hooks/useDebounce.ts
52
+ function f(n, a, o = {}) {
53
+ let [s, c] = i(!1), l = o.maxWait, u = !!o.leading, d = !u && o.trailing === !1 ? !0 : o.trailing ?? !u, f = r(n), p = r(null), m = r(-1), h = r(-1), g = r(null);
54
+ t(() => {
55
+ f.current = n;
56
+ }, [n]);
57
+ let _ = e(() => {
58
+ p.current &&= (clearTimeout(p.current), null), m.current = -1, h.current = -1, g.current = null, c(!1);
59
+ }, []), v = e(() => {
60
+ p.current && g.current && (clearTimeout(p.current), p.current = null, f.current(...g.current), m.current = Date.now(), h.current = -1, g.current = null, c(!1));
61
+ }, []);
62
+ return t(() => () => {
63
+ _();
64
+ }, [_]), {
65
+ debouncedFunc: e((...e) => {
66
+ c(!0), h.current = Date.now(), g.current = e, m.current === -1 && (m.current = Date.now(), u === !0 && f.current(...e)), l !== void 0 && Date.now() - m.current >= l ? (p.current && clearTimeout(p.current), f.current(...e), m.current = -1, c(!1)) : (p.current && clearTimeout(p.current), p.current = setTimeout(() => {
67
+ h.current !== m.current && d !== !1 && f.current(...e), m.current = -1, c(!1);
68
+ }, a));
69
+ }, [
70
+ a,
71
+ l,
72
+ d,
73
+ u
74
+ ]),
75
+ cancel: _,
76
+ flush: v,
77
+ isPending: s
78
+ };
79
+ }
80
+ //#endregion
81
+ //#region src/hooks/useDebouncedValue.ts
82
+ function p(e, n, r = {}) {
83
+ let [a, o] = i(e), { debouncedFunc: s } = f((e) => {
84
+ o(e);
85
+ }, n, r);
86
+ return t(() => {
87
+ s(e);
88
+ }, [e, s]), a;
89
+ }
90
+ //#endregion
51
91
  //#region src/utils/filterUtils.ts
52
- var f = {
92
+ var m = {
53
93
  text: {
54
94
  contains: (e, t, n) => n?.caseSensitive ? String(e).includes(String(t)) : String(e).toLowerCase().includes(String(t).toLowerCase()),
55
95
  equals: (e, t, n) => n?.caseSensitive ? String(e) === String(t) : String(e).toLowerCase() === String(t).toLowerCase(),
@@ -100,18 +140,18 @@ var f = {
100
140
  };
101
141
  //#endregion
102
142
  //#region src/utils/utils.ts
103
- function p(e, t, n) {
143
+ function h(e, t, n) {
104
144
  if (typeof e != "object" || !e) return e;
105
145
  let r = (t || n) ?? "";
106
146
  return r.includes(".") ? r.split(".").reduce((e, t) => e?.[t], e) : e?.[r];
107
147
  }
108
148
  //#endregion
109
149
  //#region src/hooks/useFilter.ts
110
- function m(t, r = []) {
150
+ function g(t, r = []) {
111
151
  let [a, o] = i(r), s = n(() => t.filter((e) => a.every((t) => {
112
- let { id: n, field: r, type: i, operator: a, value: o, compare: s } = t, c = p(e, r, n);
152
+ let { id: n, field: r, type: i, operator: a, value: o, compare: s } = t, c = h(e, r, n);
113
153
  if (i === "custom" || a === "custom") return s ? s(c, o, e) : !0;
114
- let l = f[i];
154
+ let l = m[i];
115
155
  if (!l) return !0;
116
156
  let u = l[a];
117
157
  return u ? u(c, o, t) : !0;
@@ -123,9 +163,9 @@ function m(t, r = []) {
123
163
  o([]);
124
164
  }, []), d = e(() => {
125
165
  o(r);
126
- }, [r]), m = e((e) => {
166
+ }, [r]), f = e((e) => {
127
167
  o(e);
128
- }, []), h = e((e) => a.find((t) => t.id === e)?.value, [a]), g = e((e, t) => {
168
+ }, []), p = e((e) => a.find((t) => t.id === e)?.value, [a]), g = e((e, t) => {
129
169
  o((n) => n.map((n) => n.id === e ? {
130
170
  ...n,
131
171
  value: t
@@ -147,15 +187,15 @@ function m(t, r = []) {
147
187
  ...t
148
188
  } : n));
149
189
  }, []),
150
- setFilters: m,
151
- getFilterValue: h,
190
+ setFilters: f,
191
+ getFilterValue: p,
152
192
  updateFilterValue: g,
153
193
  isFilterActive: _
154
194
  };
155
195
  }
156
196
  //#endregion
157
197
  //#region src/hooks/usePagination.ts
158
- function h(t, r, a = 0) {
198
+ function _(t, r, a = 0) {
159
199
  let [o, s] = i(r), [c, l] = i(a), u = Math.max(1, Math.ceil(t.length / Math.max(1, o))), d = Math.min(Math.max(0, c), u - 1);
160
200
  return c !== d && l(d), {
161
201
  currentItems: n(() => typeof o != "number" || o < 1 ? [] : t.slice(c * o, (c + 1) * o), [
@@ -202,26 +242,26 @@ function h(t, r, a = 0) {
202
242
  }
203
243
  //#endregion
204
244
  //#region src/utils/sortUtils.ts
205
- function g(e, t, n = {}) {
206
- let { sortUndefined: r, desc: i = !1 } = n, a = C(e, t, r, i);
245
+ function v(e, t, n = {}) {
246
+ let { sortUndefined: r, desc: i = !1 } = n, a = T(e, t, r, i);
207
247
  if (a !== null) return a;
208
248
  let o = !!e;
209
249
  return o === !!t ? 0 : o ? 1 : -1;
210
250
  }
211
- function _(e, t, n = {}) {
212
- let { sortUndefined: r, desc: i = !1 } = n, a = C(e, t, r, i);
251
+ function y(e, t, n = {}) {
252
+ let { sortUndefined: r, desc: i = !1 } = n, a = T(e, t, r, i);
213
253
  if (a !== null) return a;
214
254
  let o = e === "" || e === null ? NaN : Number(e), s = t === "" || t === null ? NaN : Number(t), c = Number.isNaN(o), l = Number.isNaN(s);
215
- return c || l ? C(c ? void 0 : o, l ? void 0 : s, r, i) ?? 0 : o - s;
255
+ return c || l ? T(c ? void 0 : o, l ? void 0 : s, r, i) ?? 0 : o - s;
216
256
  }
217
- function v(e, t, n = {}) {
218
- let { desc: r = !1, sortUndefined: i, caseSensitive: a = !1 } = n, o = C(e, t, i, r);
257
+ function b(e, t, n = {}) {
258
+ let { desc: r = !1, sortUndefined: i, caseSensitive: a = !1 } = n, o = T(e, t, i, r);
219
259
  if (o !== null) return o;
220
260
  let s = String(e), c = String(t);
221
261
  return s.localeCompare(c, void 0, { sensitivity: a ? "variant" : "base" });
222
262
  }
223
- function y(e, t, n = {}) {
224
- let { desc: r = !1, sortUndefined: i, caseSensitive: a = !1 } = n, o = C(e, t, i, r);
263
+ function x(e, t, n = {}) {
264
+ let { desc: r = !1, sortUndefined: i, caseSensitive: a = !1 } = n, o = T(e, t, i, r);
225
265
  if (o !== null) return o;
226
266
  let s = String(e), c = String(t);
227
267
  return s.localeCompare(c, void 0, {
@@ -229,21 +269,21 @@ function y(e, t, n = {}) {
229
269
  sensitivity: a ? "variant" : "base"
230
270
  });
231
271
  }
232
- function b(e, t, n = {}) {
233
- let { desc: r = !1, sortUndefined: i } = n, a = C(e, t, i, r);
272
+ function S(e, t, n = {}) {
273
+ let { desc: r = !1, sortUndefined: i } = n, a = T(e, t, i, r);
234
274
  if (a !== null) return a;
235
275
  let o = new Date(e).getTime(), s = new Date(t).getTime();
236
276
  return (Number.isNaN(o) ? 0 : o) - (Number.isNaN(s) ? 0 : s);
237
277
  }
238
- function x(e, t, n = {}) {
239
- let { desc: r = !1, sortUndefined: i } = n, a = C(e, t, i, r);
278
+ function C(e, t, n = {}) {
279
+ let { desc: r = !1, sortUndefined: i } = n, a = T(e, t, i, r);
240
280
  return a === null ? e < t ? -1 : +(e > t) : a;
241
281
  }
242
- function S(e, t, n = {}) {
243
- let { desc: r = !1, sortUndefined: i, compare: a } = n, o = C(e, t, i, r);
282
+ function w(e, t, n = {}) {
283
+ let { desc: r = !1, sortUndefined: i, compare: a } = n, o = T(e, t, i, r);
244
284
  return o === null ? typeof a == "function" ? a(e, t) : e < t ? -1 : +(e > t) : o;
245
285
  }
246
- function C(e, t, n = !1, r) {
286
+ function T(e, t, n = !1, r) {
247
287
  let i = e ?? void 0, a = t ?? void 0;
248
288
  if (i !== void 0 && a !== void 0) return null;
249
289
  if (i === void 0 && a === void 0 || n === !1) return 0;
@@ -261,35 +301,35 @@ function C(e, t, n = !1, r) {
261
301
  }
262
302
  //#endregion
263
303
  //#region src/hooks/useSort.ts
264
- function w(t, r = []) {
304
+ function E(t, r = []) {
265
305
  let [a, o] = i(r);
266
306
  return {
267
307
  sortedItems: n(() => t.toSorted((e, t) => {
268
308
  for (let n of a) {
269
- let { type: r, field: i, id: a, desc: o = !1, invertSorting: s = !1 } = n, c = p(e, i, a), l = p(t, i, a), u;
309
+ let { type: r, field: i, id: a, desc: o = !1, invertSorting: s = !1 } = n, c = h(e, i, a), l = h(t, i, a), u;
270
310
  switch (r) {
271
311
  case "numeric":
272
- u = _(c, l, n);
312
+ u = y(c, l, n);
273
313
  break;
274
314
  case "alphabetical":
275
- u = v(c, l, n);
315
+ u = b(c, l, n);
276
316
  break;
277
317
  case "alphanumeric":
278
- u = y(c, l, n);
318
+ u = x(c, l, n);
279
319
  break;
280
320
  case "boolean":
281
- u = g(c, l, n);
321
+ u = v(c, l, n);
282
322
  break;
283
323
  case "date":
284
- u = b(c, l, n);
324
+ u = S(c, l, n);
285
325
  break;
286
326
  case "basic":
287
- u = x(c, l, n);
327
+ u = C(c, l, n);
288
328
  break;
289
329
  case "custom":
290
- u = S(c, l, n);
330
+ u = w(c, l, n);
291
331
  break;
292
- default: u = x(c, l, n);
332
+ default: u = C(c, l, n);
293
333
  }
294
334
  if (u !== 0) return u * (o === s ? 1 : -1);
295
335
  }
@@ -350,4 +390,4 @@ function w(t, r = []) {
350
390
  };
351
391
  }
352
392
  //#endregion
353
- export { d as ModalLayout, m as useFilter, u as useModal, h as usePagination, w as useSort };
393
+ export { d as ModalLayout, f as useDebounce, p as useDebouncedValue, g as useFilter, u as useModal, _ as usePagination, E as useSort };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@himanshu-sorathiya/react-kit",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "An opinionated collection of react hooks, and reusable UI components.",
5
5
  "type": "module",
6
6
  "module": "./dist/index.js",