@companix/formkit 0.0.5 → 0.0.6

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/README.md CHANGED
@@ -1,27 +1,37 @@
1
1
  ## Create form primitives
2
2
 
3
3
  ```tsx
4
- const SelectForm = (name: string, x: any) => {}
5
- const InputForm = (name: string, x: any) => {}
4
+ const SelectForm = (name: string) => {
5
+ // select form code
6
+ }
7
+ const InputForm = (name: string) => {
8
+ // input form code
9
+ }
6
10
  ```
7
11
 
8
12
  ## Create layouts
9
13
 
10
14
  ```tsx
11
- const Label = ({ name }) => {
12
- return ({ children }) => <div></div>
13
- }
15
+ export const Label = createLayout((label: React.ReactNode) => {
16
+ return ({ children }) => {
17
+ return <FormGroup label={label}>{children}</FormGroup>
18
+ }
19
+ })
14
20
 
15
- const Double = () => {
16
- return () => {
17
- return <>{children}</>
21
+ export const Double = createLayout(() => {
22
+ return ({ children }) => {
23
+ return <div className="form-double-primitive">{children}</div>
18
24
  }
19
- }
25
+ })
20
26
 
21
- const Splitter = () => {}
27
+ export const Splitter = createLayout(() => {
28
+ return () => {
29
+ return <div className="form-splitter" />
30
+ }
31
+ })
22
32
  ```
23
33
 
24
- ## Define a scheme
34
+ ## Create a scheme
25
35
 
