@himanshu-sorathiya/react-kit 1.0.6 → 1.0.8
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 +50 -0
- package/dist/index.js +203 -133
- package/package.json +4 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
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;
|
|
6
8
|
}
|
|
7
9
|
export declare function ModalLayout({ children, className }: ModalLayoutProps): import("react").ReactPortal;
|
|
10
|
+
export interface DebounceOptions {
|
|
11
|
+
maxWait?: number;
|
|
12
|
+
leading?: boolean;
|
|
13
|
+
trailing?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface UseDebounceReturn<Args extends any[]> {
|
|
16
|
+
debouncedFunc: (...args: Args) => void;
|
|
17
|
+
cancel: () => void;
|
|
18
|
+
flush: () => void;
|
|
19
|
+
isPending: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare function useDebounce<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: DebounceOptions): UseDebounceReturn<Args>;
|
|
22
|
+
export interface DebouncedValueOptions {
|
|
23
|
+
maxWait?: number;
|
|
24
|
+
leading?: boolean;
|
|
25
|
+
trailing?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export type UseDebouncedValueReturn<T> = T;
|
|
28
|
+
export declare function useDebouncedValue<T>(value: T, delay: number, options?: DebouncedValueOptions): UseDebouncedValueReturn<T>;
|
|
8
29
|
export type FilterType = "text" | "number" | "boolean" | "date" | "select" | "multiselect" | "custom";
|
|
9
30
|
export type TextOperator = "contains" | "equals" | "startsWith" | "endsWith" | "notContains";
|
|
10
31
|
export type NumberOperator = "equals" | "greaterThan" | "lessThan" | "greaterThanOrEqual" | "lessThanOrEqual" | "between";
|
|
@@ -41,6 +62,35 @@ export interface UseFilterReturn<T> {
|
|
|
41
62
|
isFilterActive: (id: string) => boolean;
|
|
42
63
|
}
|
|
43
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;
|
|
44
94
|
export interface UseModalReturn<TData> {
|
|
45
95
|
id: string | null;
|
|
46
96
|
data: TData | undefined;
|
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
|
|
@@ -48,8 +30,48 @@ function d({ children: e, className: n }) {
|
|
|
48
30
|
}), document.body);
|
|
49
31
|
}
|
|
50
32
|
//#endregion
|
|
33
|
+
//#region src/hooks/useDebounce.ts
|
|
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(() => {
|
|
40
|
+
p.current &&= (clearTimeout(p.current), null), m.current = -1, h.current = -1, g.current = null, c(!1);
|
|
41
|
+
}, []), v = t(() => {
|
|
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));
|
|
43
|
+
}, []);
|
|
44
|
+
return r(() => () => {
|
|
45
|
+
_();
|
|
46
|
+
}, [_]), {
|
|
47
|
+
debouncedFunc: t((...e) => {
|
|
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(() => {
|
|
49
|
+
h.current !== m.current && d !== !1 && f.current(...e), m.current = -1, c(!1);
|
|
50
|
+
}, n));
|
|
51
|
+
}, [
|
|
52
|
+
n,
|
|
53
|
+
l,
|
|
54
|
+
d,
|
|
55
|
+
u
|
|
56
|
+
]),
|
|
57
|
+
cancel: _,
|
|
58
|
+
flush: v,
|
|
59
|
+
isPending: s
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/hooks/useDebouncedValue.ts
|
|
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(() => {
|
|
69
|
+
s(e);
|
|
70
|
+
}, [e, s]), i;
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
51
73
|
//#region src/utils/filterUtils.ts
|
|
52
|
-
var
|
|
74
|
+
var m = {
|
|
53
75
|
text: {
|
|
54
76
|
contains: (e, t, n) => n?.caseSensitive ? String(e).includes(String(t)) : String(e).toLowerCase().includes(String(t).toLowerCase()),
|
|
55
77
|
equals: (e, t, n) => n?.caseSensitive ? String(e) === String(t) : String(e).toLowerCase() === String(t).toLowerCase(),
|
|
@@ -100,128 +122,176 @@ var f = {
|
|
|
100
122
|
};
|
|
101
123
|
//#endregion
|
|
102
124
|
//#region src/utils/utils.ts
|
|
103
|
-
function
|
|
125
|
+
function h(e, t, n) {
|
|
104
126
|
if (typeof e != "object" || !e) return e;
|
|
105
127
|
let r = (t || n) ?? "";
|
|
106
128
|
return r.includes(".") ? r.split(".").reduce((e, t) => e?.[t], e) : e?.[r];
|
|
107
129
|
}
|
|
108
130
|
//#endregion
|
|
109
131
|
//#region src/hooks/useFilter.ts
|
|
110
|
-
function
|
|
111
|
-
let [
|
|
112
|
-
let { id: n, field: r, type: i, operator: a, value: o, compare: s } = t, c =
|
|
132
|
+
function g(e, n = []) {
|
|
133
|
+
let [r, a] = o(n), s = i(() => e.filter((e) => r.every((t) => {
|
|
134
|
+
let { id: n, field: r, type: i, operator: a, value: o, compare: s } = t, c = h(e, r, n);
|
|
113
135
|
if (i === "custom" || a === "custom") return s ? s(c, o, e) : !0;
|
|
114
|
-
let l =
|
|
136
|
+
let l = m[i];
|
|
115
137
|
if (!l) return !0;
|
|
116
138
|
let u = l[a];
|
|
117
139
|
return u ? u(c, o, t) : !0;
|
|
118
|
-
})), [
|
|
119
|
-
|
|
120
|
-
}, []), l =
|
|
121
|
-
|
|
122
|
-
}, []), u =
|
|
123
|
-
|
|
124
|
-
}, []), d =
|
|
125
|
-
|
|
126
|
-
}, [
|
|
127
|
-
|
|
128
|
-
}, []),
|
|
129
|
-
|
|
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 ? {
|
|
130
152
|
...n,
|
|
131
153
|
value: t
|
|
132
154
|
} : n));
|
|
133
|
-
}, []), _ =
|
|
155
|
+
}, []), _ = t((e) => r.some((t) => t.id === e), [r]);
|
|
134
156
|
return {
|
|
135
157
|
filteredItems: s,
|
|
136
|
-
filters:
|
|
158
|
+
filters: r,
|
|
137
159
|
addFilter: c,
|
|
138
160
|
removeFilter: l,
|
|
139
161
|
clearFilters: u,
|
|
140
162
|
resetFilters: d,
|
|
141
|
-
toggleFilter:
|
|
142
|
-
|
|
163
|
+
toggleFilter: t((e) => {
|
|
164
|
+
a((t) => t.some((t) => t.id === e.id) ? t.filter((t) => t.id !== e.id) : [...t, e]);
|
|
143
165
|
}, []),
|
|
144
|
-
updateFilterConfig:
|
|
145
|
-
|
|
166
|
+
updateFilterConfig: t((e, t) => {
|
|
167
|
+
a((n) => n.map((n) => n.id === e ? {
|
|
146
168
|
...n,
|
|
147
169
|
...t
|
|
148
170
|
} : n));
|
|
149
171
|
}, []),
|
|
150
|
-
setFilters:
|
|
151
|
-
getFilterValue:
|
|
172
|
+
setFilters: f,
|
|
173
|
+
getFilterValue: p,
|
|
152
174
|
updateFilterValue: g,
|
|
153
175
|
isFilterActive: _
|
|
154
176
|
};
|
|
155
177
|
}
|
|
156
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
|
|
157
227
|
//#region src/hooks/usePagination.ts
|
|
158
|
-
function
|
|
159
|
-
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);
|
|
160
230
|
return c !== d && l(d), {
|
|
161
|
-
currentItems:
|
|
231
|
+
currentItems: i(() => typeof a != "number" || a < 1 ? [] : e.slice(c * a, (c + 1) * a), [
|
|
162
232
|
c,
|
|
163
|
-
|
|
164
|
-
|
|
233
|
+
a,
|
|
234
|
+
e
|
|
165
235
|
]),
|
|
166
|
-
pageSize:
|
|
236
|
+
pageSize: a,
|
|
167
237
|
pageIndex: c,
|
|
168
238
|
totalPages: u,
|
|
169
239
|
canPrevious: c > 0,
|
|
170
240
|
canNext: c < u - 1,
|
|
171
|
-
nextPage:
|
|
241
|
+
nextPage: t(() => {
|
|
172
242
|
l((e) => e < u - 1 ? e + 1 : e);
|
|
173
243
|
}, [u]),
|
|
174
|
-
previousPage:
|
|
244
|
+
previousPage: t(() => {
|
|
175
245
|
l((e) => e > 0 ? e - 1 : e);
|
|
176
246
|
}, []),
|
|
177
|
-
firstPage:
|
|
247
|
+
firstPage: t(() => {
|
|
178
248
|
l(0);
|
|
179
249
|
}, []),
|
|
180
|
-
lastPage:
|
|
250
|
+
lastPage: t(() => {
|
|
181
251
|
l(u - 1);
|
|
182
252
|
}, [u]),
|
|
183
|
-
setPageIndex:
|
|
253
|
+
setPageIndex: t((e) => {
|
|
184
254
|
typeof e == "number" && e >= 0 && e <= u - 1 && l(e);
|
|
185
255
|
}, [u]),
|
|
186
|
-
setPageSize:
|
|
187
|
-
if (typeof
|
|
188
|
-
s(
|
|
189
|
-
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)));
|
|
190
260
|
l((e) => Math.min(Math.max(0, e), n - 1));
|
|
191
261
|
}
|
|
192
|
-
}, [
|
|
193
|
-
resetPageIndex:
|
|
194
|
-
l(Math.min(Math.max(0,
|
|
195
|
-
}, [
|
|
196
|
-
resetPageSize:
|
|
197
|
-
s(Math.max(1,
|
|
198
|
-
let
|
|
199
|
-
l((
|
|
200
|
-
}, [
|
|
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])
|
|
201
271
|
};
|
|
202
272
|
}
|
|
203
273
|
//#endregion
|
|
204
274
|
//#region src/utils/sortUtils.ts
|
|
205
|
-
function
|
|
206
|
-
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);
|
|
207
277
|
if (a !== null) return a;
|
|
208
278
|
let o = !!e;
|
|
209
279
|
return o === !!t ? 0 : o ? 1 : -1;
|
|
210
280
|
}
|
|
211
|
-
function
|
|
212
|
-
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);
|
|
213
283
|
if (a !== null) return a;
|
|
214
284
|
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 ?
|
|
285
|
+
return c || l ? D(c ? void 0 : o, l ? void 0 : s, r, i) ?? 0 : o - s;
|
|
216
286
|
}
|
|
217
|
-
function
|
|
218
|
-
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);
|
|
219
289
|
if (o !== null) return o;
|
|
220
290
|
let s = String(e), c = String(t);
|
|
221
291
|
return s.localeCompare(c, void 0, { sensitivity: a ? "variant" : "base" });
|
|
222
292
|
}
|
|
223
|
-
function
|
|
224
|
-
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);
|
|
225
295
|
if (o !== null) return o;
|
|
226
296
|
let s = String(e), c = String(t);
|
|
227
297
|
return s.localeCompare(c, void 0, {
|
|
@@ -229,21 +299,21 @@ function y(e, t, n = {}) {
|
|
|
229
299
|
sensitivity: a ? "variant" : "base"
|
|
230
300
|
});
|
|
231
301
|
}
|
|
232
|
-
function
|
|
233
|
-
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);
|
|
234
304
|
if (a !== null) return a;
|
|
235
305
|
let o = new Date(e).getTime(), s = new Date(t).getTime();
|
|
236
306
|
return (Number.isNaN(o) ? 0 : o) - (Number.isNaN(s) ? 0 : s);
|
|
237
307
|
}
|
|
238
|
-
function
|
|
239
|
-
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);
|
|
240
310
|
return a === null ? e < t ? -1 : +(e > t) : a;
|
|
241
311
|
}
|
|
242
|
-
function
|
|
243
|
-
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);
|
|
244
314
|
return o === null ? typeof a == "function" ? a(e, t) : e < t ? -1 : +(e > t) : o;
|
|
245
315
|
}
|
|
246
|
-
function
|
|
316
|
+
function D(e, t, n = !1, r) {
|
|
247
317
|
let i = e ?? void 0, a = t ?? void 0;
|
|
248
318
|
if (i !== void 0 && a !== void 0) return null;
|
|
249
319
|
if (i === void 0 && a === void 0 || n === !1) return 0;
|
|
@@ -261,55 +331,55 @@ function C(e, t, n = !1, r) {
|
|
|
261
331
|
}
|
|
262
332
|
//#endregion
|
|
263
333
|
//#region src/hooks/useSort.ts
|
|
264
|
-
function
|
|
265
|
-
let [
|
|
334
|
+
function O(e, n = []) {
|
|
335
|
+
let [r, a] = o(n);
|
|
266
336
|
return {
|
|
267
|
-
sortedItems:
|
|
268
|
-
for (let n of
|
|
269
|
-
let { type: r, field: i, id: a, desc: o = !1, invertSorting: s = !1 } = n, c =
|
|
337
|
+
sortedItems: i(() => e.toSorted((e, t) => {
|
|
338
|
+
for (let n of r) {
|
|
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;
|
|
270
340
|
switch (r) {
|
|
271
341
|
case "numeric":
|
|
272
|
-
u =
|
|
342
|
+
u = x(c, l, n);
|
|
273
343
|
break;
|
|
274
344
|
case "alphabetical":
|
|
275
|
-
u =
|
|
345
|
+
u = S(c, l, n);
|
|
276
346
|
break;
|
|
277
347
|
case "alphanumeric":
|
|
278
|
-
u =
|
|
348
|
+
u = C(c, l, n);
|
|
279
349
|
break;
|
|
280
350
|
case "boolean":
|
|
281
|
-
u =
|
|
351
|
+
u = b(c, l, n);
|
|
282
352
|
break;
|
|
283
353
|
case "date":
|
|
284
|
-
u =
|
|
354
|
+
u = w(c, l, n);
|
|
285
355
|
break;
|
|
286
356
|
case "basic":
|
|
287
|
-
u =
|
|
357
|
+
u = T(c, l, n);
|
|
288
358
|
break;
|
|
289
359
|
case "custom":
|
|
290
|
-
u =
|
|
360
|
+
u = E(c, l, n);
|
|
291
361
|
break;
|
|
292
|
-
default: u =
|
|
362
|
+
default: u = T(c, l, n);
|
|
293
363
|
}
|
|
294
364
|
if (u !== 0) return u * (o === s ? 1 : -1);
|
|
295
365
|
}
|
|
296
366
|
return 0;
|
|
297
|
-
}), [
|
|
298
|
-
sorts:
|
|
299
|
-
addSort:
|
|
300
|
-
removeSort:
|
|
301
|
-
|
|
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));
|
|
302
372
|
}, []),
|
|
303
|
-
clearSorts:
|
|
304
|
-
resetSorts:
|
|
305
|
-
setSorts:
|
|
306
|
-
getSortDirection:
|
|
307
|
-
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);
|
|
308
378
|
return t ? t.desc ? "desc" : "asc" : void 0;
|
|
309
|
-
}, [
|
|
310
|
-
toggleSort:
|
|
379
|
+
}, [r]),
|
|
380
|
+
toggleSort: t((e, t, n) => {
|
|
311
381
|
let { multi: r = !1, ...i } = n || {};
|
|
312
|
-
|
|
382
|
+
a((n) => {
|
|
313
383
|
let a = n.find((t) => t.id === e);
|
|
314
384
|
if (!a) {
|
|
315
385
|
let a = {
|
|
@@ -339,15 +409,15 @@ function w(t, r = []) {
|
|
|
339
409
|
return r ? n.filter((t) => t.id !== e) : [];
|
|
340
410
|
});
|
|
341
411
|
}, []),
|
|
342
|
-
getNextSortingOrder:
|
|
343
|
-
let t =
|
|
412
|
+
getNextSortingOrder: t((e) => {
|
|
413
|
+
let t = r.find((t) => t.id === e);
|
|
344
414
|
return t ? t.desc ? t.disableSortRemoval ? "asc" : "none" : "desc" : "asc";
|
|
345
|
-
}, [
|
|
346
|
-
getSortIndex:
|
|
347
|
-
let t =
|
|
415
|
+
}, [r]),
|
|
416
|
+
getSortIndex: t((e) => {
|
|
417
|
+
let t = r.findIndex((t) => t.id === e);
|
|
348
418
|
return t === -1 ? void 0 : t + 1;
|
|
349
|
-
}, [
|
|
419
|
+
}, [r])
|
|
350
420
|
};
|
|
351
421
|
}
|
|
352
422
|
//#endregion
|
|
353
|
-
export { d as ModalLayout,
|
|
423
|
+
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 };
|
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.8",
|
|
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": {
|