@himanshu-sorathiya/react-kit 1.0.7 → 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 +42 -0
- package/dist/index.js +208 -143
- package/package.json +4 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Generated by dts-bundle-generator v9.5.1
|
|
2
2
|
|
|
3
|
+
import { RefObject } from 'react';
|
|
4
|
+
|
|
3
5
|
export interface ModalLayoutProps {
|
|
4
6
|
children: React.ReactNode;
|
|
5
7
|
className?: string;
|
|
@@ -60,6 +62,35 @@ export interface UseFilterReturn<T> {
|
|
|
60
62
|
isFilterActive: (id: string) => boolean;
|
|
61
63
|
}
|
|
62
64
|
export declare function useFilter<T>(data: T[], initialFilters?: FilterState): UseFilterReturn<T>;
|
|
65
|
+
declare const validKeyEventTypes: readonly [
|
|
66
|
+
"keydown",
|
|
67
|
+
"keyup",
|
|
68
|
+
"keypress"
|
|
69
|
+
];
|
|
70
|
+
export type KeyEventType = (typeof validKeyEventTypes)[number];
|
|
71
|
+
export interface KeyLifecycleOptions {
|
|
72
|
+
enabled?: boolean;
|
|
73
|
+
target?: RefObject<HTMLElement | null> | Window;
|
|
74
|
+
}
|
|
75
|
+
export interface KeyEventModifiers {
|
|
76
|
+
preventDefault?: boolean;
|
|
77
|
+
stopPropagation?: boolean;
|
|
78
|
+
eventType?: KeyEventType;
|
|
79
|
+
}
|
|
80
|
+
export interface KeyChordModifiers {
|
|
81
|
+
ctrlKey?: boolean;
|
|
82
|
+
shiftKey?: boolean;
|
|
83
|
+
altKey?: boolean;
|
|
84
|
+
metaKey?: boolean;
|
|
85
|
+
}
|
|
86
|
+
export interface KeyFilterOptions {
|
|
87
|
+
requireReset?: boolean;
|
|
88
|
+
ignoreInputs?: boolean;
|
|
89
|
+
}
|
|
90
|
+
export interface KeyOptions extends KeyLifecycleOptions, KeyEventModifiers, KeyChordModifiers, KeyFilterOptions {
|
|
91
|
+
}
|
|
92
|
+
export type UseKeyReturn = void;
|
|
93
|
+
export declare function useKey(key: string, func: (...args: any[]) => void, { enabled, preventDefault, stopPropagation, eventType, requireReset, ignoreInputs, ctrlKey, shiftKey, altKey, metaKey, target, }?: KeyOptions): UseKeyReturn;
|
|
63
94
|
export interface UseModalReturn<TData> {
|
|
64
95
|
id: string | null;
|
|
65
96
|
data: TData | undefined;
|
|
@@ -115,5 +146,16 @@ export interface UseSortReturn<T> {
|
|
|
115
146
|
getSortIndex: (id: string) => number | undefined;
|
|
116
147
|
}
|
|
117
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>;
|
|
118
160
|
|
|
119
161
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,46 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createPortal as
|
|
3
|
-
import { createStore as o, useSelector as s } from "@tanstack/react-store";
|
|
1
|
+
import { createContext as e, useCallback as t, useContext as n, useEffect as r, useMemo as i, useRef as a, useState as o } from "react";
|
|
2
|
+
import { createPortal as s } from "react-dom";
|
|
4
3
|
import { jsx as c } from "react/jsx-runtime";
|
|
5
|
-
//#region src/
|
|
6
|
-
var l =
|
|
7
|
-
id: null,
|
|
8
|
-
data: void 0
|
|
9
|
-
}, ({ setState: e }) => ({
|
|
10
|
-
open: (t, n) => e((e) => ({
|
|
11
|
-
...e,
|
|
12
|
-
id: t,
|
|
13
|
-
data: n
|
|
14
|
-
})),
|
|
15
|
-
close: () => e((e) => ({
|
|
16
|
-
...e,
|
|
17
|
-
id: null,
|
|
18
|
-
data: void 0
|
|
19
|
-
}))
|
|
20
|
-
}));
|
|
4
|
+
//#region src/context/ModalContext.tsx
|
|
5
|
+
var l = e(void 0);
|
|
21
6
|
//#endregion
|
|
22
7
|
//#region src/hooks/useModal.ts
|
|
23
8
|
function u() {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
openModal: l.actions.open,
|
|
28
|
-
closeModal: l.actions.close
|
|
29
|
-
};
|
|
9
|
+
let e = n(l);
|
|
10
|
+
if (!e) throw Error("useModal must be used within a ModalProvider");
|
|
11
|
+
return e;
|
|
30
12
|
}
|
|
31
13
|
//#endregion
|
|
32
14
|
//#region src/components/Modal.tsx
|
|
33
|
-
function d({ children: e, className:
|
|
34
|
-
let { id:
|
|
35
|
-
return
|
|
36
|
-
let e =
|
|
37
|
-
e && (
|
|
38
|
-
}, [
|
|
39
|
-
ref:
|
|
15
|
+
function d({ children: e, className: t }) {
|
|
16
|
+
let { id: n, closeModal: i } = u(), o = a(null);
|
|
17
|
+
return r(() => {
|
|
18
|
+
let e = o.current;
|
|
19
|
+
e && (n && !e.open ? e.showModal() : !n && e.open && e.close());
|
|
20
|
+
}, [n]), s(/* @__PURE__ */ c("dialog", {
|
|
21
|
+
ref: o,
|
|
40
22
|
onCancel: (e) => {
|
|
41
|
-
e.preventDefault(),
|
|
23
|
+
e.preventDefault(), i();
|
|
42
24
|
},
|
|
43
|
-
className: `universal-modal ${
|
|
25
|
+
className: `universal-modal ${t}`,
|
|
44
26
|
children: /* @__PURE__ */ c("div", {
|
|
45
27
|
className: "modal-container",
|
|
46
28
|
children: e
|
|
@@ -49,25 +31,25 @@ function d({ children: e, className: n }) {
|
|
|
49
31
|
}
|
|
50
32
|
//#endregion
|
|
51
33
|
//#region src/hooks/useDebounce.ts
|
|
52
|
-
function f(
|
|
53
|
-
let [s, c] =
|
|
54
|
-
|
|
55
|
-
f.current =
|
|
56
|
-
}, [
|
|
57
|
-
let _ =
|
|
34
|
+
function f(e, n, i = {}) {
|
|
35
|
+
let [s, c] = o(!1), l = i.maxWait, u = !!i.leading, d = !u && i.trailing === !1 ? !0 : i.trailing ?? !u, f = a(e), p = a(null), m = a(-1), h = a(-1), g = a(null);
|
|
36
|
+
r(() => {
|
|
37
|
+
f.current = e;
|
|
38
|
+
}, [e]);
|
|
39
|
+
let _ = t(() => {
|
|
58
40
|
p.current &&= (clearTimeout(p.current), null), m.current = -1, h.current = -1, g.current = null, c(!1);
|
|
59
|
-
}, []), v =
|
|
41
|
+
}, []), v = t(() => {
|
|
60
42
|
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
43
|
}, []);
|
|
62
|
-
return
|
|
44
|
+
return r(() => () => {
|
|
63
45
|
_();
|
|
64
46
|
}, [_]), {
|
|
65
|
-
debouncedFunc:
|
|
47
|
+
debouncedFunc: t((...e) => {
|
|
66
48
|
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
49
|
h.current !== m.current && d !== !1 && f.current(...e), m.current = -1, c(!1);
|
|
68
|
-
},
|
|
50
|
+
}, n));
|
|
69
51
|
}, [
|
|
70
|
-
|
|
52
|
+
n,
|
|
71
53
|
l,
|
|
72
54
|
d,
|
|
73
55
|
u
|
|
@@ -79,13 +61,13 @@ function f(n, a, o = {}) {
|
|
|
79
61
|
}
|
|
80
62
|
//#endregion
|
|
81
63
|
//#region src/hooks/useDebouncedValue.ts
|
|
82
|
-
function p(e,
|
|
83
|
-
let [
|
|
84
|
-
|
|
85
|
-
},
|
|
86
|
-
return
|
|
64
|
+
function p(e, t, n = {}) {
|
|
65
|
+
let [i, a] = o(e), { debouncedFunc: s } = f((e) => {
|
|
66
|
+
a(e);
|
|
67
|
+
}, t, n);
|
|
68
|
+
return r(() => {
|
|
87
69
|
s(e);
|
|
88
|
-
}, [e, s]),
|
|
70
|
+
}, [e, s]), i;
|
|
89
71
|
}
|
|
90
72
|
//#endregion
|
|
91
73
|
//#region src/utils/filterUtils.ts
|
|
@@ -147,42 +129,42 @@ function h(e, t, n) {
|
|
|
147
129
|
}
|
|
148
130
|
//#endregion
|
|
149
131
|
//#region src/hooks/useFilter.ts
|
|
150
|
-
function g(
|
|
151
|
-
let [
|
|
132
|
+
function g(e, n = []) {
|
|
133
|
+
let [r, a] = o(n), s = i(() => e.filter((e) => r.every((t) => {
|
|
152
134
|
let { id: n, field: r, type: i, operator: a, value: o, compare: s } = t, c = h(e, r, n);
|
|
153
135
|
if (i === "custom" || a === "custom") return s ? s(c, o, e) : !0;
|
|
154
136
|
let l = m[i];
|
|
155
137
|
if (!l) return !0;
|
|
156
138
|
let u = l[a];
|
|
157
139
|
return u ? u(c, o, t) : !0;
|
|
158
|
-
})), [
|
|
159
|
-
|
|
160
|
-
}, []), l =
|
|
161
|
-
|
|
162
|
-
}, []), u =
|
|
163
|
-
|
|
164
|
-
}, []), d =
|
|
165
|
-
|
|
166
|
-
}, [
|
|
167
|
-
|
|
168
|
-
}, []), p =
|
|
169
|
-
|
|
140
|
+
})), [e, r]), c = t((e) => {
|
|
141
|
+
a((t) => t.some((t) => t.id === e.id) ? t.map((t) => t.id === e.id ? e : t) : [...t, e]);
|
|
142
|
+
}, []), l = t((e) => {
|
|
143
|
+
a((t) => Array.isArray(e) ? t.filter((t) => !e.includes(t.id)) : t.filter((t) => t.id !== e));
|
|
144
|
+
}, []), u = t(() => {
|
|
145
|
+
a([]);
|
|
146
|
+
}, []), d = t(() => {
|
|
147
|
+
a(n);
|
|
148
|
+
}, [n]), f = t((e) => {
|
|
149
|
+
a(e);
|
|
150
|
+
}, []), p = t((e) => r.find((t) => t.id === e)?.value, [r]), g = t((e, t) => {
|
|
151
|
+
a((n) => n.map((n) => n.id === e ? {
|
|
170
152
|
...n,
|
|
171
153
|
value: t
|
|
172
154
|
} : n));
|
|
173
|
-
}, []), _ =
|
|
155
|
+
}, []), _ = t((e) => r.some((t) => t.id === e), [r]);
|
|
174
156
|
return {
|
|
175
157
|
filteredItems: s,
|
|
176
|
-
filters:
|
|
158
|
+
filters: r,
|
|
177
159
|
addFilter: c,
|
|
178
160
|
removeFilter: l,
|
|
179
161
|
clearFilters: u,
|
|
180
162
|
resetFilters: d,
|
|
181
|
-
toggleFilter:
|
|
182
|
-
|
|
163
|
+
toggleFilter: t((e) => {
|
|
164
|
+
a((t) => t.some((t) => t.id === e.id) ? t.filter((t) => t.id !== e.id) : [...t, e]);
|
|
183
165
|
}, []),
|
|
184
|
-
updateFilterConfig:
|
|
185
|
-
|
|
166
|
+
updateFilterConfig: t((e, t) => {
|
|
167
|
+
a((n) => n.map((n) => n.id === e ? {
|
|
186
168
|
...n,
|
|
187
169
|
...t
|
|
188
170
|
} : n));
|
|
@@ -194,74 +176,122 @@ function g(t, r = []) {
|
|
|
194
176
|
};
|
|
195
177
|
}
|
|
196
178
|
//#endregion
|
|
179
|
+
//#region src/constants/keyConstants.ts
|
|
180
|
+
var _ = [
|
|
181
|
+
"keydown",
|
|
182
|
+
"keyup",
|
|
183
|
+
"keypress"
|
|
184
|
+
];
|
|
185
|
+
//#endregion
|
|
186
|
+
//#region src/hooks/useKey.ts
|
|
187
|
+
function v(e, t, { enabled: n = !0, preventDefault: i = !0, stopPropagation: o = !0, eventType: s = "keydown", requireReset: c = !1, ignoreInputs: l = !0, ctrlKey: u = !1, shiftKey: d = !1, altKey: f = !1, metaKey: p = !1, target: m = window } = {}) {
|
|
188
|
+
let h = a(t), g = a(n), v = a(!1), y = m && "current" in m && m.current || window;
|
|
189
|
+
r(() => {
|
|
190
|
+
h.current = t;
|
|
191
|
+
}, [t]), r(() => {
|
|
192
|
+
g.current = n;
|
|
193
|
+
}, [n]), r(() => {
|
|
194
|
+
if (!y) return;
|
|
195
|
+
let t = _.includes(s) ? s : "keydown";
|
|
196
|
+
function n(t) {
|
|
197
|
+
if (!(t instanceof KeyboardEvent)) return;
|
|
198
|
+
let n = t.key.toLowerCase();
|
|
199
|
+
(n === e.toLowerCase() || u && n === "control" || d && n === "shift" || f && n === "alt" || p && n === "meta") && (v.current = !1);
|
|
200
|
+
}
|
|
201
|
+
function r(t) {
|
|
202
|
+
if (!(t instanceof KeyboardEvent) || !g.current) return;
|
|
203
|
+
let n = t.target;
|
|
204
|
+
if (!(l && n instanceof HTMLElement && (n instanceof HTMLInputElement || n instanceof HTMLTextAreaElement || n instanceof HTMLSelectElement || n.isContentEditable)) && t.ctrlKey === u && t.shiftKey === d && t.altKey === f && t.metaKey === p && e.toLowerCase() === t.key.toLowerCase()) {
|
|
205
|
+
if (c && v.current) return;
|
|
206
|
+
v.current = !0, h.current(t), i && t.preventDefault(), o && t.stopPropagation();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return y.addEventListener(t, r), y.addEventListener("keyup", n), () => {
|
|
210
|
+
y.removeEventListener(t, r), y.removeEventListener("keyup", n);
|
|
211
|
+
};
|
|
212
|
+
}, [
|
|
213
|
+
e,
|
|
214
|
+
i,
|
|
215
|
+
o,
|
|
216
|
+
s,
|
|
217
|
+
c,
|
|
218
|
+
l,
|
|
219
|
+
u,
|
|
220
|
+
d,
|
|
221
|
+
f,
|
|
222
|
+
p,
|
|
223
|
+
y
|
|
224
|
+
]);
|
|
225
|
+
}
|
|
226
|
+
//#endregion
|
|
197
227
|
//#region src/hooks/usePagination.ts
|
|
198
|
-
function
|
|
199
|
-
let [
|
|
228
|
+
function y(e, n, r = 0) {
|
|
229
|
+
let [a, s] = o(n), [c, l] = o(r), u = Math.max(1, Math.ceil(e.length / Math.max(1, a))), d = Math.min(Math.max(0, c), u - 1);
|
|
200
230
|
return c !== d && l(d), {
|
|
201
|
-
currentItems:
|
|
231
|
+
currentItems: i(() => typeof a != "number" || a < 1 ? [] : e.slice(c * a, (c + 1) * a), [
|
|
202
232
|
c,
|
|
203
|
-
|
|
204
|
-
|
|
233
|
+
a,
|
|
234
|
+
e
|
|
205
235
|
]),
|
|
206
|
-
pageSize:
|
|
236
|
+
pageSize: a,
|
|
207
237
|
pageIndex: c,
|
|
208
238
|
totalPages: u,
|
|
209
239
|
canPrevious: c > 0,
|
|
210
240
|
canNext: c < u - 1,
|
|
211
|
-
nextPage:
|
|
241
|
+
nextPage: t(() => {
|
|
212
242
|
l((e) => e < u - 1 ? e + 1 : e);
|
|
213
243
|
}, [u]),
|
|
214
|
-
previousPage:
|
|
244
|
+
previousPage: t(() => {
|
|
215
245
|
l((e) => e > 0 ? e - 1 : e);
|
|
216
246
|
}, []),
|
|
217
|
-
firstPage:
|
|
247
|
+
firstPage: t(() => {
|
|
218
248
|
l(0);
|
|
219
249
|
}, []),
|
|
220
|
-
lastPage:
|
|
250
|
+
lastPage: t(() => {
|
|
221
251
|
l(u - 1);
|
|
222
252
|
}, [u]),
|
|
223
|
-
setPageIndex:
|
|
253
|
+
setPageIndex: t((e) => {
|
|
224
254
|
typeof e == "number" && e >= 0 && e <= u - 1 && l(e);
|
|
225
255
|
}, [u]),
|
|
226
|
-
setPageSize:
|
|
227
|
-
if (typeof
|
|
228
|
-
s(
|
|
229
|
-
let n = Math.max(1, Math.ceil(
|
|
256
|
+
setPageSize: t((t) => {
|
|
257
|
+
if (typeof t == "number" && t > 0) {
|
|
258
|
+
s(t);
|
|
259
|
+
let n = Math.max(1, Math.ceil(e.length / Math.max(1, t)));
|
|
230
260
|
l((e) => Math.min(Math.max(0, e), n - 1));
|
|
231
261
|
}
|
|
232
|
-
}, [
|
|
233
|
-
resetPageIndex:
|
|
234
|
-
l(Math.min(Math.max(0,
|
|
235
|
-
}, [
|
|
236
|
-
resetPageSize:
|
|
237
|
-
s(Math.max(1,
|
|
238
|
-
let
|
|
239
|
-
l((
|
|
240
|
-
}, [
|
|
262
|
+
}, [e.length]),
|
|
263
|
+
resetPageIndex: t(() => {
|
|
264
|
+
l(Math.min(Math.max(0, r), u - 1));
|
|
265
|
+
}, [r, u]),
|
|
266
|
+
resetPageSize: t(() => {
|
|
267
|
+
s(Math.max(1, n));
|
|
268
|
+
let t = Math.max(1, Math.ceil(e.length / Math.max(1, n)));
|
|
269
|
+
l((e) => Math.min(Math.max(0, e), t - 1));
|
|
270
|
+
}, [n, e.length])
|
|
241
271
|
};
|
|
242
272
|
}
|
|
243
273
|
//#endregion
|
|
244
274
|
//#region src/utils/sortUtils.ts
|
|
245
|
-
function
|
|
246
|
-
let { sortUndefined: r, desc: i = !1 } = n, a =
|
|
275
|
+
function b(e, t, n = {}) {
|
|
276
|
+
let { sortUndefined: r, desc: i = !1 } = n, a = D(e, t, r, i);
|
|
247
277
|
if (a !== null) return a;
|
|
248
278
|
let o = !!e;
|
|
249
279
|
return o === !!t ? 0 : o ? 1 : -1;
|
|
250
280
|
}
|
|
251
|
-
function
|
|
252
|
-
let { sortUndefined: r, desc: i = !1 } = n, a =
|
|
281
|
+
function x(e, t, n = {}) {
|
|
282
|
+
let { sortUndefined: r, desc: i = !1 } = n, a = D(e, t, r, i);
|
|
253
283
|
if (a !== null) return a;
|
|
254
284
|
let o = e === "" || e === null ? NaN : Number(e), s = t === "" || t === null ? NaN : Number(t), c = Number.isNaN(o), l = Number.isNaN(s);
|
|
255
|
-
return c || l ?
|
|
285
|
+
return c || l ? D(c ? void 0 : o, l ? void 0 : s, r, i) ?? 0 : o - s;
|
|
256
286
|
}
|
|
257
|
-
function
|
|
258
|
-
let { desc: r = !1, sortUndefined: i, caseSensitive: a = !1 } = n, o =
|
|
287
|
+
function S(e, t, n = {}) {
|
|
288
|
+
let { desc: r = !1, sortUndefined: i, caseSensitive: a = !1 } = n, o = D(e, t, i, r);
|
|
259
289
|
if (o !== null) return o;
|
|
260
290
|
let s = String(e), c = String(t);
|
|
261
291
|
return s.localeCompare(c, void 0, { sensitivity: a ? "variant" : "base" });
|
|
262
292
|
}
|
|
263
|
-
function
|
|
264
|
-
let { desc: r = !1, sortUndefined: i, caseSensitive: a = !1 } = n, o =
|
|
293
|
+
function C(e, t, n = {}) {
|
|
294
|
+
let { desc: r = !1, sortUndefined: i, caseSensitive: a = !1 } = n, o = D(e, t, i, r);
|
|
265
295
|
if (o !== null) return o;
|
|
266
296
|
let s = String(e), c = String(t);
|
|
267
297
|
return s.localeCompare(c, void 0, {
|
|
@@ -269,21 +299,21 @@ function x(e, t, n = {}) {
|
|
|
269
299
|
sensitivity: a ? "variant" : "base"
|
|
270
300
|
});
|
|
271
301
|
}
|
|
272
|
-
function
|
|
273
|
-
let { desc: r = !1, sortUndefined: i } = n, a =
|
|
302
|
+
function w(e, t, n = {}) {
|
|
303
|
+
let { desc: r = !1, sortUndefined: i } = n, a = D(e, t, i, r);
|
|
274
304
|
if (a !== null) return a;
|
|
275
305
|
let o = new Date(e).getTime(), s = new Date(t).getTime();
|
|
276
306
|
return (Number.isNaN(o) ? 0 : o) - (Number.isNaN(s) ? 0 : s);
|
|
277
307
|
}
|
|
278
|
-
function
|
|
279
|
-
let { desc: r = !1, sortUndefined: i } = n, a =
|
|
308
|
+
function T(e, t, n = {}) {
|
|
309
|
+
let { desc: r = !1, sortUndefined: i } = n, a = D(e, t, i, r);
|
|
280
310
|
return a === null ? e < t ? -1 : +(e > t) : a;
|
|
281
311
|
}
|
|
282
|
-
function
|
|
283
|
-
let { desc: r = !1, sortUndefined: i, compare: a } = n, o =
|
|
312
|
+
function E(e, t, n = {}) {
|
|
313
|
+
let { desc: r = !1, sortUndefined: i, compare: a } = n, o = D(e, t, i, r);
|
|
284
314
|
return o === null ? typeof a == "function" ? a(e, t) : e < t ? -1 : +(e > t) : o;
|
|
285
315
|
}
|
|
286
|
-
function
|
|
316
|
+
function D(e, t, n = !1, r) {
|
|
287
317
|
let i = e ?? void 0, a = t ?? void 0;
|
|
288
318
|
if (i !== void 0 && a !== void 0) return null;
|
|
289
319
|
if (i === void 0 && a === void 0 || n === !1) return 0;
|
|
@@ -301,55 +331,55 @@ function T(e, t, n = !1, r) {
|
|
|
301
331
|
}
|
|
302
332
|
//#endregion
|
|
303
333
|
//#region src/hooks/useSort.ts
|
|
304
|
-
function
|
|
305
|
-
let [
|
|
334
|
+
function O(e, n = []) {
|
|
335
|
+
let [r, a] = o(n);
|
|
306
336
|
return {
|
|
307
|
-
sortedItems:
|
|
308
|
-
for (let n of
|
|
337
|
+
sortedItems: i(() => e.toSorted((e, t) => {
|
|
338
|
+
for (let n of r) {
|
|
309
339
|
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;
|
|
310
340
|
switch (r) {
|
|
311
341
|
case "numeric":
|
|
312
|
-
u =
|
|
342
|
+
u = x(c, l, n);
|
|
313
343
|
break;
|
|
314
344
|
case "alphabetical":
|
|
315
|
-
u =
|
|
345
|
+
u = S(c, l, n);
|
|
316
346
|
break;
|
|
317
347
|
case "alphanumeric":
|
|
318
|
-
u =
|
|
348
|
+
u = C(c, l, n);
|
|
319
349
|
break;
|
|
320
350
|
case "boolean":
|
|
321
|
-
u =
|
|
351
|
+
u = b(c, l, n);
|
|
322
352
|
break;
|
|
323
353
|
case "date":
|
|
324
|
-
u =
|
|
354
|
+
u = w(c, l, n);
|
|
325
355
|
break;
|
|
326
356
|
case "basic":
|
|
327
|
-
u =
|
|
357
|
+
u = T(c, l, n);
|
|
328
358
|
break;
|
|
329
359
|
case "custom":
|
|
330
|
-
u =
|
|
360
|
+
u = E(c, l, n);
|
|
331
361
|
break;
|
|
332
|
-
default: u =
|
|
362
|
+
default: u = T(c, l, n);
|
|
333
363
|
}
|
|
334
364
|
if (u !== 0) return u * (o === s ? 1 : -1);
|
|
335
365
|
}
|
|
336
366
|
return 0;
|
|
337
|
-
}), [
|
|
338
|
-
sorts:
|
|
339
|
-
addSort:
|
|
340
|
-
removeSort:
|
|
341
|
-
|
|
367
|
+
}), [r, e]),
|
|
368
|
+
sorts: r,
|
|
369
|
+
addSort: t((e) => a((t) => t.some((t) => t.id === e.id) ? t.map((t) => t.id === e.id ? e : t) : [...t, e]), []),
|
|
370
|
+
removeSort: t((e) => {
|
|
371
|
+
a((t) => Array.isArray(e) ? t.filter((t) => !e.includes(t.id)) : t.filter((t) => t.id !== e));
|
|
342
372
|
}, []),
|
|
343
|
-
clearSorts:
|
|
344
|
-
resetSorts:
|
|
345
|
-
setSorts:
|
|
346
|
-
getSortDirection:
|
|
347
|
-
let t =
|
|
373
|
+
clearSorts: t(() => a([]), []),
|
|
374
|
+
resetSorts: t(() => a(n), [n]),
|
|
375
|
+
setSorts: t((e) => a(e), []),
|
|
376
|
+
getSortDirection: t((e) => {
|
|
377
|
+
let t = r.find((t) => t.id === e);
|
|
348
378
|
return t ? t.desc ? "desc" : "asc" : void 0;
|
|
349
|
-
}, [
|
|
350
|
-
toggleSort:
|
|
379
|
+
}, [r]),
|
|
380
|
+
toggleSort: t((e, t, n) => {
|
|
351
381
|
let { multi: r = !1, ...i } = n || {};
|
|
352
|
-
|
|
382
|
+
a((n) => {
|
|
353
383
|
let a = n.find((t) => t.id === e);
|
|
354
384
|
if (!a) {
|
|
355
385
|
let a = {
|
|
@@ -379,15 +409,50 @@ function E(t, r = []) {
|
|
|
379
409
|
return r ? n.filter((t) => t.id !== e) : [];
|
|
380
410
|
});
|
|
381
411
|
}, []),
|
|
382
|
-
getNextSortingOrder:
|
|
383
|
-
let t =
|
|
412
|
+
getNextSortingOrder: t((e) => {
|
|
413
|
+
let t = r.find((t) => t.id === e);
|
|
384
414
|
return t ? t.desc ? t.disableSortRemoval ? "asc" : "none" : "desc" : "asc";
|
|
385
|
-
}, [
|
|
386
|
-
getSortIndex:
|
|
387
|
-
let t =
|
|
415
|
+
}, [r]),
|
|
416
|
+
getSortIndex: t((e) => {
|
|
417
|
+
let t = r.findIndex((t) => t.id === e);
|
|
388
418
|
return t === -1 ? void 0 : t + 1;
|
|
389
|
-
}, [
|
|
419
|
+
}, [r])
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
//#endregion
|
|
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
|
|
390
455
|
};
|
|
391
456
|
}
|
|
392
457
|
//#endregion
|
|
393
|
-
export { d as ModalLayout, f as useDebounce, p as useDebouncedValue, g as useFilter, u as useModal,
|
|
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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@himanshu-sorathiya/react-kit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "An opinionated collection of react hooks, and reusable UI components.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -22,9 +22,6 @@
|
|
|
22
22
|
"preview": "vite preview",
|
|
23
23
|
"format": "prettier . --write"
|
|
24
24
|
},
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"@tanstack/react-store": "^0.11.0"
|
|
27
|
-
},
|
|
28
25
|
"peerDependencies": {
|
|
29
26
|
"react": "^17.0.0 || ^18.0.0 || ^19.0.0",
|
|
30
27
|
"react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
@@ -55,7 +52,9 @@
|
|
|
55
52
|
"react-kit",
|
|
56
53
|
"hooks",
|
|
57
54
|
"components",
|
|
58
|
-
"state-management"
|
|
55
|
+
"state-management",
|
|
56
|
+
"utility",
|
|
57
|
+
"utilities"
|
|
59
58
|
],
|
|
60
59
|
"homepage": "https://github.com/Himanshu-Sorathiya-Simform/react-kit#readme",
|
|
61
60
|
"bugs": {
|