@maskingtech/react-toolkit 0.0.1 → 0.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/README.md +165 -0
- package/dist/mtreact.d.ts +4 -4
- package/dist/mtreact.js +75 -73
- package/package.json +1 -1
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' | '
|
|
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
|
|
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<
|
|
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
|
|
2
|
-
import { Component as
|
|
3
|
-
class
|
|
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__ */
|
|
22
|
+
return /* @__PURE__ */ b(t, { error: this.state.error });
|
|
23
23
|
}
|
|
24
24
|
return this.props.children;
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
function
|
|
28
|
-
const [a, u] =
|
|
27
|
+
function k(n, t, e = 500) {
|
|
28
|
+
const [a, u] = i(n), [s, r] = i(n);
|
|
29
29
|
return m(() => {
|
|
30
|
-
const
|
|
31
|
-
return () => clearTimeout(
|
|
30
|
+
const l = setTimeout(() => r(a), e);
|
|
31
|
+
return () => clearTimeout(l);
|
|
32
32
|
}, [a, e]), m(() => {
|
|
33
|
-
t(
|
|
34
|
-
}, [
|
|
33
|
+
t !== void 0 && t(s);
|
|
34
|
+
}, [s, t]), [s, u];
|
|
35
35
|
}
|
|
36
|
-
function
|
|
37
|
-
const t = n ??
|
|
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
|
|
43
|
-
const e = t ??
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
}, [e]), d =
|
|
47
|
-
const
|
|
48
|
-
if (
|
|
49
|
-
const f = new FormData(
|
|
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
|
-
|
|
51
|
+
r("dirty");
|
|
52
52
|
return;
|
|
53
53
|
}
|
|
54
|
-
for (const [
|
|
55
|
-
if (a.get(
|
|
56
|
-
|
|
54
|
+
for (const [D, F] of f.entries())
|
|
55
|
+
if (a.get(D) !== F) {
|
|
56
|
+
r("dirty");
|
|
57
57
|
return;
|
|
58
58
|
}
|
|
59
|
-
|
|
60
|
-
}, [e, a]),
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
}, [e,
|
|
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
|
|
66
|
-
if (
|
|
65
|
+
const o = e.current;
|
|
66
|
+
if (o === null) return;
|
|
67
67
|
const f = () => d();
|
|
68
|
-
return
|
|
69
|
-
}, [e, d]), m(() =>
|
|
68
|
+
return o.addEventListener("input", f), () => o.removeEventListener("input", f);
|
|
69
|
+
}, [e, d]), m(() => l(), [l]), [e, s, h];
|
|
70
70
|
}
|
|
71
|
-
function
|
|
72
|
-
const e = t ??
|
|
73
|
-
const
|
|
74
|
-
|
|
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,
|
|
76
|
+
return [e, a, s];
|
|
77
77
|
}
|
|
78
|
-
function
|
|
79
|
-
const [e, a] =
|
|
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
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
},
|
|
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
|
|
89
|
-
}, [n]),
|
|
90
|
-
a(void 0),
|
|
91
|
-
}, [
|
|
92
|
-
return m(
|
|
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
|
|
95
|
-
const [a, u] =
|
|
96
|
-
let
|
|
97
|
-
const
|
|
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
|
|
100
|
-
|
|
101
|
-
},
|
|
102
|
-
|
|
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
|
|
105
|
-
}, [n, t,
|
|
106
|
-
u([]), d(!1),
|
|
107
|
-
}, []),
|
|
108
|
-
|
|
109
|
-
}, []),
|
|
110
|
-
|
|
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(
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
};
|