@himanshu-sorathiya/react-kit 1.0.1 → 1.0.2
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 +33 -0
- package/dist/index.js +69 -22
- package/package.json +3 -2
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
+
|
|
3
|
+
export interface ModalProps {
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
className?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function Modal({ children, className }: ModalProps): import("react").ReactPortal;
|
|
8
|
+
export interface UseModalReturn<TData> {
|
|
9
|
+
id: string | null;
|
|
10
|
+
data: TData | undefined;
|
|
11
|
+
openModal: <T = any>(id: string, data?: T) => void;
|
|
12
|
+
closeModal: () => void;
|
|
13
|
+
}
|
|
14
|
+
export declare function useModal<TData = any>(): UseModalReturn<TData>;
|
|
15
|
+
export interface UsePaginationReturn<T> {
|
|
16
|
+
currentItems: T[];
|
|
17
|
+
pageSize: number;
|
|
18
|
+
pageIndex: number;
|
|
19
|
+
totalPages: number;
|
|
20
|
+
canPrevious: boolean;
|
|
21
|
+
canNext: boolean;
|
|
22
|
+
nextPage: () => void;
|
|
23
|
+
previousPage: () => void;
|
|
24
|
+
firstPage: () => void;
|
|
25
|
+
lastPage: () => void;
|
|
26
|
+
setPageIndex: (newPageIndex: number) => void;
|
|
27
|
+
setPageSize: (newPageSize: number) => void;
|
|
28
|
+
resetPageIndex: () => void;
|
|
29
|
+
resetPageSize: () => void;
|
|
30
|
+
}
|
|
31
|
+
export declare function usePagination<T>(data: T[], initialPageSize: number, initialPageIndex?: number): UsePaginationReturn<T>;
|
|
32
|
+
|
|
33
|
+
export { };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createPortal as
|
|
3
|
-
import { createStore as
|
|
4
|
-
import { jsx as
|
|
1
|
+
import { useCallback as e, useEffect as t, useMemo as n, useRef as r, useState as i } from "react";
|
|
2
|
+
import { createPortal as a } from "react-dom";
|
|
3
|
+
import { createStore as o, useSelector as s } from "@tanstack/react-store";
|
|
4
|
+
import { jsx as c } from "react/jsx-runtime";
|
|
5
5
|
//#region src/store/modalStore.ts
|
|
6
|
-
var
|
|
6
|
+
var l = o({
|
|
7
7
|
id: null,
|
|
8
8
|
data: void 0
|
|
9
9
|
}, ({ setState: e }) => ({
|
|
@@ -20,32 +20,79 @@ var o = r({
|
|
|
20
20
|
}));
|
|
21
21
|
//#endregion
|
|
22
22
|
//#region src/hooks/useModal.ts
|
|
23
|
-
function
|
|
23
|
+
function u() {
|
|
24
24
|
return {
|
|
25
|
-
id:
|
|
26
|
-
data:
|
|
27
|
-
openModal:
|
|
28
|
-
closeModal:
|
|
25
|
+
id: s(l, (e) => e.id),
|
|
26
|
+
data: s(l, (e) => e.data),
|
|
27
|
+
openModal: l.actions.open,
|
|
28
|
+
closeModal: l.actions.close
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
//#endregion
|
|
32
32
|
//#region src/components/Modal.tsx
|
|
33
|
-
function
|
|
34
|
-
let { id:
|
|
35
|
-
return
|
|
36
|
-
let e =
|
|
37
|
-
e && (
|
|
38
|
-
}, [
|
|
39
|
-
ref:
|
|
33
|
+
function d({ children: e, className: n = "" }) {
|
|
34
|
+
let { id: i, closeModal: o } = u(), s = r(null);
|
|
35
|
+
return t(() => {
|
|
36
|
+
let e = s.current;
|
|
37
|
+
e && (i && !e.open ? e.showModal() : !i && e.open && e.close());
|
|
38
|
+
}, [i]), a(/* @__PURE__ */ c("dialog", {
|
|
39
|
+
ref: s,
|
|
40
40
|
onCancel: (e) => {
|
|
41
|
-
e.preventDefault(),
|
|
41
|
+
e.preventDefault(), o();
|
|
42
42
|
},
|
|
43
|
-
className: `universal-modal ${
|
|
44
|
-
children: /* @__PURE__ */
|
|
43
|
+
className: `universal-modal ${n ?? ""}`,
|
|
44
|
+
children: /* @__PURE__ */ c("div", {
|
|
45
45
|
className: "modal-container",
|
|
46
|
-
children:
|
|
46
|
+
children: e
|
|
47
47
|
})
|
|
48
48
|
}), document.body);
|
|
49
49
|
}
|
|
50
50
|
//#endregion
|
|
51
|
-
|
|
51
|
+
//#region src/hooks/usePagination.ts
|
|
52
|
+
function f(t, r, a = 0) {
|
|
53
|
+
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);
|
|
54
|
+
return c !== d && l(d), {
|
|
55
|
+
currentItems: n(() => typeof o != "number" || o < 1 ? [] : t.slice(c * o, (c + 1) * o), [
|
|
56
|
+
c,
|
|
57
|
+
o,
|
|
58
|
+
t
|
|
59
|
+
]),
|
|
60
|
+
pageSize: o,
|
|
61
|
+
pageIndex: c,
|
|
62
|
+
totalPages: u,
|
|
63
|
+
canPrevious: c > 0,
|
|
64
|
+
canNext: c < u - 1,
|
|
65
|
+
nextPage: e(() => {
|
|
66
|
+
l((e) => e < u - 1 ? e + 1 : e);
|
|
67
|
+
}, [u]),
|
|
68
|
+
previousPage: e(() => {
|
|
69
|
+
l((e) => e > 0 ? e - 1 : e);
|
|
70
|
+
}, []),
|
|
71
|
+
firstPage: e(() => {
|
|
72
|
+
l(0);
|
|
73
|
+
}, []),
|
|
74
|
+
lastPage: e(() => {
|
|
75
|
+
l(u - 1);
|
|
76
|
+
}, [u]),
|
|
77
|
+
setPageIndex: e((e) => {
|
|
78
|
+
typeof e == "number" && e >= 0 && e <= u - 1 && l(e);
|
|
79
|
+
}, [u]),
|
|
80
|
+
setPageSize: e((e) => {
|
|
81
|
+
if (typeof e == "number" && e > 0) {
|
|
82
|
+
s(e);
|
|
83
|
+
let n = Math.max(1, Math.ceil(t.length / Math.max(1, e)));
|
|
84
|
+
l((e) => Math.min(Math.max(0, e), n - 1));
|
|
85
|
+
}
|
|
86
|
+
}, [t.length]),
|
|
87
|
+
resetPageIndex: e(() => {
|
|
88
|
+
l(Math.min(Math.max(0, a), u - 1));
|
|
89
|
+
}, [a, u]),
|
|
90
|
+
resetPageSize: e(() => {
|
|
91
|
+
s(Math.max(1, r));
|
|
92
|
+
let e = Math.max(1, Math.ceil(t.length / Math.max(1, r)));
|
|
93
|
+
l((t) => Math.min(Math.max(0, t), e - 1));
|
|
94
|
+
}, [r, t.length])
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
export { d as Modal, u as useModal, f as usePagination };
|
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.2",
|
|
4
4
|
"description": "An opinionated collection of react hooks, and reusable UI components.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"dev": "vite",
|
|
20
|
-
"build": "
|
|
20
|
+
"build": "vite build && dts-bundle-generator --project tsconfig.app.json -o dist/index.d.ts src/index.ts",
|
|
21
21
|
"lint": "eslint .",
|
|
22
22
|
"preview": "vite preview",
|
|
23
23
|
"format": "prettier . --write"
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"@types/react": "^19.2.14",
|
|
36
36
|
"@types/react-dom": "^19.2.3",
|
|
37
37
|
"@vitejs/plugin-react": "^6.0.1",
|
|
38
|
+
"dts-bundle-generator": "^9.5.1",
|
|
38
39
|
"eslint": "^10.3.0",
|
|
39
40
|
"eslint-plugin-react-hooks": "^7.1.1",
|
|
40
41
|
"eslint-plugin-react-refresh": "^0.5.2",
|