@companix/formkit 0.0.1

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 ADDED
@@ -0,0 +1,44 @@
1
+ // generics
2
+
3
+ // implements form
4
+
5
+ const SelectForm = (name: string, x: any) => {}
6
+ const InputForm = (name: string, x: any) => {}
7
+
8
+ // implements layout
9
+
10
+ const Label = (v: any) => {
11
+ return (x: any) => {}
12
+ }
13
+
14
+ const Double = () => {
15
+ return (...x: void[]) => {
16
+ // x.
17
+ }
18
+ }
19
+
20
+ const Splitter = () => {}
21
+
22
+ // scheme
23
+
24
+ const scheme = [
25
+ Label('Имя')(
26
+ SelectForm('name', {
27
+ placeholder: 'Введите имя',
28
+ options: []
29
+ })
30
+ ),
31
+ Splitter(),
32
+ Double()(
33
+ Label('Фамилия')(
34
+ InputForm('surname', {
35
+ placeholder: 'Введите фамилию'
36
+ })
37
+ ),
38
+ Label('Отчество')(
39
+ InputForm('patronymic', {
40
+ placeholder: 'Введите отчество'
41
+ })
42
+ )
43
+ )
44
+ ]
@@ -0,0 +1,394 @@
1
+ import { jsx as g, Fragment as q, jsxs as T } from "react/jsx-runtime";
2
+ import { createContext as N, useState as R, useContext as L, useEffect as D, useMemo as A, useCallback as S, useRef as P } from "react";
3
+ import w from "fast-deep-equal";
4
+ const j = N({}), M = (o) => {
5
+ const [, r] = R([]), e = L(j), s = e.getForm(o);
6
+ return D(() => {
7
+ const { unsubscribe: t } = e.subscribeToForm(o, () => {
8
+ r([]);
9
+ });
10
+ return () => {
11
+ t();
12
+ };
13
+ }, []), s?.value;
14
+ }, _ = ({ canActivate: o, name: r, children: e }) => {
15
+ const s = M(r);
16
+ return o(s) ? /* @__PURE__ */ g(q, { children: e }) : null;
17
+ }, z = ({ item: { Form: o }, name: r }) => {
18
+ const [, e] = R([]), s = L(j), { unregistry: t } = A(() => s.registry(r, () => {
19
+ e([]);
20
+ }), []), l = S(
21
+ (d) => {
22
+ if (d && d.focus) {
23
+ const v = s.getForm(r);
24
+ v.focus = () => {
25
+ d.focus();
26
+ };
27
+ }
28
+ },
29
+ [r]
30
+ );
31
+ D(() => () => {
32
+ t();
33
+ }, []);
34
+ const { value: y, error: b } = s.getForm(r);
35
+ return /* @__PURE__ */ g(
36
+ o,
37
+ {
38
+ field: {
39
+ value: y,
40
+ onChange: (d) => {
41
+ s.onChange(r, d);
42
+ },
43
+ onBlur: () => {
44
+ },
45
+ ref: l
46
+ },
47
+ isDirty: !1,
48
+ error: b,
49
+ disabled: s.disabled
50
+ }
51
+ );
52
+ }, G = ({ item: o, path: r }) => {
53
+ const { getController: e, items: s } = o, t = L(j), l = A(() => {
54
+ const i = {};
55
+ return s.forEach(({ name: f, getForm: F }) => {
56
+ i[f] = F(f, () => x(f));
57
+ }), i;
58
+ }, []), y = S(() => {
59
+ const i = [];
60
+ return s.forEach(({ name: f }) => {
61
+ t.getForm([...r, f].join(".")).value !== void 0 && i.push(f);
62
+ }), i;
63
+ }, []), b = A(() => y(), []), [d, v] = R(b);
64
+ D(() => {
65
+ const { unregistry: i } = t.registryExtraForm(
66
+ s.map(({ name: f }) => [...r, f].join(".")),
67
+ () => {
68
+ v(y());
69
+ }
70
+ );
71
+ return () => {
72
+ i();
73
+ };
74
+ }, []);
75
+ const C = S(
76
+ (i) => {
77
+ l[i] && !d.includes(i) && v((f) => [...f, i]);
78
+ },
79
+ [d]
80
+ ), x = S((i) => {
81
+ v((f) => f.filter((F) => F !== i));
82
+ }, []);
83
+ return /* @__PURE__ */ T(q, { children: [
84
+ d.map((i) => {
85
+ const f = l[i];
86
+ return /* @__PURE__ */ g(V, { path: r, scheme: [f] }, `param-form--${i}`);
87
+ }),
88
+ e(C, d)
89
+ ] });
90
+ }, V = ({ scheme: o, path: r }) => /* @__PURE__ */ g(q, { children: o.map((e, s) => e.type === "layout" ? /* @__PURE__ */ g(e.Layout, { children: /* @__PURE__ */ g(V, { path: r, scheme: e.childs }) }, `layout-form--${s}-${r.length}`) : e.type === "context" ? /* @__PURE__ */ g(
91
+ V,
92
+ {
93
+ path: [...r, e.context],
94
+ scheme: e.childs
95
+ },
96
+ `context-form--${s}-${e.context}-${r.length}`
97
+ ) : e.type === "condition" ? /* @__PURE__ */ g(
98
+ _,
99
+ {
100
+ name: e.depended,
101
+ canActivate: e.canActivate,
102
+ children: /* @__PURE__ */ g(V, { path: r, scheme: e.childs })
103
+ },
104
+ `condition--${s}-${e.depended}-${r.length}`
105
+ ) : e.type === "extra" ? /* @__PURE__ */ g(G, { path: r, item: e }, `partial-item--${s}-${r.length}`) : /* @__PURE__ */ g(
106
+ z,
107
+ {
108
+ name: [...r, e.name].join("."),
109
+ item: e
110
+ },
111
+ `form-item--${s}-${e.name}-${r.length}`
112
+ )) }), m = (o, r, e, s) => {
113
+ o.forEach((t) => {
114
+ if (t.type === "form" && s(t, [...r, t.name].join("."), e[t.name]), t.type === "context") {
115
+ m(t.childs, [...r, t.context], e[t.context] ?? {}, s);
116
+ return;
117
+ }
118
+ if (t.type === "layout" || t.type === "condition") {
119
+ m(t.childs, r, e, s);
120
+ return;
121
+ }
122
+ t.type === "extra" && t.items.forEach(({ getForm: l, name: y }) => {
123
+ m([l(y, () => {
124
+ })], r, e, s);
125
+ });
126
+ });
127
+ }, E = ({ scheme: o, values: r, path: e, forms: s }) => {
128
+ o.forEach((t) => {
129
+ if (t.type === "form") {
130
+ const l = [...e, t.name].join(".");
131
+ s[l].value !== void 0 && (r[t.name] = s[l].value);
132
+ }
133
+ if (t.type === "context") {
134
+ r[t.context] = {}, E({
135
+ scheme: t.childs,
136
+ path: [...e, t.context],
137
+ values: r[t.context],
138
+ forms: s
139
+ });
140
+ return;
141
+ }
142
+ if (t.type === "layout") {
143
+ E({ scheme: t.childs, path: e, values: r, forms: s });
144
+ return;
145
+ }
146
+ if (t.type === "condition") {
147
+ const l = s[t.depended]?.value;
148
+ if (t.canActivate(l)) {
149
+ E({ scheme: t.childs, path: e, values: r, forms: s });
150
+ return;
151
+ }
152
+ }
153
+ t.type === "extra" && t.items.forEach(({ getForm: l, name: y }) => {
154
+ E({ scheme: [l(y, () => {
155
+ })], path: e, values: r, forms: s });
156
+ });
157
+ });
158
+ }, B = (o, r) => {
159
+ const e = {};
160
+ return E({ scheme: o, path: [], values: e, forms: r }), e;
161
+ }, H = (o, r) => {
162
+ const { onSubmit: e, onEqual: s, disabled: t, onDirty: l, onFormDirty: y, onChangeEvent: b, defaultValues: d } = r;
163
+ let v = !1, C = !1, x = 0;
164
+ const i = {}, f = {
165
+ subscribers: [],
166
+ names: {}
167
+ };
168
+ m(o, [], d ?? {}, (n, c, u) => {
169
+ i[c] = {
170
+ error: null,
171
+ isDirty: !1,
172
+ value: u,
173
+ initValue: u ?? n.defaultValue,
174
+ defaultValue: n.defaultValue,
175
+ rerender: () => {
176
+ },
177
+ subscribers: [],
178
+ validate: n.validate
179
+ };
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;
183
+ };
184
+ return {
185
+ disabled: t,
186
+ activate() {
187
+ v = !0;
188
+ },
189
+ disactivate() {
190
+ v = !1;
191
+ },
192
+ setError(n, c) {
193
+ const u = this.getForm(n);
194
+ u && (u.error = {
195
+ error: !0,
196
+ messages: [c.message]
197
+ }, u.rerender());
198
+ },
199
+ setFocus(n) {
200
+ const c = this.getForm(n);
201
+ c && c.focus && c.focus();
202
+ },
203
+ setValue(n, c) {
204
+ const u = this.getForm(n);
205
+ u && u.value !== c && this.onChange(n, c);
206
+ },
207
+ getValues() {
208
+ return B(o, i);
209
+ },
210
+ async handleSubmit() {
211
+ if (t)
212
+ return;
213
+ C = !0;
214
+ let n = !1, c = !1;
215
+ for (const u in i) {
216
+ const a = i[u];
217
+ if (a.value !== void 0) {
218
+ const h = a.validate(a.value);
219
+ h && (c = !0, a.error = h, a.rerender(), !n && a.focus && (a.focus(), n = !0));
220
+ }
221
+ }
222
+ if (!c) {
223
+ if (x === 0) {
224
+ s?.();
225
+ return;
226
+ }
227
+ const u = B(o, i);
228
+ await e(u, {
229
+ setError: (a, h) => {
230
+ const p = this.getForm(a);
231
+ p.error = {
232
+ error: !0,
233
+ messages: [h.message]
234
+ }, p.rerender();
235
+ }
236
+ });
237
+ }
238
+ },
239
+ reset(n) {
240
+ const c = [];
241
+ m(o, [], n, (u, a, h) => {
242
+ if (h === void 0 && i[a].value === void 0)
243
+ return;
244
+ if (f.names[a] && h === void 0) {
245
+ c.push(a), i[a].value = h;
246
+ return;
247
+ }
248
+ const p = h ?? u.defaultValue;
249
+ i[a].value !== p && (c.push(a), this.onChange(a, p));
250
+ }), c.some((u) => f.names[u]) && f.subscribers.forEach(({ callback: u }) => {
251
+ u();
252
+ });
253
+ },
254
+ registry(n, c) {
255
+ return i[n].rerender = c, i[n].value === void 0 && (i[n].value = i[n].defaultValue), {
256
+ unregistry() {
257
+ if (i[n].rerender = () => {
258
+ }, i[n].focus = void 0, v) {
259
+ i[n].value = void 0;
260
+ const u = !w(i[n].defaultValue, i[n].initValue);
261
+ i[n].isDirty !== u && F(n, u), i[n].isDirty = u;
262
+ }
263
+ }
264
+ };
265
+ },
266
+ getForm(n) {
267
+ return i[n];
268
+ },
269
+ subscribeToForm(n, c) {
270
+ const u = { callback: c }, a = this.getForm(n);
271
+ return a.subscribers.push(u), {
272
+ unsubscribe: () => {
273
+ a.subscribers = a.subscribers.filter((h) => h !== u);
274
+ }
275
+ };
276
+ },
277
+ registryExtraForm(n, c) {
278
+ const u = { callback: c };
279
+ return n.forEach((a) => {
280
+ f.names[a] = !0;
281
+ }), f.subscribers.push(u), {
282
+ unregistry() {
283
+ f.subscribers = f.subscribers.filter((a) => a !== u);
284
+ }
285
+ };
286
+ },
287
+ onChange(n, c) {
288
+ const u = this.getForm(n), a = !w(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: h }) => {
290
+ h();
291
+ });
292
+ }
293
+ };
294
+ }, U = (o, r) => {
295
+ const e = P(null);
296
+ e.current === null && (e.current = H(o, r));
297
+ const s = e.current;
298
+ return {
299
+ useValue: (t) => M(t),
300
+ reset: (t) => {
301
+ s.reset(t);
302
+ },
303
+ setFocus: (t) => {
304
+ s.setFocus(t);
305
+ },
306
+ setError: (t, l) => {
307
+ s.setError(t, l);
308
+ },
309
+ setValue: (t, l) => {
310
+ s.setValue(t, l);
311
+ },
312
+ getValues: () => s.getValues(),
313
+ handleSubmit: async () => {
314
+ await s.handleSubmit();
315
+ },
316
+ Form: /* @__PURE__ */ g(J, { manager: s, scheme: o })
317
+ };
318
+ }, J = ({ manager: o, scheme: r }) => (A(() => {
319
+ o.activate();
320
+ }, []), D(() => () => {
321
+ o.disactivate();
322
+ }, []), /* @__PURE__ */ g(j.Provider, { value: o, children: /* @__PURE__ */ g(V, { scheme: r, path: [] }) })), $ = (o) => {
323
+ try {
324
+ o();
325
+ } catch (r) {
326
+ return r && typeof r == "string" ? {
327
+ error: !0,
328
+ messages: [r]
329
+ } : r instanceof Array && r.every((e) => e && typeof e == "string") ? {
330
+ error: !0,
331
+ messages: r
332
+ } : {
333
+ error: !0
334
+ };
335
+ }
336
+ }, W = (o) => (r, e) => {
337
+ const s = o.getRequireCheck(e), { validate: t, required: l } = e.$rules ?? {};
338
+ return {
339
+ type: "form",
340
+ name: r,
341
+ defaultValue: o.defaultValue,
342
+ Form: o.getForm(e),
343
+ validate: (y) => {
344
+ if (l) {
345
+ const b = $(() => s(y));
346
+ if (b)
347
+ return b;
348
+ if (t) {
349
+ const d = $(() => t(y));
350
+ if (d)
351
+ return d;
352
+ }
353
+ return;
354
+ }
355
+ if (t && !$(() => s(y))) {
356
+ const d = $(() => t(y));
357
+ if (d)
358
+ return d;
359
+ }
360
+ }
361
+ };
362
+ }, X = (o) => (r) => (...e) => ({
363
+ type: "layout",
364
+ Layout: o(r),
365
+ childs: e
366
+ }), Y = ({
367
+ getItems: o,
368
+ getController: r
369
+ }) => ({
370
+ type: "extra",
371
+ items: o((e) => (s) => ({
372
+ type: "registry",
373
+ name: e,
374
+ getForm: s
375
+ })),
376
+ getController: r
377
+ }), Z = (o) => (...r) => ({
378
+ type: "context",
379
+ context: o,
380
+ childs: r
381
+ }), k = (o, r) => (...e) => ({
382
+ type: "condition",
383
+ depended: o,
384
+ canActivate: r.canActivate,
385
+ childs: e
386
+ });
387
+ export {
388
+ k as Condition,
389
+ Z as Context,
390
+ Y as createExtraForm,
391
+ W as createForm,
392
+ X as createLayout,
393
+ U as useForm
394
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@companix/formkit",
3
+ "version": "0.0.01",
4
+ "module": "./dist/bundle.es.js",
5
+ "types": "./src/index.ts",
6
+ "type": "module",
7
+ "author": "Pavel Victorov",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "echo": "echo \"form-scheme\"",
13
+ "dev": "vite --config vite.config.ts",
14
+ "build": "vite build --config vite.build.ts"
15
+ },
16
+ "license": "ISC",
17
+ "peerDependencies": {
18
+ "react": "^18.3.1",
19
+ "react-dom": "^18.3.1"
20
+ },
21
+ "dependencies": {
22
+ "fast-deep-equal": "^3.1.3"
23
+ },
24
+ "devDependencies": {
25
+ "@rollup/plugin-eslint": "^9.0.5",
26
+ "@rollup/plugin-typescript": "^11.1.6",
27
+ "@types/react": "^18.3.1",
28
+ "@types/react-dom": "^18.3.1",
29
+ "@typescript-eslint/parser": "^5.62.0",
30
+ "@vitejs/plugin-react": "^4.6.0",
31
+ "eslint-config-react-app": "^7.0.1",
32
+ "eslint-plugin-import": "^2.29.1",
33
+ "eslint-plugin-react": "^7.34.3",
34
+ "eslint-plugin-react-hooks": "^4.6.2",
35
+ "eslint-webpack-plugin": "^4.2.0",
36
+ "react": "^18.3.1",
37
+ "react-dom": "^18.3.1",
38
+ "rollup-plugin-auto-external": "^2.0.0",
39
+ "sass": "^1.96.0",
40
+ "sass-embedded": "^1.96.0",
41
+ "vite": "^7.0.0",
42
+ "vite-plugin-checker": "^0.9.3"
43
+ }
44
+ }