@paragrav/rhf-utils 0.62.0 → 0.63.0
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/esm/FormRelaySetter.mjs +88 -139
- package/dist/esm/index.mjs +352 -602
- package/dist/esm/relay.mjs +48 -83
- package/dist/esm/trpc.mjs +24 -40
- package/package.json +25 -26
|
@@ -1,140 +1,89 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { get as
|
|
4
|
-
import { flatten as
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import e from "react";
|
|
2
|
+
import { jsx as t } from "react/jsx-runtime";
|
|
3
|
+
import { get as n, useFormState as r } from "react-hook-form";
|
|
4
|
+
import { flatten as i } from "flat";
|
|
5
|
+
//#region src/utils/createContext.ts
|
|
6
|
+
function a() {
|
|
7
|
+
let t = e.createContext(void 0), n = () => e.useContext(t);
|
|
8
|
+
return {
|
|
9
|
+
Provider: t.Provider,
|
|
10
|
+
useMaybe: n,
|
|
11
|
+
useRequired: () => {
|
|
12
|
+
let e = n();
|
|
13
|
+
if (e === void 0) throw Error();
|
|
14
|
+
return e;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
message: t.message,
|
|
90
|
-
hasRef: !!t.ref
|
|
91
|
-
// strip out actual ref, etc. (avoid circular JSON error)
|
|
92
|
-
}
|
|
93
|
-
])
|
|
94
|
-
), O = (e) => {
|
|
95
|
-
if (!e) return;
|
|
96
|
-
const [r] = s.useState(e);
|
|
97
|
-
g(r), h(r);
|
|
98
|
-
}, g = (e) => {
|
|
99
|
-
const r = d(), t = l(), o = y(e);
|
|
100
|
-
s.useEffect(
|
|
101
|
-
() => (t.add(o, r, e), () => {
|
|
102
|
-
t.remove(r.formId);
|
|
103
|
-
}),
|
|
104
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
105
|
-
[]
|
|
106
|
-
);
|
|
107
|
-
}, h = (e) => {
|
|
108
|
-
const r = d(), t = l(), o = y(e);
|
|
109
|
-
s.useEffect(
|
|
110
|
-
// when form state changes, publish to relay context
|
|
111
|
-
() => {
|
|
112
|
-
t.update(r.formId, o);
|
|
113
|
-
},
|
|
114
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
115
|
-
[o]
|
|
116
|
-
);
|
|
117
|
-
}, y = (e) => {
|
|
118
|
-
const r = R(), t = e.select(r), o = JSON.stringify({
|
|
119
|
-
...t,
|
|
120
|
-
errors: t.errors && C(t.errors)
|
|
121
|
-
});
|
|
122
|
-
return s.useMemo(
|
|
123
|
-
// when watch JSON changes, update memo
|
|
124
|
-
() => t,
|
|
125
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
126
|
-
[o]
|
|
127
|
-
);
|
|
128
|
-
}, _ = ({ options: e }) => (O(e), null);
|
|
129
|
-
export {
|
|
130
|
-
k as F,
|
|
131
|
-
I as _,
|
|
132
|
-
_ as a,
|
|
133
|
-
l as b,
|
|
134
|
-
f as c,
|
|
135
|
-
q as d,
|
|
136
|
-
O as e,
|
|
137
|
-
p as g,
|
|
138
|
-
U as i,
|
|
139
|
-
d as u
|
|
140
|
-
};
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/form/relay/context/useFormRelayContext.ts
|
|
20
|
+
var { Provider: o, useRequired: s } = a(), c = ({ children: n }) => {
|
|
21
|
+
let [r, i] = e.useState({});
|
|
22
|
+
return /* @__PURE__ */ t(o, {
|
|
23
|
+
value: {
|
|
24
|
+
add: e.useCallback((e, t, n) => {
|
|
25
|
+
if (r[t.formId]) throw Error("Form id already exists.");
|
|
26
|
+
i((r) => ({
|
|
27
|
+
...r,
|
|
28
|
+
[t.formId]: {
|
|
29
|
+
state: e,
|
|
30
|
+
utils: t,
|
|
31
|
+
options: n
|
|
32
|
+
}
|
|
33
|
+
}));
|
|
34
|
+
}, [r]),
|
|
35
|
+
update: e.useCallback((e, t) => {
|
|
36
|
+
i((n) => {
|
|
37
|
+
let r = n[e];
|
|
38
|
+
if (!r) throw Error("Form id doesn't exist");
|
|
39
|
+
return {
|
|
40
|
+
...n,
|
|
41
|
+
[e]: {
|
|
42
|
+
...r,
|
|
43
|
+
state: t
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
}, []),
|
|
48
|
+
remove: e.useCallback((e) => {
|
|
49
|
+
i((t) => Object.fromEntries(Object.entries(t).filter(([t]) => t !== e)));
|
|
50
|
+
}, []),
|
|
51
|
+
state: r
|
|
52
|
+
},
|
|
53
|
+
children: n
|
|
54
|
+
});
|
|
55
|
+
}, { Provider: l, useRequired: u, useMaybe: d } = a(), f = (e) => Object.keys(e).length === 0, p = (e) => i(e), m = (e) => {
|
|
56
|
+
let t = p(e);
|
|
57
|
+
return Object.fromEntries(Object.keys(t).reduce((t, r) => {
|
|
58
|
+
let i = r.replace(h, "");
|
|
59
|
+
if (i === r) return t;
|
|
60
|
+
let a = n(e, i, void 0);
|
|
61
|
+
return a && t.push([i, a]), t;
|
|
62
|
+
}, []));
|
|
63
|
+
}, h = /\.(?:type|message)\b$/, g = (e) => Object.fromEntries(Object.entries(m(e)).map(([e, t]) => [e, {
|
|
64
|
+
type: t.type,
|
|
65
|
+
message: t.message,
|
|
66
|
+
hasRef: !!t.ref
|
|
67
|
+
}])), _ = (t) => {
|
|
68
|
+
if (!t) return;
|
|
69
|
+
let [n] = e.useState(t);
|
|
70
|
+
v(n), y(n);
|
|
71
|
+
}, v = (t) => {
|
|
72
|
+
let n = u(), r = s(), i = b(t);
|
|
73
|
+
e.useEffect(() => (r.add(i, n, t), () => {
|
|
74
|
+
r.remove(n.formId);
|
|
75
|
+
}), []);
|
|
76
|
+
}, y = (t) => {
|
|
77
|
+
let n = u(), r = s(), i = b(t);
|
|
78
|
+
e.useEffect(() => {
|
|
79
|
+
r.update(n.formId, i);
|
|
80
|
+
}, [i]);
|
|
81
|
+
}, b = (t) => {
|
|
82
|
+
let n = r(), i = t.select(n), a = JSON.stringify({
|
|
83
|
+
...i,
|
|
84
|
+
errors: i.errors && g(i.errors)
|
|
85
|
+
});
|
|
86
|
+
return e.useMemo(() => i, [a]);
|
|
87
|
+
}, x = ({ options: e }) => (_(e), null);
|
|
88
|
+
//#endregion
|
|
89
|
+
export { l as a, c, f as i, s as l, _ as n, u as o, m as r, d as s, x as t, a as u };
|