@companix/formkit 0.0.6 → 0.0.7

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.
Files changed (2) hide show
  1. package/dist/bundle.es.js +137 -137
  2. package/package.json +1 -1
package/dist/bundle.es.js CHANGED
@@ -1,27 +1,27 @@
1
- import { jsx as h, Fragment as B, jsxs as T } from "react/jsx-runtime";
2
- import { createContext as N, useState as q, useContext as L, useEffect as D, useMemo as A, useCallback as S, useRef as P } from "react";
1
+ import { jsx as b, Fragment as B, jsxs as P } from "react/jsx-runtime";
2
+ import { createContext as T, useState as q, useContext as L, useEffect as D, useMemo as A, useCallback as S, useRef as N } from "react";
3
3
  import R from "fast-deep-equal";
4
- const j = N({}), M = (s) => {
5
- const [, r] = q([]), o = L(j), t = o.getForm(s);
4
+ const j = T({}), M = (s) => {
5
+ const [, r] = q([]), o = L(j), e = o.getForm(s);
6
6
  return D(() => {
7
- const { unsubscribe: e } = o.subscribeToForm(s, () => {
7
+ const { unsubscribe: t } = o.subscribeToForm(s, () => {
8
8
  r([]);
9
9
  });
10
10
  return () => {
11
- e();
11
+ t();
12
12
  };
13
- }, []), t?.value;
13
+ }, []), e?.value;
14
14
  }, _ = ({ canActivate: s, name: r, children: o }) => {
15
- const t = M(r);
16
- return s(t) ? /* @__PURE__ */ h(B, { children: o }) : null;
15
+ const e = M(r);
16
+ return s(e) ? /* @__PURE__ */ b(B, { children: o }) : null;
17
17
  }, z = ({ item: { Form: s }, name: r }) => {
18
- const [, o] = q([]), t = L(j), { unregistry: e } = A(() => t.registry(r, () => {
18
+ const [, o] = q([]), e = L(j), { unregistry: t } = A(() => e.registry(r, () => {
19
19
  o([]);
20
20
  }), []), l = S(
21
21
  (d) => {
22
22
  if (d && d.focus) {
23
- const v = t.getForm(r);
24
- v.focus = () => {
23
+ const g = e.getForm(r);
24
+ g.focus = () => {
25
25
  d.focus();
26
26
  };
27
27
  }
@@ -29,143 +29,143 @@ const j = N({}), M = (s) => {
29
29
  [r]
30
30
  );
31
31
  D(() => () => {
32
- e();
32
+ t();
33
33
  }, []);
34
- const { value: y, error: b } = t.getForm(r);
35
- return /* @__PURE__ */ h(
34
+ const { value: y, error: F } = e.getForm(r);
35
+ return /* @__PURE__ */ b(
36
36
  s,
37
37
  {
38
38
  field: {
39
39
  value: y,
40
40
  onChange: (d) => {
41
- t.onChange(r, d);
41
+ e.onChange(r, d);
42
42
  },
43
43
  onBlur: () => {
44
44
  },
45
45
  ref: l
46
46
  },
47
47
  isDirty: !1,
48
- error: b,
49
- disabled: t.disabled
48
+ error: F,
49
+ disabled: e.disabled
50
50
  }
51
51
  );
52
52
  }, G = ({ item: s, path: r }) => {
53
- const { getController: o, items: t } = s, e = L(j), l = A(() => {
53
+ const { getController: o, items: e } = s, t = L(j), l = A(() => {
54
54
  const i = {};
55
- return t.forEach(({ name: f, getForm: F }) => {
56
- i[f] = F(f, () => x(f));
55
+ return e.forEach(({ name: f, getForm: x }) => {
56
+ i[f] = x(f, () => p(f));
57
57
  }), i;
58
58
  }, []), y = S(() => {
59
59
  const i = [];
60
- return t.forEach(({ name: f }) => {
61
- e.getForm([...r, f].join(".")).value !== void 0 && i.push(f);
60
+ return e.forEach(({ name: f }) => {
61
+ t.getForm([...r, f].join(".")).value !== void 0 && i.push(f);
62
62
  }), i;
63
- }, []), b = A(() => y(), []), [d, v] = q(b);
63
+ }, []), F = A(() => y(), []), [d, g] = q(F);
64
64
  D(() => {
65
- const { unregistry: i } = e.registryExtraForm(
66
- t.map(({ name: f }) => [...r, f].join(".")),
65
+ const { unregistry: i } = t.registryExtraForm(
66
+ e.map(({ name: f }) => [...r, f].join(".")),
67
67
  () => {
68
- v(y());
68
+ g(y());
69
69
  }
70
70
  );
71
71
  return () => {
72
72
  i();
73
73
  };
74
74
  }, []);
75
- const C = S(
75
+ const v = S(
76
76
  (i) => {
77
- l[i] && !d.includes(i) && v((f) => [...f, i]);
77
+ l[i] && !d.includes(i) && g((f) => [...f, i]);
78
78
  },
79
79
  [d]
80
- ), x = S((i) => {
81
- v((f) => f.filter((F) => F !== i));
80
+ ), p = S((i) => {
81
+ g((f) => f.filter((x) => x !== i));
82
82
  }, []);
83
- return /* @__PURE__ */ T(B, { children: [
83
+ return /* @__PURE__ */ P(B, { children: [
84
84
  d.map((i) => {
85
85
  const f = l[i];
86
- return /* @__PURE__ */ h(V, { path: r, scheme: [f] }, `param-form--${i}`);
86
+ return /* @__PURE__ */ b(V, { path: r, scheme: [f] }, `param-form--${i}`);
87
87
  }),
88
- o(C, d)
88
+ o(v, d)
89
89
  ] });
90
- }, V = ({ scheme: s, path: r, className: o }) => /* @__PURE__ */ h("div", { className: o, children: s.map((t, e) => t.type === "layout" ? /* @__PURE__ */ h(t.Layout, { children: /* @__PURE__ */ h(V, { path: r, scheme: t.childs }) }, `layout-form--${e}-${r.length}`) : t.type === "context" ? /* @__PURE__ */ h(
90
+ }, V = ({ scheme: s, path: r, className: o }) => /* @__PURE__ */ b("div", { className: o, children: s.map((e, t) => e.type === "layout" ? /* @__PURE__ */ b(e.Layout, { children: /* @__PURE__ */ b(V, { path: r, scheme: e.childs }) }, `layout-form--${t}-${r.length}`) : e.type === "context" ? /* @__PURE__ */ b(
91
91
  V,
92
92
  {
93
- path: [...r, t.context],
94
- scheme: t.childs
93
+ path: [...r, e.context],
94
+ scheme: e.childs
95
95
  },
96
- `context-form--${e}-${t.context}-${r.length}`
97
- ) : t.type === "condition" ? /* @__PURE__ */ h(
96
+ `context-form--${t}-${e.context}-${r.length}`
97
+ ) : e.type === "condition" ? /* @__PURE__ */ b(
98
98
  _,
99
99
  {
100
- name: t.depended,
101
- canActivate: t.canActivate,
102
- children: /* @__PURE__ */ h(V, { path: r, scheme: t.childs })
100
+ name: e.depended,
101
+ canActivate: e.canActivate,
102
+ children: /* @__PURE__ */ b(V, { path: r, scheme: e.childs })
103
103
  },
104
- `condition--${e}-${t.depended}-${r.length}`
105
- ) : t.type === "extra" ? /* @__PURE__ */ h(G, { path: r, item: t }, `partial-item--${e}-${r.length}`) : /* @__PURE__ */ h(
104
+ `condition--${t}-${e.depended}-${r.length}`
105
+ ) : e.type === "extra" ? /* @__PURE__ */ b(G, { path: r, item: e }, `partial-item--${t}-${r.length}`) : /* @__PURE__ */ b(
106
106
  z,
107
107
  {
108
- name: [...r, t.name].join("."),
109
- item: t
108
+ name: [...r, e.name].join("."),
109
+ item: e
110
110
  },
111
- `form-item--${e}-${t.name}-${r.length}`
112
- )) }), m = (s, r, o, t) => {
113
- s.forEach((e) => {
114
- if (e.type === "form" && t(e, [...r, e.name].join("."), o[e.name]), e.type === "context") {
115
- m(e.childs, [...r, e.context], o[e.context] ?? {}, t);
111
+ `form-item--${t}-${e.name}-${r.length}`
112
+ )) }), C = (s, r, o, e) => {
113
+ s.forEach((t) => {
114
+ if (t.type === "form" && e(t, [...r, t.name].join("."), o[t.name]), t.type === "context") {
115
+ C(t.childs, [...r, t.context], o[t.context] ?? {}, e);
116
116
  return;
117
117
  }
118
- if (e.type === "layout" || e.type === "condition") {
119
- m(e.childs, r, o, t);
118
+ if (t.type === "layout" || t.type === "condition") {
119
+ C(t.childs, r, o, e);
120
120
  return;
121
121
  }
122
- e.type === "extra" && e.items.forEach(({ getForm: l, name: y }) => {
123
- m([l(y, () => {
124
- })], r, o, t);
122
+ t.type === "extra" && t.items.forEach(({ getForm: l, name: y }) => {
123
+ C([l(y, () => {
124
+ })], r, o, e);
125
125
  });
126
126
  });
127
- }, E = ({ scheme: s, values: r, path: o, forms: t }) => {
128
- s.forEach((e) => {
129
- if (e.type === "form") {
130
- const l = [...o, e.name].join(".");
131
- t[l].value !== void 0 && (r[e.name] = t[l].value);
127
+ }, E = ({ scheme: s, values: r, path: o, forms: e }) => {
128
+ s.forEach((t) => {
129
+ if (t.type === "form") {
130
+ const l = [...o, t.name].join(".");
131
+ e[l].value !== void 0 && (r[t.name] = e[l].value);
132
132
  }
133
- if (e.type === "context") {
134
- r[e.context] = {}, E({
135
- scheme: e.childs,
136
- path: [...o, e.context],
137
- values: r[e.context],
138
- forms: t
133
+ if (t.type === "context") {
134
+ r[t.context] = {}, E({
135
+ scheme: t.childs,
136
+ path: [...o, t.context],
137
+ values: r[t.context],
138
+ forms: e
139
139
  });
140
140
  return;
141
141
  }
142
- if (e.type === "layout") {
143
- E({ scheme: e.childs, path: o, values: r, forms: t });
142
+ if (t.type === "layout") {
143
+ E({ scheme: t.childs, path: o, values: r, forms: e });
144
144
  return;
145
145
  }
146
- if (e.type === "condition") {
147
- const l = t[e.depended]?.value;
148
- if (e.canActivate(l)) {
149
- E({ scheme: e.childs, path: o, values: r, forms: t });
146
+ if (t.type === "condition") {
147
+ const l = e[t.depended]?.value;
148
+ if (t.canActivate(l)) {
149
+ E({ scheme: t.childs, path: o, values: r, forms: e });
150
150
  return;
151
151
  }
152
152
  }
153
- e.type === "extra" && e.items.forEach(({ getForm: l, name: y }) => {
153
+ t.type === "extra" && t.items.forEach(({ getForm: l, name: y }) => {
154
154
  E({ scheme: [l(y, () => {
155
- })], path: o, values: r, forms: t });
155
+ })], path: o, values: r, forms: e });
156
156
  });
157
157
  });
158
158
  }, w = (s, r) => {
159
159
  const o = {};
160
160
  return E({ scheme: s, path: [], values: o, forms: r }), o;
161
161
  }, H = (s, r) => {
162
- const { onSubmit: o, onEqual: t, disabled: e, onDirty: l, onFormDirty: y, onChangeEvent: b, defaultValues: d } = r;
163
- let v = !1, C = !1, x = 0;
162
+ const { onSubmit: o, onEqual: e, disabled: t, onDirty: l, onFormDirty: y, onChangeEvent: F, defaultValues: d } = r;
163
+ let g = !1, v = !1, p = 0;
164
164
  const i = {}, f = {
165
165
  subscribers: [],
166
166
  names: {}
167
167
  };
168
- m(s, [], d ?? {}, (n, c, u) => {
168
+ C(s, [], d ?? {}, (n, c, u) => {
169
169
  i[c] = {
170
170
  error: null,
171
171
  isDirty: !1,
@@ -178,17 +178,17 @@ const j = N({}), M = (s) => {
178
178
  validate: n.validate
179
179
  };
180
180
  });
181
- const F = (n, c) => {
182
- l?.({ name: n, isDirty: c }), c && x === 0 && y?.(!0), !c && x === 1 && y?.(!1), x += c ? 1 : -1;
181
+ const x = (n, c) => {
182
+ l?.({ name: n, isDirty: c }), c && p === 0 && y?.(!0), !c && p === 1 && y?.(!1), p += c ? 1 : -1;
183
183
  };
184
184
  return {
185
185
  scheme: s,
186
- disabled: e,
186
+ disabled: t,
187
187
  activate() {
188
- v = !0;
188
+ g = !0;
189
189
  },
190
190
  disactivate() {
191
- v = !1;
191
+ g = !1;
192
192
  },
193
193
  setError(n, c) {
194
194
  const u = this.getForm(n);
@@ -209,45 +209,45 @@ const j = N({}), M = (s) => {
209
209
  return w(s, i);
210
210
  },
211
211
  async handleSubmit() {
212
- if (e)
212
+ if (t)
213
213
  return;
214
- C = !0;
214
+ v = !0;
215
215
  let n = !1, c = !1;
216
216
  for (const u in i) {
217
217
  const a = i[u];
218
218
  if (a.value !== void 0) {
219
- const g = a.validate(a.value);
220
- g && (c = !0, a.error = g, a.rerender(), !n && a.focus && (a.focus(), n = !0));
219
+ const h = a.validate(a.value);
220
+ h && (c = !0, a.error = h, a.rerender(), !n && a.focus && (a.focus(), n = !0));
221
221
  }
222
222
  }
223
223
  if (!c) {
224
- if (x === 0) {
225
- t?.();
224
+ if (p === 0) {
225
+ e?.();
226
226
  return;
227
227
  }
228
228
  const u = w(s, i);
229
229
  await o(u, {
230
- setError: (a, g) => {
231
- const p = this.getForm(a);
232
- p.error = {
230
+ setError: (a, h) => {
231
+ const m = this.getForm(a);
232
+ m.error = {
233
233
  error: !0,
234
- messages: [g.message]
235
- }, p.rerender();
234
+ messages: [h.message]
235
+ }, m.rerender();
236
236
  }
237
237
  });
238
238
  }
239
239
  },
240
240
  reset(n) {
241
241
  const c = [];
242
- m(s, [], n, (u, a, g) => {
243
- if (g === void 0 && i[a].value === void 0)
242
+ C(s, [], n, (u, a, h) => {
243
+ if (h === void 0 && i[a].value === void 0)
244
244
  return;
245
- if (f.names[a] && g === void 0) {
246
- c.push(a), i[a].value = g;
245
+ if (f.names[a] && h === void 0) {
246
+ c.push(a), i[a].value = h;
247
247
  return;
248
248
  }
249
- const p = g ?? u.defaultValue;
250
- i[a].value !== p && (c.push(a), this.onChange(a, p));
249
+ const m = h ?? u.defaultValue;
250
+ i[a].value !== m && (c.push(a), this.onChange(a, m));
251
251
  }), c.some((u) => f.names[u]) && f.subscribers.forEach(({ callback: u }) => {
252
252
  u();
253
253
  });
@@ -256,10 +256,10 @@ const j = N({}), M = (s) => {
256
256
  return i[n].rerender = c, i[n].value === void 0 && (i[n].value = i[n].defaultValue), {
257
257
  unregistry() {
258
258
  if (i[n].rerender = () => {
259
- }, i[n].focus = void 0, v) {
259
+ }, i[n].focus = void 0, g) {
260
260
  i[n].value = void 0;
261
261
  const u = !R(i[n].defaultValue, i[n].initValue);
262
- i[n].isDirty !== u && F(n, u), i[n].isDirty = u;
262
+ i[n].isDirty !== u && x(n, u), i[n].isDirty = u;
263
263
  }
264
264
  }
265
265
  };
@@ -271,7 +271,7 @@ const j = N({}), M = (s) => {
271
271
  const u = { callback: c }, a = this.getForm(n);
272
272
  return a.subscribers.push(u), {
273
273
  unsubscribe: () => {
274
- a.subscribers = a.subscribers.filter((g) => g !== u);
274
+ a.subscribers = a.subscribers.filter((h) => h !== u);
275
275
  }
276
276
  };
277
277
  },
@@ -287,40 +287,40 @@ const j = N({}), M = (s) => {
287
287
  },
288
288
  onChange(n, c) {
289
289
  const u = this.getForm(n), a = !R(c, u.initValue);
290
- u.isDirty !== a && F(n, a), u.isDirty = a, b?.({ name: n, value: c }), (u.error || C) && (u.error = u.validate(c) || null), u.value = c, u.rerender(), u.subscribers.forEach(({ callback: g }) => {
291
- g();
290
+ u.isDirty !== a && x(n, a), u.isDirty = a, F?.({ name: n, value: c }), (u.error || v) && (u.error = u.validate(c) || null), u.value = c, u.rerender(), u.subscribers.forEach(({ callback: h }) => {
291
+ h();
292
292
  });
293
293
  }
294
294
  };
295
295
  }, Q = (s, r) => {
296
- const o = P(null);
296
+ const o = N(null);
297
297
  o.current === null && (o.current = H(s, r));
298
- const t = o.current;
298
+ const e = o.current;
299
299
  return {
300
- useValue: (e) => M(e),
301
- reset: (e) => {
302
- t.reset(e);
300
+ useValue: (t) => M(t),
301
+ reset: (t) => {
302
+ e.reset(t);
303
303
  },
304
- setFocus: (e) => {
305
- t.setFocus(e);
304
+ setFocus: (t) => {
305
+ e.setFocus(t);
306
306
  },
307
- setError: (e, l) => {
308
- t.setError(e, l);
307
+ setError: (t, l) => {
308
+ e.setError(t, l);
309
309
  },
310
- setValue: (e, l) => {
311
- t.setValue(e, l);
310
+ setValue: (t, l) => {
311
+ e.setValue(t, l);
312
312
  },
313
- getValues: () => t.getValues(),
313
+ getValues: () => e.getValues(),
314
314
  handleSubmit: async () => {
315
- await t.handleSubmit();
315
+ await e.handleSubmit();
316
316
  },
317
- manager: t
317
+ manager: e
318
318
  };
319
319
  }, U = ({ manager: s, className: r }) => (A(() => {
320
320
  s.activate();
321
321
  }, []), D(() => () => {
322
322
  s.disactivate();
323
- }, []), /* @__PURE__ */ h(j.Provider, { value: s, children: /* @__PURE__ */ h(V, { className: r, scheme: s.scheme, path: [] }) })), $ = (s) => {
323
+ }, []), /* @__PURE__ */ b(j.Provider, { value: s, children: /* @__PURE__ */ b(V, { className: r, scheme: s.scheme, path: [] }) })), $ = (s) => {
324
324
  try {
325
325
  s();
326
326
  } catch (r) {
@@ -335,28 +335,28 @@ const j = N({}), M = (s) => {
335
335
  };
336
336
  }
337
337
  }, W = (s) => (r, o) => {
338
- const t = s.getRequireCheck(o), { validate: e, required: l } = o.$rules ?? {};
338
+ const e = s.getRequireCheck(o), { $rules: t = {}, ...l } = o, { validate: y, required: F } = t;
339
339
  return {
340
340
  type: "form",
341
341
  name: r,
342
342
  defaultValue: s.defaultValue,
343
- Form: s.getForm(o),
344
- validate: (y) => {
345
- if (l) {
346
- const b = $(() => t(y));
347
- if (b)
348
- return b;
349
- if (e) {
350
- const d = $(() => e(y));
351
- if (d)
352
- return d;
343
+ Form: s.getForm(l),
344
+ validate: (d) => {
345
+ if (F) {
346
+ const g = $(() => e(d));
347
+ if (g)
348
+ return g;
349
+ if (y) {
350
+ const v = $(() => y(d));
351
+ if (v)
352
+ return v;
353
353
  }
354
354
  return;
355
355
  }
356
- if (e && !$(() => t(y))) {
357
- const d = $(() => e(y));
358
- if (d)
359
- return d;
356
+ if (y && !$(() => e(d))) {
357
+ const v = $(() => y(d));
358
+ if (v)
359
+ return v;
360
360
  }
361
361
  }
362
362
  };
@@ -369,10 +369,10 @@ const j = N({}), M = (s) => {
369
369
  getController: r
370
370
  }) => ({
371
371
  type: "extra",
372
- items: s((o) => (t) => ({
372
+ items: s((o) => (e) => ({
373
373
  type: "registry",
374
374
  name: o,
375
- getForm: t
375
+ getForm: e
376
376
  })),
377
377
  getController: r
378
378
  }), Z = (s) => (...r) => ({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@companix/formkit",
3
- "version": "0.0.06",
3
+ "version": "0.0.07",
4
4
  "module": "./dist/bundle.es.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",