@maskingtech/react-toolkit 0.0.1 → 0.0.3

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/README.md ADDED
@@ -0,0 +1,165 @@
1
+ # React Toolkit
2
+
3
+ A set of reusables for React mainly used by ourselves.
4
+
5
+ ## Components
6
+
7
+ ### ErrorBoundary
8
+
9
+ Catches errors and passes it to the provided element.
10
+
11
+ Usage:
12
+
13
+ ```tsx
14
+ import { ErrorBoundary } from '@maskingtech/react-toolkit';
15
+
16
+ function ErrorHandler({ error }: { error: unknown })
17
+ {
18
+ return <>Oops...</>;
19
+ }
20
+
21
+ <ErrorBoundary element={ErrorHandler}>
22
+ {/* Content goes here */}
23
+ </ErrorBoundary>;
24
+ ```
25
+
26
+ ## Hooks
27
+
28
+ ### useDebouncedValue
29
+
30
+ Delays value updates for a period of time.
31
+
32
+ Usage:
33
+
34
+ ```tsx
35
+ import { useDebouncedValue } from '@maskingtech/react-toolkit';
36
+
37
+ function MyComponent()
38
+ {
39
+ const initialValue: number = 0; // required
40
+ const onChange = (debouncedValue: number) => { }; // optional
41
+ const delay = 300; // optional, default 500
42
+
43
+ const [debouncedValue, setValue] = useDebouncedValue(initialValue, onChange, delay);
44
+
45
+ return <p>
46
+ {debouncedValue}
47
+ <button onClick={() => setValue(debouncedValue + 1)}>Increase</button>
48
+ </p>;
49
+ }
50
+ ```
51
+
52
+ ### useFocusOnMount
53
+
54
+ Gives a form element focus after mount.
55
+
56
+ Usage:
57
+
58
+ ```tsx
59
+ import { useFocusOnMount } from '@maskingtech/react-toolkit';
60
+
61
+ function MyComponent()
62
+ {
63
+ const ref = useFocusOnMount();
64
+
65
+ return <input type="text" ref={ref} />;
66
+ }
67
+ ```
68
+
69
+ ### useForm
70
+
71
+ Provides access to the data of a form after submitting.
72
+
73
+ Usage:
74
+
75
+ ```tsx
76
+ import { useForm } from '@maskingtech/react-toolkit';
77
+
78
+ function MyComponent()
79
+ {
80
+ const submitHandler = (data: FormData) { console.log(data.get('name')); };
81
+
82
+ const [ref, state, handleSubmit] = useForm(submitHandler);
83
+
84
+ // states: 'pristine' | 'dirty' | 'submitting'
85
+
86
+ return <form ref={ref} onSubmit={handleSubmit}>
87
+ <input type="text" name="name" />
88
+ <input type="submit" value="Submit" disabled={state !== 'dirty'} />
89
+ </form>;
90
+ }
91
+ ```
92
+
93
+ ### useFormData
94
+
95
+ Provides access to the data of a form without submitting.
96
+
97
+ Usage:
98
+
99
+ ```tsx
100
+ import { useFormData } from '@maskingtech/react-toolkit';
101
+
102
+ function MyComponent()
103
+ {
104
+ const dataHandler = (data: FormData) { console.log(data.get('name')); };
105
+
106
+ const [ref, state, handleData] = useFormData(dataHandler);
107
+
108
+ // states: 'idle' | 'working'
109
+
110
+ return <form ref={ref}>
111
+ <input type="text" name="name" />
112
+ <input type="button" value="Go!" disabled={state !== 'working'} />
113
+ </form>;
114
+ }
115
+ ```
116
+
117
+ ### useLoadData
118
+
119
+ Provides helpers for loading data.
120
+
121
+ Usage:
122
+
123
+ ```tsx
124
+ import { useLoadData } from '@maskingtech/react-toolkit';
125
+
126
+ async function getData() { /* get data here */ }
127
+
128
+ function MyComponent()
129
+ {
130
+ const [data, isLoading, refresh, setData] = useLoadData(getData);
131
+
132
+ if (isLoading) return <>Loading...</>;
133
+
134
+ return <p>
135
+ {data}
136
+ <button onClick={() => refresh()}>Refresh</button>
137
+ </p>;
138
+ }
139
+ ```
140
+
141
+ ### usePagination
142
+
143
+ Provides helpers for loading paginated data.
144
+
145
+ Usage:
146
+
147
+ ```tsx
148
+ import { useLoadData } from '@maskingtech/react-toolkit';
149
+
150
+ async function getPageData(page: number) { /* get data here */ }
151
+
152
+ function MyComponent()
153
+ {
154
+ const [data, isLoading, isFinished, next, previous, reset, setData] = useLoadData(useLoadData);
155
+
156
+ if (isLoading) return <>Loading...</>;
157
+
158
+ return <p>
159
+ {data}
160
+ <button onClick={() => previous()}>Previous</button>
161
+ <button onClick={() => next()}>Next</button>
162
+ <button onClick={() => reset()}>Reset</button>
163
+ </p>;
164
+ }
165
+ ```
package/dist/mtreact.d.ts CHANGED
@@ -38,13 +38,13 @@ declare type State = {
38
38
 
39
39
  declare type States = 'pristine' | 'dirty' | 'submitting';
40
40
 
41
- declare type States_2 = 'idle' | 'submitting';
41
+ declare type States_2 = 'idle' | 'working';
42
42
 
43
43
  declare type SubmitHandler = (data: FormData) => Promise<void>;
44
44
 
45
- export declare function useDebouncedValue<T>(initialValue: T, onChange: ChangeHandler<T>, delay?: number): readonly [T, Dispatch<SetStateAction<T>>];
45
+ export declare function useDebouncedValue<T>(initialValue: T, onChange?: ChangeHandler<T>, delay?: number): readonly [T, Dispatch<SetStateAction<T>>];
46
46
 
47
- export declare function useFocusOnMount(ref?: RefObject<HTMLElement>): RefObject<HTMLElement>;
47
+ export declare function useFocusOnMount(ref?: RefObject<HTMLFormElement>): RefObject<HTMLFormElement>;
48
48
 
49
49
  export declare function useForm(submitData: SubmitHandler, ref?: FormRef): readonly [RefObject<HTMLFormElement>, States, () => Promise<void>];
50
50
 
@@ -52,7 +52,7 @@ export declare function useFormData(handler: DataHandler, ref?: FormRef_2): read
52
52
 
53
53
  export declare function useLoadData<T>(getData: GetData<T>, deps?: DependencyList): readonly [T, boolean, () => void, Dispatch<SetStateAction<T>>];
54
54
 
55
- export declare function usePagination<T>(getData: GetData_2<T>, limit: number, deps?: DependencyList): readonly [T[], boolean, boolean, () => void, () => void, Dispatch<SetStateAction<T[]>>];
55
+ export declare function usePagination<T>(getData: GetData_2<T>, limit: number, deps?: DependencyList): readonly [T[], boolean, boolean, () => void, () => void, () => void, Dispatch<SetStateAction<T[]>>];
56
56
 
57
57
  declare type ViewProps = {
58
58
  error: unknown;
package/dist/mtreact.js CHANGED
@@ -1,6 +1,6 @@
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 {
1
+ import { jsx as b } from "react/jsx-runtime";
2
+ import { Component as V, useState as i, useEffect as m, useRef as w, useCallback as c } from "react";
3
+ class j extends V {
4
4
  #t = (t) => {
5
5
  this.setState({ error: t.reason }), t.preventDefault();
6
6
  };
@@ -19,104 +19,106 @@ class R extends b {
19
19
  render() {
20
20
  if (this.state.error !== void 0) {
21
21
  const t = this.props.element;
22
- return /* @__PURE__ */ S(t, { error: this.state.error });
22
+ return /* @__PURE__ */ b(t, { error: this.state.error });
23
23
  }
24
24
  return this.props.children;
25
25
  }
26
26
  }
27
- function j(n, t, e = 500) {
28
- const [a, u] = c(n), [o, s] = c(n);
27
+ function k(n, t, e = 500) {
28
+ const [a, u] = i(n), [s, r] = i(n);
29
29
  return m(() => {
30
- const i = setTimeout(() => s(a), e);
31
- return () => clearTimeout(i);
30
+ const l = setTimeout(() => r(a), e);
31
+ return () => clearTimeout(l);
32
32
  }, [a, e]), m(() => {
33
- t(o);
34
- }, [o, t]), [o, u];
33
+ t !== void 0 && t(s);
34
+ }, [s, t]), [s, u];
35
35
  }
36
- function C(n) {
37
- const t = n ?? v(null);
36
+ function I(n) {
37
+ const t = n ?? w(null);
38
38
  return m(() => {
39
39
  t !== null && t.current?.focus();
40
40
  }, [t]), t;
41
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);
42
+ function A(n, t) {
43
+ const e = t ?? w(null), [a, u] = i(new FormData()), [s, r] = i("pristine"), l = c(() => {
44
+ const o = e.current;
45
+ o !== null && (u(new FormData(o)), r("pristine"));
46
+ }, [e]), d = c(() => {
47
+ const o = e.current;
48
+ if (o === null) return;
49
+ const f = new FormData(o);
50
50
  if (a.values().toArray().length !== f.values().toArray().length) {
51
- s("dirty");
51
+ r("dirty");
52
52
  return;
53
53
  }
54
- for (const [F, g] of f.entries())
55
- if (a.get(F) !== g) {
56
- s("dirty");
54
+ for (const [D, F] of f.entries())
55
+ if (a.get(D) !== F) {
56
+ r("dirty");
57
57
  return;
58
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]);
59
+ r("pristine");
60
+ }, [e, a]), h = c(async () => {
61
+ const o = e.current;
62
+ o !== null && (r("submitting"), await n(new FormData(o)), l());
63
+ }, [e, s, n, l]);
64
64
  return m(() => {
65
- const r = e.current;
66
- if (r === null) return;
65
+ const o = e.current;
66
+ if (o === null) return;
67
67
  const f = () => d();
68
- return r.addEventListener("input", f), () => r.removeEventListener("input", f);
69
- }, [e, d]), m(() => i(), [i]), [e, o, p];
68
+ return o.addEventListener("input", f), () => o.removeEventListener("input", f);
69
+ }, [e, d]), m(() => l(), [l]), [e, s, h];
70
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"));
71
+ function C(n, t) {
72
+ const e = t ?? w(null), [a, u] = i("idle"), s = c(async () => {
73
+ const r = e.current;
74
+ r !== null && (u("working"), await n(new FormData(r)), u("idle"));
75
75
  }, [e, n]);
76
- return [e, a, o];
76
+ return [e, a, s];
77
77
  }
78
- function k(n, t = []) {
79
- const [e, a] = c(void 0), [u, o] = c(!1), s = l(() => {
78
+ function H(n, t = []) {
79
+ const [e, a] = i(void 0), [u, s] = i(!1), r = c(() => {
80
80
  let d = !1;
81
- const p = async () => {
82
- o(!0);
83
- const r = await n();
84
- o(!1), !d && a(r);
85
- }, h = () => {
81
+ const h = async () => {
82
+ s(!0);
83
+ const o = await n();
84
+ s(!1), !d && a(o);
85
+ }, v = () => {
86
86
  d = !0;
87
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];
88
+ return h(), v;
89
+ }, [n]), l = c(() => {
90
+ a(void 0), r();
91
+ }, [r]);
92
+ return m(r, [n, ...t]), [e, u, l, a];
93
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 () => {
94
+ function M(n, t, e = []) {
95
+ const [a, u] = i([]), [s, r] = i(0), [l, d] = i(!1), [h, v] = i(!1), o = c(() => {
96
+ let p = !1;
97
+ const L = async () => {
98
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;
99
+ const y = await n(s);
100
+ y.length < t && v(!0), !p && (d(!1), u((S) => [...S, ...y]));
101
+ }, E = () => {
102
+ p = !0;
103
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();
104
+ return L(), E;
105
+ }, [n, t, s]), f = c(() => {
106
+ u([]), d(!1), v(!1), r(0);
107
+ }, []), D = c(() => {
108
+ r((p) => p + 1);
109
+ }, []), F = c(() => {
110
+ r((p) => p - 1);
111
+ }, []), g = c(() => {
112
+ f(), o();
111
113
  }, []);
112
- return m(f, [f, t, ...e]), m(r, [n, r, o]), [a, i, p, F, g, u];
114
+ return m(f, [f, t, ...e]), m(o, [n, o, s]), [a, l, h, D, F, g, u];
113
115
  }
114
116
  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
117
+ j as ErrorBoundary,
118
+ k as useDebouncedValue,
119
+ I as useFocusOnMount,
120
+ A as useForm,
121
+ C as useFormData,
122
+ H as useLoadData,
123
+ M as usePagination
122
124
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@maskingtech/react-toolkit",
3
3
  "private": false,
4
- "version": "0.0.1",
4
+ "version": "0.0.3",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "url": "https://github.com/MaskingTechnology/react-toolkit"
@@ -23,18 +23,18 @@
23
23
  "react": "^19.2.0"
24
24
  },
25
25
  "devDependencies": {
26
- "@eslint/js": "9.39.1",
26
+ "@eslint/js": "9.39.2",
27
27
  "@types/react": "19.2.7",
28
- "@types/node": "25.0.1",
28
+ "@types/node": "25.0.3",
29
29
  "@vitejs/plugin-react": "5.1.2",
30
30
  "eslint-plugin-react": "7.37.5",
31
31
  "rimraf": "6.1.2",
32
32
  "typescript": "5.9.3",
33
- "typescript-eslint": "8.49.0",
34
- "vite": "7.2.7",
33
+ "typescript-eslint": "8.51.0",
34
+ "vite": "7.3.0",
35
35
  "vite-plugin-dts": "4.5.4"
36
36
  },
37
37
  "optionalDependencies": {
38
- "@rollup/rollup-linux-x64-gnu": "4.53.3"
38
+ "@rollup/rollup-linux-x64-gnu": "4.54.0"
39
39
  }
40
40
  }