@maskingtech/react-toolkit 0.0.1

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/LICENCE ADDED
@@ -0,0 +1,23 @@
1
+
2
+ (The MIT License)
3
+
4
+ Copyright (c) 2025 Masking Technology B.V. <https://masking.tech>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ 'Software'), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ import { Component } from 'react';
2
+ import { ComponentType } from 'react';
3
+ import { DependencyList } from 'react';
4
+ import { Dispatch } from 'react';
5
+ import { ReactNode } from 'react';
6
+ import { RefObject } from 'react';
7
+ import { SetStateAction } from 'react';
8
+
9
+ declare type ChangeHandler<T> = (value: T) => void;
10
+
11
+ declare type DataHandler = (data: FormData) => Promise<void>;
12
+
13
+ export declare class ErrorBoundary extends Component<Props, State> {
14
+ #private;
15
+ constructor(props: Props);
16
+ static getDerivedStateFromError(error: unknown): State;
17
+ componentDidMount(): void;
18
+ componentWillUnmount(): void;
19
+ render(): ReactNode;
20
+ }
21
+
22
+ declare type FormRef = RefObject<HTMLFormElement | null>;
23
+
24
+ declare type FormRef_2 = RefObject<HTMLFormElement | null>;
25
+
26
+ declare type GetData<T> = () => Promise<T>;
27
+
28
+ declare type GetData_2<T> = (page: number) => Promise<T[]>;
29
+
30
+ declare type Props = {
31
+ readonly element: ComponentType<ViewProps>;
32
+ readonly children: ReactNode;
33
+ };
34
+
35
+ declare type State = {
36
+ error: unknown;
37
+ };
38
+
39
+ declare type States = 'pristine' | 'dirty' | 'submitting';
40
+
41
+ declare type States_2 = 'idle' | 'submitting';
42
+
43
+ declare type SubmitHandler = (data: FormData) => Promise<void>;
44
+
45
+ export declare function useDebouncedValue<T>(initialValue: T, onChange: ChangeHandler<T>, delay?: number): readonly [T, Dispatch<SetStateAction<T>>];
46
+
47
+ export declare function useFocusOnMount(ref?: RefObject<HTMLElement>): RefObject<HTMLElement>;
48
+
49
+ export declare function useForm(submitData: SubmitHandler, ref?: FormRef): readonly [RefObject<HTMLFormElement>, States, () => Promise<void>];
50
+
51
+ export declare function useFormData(handler: DataHandler, ref?: FormRef_2): readonly [RefObject<HTMLFormElement>, States_2, () => Promise<void>];
52
+
53
+ export declare function useLoadData<T>(getData: GetData<T>, deps?: DependencyList): readonly [T, boolean, () => void, Dispatch<SetStateAction<T>>];
54
+
55
+ export declare function usePagination<T>(getData: GetData_2<T>, limit: number, deps?: DependencyList): readonly [T[], boolean, boolean, () => void, () => void, Dispatch<SetStateAction<T[]>>];
56
+
57
+ declare type ViewProps = {
58
+ error: unknown;
59
+ };
60
+
61
+ export { }
@@ -0,0 +1,122 @@
1
+ import { jsx as S } from "react/jsx-runtime";
2
+ import { Component as b, useState as c, useEffect as m, useRef as v, useCallback as l } from "react";
3
+ class R extends b {
4
+ #t = (t) => {
5
+ this.setState({ error: t.reason }), t.preventDefault();
6
+ };
7
+ constructor(t) {
8
+ super(t), this.state = { error: void 0 };
9
+ }
10
+ static getDerivedStateFromError(t) {
11
+ return { error: t };
12
+ }
13
+ componentDidMount() {
14
+ window.addEventListener("unhandledrejection", this.#t);
15
+ }
16
+ componentWillUnmount() {
17
+ window.removeEventListener("unhandledrejection", this.#t);
18
+ }
19
+ render() {
20
+ if (this.state.error !== void 0) {
21
+ const t = this.props.element;
22
+ return /* @__PURE__ */ S(t, { error: this.state.error });
23
+ }
24
+ return this.props.children;
25
+ }
26
+ }
27
+ function j(n, t, e = 500) {
28
+ const [a, u] = c(n), [o, s] = c(n);
29
+ return m(() => {
30
+ const i = setTimeout(() => s(a), e);
31
+ return () => clearTimeout(i);
32
+ }, [a, e]), m(() => {
33
+ t(o);
34
+ }, [o, t]), [o, u];
35
+ }
36
+ function C(n) {
37
+ const t = n ?? v(null);
38
+ return m(() => {
39
+ t !== null && t.current?.focus();
40
+ }, [t]), t;
41
+ }
42
+ function I(n, t) {
43
+ const e = t ?? v(null), [a, u] = c(new FormData()), [o, s] = c("pristine"), i = l(() => {
44
+ const r = e.current;
45
+ r !== null && (u(new FormData(r)), s("pristine"));
46
+ }, [e]), d = l(() => {
47
+ const r = e.current;
48
+ if (r === null) return;
49
+ const f = new FormData(r);
50
+ if (a.values().toArray().length !== f.values().toArray().length) {
51
+ s("dirty");
52
+ return;
53
+ }
54
+ for (const [F, g] of f.entries())
55
+ if (a.get(F) !== g) {
56
+ s("dirty");
57
+ return;
58
+ }
59
+ s("pristine");
60
+ }, [e, a]), p = l(async () => {
61
+ const r = e.current;
62
+ r !== null && (s("submitting"), await n(new FormData(r)), i());
63
+ }, [e, o, n, i]);
64
+ return m(() => {
65
+ const r = e.current;
66
+ if (r === null) return;
67
+ const f = () => d();
68
+ return r.addEventListener("input", f), () => r.removeEventListener("input", f);
69
+ }, [e, d]), m(() => i(), [i]), [e, o, p];
70
+ }
71
+ function P(n, t) {
72
+ const e = t ?? v(null), [a, u] = c("idle"), o = l(async () => {
73
+ const s = e.current;
74
+ s !== null && (u("submitting"), await n(new FormData(s)), u("idle"));
75
+ }, [e, n]);
76
+ return [e, a, o];
77
+ }
78
+ function k(n, t = []) {
79
+ const [e, a] = c(void 0), [u, o] = c(!1), s = l(() => {
80
+ let d = !1;
81
+ const p = async () => {
82
+ o(!0);
83
+ const r = await n();
84
+ o(!1), !d && a(r);
85
+ }, h = () => {
86
+ d = !0;
87
+ };
88
+ return p(), h;
89
+ }, [n]), i = l(() => {
90
+ a(void 0), s();
91
+ }, [s]);
92
+ return m(s, [n, ...t]), [e, u, i, a];
93
+ }
94
+ function A(n, t, e = []) {
95
+ const [a, u] = c([]), [o, s] = c(0), [i, d] = c(!1), [p, h] = c(!1), r = l(() => {
96
+ let D = !1;
97
+ const y = async () => {
98
+ d(!0);
99
+ const w = await n(o);
100
+ w.length < t && h(!0), !D && (d(!1), u((E) => [...E, ...w]));
101
+ }, L = () => {
102
+ D = !0;
103
+ };
104
+ return y(), L;
105
+ }, [n, t, o]), f = l(() => {
106
+ u([]), d(!1), h(!1), s(0);
107
+ }, []), F = l(() => {
108
+ s((D) => D + 1);
109
+ }, []), g = l(() => {
110
+ f(), r();
111
+ }, []);
112
+ return m(f, [f, t, ...e]), m(r, [n, r, o]), [a, i, p, F, g, u];
113
+ }
114
+ export {
115
+ R as ErrorBoundary,
116
+ j as useDebouncedValue,
117
+ C as useFocusOnMount,
118
+ I as useForm,
119
+ P as useFormData,
120
+ k as useLoadData,
121
+ A as usePagination
122
+ };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@maskingtech/react-toolkit",
3
+ "private": false,
4
+ "version": "0.0.1",
5
+ "type": "module",
6
+ "repository": {
7
+ "url": "https://github.com/MaskingTechnology/react-toolkit"
8
+ },
9
+ "scripts": {
10
+ "build": "vite build",
11
+ "clean": "rimraf dist",
12
+ "lint": "eslint",
13
+ "prepublishOnly": "npm run build"
14
+ },
15
+ "exports": {
16
+ ".": "./dist/mtreact.js"
17
+ },
18
+ "files": [
19
+ "README.md",
20
+ "dist"
21
+ ],
22
+ "peerDependencies": {
23
+ "react": "^19.2.0"
24
+ },
25
+ "devDependencies": {
26
+ "@eslint/js": "9.39.1",
27
+ "@types/react": "19.2.7",
28
+ "@types/node": "25.0.1",
29
+ "@vitejs/plugin-react": "5.1.2",
30
+ "eslint-plugin-react": "7.37.5",
31
+ "rimraf": "6.1.2",
32
+ "typescript": "5.9.3",
33
+ "typescript-eslint": "8.49.0",
34
+ "vite": "7.2.7",
35
+ "vite-plugin-dts": "4.5.4"
36
+ },
37
+ "optionalDependencies": {
38
+ "@rollup/rollup-linux-x64-gnu": "4.53.3"
39
+ }
40
+ }