@companix/formkit 0.0.27 → 0.0.29
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/bundle.es.js +249 -224
- package/dist/index.d.ts +1 -1
- package/dist/manager/manager.d.ts +6 -1
- package/dist/useForm.d.ts +6 -1
- package/dist/useValue.d.ts +1 -0
- package/package.json +1 -1
package/dist/bundle.es.js
CHANGED
|
@@ -1,36 +1,43 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { createContext as
|
|
3
|
-
import
|
|
4
|
-
const
|
|
5
|
-
const [, r] =
|
|
1
|
+
import { jsx as b, Fragment as q, jsxs as P } from "react/jsx-runtime";
|
|
2
|
+
import { createContext as O, useState as L, useContext as R, useMemo as p, useEffect as $, useCallback as j, useRef as U } from "react";
|
|
3
|
+
import M from "fast-deep-equal";
|
|
4
|
+
const A = O({}), N = (n) => {
|
|
5
|
+
const [, r] = L([]), e = R(A);
|
|
6
6
|
if (!e)
|
|
7
7
|
throw new Error("useValue cannot be used outside Form context");
|
|
8
|
-
const s =
|
|
9
|
-
const t = e.getForm(
|
|
8
|
+
const s = p(() => {
|
|
9
|
+
const t = e.getForm(n);
|
|
10
10
|
if (!t)
|
|
11
|
-
throw new Error(`form with name "${
|
|
11
|
+
throw new Error(`form with name "${n}" doesnt exist in form`);
|
|
12
12
|
return t;
|
|
13
|
-
}, [e,
|
|
13
|
+
}, [e, n]);
|
|
14
14
|
return $(() => {
|
|
15
|
-
const t = e.subscribeToForm(
|
|
15
|
+
const t = e.subscribeToForm(n, () => {
|
|
16
16
|
r([]);
|
|
17
17
|
});
|
|
18
18
|
return () => {
|
|
19
19
|
t();
|
|
20
20
|
};
|
|
21
21
|
}, []), s.value;
|
|
22
|
-
},
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
}, W = (n) => {
|
|
23
|
+
const r = R(A);
|
|
24
|
+
if (!r)
|
|
25
|
+
throw new Error("useSetValue cannot be used outside Form context");
|
|
26
|
+
return (e) => {
|
|
27
|
+
r.setValue(n, e);
|
|
28
|
+
};
|
|
29
|
+
}, X = () => (n) => N(n), _ = ({ canActivate: n, name: r, children: e }) => {
|
|
30
|
+
const s = N(r);
|
|
31
|
+
return n(s) ? /* @__PURE__ */ b(q, { children: e }) : null;
|
|
32
|
+
}, z = ({ item: { Form: n }, name: r }) => {
|
|
33
|
+
const [, e] = L([]), s = R(A), { unregistry: t } = p(() => s.registry(r, () => {
|
|
27
34
|
e([]);
|
|
28
|
-
}), []),
|
|
29
|
-
(
|
|
30
|
-
if (
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
}), []), f = j(
|
|
36
|
+
(d) => {
|
|
37
|
+
if (d && d.focus) {
|
|
38
|
+
const h = s.getForm(r);
|
|
39
|
+
h.focus = () => {
|
|
40
|
+
d.focus();
|
|
34
41
|
};
|
|
35
42
|
}
|
|
36
43
|
},
|
|
@@ -39,107 +46,107 @@ const D = M({}), q = (o) => {
|
|
|
39
46
|
$(() => () => {
|
|
40
47
|
t();
|
|
41
48
|
}, []);
|
|
42
|
-
const { value:
|
|
43
|
-
return /* @__PURE__ */
|
|
44
|
-
|
|
49
|
+
const { value: y, error: m } = s.getForm(r);
|
|
50
|
+
return /* @__PURE__ */ b(
|
|
51
|
+
n,
|
|
45
52
|
{
|
|
46
53
|
field: {
|
|
47
|
-
value:
|
|
48
|
-
onChange: (
|
|
49
|
-
s.onChange(r,
|
|
54
|
+
value: y,
|
|
55
|
+
onChange: (d) => {
|
|
56
|
+
s.onChange(r, d);
|
|
50
57
|
},
|
|
51
58
|
onBlur: () => {
|
|
52
59
|
},
|
|
53
|
-
ref:
|
|
60
|
+
ref: f
|
|
54
61
|
},
|
|
55
62
|
isDirty: !1,
|
|
56
|
-
error:
|
|
63
|
+
error: m,
|
|
57
64
|
disabled: s.disabled
|
|
58
65
|
}
|
|
59
66
|
);
|
|
60
|
-
},
|
|
61
|
-
const { getController: e, items: s } =
|
|
62
|
-
const
|
|
63
|
-
return s.forEach(({ name:
|
|
64
|
-
|
|
65
|
-
}),
|
|
66
|
-
}, []),
|
|
67
|
-
const
|
|
68
|
-
return s.forEach(({ name:
|
|
69
|
-
t.getForm([...r,
|
|
70
|
-
}),
|
|
71
|
-
}, []),
|
|
67
|
+
}, G = ({ item: n, path: r }) => {
|
|
68
|
+
const { getController: e, items: s } = n, t = R(A), f = p(() => {
|
|
69
|
+
const l = {};
|
|
70
|
+
return s.forEach(({ name: i, getForm: x }) => {
|
|
71
|
+
l[i] = x(i, () => D(i));
|
|
72
|
+
}), l;
|
|
73
|
+
}, []), y = j(() => {
|
|
74
|
+
const l = [];
|
|
75
|
+
return s.forEach(({ name: i }) => {
|
|
76
|
+
t.getForm([...r, i].join(".")).value !== void 0 && l.push(i);
|
|
77
|
+
}), l;
|
|
78
|
+
}, []), m = p(() => y(), []), [d, h] = L(m);
|
|
72
79
|
$(() => {
|
|
73
|
-
const { unregistry:
|
|
74
|
-
s.map(({ name:
|
|
80
|
+
const { unregistry: l } = t.registryExtraForm(
|
|
81
|
+
s.map(({ name: i }) => [...r, i].join(".")),
|
|
75
82
|
() => {
|
|
76
|
-
|
|
83
|
+
h(y());
|
|
77
84
|
}
|
|
78
85
|
);
|
|
79
86
|
return () => {
|
|
80
|
-
|
|
87
|
+
l();
|
|
81
88
|
};
|
|
82
89
|
}, []);
|
|
83
|
-
const
|
|
84
|
-
(
|
|
85
|
-
l
|
|
90
|
+
const v = j(
|
|
91
|
+
(l) => {
|
|
92
|
+
f[l] && !d.includes(l) && h((i) => [...i, l]);
|
|
86
93
|
},
|
|
87
|
-
[
|
|
88
|
-
),
|
|
89
|
-
|
|
94
|
+
[d]
|
|
95
|
+
), D = j((l) => {
|
|
96
|
+
h((i) => i.filter((x) => x !== l));
|
|
90
97
|
}, []);
|
|
91
|
-
return /* @__PURE__ */
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
return /* @__PURE__ */
|
|
98
|
+
return /* @__PURE__ */ P(q, { children: [
|
|
99
|
+
d.map((l) => {
|
|
100
|
+
const i = f[l];
|
|
101
|
+
return /* @__PURE__ */ b(C, { path: r, scheme: [i] }, `param-form--${l}`);
|
|
95
102
|
}),
|
|
96
|
-
e(
|
|
103
|
+
e(v, d)
|
|
97
104
|
] });
|
|
98
|
-
},
|
|
99
|
-
|
|
105
|
+
}, C = ({ scheme: n, path: r }) => /* @__PURE__ */ b(q, { children: n.map((e, s) => e.type === "layout" ? /* @__PURE__ */ b(e.Layout, { children: /* @__PURE__ */ b(C, { path: r, scheme: e.childs }) }, `layout-form--${s}-${r.length}`) : e.type === "context" ? /* @__PURE__ */ b(
|
|
106
|
+
C,
|
|
100
107
|
{
|
|
101
108
|
path: [...r, e.context],
|
|
102
109
|
scheme: e.childs
|
|
103
110
|
},
|
|
104
111
|
`context-form--${s}-${e.context}-${r.length}`
|
|
105
|
-
) : e.type === "condition" ? /* @__PURE__ */
|
|
106
|
-
|
|
112
|
+
) : e.type === "condition" ? /* @__PURE__ */ b(
|
|
113
|
+
_,
|
|
107
114
|
{
|
|
108
115
|
name: e.depended,
|
|
109
116
|
canActivate: e.canActivate,
|
|
110
|
-
children: /* @__PURE__ */
|
|
117
|
+
children: /* @__PURE__ */ b(C, { path: r, scheme: e.childs })
|
|
111
118
|
},
|
|
112
119
|
`condition--${s}-${e.depended}-${r.length}`
|
|
113
|
-
) : e.type === "extra" ? /* @__PURE__ */
|
|
114
|
-
|
|
120
|
+
) : e.type === "extra" ? /* @__PURE__ */ b(G, { path: r, item: e }, `partial-item--${s}-${r.length}`) : e.type === "visability" ? e.hidden ? null : /* @__PURE__ */ b(C, { path: r, scheme: e.childs }, `visability--${s}-${r.length}`) : /* @__PURE__ */ b(
|
|
121
|
+
z,
|
|
115
122
|
{
|
|
116
123
|
name: [...r, e.name].join("."),
|
|
117
124
|
item: e
|
|
118
125
|
},
|
|
119
126
|
`form-item--${s}-${e.name}-${r.length}`
|
|
120
|
-
)) }),
|
|
121
|
-
|
|
127
|
+
)) }), S = (n, r, e, s) => {
|
|
128
|
+
n.forEach((t) => {
|
|
122
129
|
if (t.type === "form" && s(t, [...r, t.name].join("."), e[t.name]), t.type === "context") {
|
|
123
|
-
|
|
130
|
+
S(t.childs, [...r, t.context], e[t.context] ?? {}, s);
|
|
124
131
|
return;
|
|
125
132
|
}
|
|
126
133
|
if (t.type === "layout" || t.type === "condition" || t.type === "visability") {
|
|
127
|
-
|
|
134
|
+
S(t.childs, r, e, s);
|
|
128
135
|
return;
|
|
129
136
|
}
|
|
130
|
-
t.type === "extra" && t.items.forEach(({ getForm:
|
|
131
|
-
|
|
137
|
+
t.type === "extra" && t.items.forEach(({ getForm: f, name: y }) => {
|
|
138
|
+
S([f(y, () => {
|
|
132
139
|
})], r, e, s);
|
|
133
140
|
});
|
|
134
141
|
});
|
|
135
|
-
},
|
|
136
|
-
|
|
142
|
+
}, E = ({ scheme: n, values: r, path: e, forms: s }) => {
|
|
143
|
+
n.forEach((t) => {
|
|
137
144
|
if (t.type === "form") {
|
|
138
|
-
const
|
|
139
|
-
s[
|
|
145
|
+
const f = [...e, t.name].join(".");
|
|
146
|
+
s[f].value !== void 0 && (r[t.name] = s[f].value);
|
|
140
147
|
}
|
|
141
148
|
if (t.type === "context") {
|
|
142
|
-
r[t.context] = {},
|
|
149
|
+
r[t.context] = {}, E({
|
|
143
150
|
scheme: t.childs,
|
|
144
151
|
path: [...e, t.context],
|
|
145
152
|
values: r[t.context],
|
|
@@ -148,185 +155,198 @@ const D = M({}), q = (o) => {
|
|
|
148
155
|
return;
|
|
149
156
|
}
|
|
150
157
|
if (t.type === "layout") {
|
|
151
|
-
|
|
158
|
+
E({ scheme: t.childs, path: e, values: r, forms: s });
|
|
152
159
|
return;
|
|
153
160
|
}
|
|
154
161
|
if (t.type === "visability") {
|
|
155
|
-
|
|
162
|
+
E({ scheme: t.childs, path: e, values: r, forms: s });
|
|
156
163
|
return;
|
|
157
164
|
}
|
|
158
165
|
if (t.type === "condition") {
|
|
159
|
-
const
|
|
160
|
-
if (t.canActivate(
|
|
161
|
-
|
|
166
|
+
const f = s[t.depended]?.value;
|
|
167
|
+
if (t.canActivate(f)) {
|
|
168
|
+
E({ scheme: t.childs, path: e, values: r, forms: s });
|
|
162
169
|
return;
|
|
163
170
|
}
|
|
164
171
|
}
|
|
165
|
-
t.type === "extra" && t.items.forEach(({ getForm:
|
|
166
|
-
|
|
172
|
+
t.type === "extra" && t.items.forEach(({ getForm: f, name: y }) => {
|
|
173
|
+
E({ scheme: [f(y, () => {
|
|
167
174
|
})], path: e, values: r, forms: s });
|
|
168
175
|
});
|
|
169
176
|
});
|
|
170
|
-
}, T = (
|
|
177
|
+
}, T = (n, r) => {
|
|
171
178
|
const e = {};
|
|
172
|
-
return
|
|
173
|
-
},
|
|
174
|
-
const { onSubmit: e,
|
|
179
|
+
return E({ scheme: n, path: [], values: e, forms: r }), e;
|
|
180
|
+
}, H = (n, r) => {
|
|
181
|
+
const { onSubmit: e, onChange: s, onEqual: t, disabled: f, onDirty: y, onFormDirty: m, onChangeEvent: d, defaultValues: h } = r, v = { onSubmit: e }, D = { onChange: s }, l = {
|
|
175
182
|
isActive: !1,
|
|
176
183
|
isSubmitted: !1,
|
|
177
184
|
dirtyCount: 0
|
|
178
|
-
},
|
|
185
|
+
}, i = {}, x = /* @__PURE__ */ new Set(), V = {
|
|
179
186
|
subscribers: [],
|
|
180
187
|
names: {}
|
|
181
188
|
};
|
|
182
|
-
|
|
183
|
-
|
|
189
|
+
S(n, [], h ?? {}, (o, c, u) => {
|
|
190
|
+
i[c] = {
|
|
184
191
|
error: null,
|
|
185
192
|
isDirty: !1,
|
|
186
|
-
value:
|
|
193
|
+
value: u,
|
|
187
194
|
// текущее (динамическое) состояние поля. Значение равно переданному дефолтному, либо: определяется как дефолтное значение компонента формы в момент его монтирования, а до этого - undefined.
|
|
188
|
-
startValue:
|
|
195
|
+
startValue: u ?? o.defaultValue,
|
|
189
196
|
// начальное (стартовое) значение (равно переданному дефолтному, либо значению формы по умолчанию). Исходя из сравнения текущего значения со стартовым, можно понять, модифицированно ли поле
|
|
190
|
-
defaultValue:
|
|
197
|
+
defaultValue: o.defaultValue,
|
|
191
198
|
// дефолтное значение компонента формы
|
|
192
199
|
rerender: () => {
|
|
193
200
|
},
|
|
194
201
|
subscribers: [],
|
|
195
|
-
validate:
|
|
202
|
+
validate: o.validate
|
|
196
203
|
};
|
|
197
204
|
});
|
|
198
|
-
const
|
|
199
|
-
|
|
205
|
+
const B = (o, c) => {
|
|
206
|
+
y?.({ name: o, isDirty: c }), c && l.dirtyCount === 0 && m?.(!0), !c && l.dirtyCount === 1 && m?.(!1), l.dirtyCount += c ? 1 : -1;
|
|
200
207
|
};
|
|
201
208
|
return {
|
|
202
209
|
submitRef: v,
|
|
203
|
-
|
|
204
|
-
|
|
210
|
+
changeRef: D,
|
|
211
|
+
scheme: n,
|
|
212
|
+
disabled: f,
|
|
205
213
|
activate() {
|
|
206
|
-
|
|
214
|
+
l.isActive = !0;
|
|
207
215
|
},
|
|
208
216
|
disactivate() {
|
|
209
|
-
|
|
217
|
+
l.isActive = !1;
|
|
210
218
|
},
|
|
211
|
-
setError(
|
|
212
|
-
const
|
|
213
|
-
|
|
219
|
+
setError(o, c) {
|
|
220
|
+
const u = this.getForm(o);
|
|
221
|
+
u && (u.error = {
|
|
214
222
|
error: !0,
|
|
215
|
-
messages: [
|
|
216
|
-
},
|
|
223
|
+
messages: [c.message]
|
|
224
|
+
}, u.rerender());
|
|
217
225
|
},
|
|
218
|
-
setFocus(
|
|
219
|
-
const
|
|
220
|
-
|
|
226
|
+
setFocus(o) {
|
|
227
|
+
const c = this.getForm(o);
|
|
228
|
+
c && c.focus && c.focus();
|
|
221
229
|
},
|
|
222
|
-
setValue(
|
|
223
|
-
const
|
|
224
|
-
|
|
230
|
+
setValue(o, c) {
|
|
231
|
+
const u = this.getForm(o);
|
|
232
|
+
u && u.value !== c && this.onChange(o, c);
|
|
225
233
|
},
|
|
226
234
|
getValues() {
|
|
227
|
-
return T(
|
|
235
|
+
return T(n, i);
|
|
228
236
|
},
|
|
229
237
|
async handleSubmit() {
|
|
230
|
-
if (
|
|
238
|
+
if (f)
|
|
231
239
|
return;
|
|
232
|
-
|
|
233
|
-
let
|
|
234
|
-
for (const
|
|
235
|
-
const
|
|
236
|
-
if (
|
|
237
|
-
const
|
|
238
|
-
|
|
240
|
+
l.isSubmitted = !0;
|
|
241
|
+
let o = !1, c = !1;
|
|
242
|
+
for (const u in i) {
|
|
243
|
+
const a = i[u];
|
|
244
|
+
if (a.value !== void 0) {
|
|
245
|
+
const g = a.validate(a.value);
|
|
246
|
+
g && (c = !0, a.error = g, a.rerender(), !o && a.focus && (a.focus(), o = !0));
|
|
239
247
|
}
|
|
240
248
|
}
|
|
241
|
-
if (!
|
|
242
|
-
if (
|
|
243
|
-
|
|
249
|
+
if (!c) {
|
|
250
|
+
if (l.dirtyCount === 0 && t) {
|
|
251
|
+
t();
|
|
244
252
|
return;
|
|
245
253
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
254
|
+
if (!v.onSubmit)
|
|
255
|
+
return;
|
|
256
|
+
const u = T(n, i);
|
|
257
|
+
await v.onSubmit(u, {
|
|
258
|
+
setError: (a, g) => {
|
|
259
|
+
const F = this.getForm(a);
|
|
260
|
+
F.error = {
|
|
251
261
|
error: !0,
|
|
252
|
-
messages: [
|
|
253
|
-
},
|
|
262
|
+
messages: [g.message]
|
|
263
|
+
}, F.rerender();
|
|
254
264
|
}
|
|
255
|
-
}).catch((
|
|
256
|
-
console.log("[formkit] submit exception:",
|
|
265
|
+
}).catch((a) => {
|
|
266
|
+
console.log("[formkit] submit exception:", a);
|
|
257
267
|
});
|
|
258
268
|
}
|
|
259
269
|
},
|
|
260
|
-
reset(
|
|
261
|
-
|
|
262
|
-
if (
|
|
270
|
+
reset(o, { asDirty: c }) {
|
|
271
|
+
S(n, [], o, (u, a, g) => {
|
|
272
|
+
if (g === void 0 && i[a].value === void 0)
|
|
263
273
|
return;
|
|
264
|
-
if (
|
|
265
|
-
a
|
|
274
|
+
if (V.names[a] && g === void 0) {
|
|
275
|
+
i[a].value = void 0;
|
|
266
276
|
return;
|
|
267
277
|
}
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
}),
|
|
271
|
-
|
|
278
|
+
const F = g ?? u.defaultValue;
|
|
279
|
+
c || (i[a].startValue = F, i[a].isDirty = !1), i[a].value !== F && this.onChange(a, F);
|
|
280
|
+
}), c || (l.dirtyCount = 0, l.isSubmitted = !1), V.subscribers.forEach(({ visualizeEvalueatedForms: u }) => {
|
|
281
|
+
u();
|
|
272
282
|
});
|
|
273
283
|
},
|
|
274
284
|
setAsDefault() {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}),
|
|
285
|
+
S(n, [], {}, (o, c) => {
|
|
286
|
+
i[c].startValue = i[c].value, i[c].isDirty = !1;
|
|
287
|
+
}), l.dirtyCount = 0, l.isSubmitted = !1;
|
|
278
288
|
},
|
|
279
|
-
registry(
|
|
280
|
-
return
|
|
289
|
+
registry(o, c) {
|
|
290
|
+
return i[o].rerender = c, i[o].value === void 0 && (i[o].value = i[o].defaultValue), {
|
|
281
291
|
unregistry() {
|
|
282
|
-
if (
|
|
283
|
-
},
|
|
284
|
-
|
|
285
|
-
const
|
|
286
|
-
|
|
292
|
+
if (i[o].rerender = () => {
|
|
293
|
+
}, i[o].focus = void 0, l.isActive) {
|
|
294
|
+
i[o].value = void 0;
|
|
295
|
+
const u = !M(i[o].startValue, i[o].defaultValue);
|
|
296
|
+
i[o].isDirty !== u && B(o, u), i[o].isDirty = u;
|
|
287
297
|
}
|
|
288
298
|
}
|
|
289
299
|
};
|
|
290
300
|
},
|
|
291
|
-
getForm(
|
|
292
|
-
return
|
|
301
|
+
getForm(o) {
|
|
302
|
+
return i[o];
|
|
293
303
|
},
|
|
294
|
-
subscribeToForm(
|
|
295
|
-
const
|
|
296
|
-
return
|
|
297
|
-
|
|
304
|
+
subscribeToForm(o, c) {
|
|
305
|
+
const u = { callback: c }, a = this.getForm(o);
|
|
306
|
+
return a.subscribers.push(u), () => {
|
|
307
|
+
a.subscribers = a.subscribers.filter((g) => g !== u);
|
|
298
308
|
};
|
|
299
309
|
},
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
310
|
+
subscribeToAny(o) {
|
|
311
|
+
return x.add(o), () => {
|
|
312
|
+
x.delete(o);
|
|
313
|
+
};
|
|
314
|
+
},
|
|
315
|
+
getNames() {
|
|
316
|
+
return Object.keys(i);
|
|
317
|
+
},
|
|
318
|
+
registryExtraForm(o, c) {
|
|
319
|
+
const u = { visualizeEvalueatedForms: c };
|
|
320
|
+
return o.forEach((a) => {
|
|
321
|
+
V.names[a] = !0;
|
|
322
|
+
}), V.subscribers.push(u), {
|
|
305
323
|
unregistry() {
|
|
306
|
-
|
|
324
|
+
V.subscribers = V.subscribers.filter((a) => a !== u);
|
|
307
325
|
}
|
|
308
326
|
};
|
|
309
327
|
},
|
|
310
|
-
onChange(
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
const
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
})
|
|
328
|
+
onChange(o, c) {
|
|
329
|
+
const u = this.getForm(o), a = !M(c, u.startValue);
|
|
330
|
+
u.isDirty !== a && B(o, a), u.isDirty = a, d?.({ name: o, value: c }), (u.error || l.isSubmitted) && (u.error = u.validate(c) || null);
|
|
331
|
+
const g = u.value;
|
|
332
|
+
u.value = c, u.rerender(), u.subscribers.forEach(({ callback: F }) => {
|
|
333
|
+
F(c, g);
|
|
334
|
+
}), x.forEach((F) => {
|
|
335
|
+
F();
|
|
336
|
+
}), D.onChange?.(T(n, i));
|
|
317
337
|
}
|
|
318
338
|
};
|
|
319
|
-
},
|
|
320
|
-
const e =
|
|
321
|
-
e.current === null && (e.current =
|
|
339
|
+
}, Y = (n, r) => {
|
|
340
|
+
const e = U(null);
|
|
341
|
+
e.current === null && (e.current = H(n, r));
|
|
322
342
|
const s = e.current;
|
|
323
343
|
return $(() => {
|
|
324
|
-
e.current && (e.current.submitRef.onSubmit = r.onSubmit);
|
|
325
|
-
}, [r.onSubmit]), {
|
|
344
|
+
e.current && (e.current.submitRef.onSubmit = r.onSubmit, e.current.changeRef.onChange = r.onChange);
|
|
345
|
+
}, [r.onSubmit, r.onChange]), {
|
|
326
346
|
manager: s,
|
|
327
|
-
useValue: (t) =>
|
|
328
|
-
reset: (t,
|
|
329
|
-
s.reset(t,
|
|
347
|
+
useValue: (t) => N(t),
|
|
348
|
+
reset: (t, f = {}) => {
|
|
349
|
+
s.reset(t, f);
|
|
330
350
|
},
|
|
331
351
|
setAsDefault: () => {
|
|
332
352
|
s.setAsDefault();
|
|
@@ -334,11 +354,11 @@ const D = M({}), q = (o) => {
|
|
|
334
354
|
setFocus: (t) => {
|
|
335
355
|
s.setFocus(t);
|
|
336
356
|
},
|
|
337
|
-
setError: (t,
|
|
338
|
-
s.setError(t,
|
|
357
|
+
setError: (t, f) => {
|
|
358
|
+
s.setError(t, f);
|
|
339
359
|
},
|
|
340
|
-
setValue: (t,
|
|
341
|
-
s.setValue(t,
|
|
360
|
+
setValue: (t, f) => {
|
|
361
|
+
s.setValue(t, f);
|
|
342
362
|
},
|
|
343
363
|
getValue: (t) => s.getForm(t).value,
|
|
344
364
|
getValues: () => s.getValues(),
|
|
@@ -346,15 +366,19 @@ const D = M({}), q = (o) => {
|
|
|
346
366
|
await s.handleSubmit();
|
|
347
367
|
},
|
|
348
368
|
// prettier-ignore
|
|
349
|
-
subscribe: (t,
|
|
369
|
+
subscribe: (t, f) => s.subscribeToForm(t, f),
|
|
370
|
+
// подписка на любое изменение значений формы (для гейтов поверх всех полей)
|
|
371
|
+
subscribeAny: (t) => s.subscribeToAny(t),
|
|
372
|
+
// плоский список имён всех полей (dotted-пути; вложенные context раскрыты)
|
|
373
|
+
getNames: () => s.getNames()
|
|
350
374
|
};
|
|
351
|
-
},
|
|
352
|
-
|
|
375
|
+
}, Z = ({ manager: n }) => (p(() => {
|
|
376
|
+
n.activate();
|
|
353
377
|
}, []), $(() => () => {
|
|
354
|
-
|
|
355
|
-
}, []), /* @__PURE__ */
|
|
378
|
+
n.disactivate();
|
|
379
|
+
}, []), /* @__PURE__ */ b(A.Provider, { value: n, children: /* @__PURE__ */ b(C, { scheme: n.scheme, path: [] }) })), w = (n) => {
|
|
356
380
|
try {
|
|
357
|
-
|
|
381
|
+
n();
|
|
358
382
|
} catch (r) {
|
|
359
383
|
return r && typeof r == "string" ? {
|
|
360
384
|
error: !0,
|
|
@@ -366,66 +390,67 @@ const D = M({}), q = (o) => {
|
|
|
366
390
|
error: !0
|
|
367
391
|
};
|
|
368
392
|
}
|
|
369
|
-
},
|
|
370
|
-
const s =
|
|
393
|
+
}, k = (n) => (r, e) => {
|
|
394
|
+
const s = n.getRequireCheck(e), { $rules: t = {}, ...f } = e, { validate: y, required: m } = t;
|
|
371
395
|
return {
|
|
372
396
|
type: "form",
|
|
373
397
|
name: r,
|
|
374
|
-
defaultValue:
|
|
375
|
-
Form:
|
|
376
|
-
validate: (
|
|
377
|
-
if (
|
|
378
|
-
const
|
|
379
|
-
if (
|
|
380
|
-
return
|
|
381
|
-
if (
|
|
382
|
-
const
|
|
383
|
-
if (
|
|
384
|
-
return
|
|
398
|
+
defaultValue: n.defaultValue,
|
|
399
|
+
Form: n.getForm(f),
|
|
400
|
+
validate: (d) => {
|
|
401
|
+
if (m) {
|
|
402
|
+
const h = w(() => s(d));
|
|
403
|
+
if (h)
|
|
404
|
+
return h;
|
|
405
|
+
if (y) {
|
|
406
|
+
const v = w(() => y(d));
|
|
407
|
+
if (v)
|
|
408
|
+
return v;
|
|
385
409
|
}
|
|
386
410
|
return;
|
|
387
411
|
}
|
|
388
|
-
if (
|
|
389
|
-
const
|
|
390
|
-
if (
|
|
391
|
-
return
|
|
412
|
+
if (y && !w(() => s(d))) {
|
|
413
|
+
const v = w(() => y(d));
|
|
414
|
+
if (v)
|
|
415
|
+
return v;
|
|
392
416
|
}
|
|
393
417
|
}
|
|
394
418
|
};
|
|
395
|
-
},
|
|
419
|
+
}, I = (n) => (r) => (...e) => ({
|
|
396
420
|
type: "layout",
|
|
397
|
-
Layout:
|
|
421
|
+
Layout: n(r),
|
|
398
422
|
childs: e
|
|
399
|
-
}),
|
|
400
|
-
getItems:
|
|
423
|
+
}), rr = ({
|
|
424
|
+
getItems: n,
|
|
401
425
|
getController: r
|
|
402
426
|
}) => ({
|
|
403
427
|
type: "extra",
|
|
404
|
-
items:
|
|
428
|
+
items: n((e) => (s) => ({ type: "registry", name: e, getForm: s })),
|
|
405
429
|
getController: r
|
|
406
|
-
}),
|
|
430
|
+
}), er = (n) => (...r) => ({
|
|
407
431
|
type: "context",
|
|
408
|
-
context:
|
|
432
|
+
context: n,
|
|
409
433
|
childs: r
|
|
410
|
-
}),
|
|
434
|
+
}), tr = (n, r) => (...e) => ({
|
|
411
435
|
type: "condition",
|
|
412
|
-
depended:
|
|
436
|
+
depended: n,
|
|
413
437
|
canActivate: r.canActivate,
|
|
414
438
|
childs: e
|
|
415
|
-
}),
|
|
439
|
+
}), sr = ({ hidden: n }) => (...r) => ({
|
|
416
440
|
type: "visability",
|
|
417
|
-
hidden:
|
|
441
|
+
hidden: n ?? !1,
|
|
418
442
|
childs: r
|
|
419
443
|
});
|
|
420
444
|
export {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
445
|
+
tr as Condition,
|
|
446
|
+
er as Context,
|
|
447
|
+
Z as FormLayout,
|
|
448
|
+
sr as Visablility,
|
|
449
|
+
rr as createExtraForm,
|
|
450
|
+
k as createForm,
|
|
451
|
+
I as createLayout,
|
|
452
|
+
X as createUseValue,
|
|
453
|
+
Y as useForm,
|
|
454
|
+
W as useSetValue,
|
|
455
|
+
N as useValue
|
|
431
456
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { useForm, FormLayout } from './useForm';
|
|
2
|
-
export { useValue, createUseValue } from './useValue';
|
|
2
|
+
export { useValue, useSetValue, createUseValue } from './useValue';
|
|
3
3
|
export { createForm } from './core/builders/create-form';
|
|
4
4
|
export { createLayout } from './core/builders/create-layout';
|
|
5
5
|
export { createExtraForm } from './core/builders/create-extra-form';
|
|
@@ -22,7 +22,10 @@ export interface ResetOptions {
|
|
|
22
22
|
}
|
|
23
23
|
export interface FormManager<Values, FlattenValues, ClonedValues> {
|
|
24
24
|
submitRef: {
|
|
25
|
-
onSubmit
|
|
25
|
+
onSubmit?: (values: Values, callbacks: SubmitCallbacls<FlattenValues>) => Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
changeRef: {
|
|
28
|
+
onChange?: (values: Values) => void;
|
|
26
29
|
};
|
|
27
30
|
scheme: SchemeItems.All[];
|
|
28
31
|
disabled?: boolean;
|
|
@@ -33,6 +36,8 @@ export interface FormManager<Values, FlattenValues, ClonedValues> {
|
|
|
33
36
|
unregistry: () => void;
|
|
34
37
|
};
|
|
35
38
|
subscribeToForm: <K extends keyof FlattenValues>(name: K & string, callback: (value: FlattenValues[K], prevValue: FlattenValues[K]) => void) => () => void;
|
|
39
|
+
subscribeToAny: (callback: () => void) => () => void;
|
|
40
|
+
getNames: () => string[];
|
|
36
41
|
registryExtraForm: (names: string[], callback: () => void) => {
|
|
37
42
|
unregistry: () => void;
|
|
38
43
|
};
|
package/dist/useForm.d.ts
CHANGED
|
@@ -30,7 +30,8 @@ export interface MainOptions<Values extends FieldValues, FlattenValues, ClonedVa
|
|
|
30
30
|
onDirty?: (event: DirtyEvent<FlattenValues>) => void;
|
|
31
31
|
onChangeEvent?: (event: ChangeEvent<FlattenValues>) => void;
|
|
32
32
|
onFormDirty?: (isDirty: boolean) => void;
|
|
33
|
-
|
|
33
|
+
onChange?: (values: Values) => void;
|
|
34
|
+
onSubmit?: (values: Values, callbacks: SubmitCallbacls<FlattenValues>) => Promise<void>;
|
|
34
35
|
}
|
|
35
36
|
export type GetDefaultValues<Items extends SchemeItems.All[]> = DeepPartial<ExtractValues<Items[number]>>;
|
|
36
37
|
declare const useDynamicForm: (scheme: SchemeItems.All[], opts: MainOptions<FieldValues, FieldValues, FieldValues>) => {
|
|
@@ -51,6 +52,8 @@ declare const useDynamicForm: (scheme: SchemeItems.All[], opts: MainOptions<Fiel
|
|
|
51
52
|
};
|
|
52
53
|
handleSubmit: () => Promise<void>;
|
|
53
54
|
subscribe: <K extends string>(name: K, callback: (value: any, prevValue: any) => void) => () => void;
|
|
55
|
+
subscribeAny: (callback: () => void) => () => void;
|
|
56
|
+
getNames: () => string[];
|
|
54
57
|
};
|
|
55
58
|
declare const useForm: <Items extends SchemeItems.All[], Values extends ExtractValues<Items[number]>, FlattenValues extends ExtractFlatValues<Items[number]>, Cloned extends Copy<Values>>(scheme: Items, opts: MainOptions<Values, FlattenValues, Cloned>) => {
|
|
56
59
|
manager: FormManager<Values, FlattenValues, Cloned>;
|
|
@@ -66,6 +69,8 @@ declare const useForm: <Items extends SchemeItems.All[], Values extends ExtractV
|
|
|
66
69
|
getValues: () => DeepPartial<Cloned>;
|
|
67
70
|
handleSubmit: () => Promise<void>;
|
|
68
71
|
subscribe: <K extends keyof FlattenValues>(name: K & string, callback: (value: FlattenValues[K], prevValue: FlattenValues[K]) => void) => () => void;
|
|
72
|
+
subscribeAny: (callback: () => void) => () => void;
|
|
73
|
+
getNames: () => string[];
|
|
69
74
|
};
|
|
70
75
|
interface Props {
|
|
71
76
|
manager: FormManager<any, any, any>;
|
package/dist/useValue.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SchemeItems } from './core/types';
|
|
2
2
|
import { ExtractFlatValues } from './core/extract';
|
|
3
3
|
export declare const useValue: <T = any>(name: string) => T;
|
|
4
|
+
export declare const useSetValue: <T = any>(name: string) => (value: T) => void;
|
|
4
5
|
export declare const createUseValue: <Items extends SchemeItems.All[]>() => <FlattenValues extends ExtractFlatValues<Items[number]>, K extends keyof FlattenValues>(name: K & string) => FlattenValues[K];
|