26
36
  ```ts
27
37
  const scheme = [
package/dist/bundle.es.js CHANGED
@@ -31,12 +31,12 @@ const j = N({}), M = (s) => {
31
31
  D(() => () => {
32
32
  e();
33
33
  }, []);
34
- const { value: g, error: b } = t.getForm(r);
34
+ const { value: y, error: b } = t.getForm(r);
35
35
  return /* @__PURE__ */ h(
36
36
  s,
37
37
  {
38
38
  field: {
39
- value: g,
39
+ value: y,
40
40
  onChange: (d) => {
41
41
  t.onChange(r, d);
42
42
  },
@@ -55,17 +55,17 @@ const j = N({}), M = (s) => {
55
55
  return t.forEach(({ name: f, getForm: F }) => {
56
56
  i[f] = F(f, () => x(f));
57
57
  }), i;
58
- }, []), g = S(() => {
58
+ }, []), y = S(() => {
59
59
  const i = [];
60
60
  return t.forEach(({ name: f }) => {
61
61
  e.getForm([...r, f].join(".")).value !== void 0 && i.push(f);
62
62
  }), i;
63
- }, []), b = A(() => g(), []), [d, v] = q(b);
63
+ }, []), b = A(() => y(), []), [d, v] = q(b);
64
64
  D(() => {
65
65
  const { unregistry: i } = e.registryExtraForm(
66
66
  t.map(({ name: f }) => [...r, f].join(".")),
67
67
  () => {
68
- v(g());
68
+ v(y());
69
69
  }
70
70
  );
71
71
  return () => {
@@ -83,12 +83,12 @@ const j = N({}), M = (s) => {
83
83
  return /* @__PURE__ */ T(B, { children: [
84
84
  d.map((i) => {
85
85
  const f = l[i];
86
- return /* @__PURE__ */ h(E, { path: r, scheme: [f] }, `param-form--${i}`);
86
+ return /* @__PURE__ */ h(V, { path: r, scheme: [f] }, `param-form--${i}`);
87
87
  }),
88
88
  o(C, d)
89
89
  ] });
90
- }, E = ({ 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(E, { path: r, scheme: t.childs }) }, `layout-form--${e}-${r.length}`) : t.type === "context" ? /* @__PURE__ */ h(
91
- E,
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(
91
+ V,
92
92
  {
93
93
  path: [...r, t.context],
94
94
  scheme: t.childs
@@ -99,7 +99,7 @@ const j = N({}), M = (s) => {
99
99
  {
100
100
  name: t.depended,
101
101
  canActivate: t.canActivate,
102
- children: /* @__PURE__ */ h(E, { path: r, scheme: t.childs })
102
+ children: /* @__PURE__ */ h(V, { path: r, scheme: t.childs })
103
103
  },
104
104
  `condition--${e}-${t.depended}-${r.length}`
105
105
  ) : t.type === "extra" ? /* @__PURE__ */ h(G, { path: r, item: t }, `partial-item--${e}-${r.length}`) : /* @__PURE__ */ h(
@@ -109,29 +109,29 @@ const j = N({}), M = (s) => {
109
109
  item: t
110
110
  },
111
111
  `form-item--${e}-${t.name}-${r.length}`
112
- )) }), V = (s, r, o, t) => {
112
+ )) }), m = (s, r, o, t) => {
113
113
  s.forEach((e) => {
114
114
  if (e.type === "form" && t(e, [...r, e.name].join("."), o[e.name]), e.type === "context") {
115
- V(e.childs, [...r, e.context], o[e.context] ?? {}, t);
115
+ m(e.childs, [...r, e.context], o[e.context] ?? {}, t);
116
116
  return;
117
117
  }
118
118
  if (e.type === "layout" || e.type === "condition") {
119
- V(e.childs, r, o, t);
119
+ m(e.childs, r, o, t);
120
120
  return;
121
121
  }
122
- e.type === "extra" && e.items.forEach(({ getForm: l, name: g }) => {
123
- V([l(g, () => {
122
+ e.type === "extra" && e.items.forEach(({ getForm: l, name: y }) => {
123
+ m([l(y, () => {
124
124
  })], r, o, t);
125
125
  });
126
126
  });
127
- }, p = ({ scheme: s, values: r, path: o, forms: t }) => {
127
+ }, E = ({ scheme: s, values: r, path: o, forms: t }) => {
128
128
  s.forEach((e) => {
129
129
  if (e.type === "form") {
130
130
  const l = [...o, e.name].join(".");
131
131
  t[l].value !== void 0 && (r[e.name] = t[l].value);
132
132
  }
133
133
  if (e.type === "context") {
134
- r[e.context] = {}, p({
134
+ r[e.context] = {}, E({
135
135
  scheme: e.childs,
136
136
  path: [...o, e.context],
137
137
  values: r[e.context],
@@ -140,32 +140,32 @@ const j = N({}), M = (s) => {
140
140
  return;
141
141
  }
142
142
  if (e.type === "layout") {
143
- p({ scheme: e.childs, path: o, values: r, forms: t });
143
+ E({ scheme: e.childs, path: o, values: r, forms: t });
144
144
  return;
145
145
  }
146
146
  if (e.type === "condition") {
147
147
  const l = t[e.depended]?.value;
148
148
  if (e.canActivate(l)) {
149
- p({ scheme: e.childs, path: o, values: r, forms: t });
149
+ E({ scheme: e.childs, path: o, values: r, forms: t });
150
150
  return;
151
151
  }
152
152
  }
153
- e.type === "extra" && e.items.forEach(({ getForm: l, name: g }) => {
154
- p({ scheme: [l(g, () => {
153
+ e.type === "extra" && e.items.forEach(({ getForm: l, name: y }) => {
154
+ E({ scheme: [l(y, () => {
155
155
  })], path: o, values: r, forms: t });
156
156
  });
157
157
  });
158
158
  }, w = (s, r) => {
159
159
  const o = {};
160
- return p({ scheme: s, path: [], values: o, forms: r }), o;
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: g, onChangeEvent: b, defaultValues: d } = r;
162
+ const { onSubmit: o, onEqual: t, disabled: e, onDirty: l, onFormDirty: y, onChangeEvent: b, defaultValues: d } = r;
163
163
  let v = !1, C = !1, x = 0;
164
164
  const i = {}, f = {
165
165
  subscribers: [],
166
166
  names: {}
167
167
  };
168
- V(s, [], d ?? {}, (n, c, u) => {
168
+ m(s, [], d ?? {}, (n, c, u) => {
169
169
  i[c] = {
170
170
  error: null,
171
171
  isDirty: !1,
@@ -179,9 +179,10 @@ const j = N({}), M = (s) => {
179
179
  };
180
180
  });
181
181
  const F = (n, c) => {
182
- l?.({ name: n, isDirty: c }), c && x === 0 && g?.(!0), !c && x === 1 && g?.(!1), x += c ? 1 : -1;
182
+ l?.({ name: n, isDirty: c }), c && x === 0 && y?.(!0), !c && x === 1 && y?.(!1), x += c ? 1 : -1;
183
183
  };
184
184
  return {
185
+ scheme: s,
185
186
  disabled: e,
186
187
  activate() {
187
188
  v = !0;
@@ -215,8 +216,8 @@ const j = N({}), M = (s) => {
215
216
  for (const u in i) {
216
217
  const a = i[u];
217
218
  if (a.value !== void 0) {
218
- const y = a.validate(a.value);
219
- y && (c = !0, a.error = y, a.rerender(), !n && a.focus && (a.focus(), n = !0));
219
+ const g = a.validate(a.value);
220
+ g && (c = !0, a.error = g, a.rerender(), !n && a.focus && (a.focus(), n = !0));
220
221
  }
221
222
  }
222
223
  if (!c) {
@@ -226,27 +227,27 @@ const j = N({}), M = (s) => {
226
227
  }
227
228
  const u = w(s, i);
228
229
  await o(u, {
229
- setError: (a, y) => {
230
- const m = this.getForm(a);
231
- m.error = {
230
+ setError: (a, g) => {
231
+ const p = this.getForm(a);
232
+ p.error = {
232
233
  error: !0,
233
- messages: [y.message]
234
- }, m.rerender();
234
+ messages: [g.message]
235
+ }, p.rerender();
235
236
  }
236
237
  });
237
238
  }
238
239
  },
239
240
  reset(n) {
240
241
  const c = [];
241
- V(s, [], n, (u, a, y) => {
242
- if (y === void 0 && i[a].value === void 0)
242
+ m(s, [], n, (u, a, g) => {
243
+ if (g === void 0 && i[a].value === void 0)
243
244
  return;
244
- if (f.names[a] && y === void 0) {
245
- c.push(a), i[a].value = y;
245
+ if (f.names[a] && g === void 0) {
246
+ c.push(a), i[a].value = g;
246
247
  return;
247
248
  }
248
- const m = y ?? u.defaultValue;
249
- i[a].value !== m && (c.push(a), this.onChange(a, m));
249
+ const p = g ?? u.defaultValue;
250
+ i[a].value !== p && (c.push(a), this.onChange(a, p));
250
251
  }), c.some((u) => f.names[u]) && f.subscribers.forEach(({ callback: u }) => {
251
252
  u();
252
253
  });
@@ -270,7 +271,7 @@ const j = N({}), M = (s) => {
270
271
  const u = { callback: c }, a = this.getForm(n);
271
272
  return a.subscribers.push(u), {
272
273
  unsubscribe: () => {
273
- a.subscribers = a.subscribers.filter((y) => y !== u);
274
+ a.subscribers = a.subscribers.filter((g) => g !== u);
274
275
  }
275
276
  };
276
277
  },
@@ -286,8 +287,8 @@ const j = N({}), M = (s) => {
286
287
  },
287
288
  onChange(n, c) {
288
289
  const u = this.getForm(n), a = !R(c, u.initValue);
289
- 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: y }) => {
290
- y();
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();
291
292
  });
292
293
  }
293
294
  };
@@ -313,13 +314,13 @@ const j = N({}), M = (s) => {
313
314
  handleSubmit: async () => {
314
315
  await t.handleSubmit();
315
316
  },
316
- control: { manager: t, scheme: s }
317
+ manager: t
317
318
  };
318
- }, U = ({ control: s, className: r }) => (A(() => {
319
- s.manager.activate();
319
+ }, U = ({ manager: s, className: r }) => (A(() => {
320
+ s.activate();
320
321
  }, []), D(() => () => {
321
- s.manager.disactivate();
322
- }, []), /* @__PURE__ */ h(j.Provider, { value: s.manager, children: /* @__PURE__ */ h(E, { className: r, scheme: s.scheme, path: [] }) })), $ = (s) => {
322
+ s.disactivate();
323
+ }, []), /* @__PURE__ */ h(j.Provider, { value: s, children: /* @__PURE__ */ h(V, { className: r, scheme: s.scheme, path: [] }) })), $ = (s) => {
323
324
  try {
324
325
  s();
325
326
  } catch (r) {
@@ -340,20 +341,20 @@ const j = N({}), M = (s) => {
340
341
  name: r,
341
342
  defaultValue: s.defaultValue,
342
343
  Form: s.getForm(o),
343
- validate: (g) => {
344
+ validate: (y) => {
344
345
  if (l) {
345
- const b = $(() => t(g));
346
+ const b = $(() => t(y));
346
347
  if (b)
347
348
  return b;
348
349
  if (e) {
349
- const d = $(() => e(g));
350
+ const d = $(() => e(y));
350
351
  if (d)
351
352
  return d;
352
353
  }
353
354
  return;
354
355
  }
355
- if (e && !$(() => t(g))) {
356
- const d = $(() => e(g));
356
+ if (e && !$(() => t(y))) {
357
+ const d = $(() => e(y));
357
358
  if (d)
358
359
  return d;
359
360
  }
package/dist/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export { createLayout } from './core/builders/create-layout';
4
4
  export { createExtraForm } from './core/builders/create-extra-form';
5
5
  export { Condition, Context } from './core/builders/syntax';
6
6
  export type { FieldError } from './core/types';
7
+ export type { FormManager } from './manager/manager';
@@ -18,6 +18,7 @@ export interface Forms {
18
18
  [name: string]: FormItem;
19
19
  }
20
20
  export interface FormManager<FlattenValues, ClonedValues> {
21
+ scheme: SchemeItems.All[];
21
22
  disabled?: boolean;
22
23
  activate: () => void;
23
24
  disactivate: () => void;
package/dist/useForm.d.ts CHANGED
@@ -45,10 +45,7 @@ declare const useDynamicForm: (scheme: SchemeItems.All[], opts: MainOptions<Fiel
45
45
  [x: string]: any;
46
46
  };
47
47
  handleSubmit: () => Promise<void>;
48
- control: {
49
- manager: FormManager<FieldValues, FieldValues>;
50
- scheme: [];
51
- };
48
+ manager: FormManager<FieldValues, FieldValues>;
52
49
  };
53
50
  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>) => {
54
51
  useValue: <K extends keyof FlattenValues>(name: K) => FlattenValues[K];
@@ -60,17 +57,11 @@ declare const useForm: <Items extends SchemeItems.All[], Values extends ExtractV
60
57
  setValue: <K extends keyof FlattenValues>(name: K, value: FlattenValues[K]) => void;
61
58
  getValues: () => DeepPartial<Cloned>;
62
59
  handleSubmit: () => Promise<void>;
63
- control: {
64
- manager: FormManager<FlattenValues, Cloned>;
65
- scheme: Items;
66
- };
60
+ manager: FormManager<FlattenValues, Cloned>;
67
61
  };
68
62
  interface Props {
69
63
  className?: string;
70
- control: {
71
- manager: FormManager<any, any>;
72
- scheme: SchemeItems.All[];
73
- };
64
+ manager: FormManager<any, any>;
74
65
  }
75
- export declare const FormLayout: ({ control, className }: Props) => import("react/jsx-runtime").JSX.Element;
66
+ export declare const FormLayout: ({ manager, className }: Props) => import("react/jsx-runtime").JSX.Element;
76
67
  export { useForm, useDynamicForm };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@companix/formkit",
3
- "version": "0.0.05",
3
+ "version": "0.0.06",
4
4
  "module": "./dist/bundle.es.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
7
- "author": "Pavel Victorov.",
7
+ "author": "Pavel Victorov",
8
8
  "files": [
9
9
  "dist"
10
10
  ],