@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 CHANGED
@@ -1,36 +1,43 @@
1
- import { jsx as h, Fragment as j, jsxs as B } from "react/jsx-runtime";
2
- import { createContext as M, useState as w, useContext as R, useMemo as C, useEffect as $, useCallback as A, useRef as P } from "react";
3
- import L from "fast-deep-equal";
4
- const D = M({}), q = (o) => {
5
- const [, r] = w([]), e = R(D);
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 = C(() => {
9
- const t = e.getForm(o);
8
+ const s = p(() => {
9
+ const t = e.getForm(n);
10
10
  if (!t)
11
- throw new Error(`form with name "${o}" doesnt exist in form`);
11
+ throw new Error(`form with name "${n}" doesnt exist in form`);
12
12
  return t;
13
- }, [e, o]);
13
+ }, [e, n]);
14
14
  return $(() => {
15
- const t = e.subscribeToForm(o, () => {
15
+ const t = e.subscribeToForm(n, () => {
16
16
  r([]);
17
17
  });
18
18
  return () => {
19
19
  t();
20
20
  };
21
21
  }, []), s.value;
22
- }, O = () => (o) => q(o), N = ({ canActivate: o, name: r, children: e }) => {
23
- const s = q(r);
24
- return o(s) ? /* @__PURE__ */ h(j, { children: e }) : null;
25
- }, U = ({ item: { Form: o }, name: r }) => {
26
- const [, e] = w([]), s = R(D), { unregistry: t } = C(() => s.registry(r, () => {
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
- }), []), l = A(
29
- (y) => {
30
- if (y && y.focus) {
31
- const v = s.getForm(r);
32
- v.focus = () => {
33
- y.focus();
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: g, error: V } = s.getForm(r);
43
- return /* @__PURE__ */ h(
44
- o,
49
+ const { value: y, error: m } = s.getForm(r);
50
+ return /* @__PURE__ */ b(
51
+ n,
45
52
  {
46
53
  field: {
47
- value: g,
48
- onChange: (y) => {
49
- s.onChange(r, y);
54
+ value: y,
55
+ onChange: (d) => {
56
+ s.onChange(r, d);
50
57
  },
51
58
  onBlur: () => {
52
59
  },
53
- ref: l
60
+ ref: f
54
61
  },
55
62
  isDirty: !1,
56
- error: V,
63
+ error: m,
57
64
  disabled: s.disabled
58
65
  }
59
66
  );
60
- }, z = ({ item: o, path: r }) => {
61
- const { getController: e, items: s } = o, t = R(D), l = C(() => {
62
- const f = {};
63
- return s.forEach(({ name: b, getForm: n }) => {
64
- f[b] = n(b, () => a(b));
65
- }), f;
66
- }, []), g = A(() => {
67
- const f = [];
68
- return s.forEach(({ name: b }) => {
69
- t.getForm([...r, b].join(".")).value !== void 0 && f.push(b);
70
- }), f;
71
- }, []), V = C(() => g(), []), [y, v] = w(V);
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: f } = t.registryExtraForm(
74
- s.map(({ name: b }) => [...r, b].join(".")),
80
+ const { unregistry: l } = t.registryExtraForm(
81
+ s.map(({ name: i }) => [...r, i].join(".")),
75
82
  () => {
76
- v(g());
83
+ h(y());
77
84
  }
78
85
  );
79
86
  return () => {
80
- f();
87
+ l();
81
88
  };
82
89
  }, []);
83
- const d = A(
84
- (f) => {
85
- l[f] && !y.includes(f) && v((b) => [...b, f]);
90
+ const v = j(
91
+ (l) => {
92
+ f[l] && !d.includes(l) && h((i) => [...i, l]);
86
93
  },
87
- [y]
88
- ), a = A((f) => {
89
- v((b) => b.filter((n) => n !== f));
94
+ [d]
95
+ ), D = j((l) => {
96
+ h((i) => i.filter((x) => x !== l));
90
97
  }, []);
91
- return /* @__PURE__ */ B(j, { children: [
92
- y.map((f) => {
93
- const b = l[f];
94
- return /* @__PURE__ */ h(E, { path: r, scheme: [b] }, `param-form--${f}`);
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(d, y)
103
+ e(v, d)
97
104
  ] });
98
- }, E = ({ scheme: o, path: r }) => /* @__PURE__ */ h(j, { children: o.map((e, s) => e.type === "layout" ? /* @__PURE__ */ h(e.Layout, { children: /* @__PURE__ */ h(E, { path: r, scheme: e.childs }) }, `layout-form--${s}-${r.length}`) : e.type === "context" ? /* @__PURE__ */ h(
99
- E,
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__ */ h(
106
- N,
112
+ ) : e.type === "condition" ? /* @__PURE__ */ b(
113
+ _,
107
114
  {
108
115
  name: e.depended,
109
116
  canActivate: e.canActivate,
110
- children: /* @__PURE__ */ h(E, { path: r, scheme: e.childs })
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__ */ h(z, { path: r, item: e }, `partial-item--${s}-${r.length}`) : e.type === "visability" ? e.hidden ? null : /* @__PURE__ */ h(E, { path: r, scheme: e.childs }, `visability--${s}-${r.length}`) : /* @__PURE__ */ h(
114
- U,
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
- )) }), p = (o, r, e, s) => {
121
- o.forEach((t) => {
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
- p(t.childs, [...r, t.context], e[t.context] ?? {}, s);
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
- p(t.childs, r, e, s);
134
+ S(t.childs, r, e, s);
128
135
  return;
129
136
  }
130
- t.type === "extra" && t.items.forEach(({ getForm: l, name: g }) => {
131
- p([l(g, () => {
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
- }, m = ({ scheme: o, values: r, path: e, forms: s }) => {
136
- o.forEach((t) => {
142
+ }, E = ({ scheme: n, values: r, path: e, forms: s }) => {
143
+ n.forEach((t) => {
137
144
  if (t.type === "form") {
138
- const l = [...e, t.name].join(".");
139
- s[l].value !== void 0 && (r[t.name] = s[l].value);
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] = {}, m({
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
- m({ scheme: t.childs, path: e, values: r, forms: s });
158
+ E({ scheme: t.childs, path: e, values: r, forms: s });
152
159
  return;
153
160
  }
154
161
  if (t.type === "visability") {
155
- m({ scheme: t.childs, path: e, values: r, forms: s });
162
+ E({ scheme: t.childs, path: e, values: r, forms: s });
156
163
  return;
157
164
  }
158
165
  if (t.type === "condition") {
159
- const l = s[t.depended]?.value;
160
- if (t.canActivate(l)) {
161
- m({ scheme: t.childs, path: e, values: r, forms: s });
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: l, name: g }) => {
166
- m({ scheme: [l(g, () => {
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 = (o, r) => {
177
+ }, T = (n, r) => {
171
178
  const e = {};
172
- return m({ scheme: o, path: [], values: e, forms: r }), e;
173
- }, G = (o, r) => {
174
- const { onSubmit: e, onEqual: s, disabled: t, onDirty: l, onFormDirty: g, onChangeEvent: V, defaultValues: y } = r, v = { onSubmit: e }, d = {
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
- }, a = {}, f = {
185
+ }, i = {}, x = /* @__PURE__ */ new Set(), V = {
179
186
  subscribers: [],
180
187
  names: {}
181
188
  };
182
- p(o, [], y ?? {}, (n, u, i) => {
183
- a[u] = {
189
+ S(n, [], h ?? {}, (o, c, u) => {
190
+ i[c] = {
184
191
  error: null,
185
192
  isDirty: !1,
186
- value: i,
193
+ value: u,
187
194
  // текущее (динамическое) состояние поля. Значение равно переданному дефолтному, либо: определяется как дефолтное значение компонента формы в момент его монтирования, а до этого - undefined.
188
- startValue: i ?? n.defaultValue,
195
+ startValue: u ?? o.defaultValue,
189
196
  // начальное (стартовое) значение (равно переданному дефолтному, либо значению формы по умолчанию). Исходя из сравнения текущего значения со стартовым, можно понять, модифицированно ли поле
190
- defaultValue: n.defaultValue,
197
+ defaultValue: o.defaultValue,
191
198
  // дефолтное значение компонента формы
192
199
  rerender: () => {
193
200
  },
194
201
  subscribers: [],
195
- validate: n.validate
202
+ validate: o.validate
196
203
  };
197
204
  });
198
- const b = (n, u) => {
199
- l?.({ name: n, isDirty: u }), u && d.dirtyCount === 0 && g?.(!0), !u && d.dirtyCount === 1 && g?.(!1), d.dirtyCount += u ? 1 : -1;
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
- scheme: o,
204
- disabled: t,
210
+ changeRef: D,
211
+ scheme: n,
212
+ disabled: f,
205
213
  activate() {
206
- d.isActive = !0;
214
+ l.isActive = !0;
207
215
  },
208
216
  disactivate() {
209
- d.isActive = !1;
217
+ l.isActive = !1;
210
218
  },
211
- setError(n, u) {
212
- const i = this.getForm(n);
213
- i && (i.error = {
219
+ setError(o, c) {
220
+ const u = this.getForm(o);
221
+ u && (u.error = {
214
222
  error: !0,
215
- messages: [u.message]
216
- }, i.rerender());
223
+ messages: [c.message]
224
+ }, u.rerender());
217
225
  },
218
- setFocus(n) {
219
- const u = this.getForm(n);
220
- u && u.focus && u.focus();
226
+ setFocus(o) {
227
+ const c = this.getForm(o);
228
+ c && c.focus && c.focus();
221
229
  },
222
- setValue(n, u) {
223
- const i = this.getForm(n);
224
- i && i.value !== u && this.onChange(n, u);
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(o, a);
235
+ return T(n, i);
228
236
  },
229
237
  async handleSubmit() {
230
- if (t)
238
+ if (f)
231
239
  return;
232
- d.isSubmitted = !0;
233
- let n = !1, u = !1;
234
- for (const i in a) {
235
- const c = a[i];
236
- if (c.value !== void 0) {
237
- const F = c.validate(c.value);
238
- F && (u = !0, c.error = F, c.rerender(), !n && c.focus && (c.focus(), n = !0));
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 (!u) {
242
- if (d.dirtyCount === 0 && s) {
243
- s();
249
+ if (!c) {
250
+ if (l.dirtyCount === 0 && t) {
251
+ t();
244
252
  return;
245
253
  }
246
- const i = T(o, a);
247
- await v.onSubmit(i, {
248
- setError: (c, F) => {
249
- const x = this.getForm(c);
250
- x.error = {
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: [F.message]
253
- }, x.rerender();
262
+ messages: [g.message]
263
+ }, F.rerender();
254
264
  }
255
- }).catch((c) => {
256
- console.log("[formkit] submit exception:", c);
265
+ }).catch((a) => {
266
+ console.log("[formkit] submit exception:", a);
257
267
  });
258
268
  }
259
269
  },
260
- reset(n, { asDirty: u }) {
261
- p(o, [], n, (i, c, F) => {
262
- if (F === void 0 && a[c].value === void 0)
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 (f.names[c] && F === void 0) {
265
- a[c].value = void 0;
274
+ if (V.names[a] && g === void 0) {
275
+ i[a].value = void 0;
266
276
  return;
267
277
  }
268
- const x = F ?? i.defaultValue;
269
- u || (a[c].startValue = x, a[c].isDirty = !1), a[c].value !== x && this.onChange(c, x);
270
- }), u || (d.dirtyCount = 0, d.isSubmitted = !1), f.subscribers.forEach(({ visualizeEvalueatedForms: i }) => {
271
- i();
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
- p(o, [], {}, ({}, n) => {
276
- a[n].startValue = a[n].value, a[n].isDirty = !1;
277
- }), d.dirtyCount = 0, d.isSubmitted = !1;
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(n, u) {
280
- return a[n].rerender = u, a[n].value === void 0 && (a[n].value = a[n].defaultValue), {
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 (a[n].rerender = () => {
283
- }, a[n].focus = void 0, d.isActive) {
284
- a[n].value = void 0;
285
- const i = !L(a[n].startValue, a[n].defaultValue);
286
- a[n].isDirty !== i && b(n, i), a[n].isDirty = i;
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(n) {
292
- return a[n];
301
+ getForm(o) {
302
+ return i[o];
293
303
  },
294
- subscribeToForm(n, u) {
295
- const i = { callback: u }, c = this.getForm(n);
296
- return c.subscribers.push(i), () => {
297
- c.subscribers = c.subscribers.filter((F) => F !== i);
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
- registryExtraForm(n, u) {
301
- const i = { visualizeEvalueatedForms: u };
302
- return n.forEach((c) => {
303
- f.names[c] = !0;
304
- }), f.subscribers.push(i), {
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
- f.subscribers = f.subscribers.filter((c) => c !== i);
324
+ V.subscribers = V.subscribers.filter((a) => a !== u);
307
325
  }
308
326
  };
309
327
  },
310
- onChange(n, u) {
311
- const i = this.getForm(n), c = !L(u, i.startValue);
312
- i.isDirty !== c && b(n, c), i.isDirty = c, V?.({ name: n, value: u }), (i.error || d.isSubmitted) && (i.error = i.validate(u) || null);
313
- const F = i.value;
314
- i.value = u, i.rerender(), i.subscribers.forEach(({ callback: x }) => {
315
- x(u, F);
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
- }, Q = (o, r) => {
320
- const e = P(null);
321
- e.current === null && (e.current = G(o, r));
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) => q(t),
328
- reset: (t, l = {}) => {
329
- s.reset(t, l);
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, l) => {
338
- s.setError(t, l);
357
+ setError: (t, f) => {
358
+ s.setError(t, f);
339
359
  },
340
- setValue: (t, l) => {
341
- s.setValue(t, l);
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, l) => s.subscribeToForm(t, l)
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
- }, W = ({ manager: o }) => (C(() => {
352
- o.activate();
375
+ }, Z = ({ manager: n }) => (p(() => {
376
+ n.activate();
353
377
  }, []), $(() => () => {
354
- o.disactivate();
355
- }, []), /* @__PURE__ */ h(D.Provider, { value: o, children: /* @__PURE__ */ h(E, { scheme: o.scheme, path: [] }) })), S = (o) => {
378
+ n.disactivate();
379
+ }, []), /* @__PURE__ */ b(A.Provider, { value: n, children: /* @__PURE__ */ b(C, { scheme: n.scheme, path: [] }) })), w = (n) => {
356
380
  try {
357
- o();
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
- }, X = (o) => (r, e) => {
370
- const s = o.getRequireCheck(e), { $rules: t = {}, ...l } = e, { validate: g, required: V } = t;
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: o.defaultValue,
375
- Form: o.getForm(l),
376
- validate: (y) => {
377
- if (V) {
378
- const v = S(() => s(y));
379
- if (v)
380
- return v;
381
- if (g) {
382
- const d = S(() => g(y));
383
- if (d)
384
- return d;
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 (g && !S(() => s(y))) {
389
- const d = S(() => g(y));
390
- if (d)
391
- return d;
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
- }, Y = (o) => (r) => (...e) => ({
419
+ }, I = (n) => (r) => (...e) => ({
396
420
  type: "layout",
397
- Layout: o(r),
421
+ Layout: n(r),
398
422
  childs: e
399
- }), Z = ({
400
- getItems: o,
423
+ }), rr = ({
424
+ getItems: n,
401
425
  getController: r
402
426
  }) => ({
403
427
  type: "extra",
404
- items: o((e) => (s) => ({ type: "registry", name: e, getForm: s })),
428
+ items: n((e) => (s) => ({ type: "registry", name: e, getForm: s })),
405
429
  getController: r
406
- }), _ = (o) => (...r) => ({
430
+ }), er = (n) => (...r) => ({
407
431
  type: "context",
408
- context: o,
432
+ context: n,
409
433
  childs: r
410
- }), k = (o, r) => (...e) => ({
434
+ }), tr = (n, r) => (...e) => ({
411
435
  type: "condition",
412
- depended: o,
436
+ depended: n,
413
437
  canActivate: r.canActivate,
414
438
  childs: e
415
- }), I = ({ hidden: o }) => (...r) => ({
439
+ }), sr = ({ hidden: n }) => (...r) => ({
416
440
  type: "visability",
417
- hidden: o ?? !1,
441
+ hidden: n ?? !1,
418
442
  childs: r
419
443
  });
420
444
  export {
421
- k as Condition,
422
- _ as Context,
423
- W as FormLayout,
424
- I as Visablility,
425
- Z as createExtraForm,
426
- X as createForm,
427
- Y as createLayout,
428
- O as createUseValue,
429
- Q as useForm,
430
- q as useValue
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: (values: Values, callbacks: SubmitCallbacls<FlattenValues>) => void;
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
- onSubmit: (values: Values, callbacks: SubmitCallbacls<FlattenValues>) => Promise<void>;
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>;
@@ -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];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@companix/formkit",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "module": "./dist/bundle.es.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",