@companix/formkit 0.0.16 → 0.0.17
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 +154 -145
- package/dist/index.d.ts +1 -0
- package/dist/useValue.d.ts +4 -1
- package/package.json +2 -2
package/dist/bundle.es.js
CHANGED
|
@@ -1,44 +1,52 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { createContext as P, useState as
|
|
1
|
+
import { jsx as b, Fragment as j, jsxs as M } from "react/jsx-runtime";
|
|
2
|
+
import { createContext as P, useState as w, useContext as R, useMemo as p, useEffect as S, useCallback as A, useRef as T } from "react";
|
|
3
3
|
import L from "fast-deep-equal";
|
|
4
|
-
const D = P({}),
|
|
5
|
-
const [,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
const D = P({}), q = (o) => {
|
|
5
|
+
const [, e] = w([]), r = R(D);
|
|
6
|
+
if (!r)
|
|
7
|
+
throw new Error("useValue should be used in FormContext");
|
|
8
|
+
const n = p(() => {
|
|
9
|
+
const t = r.getForm(o);
|
|
10
|
+
if (!t)
|
|
11
|
+
throw new Error(`form with name "${o}" doesnt exist in form`);
|
|
12
|
+
return t;
|
|
13
|
+
}, [r, o]);
|
|
14
|
+
return S(() => {
|
|
15
|
+
const { unsubscribe: t } = r.subscribeToForm(o, () => {
|
|
16
|
+
e([]);
|
|
9
17
|
});
|
|
10
18
|
return () => {
|
|
11
19
|
t();
|
|
12
20
|
};
|
|
13
|
-
}, []),
|
|
14
|
-
}, N = ({ canActivate:
|
|
15
|
-
const
|
|
16
|
-
return n
|
|
17
|
-
},
|
|
18
|
-
const [,
|
|
19
|
-
|
|
20
|
-
}), []), f =
|
|
21
|
+
}, []), n.value;
|
|
22
|
+
}, O = () => (o) => q(o), N = ({ canActivate: o, name: e, children: r }) => {
|
|
23
|
+
const n = q(e);
|
|
24
|
+
return o(n) ? /* @__PURE__ */ b(j, { children: r }) : null;
|
|
25
|
+
}, U = ({ item: { Form: o }, name: e }) => {
|
|
26
|
+
const [, r] = w([]), n = R(D), { unregistry: t } = p(() => n.registry(e, () => {
|
|
27
|
+
r([]);
|
|
28
|
+
}), []), f = A(
|
|
21
29
|
(y) => {
|
|
22
30
|
if (y && y.focus) {
|
|
23
|
-
const v =
|
|
31
|
+
const v = n.getForm(e);
|
|
24
32
|
v.focus = () => {
|
|
25
33
|
y.focus();
|
|
26
34
|
};
|
|
27
35
|
}
|
|
28
36
|
},
|
|
29
|
-
[
|
|
37
|
+
[e]
|
|
30
38
|
);
|
|
31
|
-
|
|
39
|
+
S(() => () => {
|
|
32
40
|
t();
|
|
33
41
|
}, []);
|
|
34
|
-
const { value:
|
|
35
|
-
return /* @__PURE__ */
|
|
36
|
-
|
|
42
|
+
const { value: h, error: x } = n.getForm(e);
|
|
43
|
+
return /* @__PURE__ */ b(
|
|
44
|
+
o,
|
|
37
45
|
{
|
|
38
46
|
field: {
|
|
39
|
-
value:
|
|
47
|
+
value: h,
|
|
40
48
|
onChange: (y) => {
|
|
41
|
-
|
|
49
|
+
n.onChange(e, y);
|
|
42
50
|
},
|
|
43
51
|
onBlur: () => {
|
|
44
52
|
},
|
|
@@ -46,120 +54,120 @@ const D = P({}), B = (n) => {
|
|
|
46
54
|
},
|
|
47
55
|
isDirty: !1,
|
|
48
56
|
error: x,
|
|
49
|
-
disabled:
|
|
57
|
+
disabled: n.disabled
|
|
50
58
|
}
|
|
51
59
|
);
|
|
52
|
-
},
|
|
53
|
-
const { getController:
|
|
60
|
+
}, z = ({ item: o, path: e }) => {
|
|
61
|
+
const { getController: r, items: n } = o, t = R(D), f = p(() => {
|
|
54
62
|
const l = {};
|
|
55
|
-
return
|
|
63
|
+
return n.forEach(({ name: g, getForm: s }) => {
|
|
56
64
|
l[g] = s(g, () => a(g));
|
|
57
65
|
}), l;
|
|
58
|
-
}, []),
|
|
66
|
+
}, []), h = A(() => {
|
|
59
67
|
const l = [];
|
|
60
|
-
return
|
|
61
|
-
t.getForm([...
|
|
68
|
+
return n.forEach(({ name: g }) => {
|
|
69
|
+
t.getForm([...e, g].join(".")).value !== void 0 && l.push(g);
|
|
62
70
|
}), l;
|
|
63
|
-
}, []), x =
|
|
64
|
-
|
|
71
|
+
}, []), x = p(() => h(), []), [y, v] = w(x);
|
|
72
|
+
S(() => {
|
|
65
73
|
const { unregistry: l } = t.registryExtraForm(
|
|
66
|
-
|
|
74
|
+
n.map(({ name: g }) => [...e, g].join(".")),
|
|
67
75
|
() => {
|
|
68
|
-
v(
|
|
76
|
+
v(h());
|
|
69
77
|
}
|
|
70
78
|
);
|
|
71
79
|
return () => {
|
|
72
80
|
l();
|
|
73
81
|
};
|
|
74
82
|
}, []);
|
|
75
|
-
const d =
|
|
83
|
+
const d = A(
|
|
76
84
|
(l) => {
|
|
77
85
|
f[l] && !y.includes(l) && v((g) => [...g, l]);
|
|
78
86
|
},
|
|
79
87
|
[y]
|
|
80
|
-
), a =
|
|
88
|
+
), a = A((l) => {
|
|
81
89
|
v((g) => g.filter((s) => s !== l));
|
|
82
90
|
}, []);
|
|
83
91
|
return /* @__PURE__ */ M(j, { children: [
|
|
84
92
|
y.map((l) => {
|
|
85
93
|
const g = f[l];
|
|
86
|
-
return /* @__PURE__ */
|
|
94
|
+
return /* @__PURE__ */ b(C, { path: e, scheme: [g] }, `param-form--${l}`);
|
|
87
95
|
}),
|
|
88
|
-
|
|
96
|
+
r(d, y)
|
|
89
97
|
] });
|
|
90
|
-
}, C = ({ scheme:
|
|
98
|
+
}, C = ({ scheme: o, path: e }) => /* @__PURE__ */ b(j, { children: o.map((r, n) => r.type === "layout" ? /* @__PURE__ */ b(r.Layout, { children: /* @__PURE__ */ b(C, { path: e, scheme: r.childs }) }, `layout-form--${n}-${e.length}`) : r.type === "context" ? /* @__PURE__ */ b(
|
|
91
99
|
C,
|
|
92
100
|
{
|
|
93
|
-
path: [...
|
|
94
|
-
scheme:
|
|
101
|
+
path: [...e, r.context],
|
|
102
|
+
scheme: r.childs
|
|
95
103
|
},
|
|
96
|
-
`context-form--${
|
|
97
|
-
) :
|
|
104
|
+
`context-form--${n}-${r.context}-${e.length}`
|
|
105
|
+
) : r.type === "condition" ? /* @__PURE__ */ b(
|
|
98
106
|
N,
|
|
99
107
|
{
|
|
100
|
-
name:
|
|
101
|
-
canActivate:
|
|
102
|
-
children: /* @__PURE__ */
|
|
108
|
+
name: r.depended,
|
|
109
|
+
canActivate: r.canActivate,
|
|
110
|
+
children: /* @__PURE__ */ b(C, { path: e, scheme: r.childs })
|
|
103
111
|
},
|
|
104
|
-
`condition--${
|
|
105
|
-
) :
|
|
106
|
-
|
|
112
|
+
`condition--${n}-${r.depended}-${e.length}`
|
|
113
|
+
) : r.type === "extra" ? /* @__PURE__ */ b(z, { path: e, item: r }, `partial-item--${n}-${e.length}`) : /* @__PURE__ */ b(
|
|
114
|
+
U,
|
|
107
115
|
{
|
|
108
|
-
name: [...
|
|
109
|
-
item:
|
|
116
|
+
name: [...e, r.name].join("."),
|
|
117
|
+
item: r
|
|
110
118
|
},
|
|
111
|
-
`form-item--${
|
|
112
|
-
)) }), V = (
|
|
113
|
-
|
|
114
|
-
if (t.type === "form" &&
|
|
115
|
-
V(t.childs, [...
|
|
119
|
+
`form-item--${n}-${r.name}-${e.length}`
|
|
120
|
+
)) }), V = (o, e, r, n) => {
|
|
121
|
+
o.forEach((t) => {
|
|
122
|
+
if (t.type === "form" && n(t, [...e, t.name].join("."), r[t.name]), t.type === "context") {
|
|
123
|
+
V(t.childs, [...e, t.context], r[t.context] ?? {}, n);
|
|
116
124
|
return;
|
|
117
125
|
}
|
|
118
126
|
if (t.type === "layout" || t.type === "condition") {
|
|
119
|
-
V(t.childs,
|
|
127
|
+
V(t.childs, e, r, n);
|
|
120
128
|
return;
|
|
121
129
|
}
|
|
122
|
-
t.type === "extra" && t.items.forEach(({ getForm: f, name:
|
|
123
|
-
V([f(
|
|
124
|
-
})],
|
|
130
|
+
t.type === "extra" && t.items.forEach(({ getForm: f, name: h }) => {
|
|
131
|
+
V([f(h, () => {
|
|
132
|
+
})], e, r, n);
|
|
125
133
|
});
|
|
126
134
|
});
|
|
127
|
-
}, E = ({ scheme:
|
|
128
|
-
|
|
135
|
+
}, E = ({ scheme: o, values: e, path: r, forms: n }) => {
|
|
136
|
+
o.forEach((t) => {
|
|
129
137
|
if (t.type === "form") {
|
|
130
|
-
const f = [...
|
|
131
|
-
|
|
138
|
+
const f = [...r, t.name].join(".");
|
|
139
|
+
n[f].value !== void 0 && (e[t.name] = n[f].value);
|
|
132
140
|
}
|
|
133
141
|
if (t.type === "context") {
|
|
134
|
-
|
|
142
|
+
e[t.context] = {}, E({
|
|
135
143
|
scheme: t.childs,
|
|
136
|
-
path: [...
|
|
137
|
-
values:
|
|
138
|
-
forms:
|
|
144
|
+
path: [...r, t.context],
|
|
145
|
+
values: e[t.context],
|
|
146
|
+
forms: n
|
|
139
147
|
});
|
|
140
148
|
return;
|
|
141
149
|
}
|
|
142
150
|
if (t.type === "layout") {
|
|
143
|
-
E({ scheme: t.childs, path:
|
|
151
|
+
E({ scheme: t.childs, path: r, values: e, forms: n });
|
|
144
152
|
return;
|
|
145
153
|
}
|
|
146
154
|
if (t.type === "condition") {
|
|
147
|
-
const f =
|
|
155
|
+
const f = n[t.depended]?.value;
|
|
148
156
|
if (t.canActivate(f)) {
|
|
149
|
-
E({ scheme: t.childs, path:
|
|
157
|
+
E({ scheme: t.childs, path: r, values: e, forms: n });
|
|
150
158
|
return;
|
|
151
159
|
}
|
|
152
160
|
}
|
|
153
|
-
t.type === "extra" && t.items.forEach(({ getForm: f, name:
|
|
154
|
-
E({ scheme: [f(
|
|
155
|
-
})], path:
|
|
161
|
+
t.type === "extra" && t.items.forEach(({ getForm: f, name: h }) => {
|
|
162
|
+
E({ scheme: [f(h, () => {
|
|
163
|
+
})], path: r, values: e, forms: n });
|
|
156
164
|
});
|
|
157
165
|
});
|
|
158
|
-
},
|
|
159
|
-
const
|
|
160
|
-
return E({ scheme:
|
|
161
|
-
},
|
|
162
|
-
const { onSubmit:
|
|
166
|
+
}, B = (o, e) => {
|
|
167
|
+
const r = {};
|
|
168
|
+
return E({ scheme: o, path: [], values: r, forms: e }), r;
|
|
169
|
+
}, G = (o, e) => {
|
|
170
|
+
const { onSubmit: r, onEqual: n, disabled: t, onDirty: f, onFormDirty: h, onChangeEvent: x, defaultValues: y } = e, v = { onSubmit: r }, d = {
|
|
163
171
|
isActive: !1,
|
|
164
172
|
isSubmitted: !1,
|
|
165
173
|
dirtyCount: 0
|
|
@@ -167,7 +175,7 @@ const D = P({}), B = (n) => {
|
|
|
167
175
|
subscribers: [],
|
|
168
176
|
names: {}
|
|
169
177
|
};
|
|
170
|
-
V(
|
|
178
|
+
V(o, [], y ?? {}, (s, u, i) => {
|
|
171
179
|
a[u] = {
|
|
172
180
|
error: null,
|
|
173
181
|
isDirty: !1,
|
|
@@ -184,11 +192,11 @@ const D = P({}), B = (n) => {
|
|
|
184
192
|
};
|
|
185
193
|
});
|
|
186
194
|
const g = (s, u) => {
|
|
187
|
-
f?.({ name: s, isDirty: u }), u && d.dirtyCount === 0 &&
|
|
195
|
+
f?.({ name: s, isDirty: u }), u && d.dirtyCount === 0 && h?.(!0), !u && d.dirtyCount === 1 && h?.(!1), d.dirtyCount += u ? 1 : -1;
|
|
188
196
|
};
|
|
189
197
|
return {
|
|
190
198
|
submitRef: v,
|
|
191
|
-
scheme:
|
|
199
|
+
scheme: o,
|
|
192
200
|
disabled: t,
|
|
193
201
|
activate() {
|
|
194
202
|
d.isActive = !0;
|
|
@@ -212,7 +220,7 @@ const D = P({}), B = (n) => {
|
|
|
212
220
|
i && i.value !== u && this.onChange(s, u);
|
|
213
221
|
},
|
|
214
222
|
getValues() {
|
|
215
|
-
return
|
|
223
|
+
return B(o, a);
|
|
216
224
|
},
|
|
217
225
|
async handleSubmit() {
|
|
218
226
|
if (t)
|
|
@@ -228,10 +236,10 @@ const D = P({}), B = (n) => {
|
|
|
228
236
|
}
|
|
229
237
|
if (!u) {
|
|
230
238
|
if (d.dirtyCount === 0) {
|
|
231
|
-
|
|
239
|
+
n?.();
|
|
232
240
|
return;
|
|
233
241
|
}
|
|
234
|
-
const i =
|
|
242
|
+
const i = B(o, a);
|
|
235
243
|
await v.onSubmit(i, {
|
|
236
244
|
setError: (c, F) => {
|
|
237
245
|
const m = this.getForm(c);
|
|
@@ -244,7 +252,7 @@ const D = P({}), B = (n) => {
|
|
|
244
252
|
}
|
|
245
253
|
},
|
|
246
254
|
reset(s, { asDirty: u }) {
|
|
247
|
-
V(
|
|
255
|
+
V(o, [], s, (i, c, F) => {
|
|
248
256
|
if (F === void 0 && a[c].value === void 0)
|
|
249
257
|
return;
|
|
250
258
|
if (l.names[c] && F === void 0) {
|
|
@@ -258,7 +266,7 @@ const D = P({}), B = (n) => {
|
|
|
258
266
|
});
|
|
259
267
|
},
|
|
260
268
|
setAsDefault() {
|
|
261
|
-
V(
|
|
269
|
+
V(o, [], {}, ({}, s) => {
|
|
262
270
|
a[s].startValue = a[s].value, a[s].isDirty = !1;
|
|
263
271
|
}), d.dirtyCount = 0, d.isSubmitted = !1;
|
|
264
272
|
},
|
|
@@ -302,106 +310,107 @@ const D = P({}), B = (n) => {
|
|
|
302
310
|
});
|
|
303
311
|
}
|
|
304
312
|
};
|
|
305
|
-
}, Q = (
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
const
|
|
309
|
-
return
|
|
310
|
-
|
|
311
|
-
}, [
|
|
312
|
-
manager:
|
|
313
|
-
useValue: (t) =>
|
|
313
|
+
}, Q = (o, e) => {
|
|
314
|
+
const r = T(null);
|
|
315
|
+
r.current === null && (r.current = G(o, e));
|
|
316
|
+
const n = r.current;
|
|
317
|
+
return S(() => {
|
|
318
|
+
r.current && (r.current.submitRef.onSubmit = e.onSubmit);
|
|
319
|
+
}, [e.onSubmit]), {
|
|
320
|
+
manager: n,
|
|
321
|
+
useValue: (t) => q(t),
|
|
314
322
|
reset: (t, f = {}) => {
|
|
315
|
-
|
|
323
|
+
n.reset(t, f);
|
|
316
324
|
},
|
|
317
325
|
setAsDefault: () => {
|
|
318
|
-
|
|
326
|
+
n.setAsDefault();
|
|
319
327
|
},
|
|
320
328
|
setFocus: (t) => {
|
|
321
|
-
|
|
329
|
+
n.setFocus(t);
|
|
322
330
|
},
|
|
323
331
|
setError: (t, f) => {
|
|
324
|
-
|
|
332
|
+
n.setError(t, f);
|
|
325
333
|
},
|
|
326
334
|
setValue: (t, f) => {
|
|
327
|
-
|
|
335
|
+
n.setValue(t, f);
|
|
328
336
|
},
|
|
329
|
-
getValues: () =>
|
|
337
|
+
getValues: () => n.getValues(),
|
|
330
338
|
handleSubmit: async () => {
|
|
331
|
-
await
|
|
339
|
+
await n.handleSubmit();
|
|
332
340
|
}
|
|
333
341
|
};
|
|
334
|
-
},
|
|
335
|
-
|
|
336
|
-
}, []),
|
|
337
|
-
|
|
338
|
-
}, []), /* @__PURE__ */
|
|
342
|
+
}, W = ({ manager: o }) => (p(() => {
|
|
343
|
+
o.activate();
|
|
344
|
+
}, []), S(() => () => {
|
|
345
|
+
o.disactivate();
|
|
346
|
+
}, []), /* @__PURE__ */ b(D.Provider, { value: o, children: /* @__PURE__ */ b(C, { scheme: o.scheme, path: [] }) })), $ = (o) => {
|
|
339
347
|
try {
|
|
340
|
-
|
|
341
|
-
} catch (
|
|
342
|
-
return
|
|
348
|
+
o();
|
|
349
|
+
} catch (e) {
|
|
350
|
+
return e && typeof e == "string" ? {
|
|
343
351
|
error: !0,
|
|
344
|
-
messages: [
|
|
345
|
-
} :
|
|
352
|
+
messages: [e]
|
|
353
|
+
} : e instanceof Array && e.every((r) => r && typeof r == "string") ? {
|
|
346
354
|
error: !0,
|
|
347
|
-
messages:
|
|
355
|
+
messages: e
|
|
348
356
|
} : {
|
|
349
357
|
error: !0
|
|
350
358
|
};
|
|
351
359
|
}
|
|
352
|
-
},
|
|
353
|
-
const
|
|
360
|
+
}, X = (o) => (e, r) => {
|
|
361
|
+
const n = o.getRequireCheck(r), { $rules: t = {}, ...f } = r, { validate: h, required: x } = t;
|
|
354
362
|
return {
|
|
355
363
|
type: "form",
|
|
356
|
-
name:
|
|
357
|
-
defaultValue:
|
|
358
|
-
Form:
|
|
364
|
+
name: e,
|
|
365
|
+
defaultValue: o.defaultValue,
|
|
366
|
+
Form: o.getForm(f),
|
|
359
367
|
validate: (y) => {
|
|
360
368
|
if (x) {
|
|
361
|
-
const v =
|
|
369
|
+
const v = $(() => n(y));
|
|
362
370
|
if (v)
|
|
363
371
|
return v;
|
|
364
|
-
if (
|
|
365
|
-
const d =
|
|
372
|
+
if (h) {
|
|
373
|
+
const d = $(() => h(y));
|
|
366
374
|
if (d)
|
|
367
375
|
return d;
|
|
368
376
|
}
|
|
369
377
|
return;
|
|
370
378
|
}
|
|
371
|
-
if (
|
|
372
|
-
const d =
|
|
379
|
+
if (h && !$(() => n(y))) {
|
|
380
|
+
const d = $(() => h(y));
|
|
373
381
|
if (d)
|
|
374
382
|
return d;
|
|
375
383
|
}
|
|
376
384
|
}
|
|
377
385
|
};
|
|
378
|
-
},
|
|
386
|
+
}, Y = (o) => (e) => (...r) => ({
|
|
379
387
|
type: "layout",
|
|
380
|
-
Layout:
|
|
381
|
-
childs:
|
|
382
|
-
}),
|
|
383
|
-
getItems:
|
|
384
|
-
getController:
|
|
388
|
+
Layout: o(e),
|
|
389
|
+
childs: r
|
|
390
|
+
}), Z = ({
|
|
391
|
+
getItems: o,
|
|
392
|
+
getController: e
|
|
385
393
|
}) => ({
|
|
386
394
|
type: "extra",
|
|
387
|
-
items:
|
|
388
|
-
getController:
|
|
389
|
-
}),
|
|
395
|
+
items: o((r) => (n) => ({ type: "registry", name: r, getForm: n })),
|
|
396
|
+
getController: e
|
|
397
|
+
}), _ = (o) => (...e) => ({
|
|
390
398
|
type: "context",
|
|
391
|
-
context:
|
|
392
|
-
childs: r
|
|
393
|
-
}), _ = (n, r) => (...e) => ({
|
|
394
|
-
type: "condition",
|
|
395
|
-
depended: n,
|
|
396
|
-
canActivate: r.canActivate,
|
|
399
|
+
context: o,
|
|
397
400
|
childs: e
|
|
401
|
+
}), k = (o, e) => (...r) => ({
|
|
402
|
+
type: "condition",
|
|
403
|
+
depended: o,
|
|
404
|
+
canActivate: e.canActivate,
|
|
405
|
+
childs: r
|
|
398
406
|
});
|
|
399
407
|
export {
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
408
|
+
k as Condition,
|
|
409
|
+
_ as Context,
|
|
410
|
+
W as FormLayout,
|
|
411
|
+
Z as createExtraForm,
|
|
412
|
+
X as createForm,
|
|
413
|
+
Y as createLayout,
|
|
414
|
+
O as createUseValue,
|
|
406
415
|
Q as useForm
|
|
407
416
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { useForm, FormLayout } from './useForm';
|
|
2
|
+
export { createUseValue } from './useValue';
|
|
2
3
|
export { createForm } from './core/builders/create-form';
|
|
3
4
|
export { createLayout } from './core/builders/create-layout';
|
|
4
5
|
export { createExtraForm } from './core/builders/create-extra-form';
|
package/dist/useValue.d.ts
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import { SchemeItems } from './core/types';
|
|
2
|
+
import { ExtractFlatValues } from './core/extract';
|
|
3
|
+
export declare const useValue: <T = any>(name: string) => T;
|
|
4
|
+
export declare const createUseValue: <Items extends SchemeItems.All[]>() => <FlattenValues extends ExtractFlatValues<Items[number]>, K extends keyof FlattenValues>(name: K & string) => FlattenValues[K];
|
package/package.json
CHANGED