@myparcel-dev/vue-form-builder 1.0.0-beta.53
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/CHANGELOG.md +301 -0
- package/dist/components/FormElement.vue.d.ts +22 -0
- package/dist/components/FormElementWrapper.d.ts +24 -0
- package/dist/components/Fragment.vue.d.ts +17 -0
- package/dist/components/MagicForm.vue.d.ts +67 -0
- package/dist/components/RenderedFormContent.vue.d.ts +2 -0
- package/dist/composables/createFieldHooks.d.ts +5 -0
- package/dist/composables/useElement.d.ts +3 -0
- package/dist/composables/useForm.d.ts +3 -0
- package/dist/composables/useFormBuilder.d.ts +3 -0
- package/dist/composables/useLifecycleHooks.d.ts +10 -0
- package/dist/composables/useTestAttributes.d.ts +3 -0
- package/dist/data/hooks.d.ts +14 -0
- package/dist/deprecated.d.ts +8 -0
- package/dist/form/Field.d.ts +55 -0
- package/dist/form/Form.d.ts +36 -0
- package/dist/hooks/HookManager.d.ts +27 -0
- package/dist/hooks/createHookManager.d.ts +4 -0
- package/dist/hooks/filterMatchingHooks.d.ts +3 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +683 -0
- package/dist/plugin/MyParcelFormBuilderPlugin.d.ts +10 -0
- package/dist/plugin/devtools/createFieldsNodes.d.ts +5 -0
- package/dist/plugin/devtools/createFormNode.d.ts +5 -0
- package/dist/plugin/devtools/getComponentName.d.ts +3 -0
- package/dist/plugin/devtools/setupDevtools.d.ts +3 -0
- package/dist/plugin/devtools/types.d.ts +7 -0
- package/dist/symbols.d.ts +6 -0
- package/dist/types/common.types.d.ts +6 -0
- package/dist/types/component.types.d.ts +17 -0
- package/dist/types/deprecated.types.d.ts +35 -0
- package/dist/types/element.types.d.ts +0 -0
- package/dist/types/field.types.d.ts +199 -0
- package/dist/types/form-builder.types.d.ts +27 -0
- package/dist/types/form.types.d.ts +202 -0
- package/dist/types/hooks.types.d.ts +11 -0
- package/dist/types/other.types.d.ts +0 -0
- package/dist/types/plain-element.types.d.ts +0 -0
- package/dist/types/utils.types.d.ts +14 -0
- package/dist/types/validator.types.d.ts +21 -0
- package/dist/utils/createField.d.ts +3 -0
- package/dist/utils/createForm.d.ts +3 -0
- package/dist/utils/defineField.d.ts +8 -0
- package/dist/utils/defineForm.d.ts +3 -0
- package/dist/utils/generateFieldName.d.ts +4 -0
- package/dist/utils/getDefaultFormConfiguration.d.ts +3 -0
- package/dist/utils/markComponentAsRaw.d.ts +4 -0
- package/dist/utils/normalizeFieldConfiguration.d.ts +12 -0
- package/dist/utils/normalizeFormConfiguration.d.ts +3 -0
- package/dist/utils/toPromise.d.ts +3 -0
- package/dist/utils/useDynamicWatcher.d.ts +4 -0
- package/dist/validators/isRequired.d.ts +4 -0
- package/package.json +51 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type { ComponentOrHtmlElement, ComponentProps } from './types/component.types';
|
|
2
|
+
export type { CreatedField, FieldConfiguration, FieldEmits, FieldHooks, FieldInstance, FieldName, FieldProps, FieldSlots, FieldWrapperProps, ModularCreatedField, FieldInstanceProps, } from './types/field.types';
|
|
3
|
+
export type { CreatedForm, FormConfiguration, FormInstance, FormValues } from './types/form.types';
|
|
4
|
+
export type { MaybeUnwrapNestedRefs } from './types/common.types';
|
|
5
|
+
export type { SelectOption } from './types/utils.types';
|
|
6
|
+
export type { Validator } from './types/validator.types';
|
|
7
|
+
export { FORM_HOOKS, FormHook } from './data/hooks';
|
|
8
|
+
export { MyParcelFormBuilderPlugin, createMyParcelFormBuilderPlugin } from './plugin/MyParcelFormBuilderPlugin';
|
|
9
|
+
export { createField } from './utils/createField';
|
|
10
|
+
export { createForm } from './utils/createForm';
|
|
11
|
+
export { default } from './components/MagicForm.vue';
|
|
12
|
+
export { defineField } from './utils/defineField';
|
|
13
|
+
export { defineForm } from './utils/defineForm';
|
|
14
|
+
export { useElement } from './composables/useElement';
|
|
15
|
+
export { useForm } from './composables/useForm';
|
|
16
|
+
export { useFormBuilder } from './composables/useFormBuilder';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
var oe = Object.defineProperty;
|
|
2
|
+
var ae = (s, e, t) => e in s ? oe(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
|
|
3
|
+
var a = (s, e, t) => ae(s, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import * as ne from "vue";
|
|
5
|
+
import { markRaw as F, watchEffect as le, ref as f, reactive as H, toValue as o, computed as y, watch as L, isRef as c, inject as U, defineComponent as w, toRefs as K, unref as v, openBlock as p, createBlock as g, resolveDynamicComponent as T, mergeProps as q, createSlots as ue, renderList as A, withCtx as k, renderSlot as D, normalizeProps as he, guardReactiveProps as fe, provide as G, h as E, onMounted as J, onUnmounted as de, defineAsyncComponent as ce, createElementBlock as _, Fragment as B, Suspense as Y, createCommentVNode as z, withModifiers as x, createVNode as me } from "vue";
|
|
6
|
+
import { isDefined as pe, useMemoize as be } from "@vueuse/core";
|
|
7
|
+
var m = /* @__PURE__ */ ((s) => (s.BeforeValidate = "beforeValidate", s.AfterValidate = "afterValidate", s.BeforeSubmit = "beforeSubmit", s.AfterSubmit = "afterSubmit", s.BeforeReset = "beforeReset", s.AfterReset = "afterReset", s.ElementChange = "afterElementChange", s.BeforeAddElement = "beforeAddElement", s.AfterAddElement = "afterAddElement", s))(m || {});
|
|
8
|
+
const Q = Object.freeze(Object.values(m)), M = Object.freeze([
|
|
9
|
+
"onCreated",
|
|
10
|
+
"onActivated",
|
|
11
|
+
"onBeforeMount",
|
|
12
|
+
"onBeforeUnmount",
|
|
13
|
+
"onBeforeUpdate",
|
|
14
|
+
"onDeactivated",
|
|
15
|
+
"onMounted",
|
|
16
|
+
"onUnmounted",
|
|
17
|
+
"onUpdated"
|
|
18
|
+
]), ye = Object.freeze([
|
|
19
|
+
...M,
|
|
20
|
+
"blur",
|
|
21
|
+
"click",
|
|
22
|
+
"focus",
|
|
23
|
+
"focus",
|
|
24
|
+
"sanitize",
|
|
25
|
+
"update",
|
|
26
|
+
"validate"
|
|
27
|
+
]);
|
|
28
|
+
function C(s, e) {
|
|
29
|
+
return (s == null ? void 0 : s[e]) !== void 0;
|
|
30
|
+
}
|
|
31
|
+
var ge = async (s, e) => {
|
|
32
|
+
for (let t = 0; t < s.length; t++)
|
|
33
|
+
if (!await e(s[t], t, s))
|
|
34
|
+
return !1;
|
|
35
|
+
return !0;
|
|
36
|
+
};
|
|
37
|
+
const N = (s) => s.charAt(0).toUpperCase() + s.slice(1), we = (s, e) => {
|
|
38
|
+
const t = [e];
|
|
39
|
+
if (e.startsWith("before")) {
|
|
40
|
+
const i = e.replace(/^before/, "").toLowerCase();
|
|
41
|
+
t.push(i), t.push(`after${N(i)}`);
|
|
42
|
+
}
|
|
43
|
+
if (e.startsWith("after")) {
|
|
44
|
+
const i = e.replace(/^after/, "").toLowerCase();
|
|
45
|
+
t.push(i), t.push(`before${N(i)}`);
|
|
46
|
+
}
|
|
47
|
+
return t.some((i) => s.includes(i));
|
|
48
|
+
};
|
|
49
|
+
class X {
|
|
50
|
+
constructor(e) {
|
|
51
|
+
a(this, "availableHooks");
|
|
52
|
+
a(this, "registeredHooks", []);
|
|
53
|
+
this.availableHooks = e.hookNames ?? [], Object.keys(e).filter((t) => we(this.availableHooks, t)).forEach((t) => {
|
|
54
|
+
this.register(t, e[t]);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
getAvailableHooks() {
|
|
58
|
+
return this.availableHooks;
|
|
59
|
+
}
|
|
60
|
+
getRegisteredHooks() {
|
|
61
|
+
return this.registeredHooks;
|
|
62
|
+
}
|
|
63
|
+
async execute(e, ...t) {
|
|
64
|
+
const i = await Promise.all(
|
|
65
|
+
this.registeredHooks.filter((r) => r.name === e).map((r) => r.callback(...t))
|
|
66
|
+
);
|
|
67
|
+
return i[i.length - 1];
|
|
68
|
+
}
|
|
69
|
+
has(e, t) {
|
|
70
|
+
return this.registeredHooks.some((i) => i.name === e && (t == null ? void 0 : t(i)));
|
|
71
|
+
}
|
|
72
|
+
register(e, t) {
|
|
73
|
+
const i = () => this.unregister(e, t);
|
|
74
|
+
return this.has(e, (r) => this.callbackMatches(r, t)) || this.registeredHooks.push({ name: e, callback: t }), i;
|
|
75
|
+
}
|
|
76
|
+
unregister(e, t) {
|
|
77
|
+
const i = () => this.registeredHooks.findIndex((l) => l.name === e && (!t || this.callbackMatches(l, t)));
|
|
78
|
+
let r;
|
|
79
|
+
for (; (r = i()) > -1; )
|
|
80
|
+
this.registeredHooks.splice(r, 1);
|
|
81
|
+
}
|
|
82
|
+
// eslint-disable-next-line class-methods-use-this
|
|
83
|
+
callbackMatches(e, t) {
|
|
84
|
+
return e.callback.toString() === t.toString();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const ve = [
|
|
88
|
+
"name",
|
|
89
|
+
"isVisible",
|
|
90
|
+
"isDirty",
|
|
91
|
+
"isDisabled",
|
|
92
|
+
"isOptional",
|
|
93
|
+
"isReadOnly",
|
|
94
|
+
"isSuspended",
|
|
95
|
+
"isTouched",
|
|
96
|
+
"isValid"
|
|
97
|
+
], V = (s) => {
|
|
98
|
+
if (typeof s == "string" || typeof s == "boolean")
|
|
99
|
+
return s;
|
|
100
|
+
if (s)
|
|
101
|
+
return F(s);
|
|
102
|
+
}, W = (s, e) => {
|
|
103
|
+
var t, i, r, l, h, n;
|
|
104
|
+
return {
|
|
105
|
+
...s,
|
|
106
|
+
...e,
|
|
107
|
+
form: {
|
|
108
|
+
...s.form,
|
|
109
|
+
...e.form,
|
|
110
|
+
wrapper: V(((t = e.form) == null ? void 0 : t.wrapper) ?? ((i = s.form) == null ? void 0 : i.wrapper)),
|
|
111
|
+
attributes: {
|
|
112
|
+
...s.form.attributes,
|
|
113
|
+
...(r = e.form) == null ? void 0 : r.attributes
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
fieldDefaults: {
|
|
117
|
+
...s.fieldDefaults,
|
|
118
|
+
...e.fieldDefaults,
|
|
119
|
+
wrapper: V(((l = e.fieldDefaults) == null ? void 0 : l.wrapper) ?? ((h = s.fieldDefaults) == null ? void 0 : h.wrapper)),
|
|
120
|
+
attributes: {
|
|
121
|
+
...s.fieldDefaults.attributes,
|
|
122
|
+
...(n = e.fieldDefaults) == null ? void 0 : n.attributes
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}, ke = () => ({
|
|
127
|
+
field: {
|
|
128
|
+
elementProp: !0
|
|
129
|
+
},
|
|
130
|
+
fieldDefaults: {
|
|
131
|
+
attributes: {},
|
|
132
|
+
wrapper: !0
|
|
133
|
+
},
|
|
134
|
+
form: {
|
|
135
|
+
attributes: {},
|
|
136
|
+
tag: "form"
|
|
137
|
+
}
|
|
138
|
+
}), Z = (s) => new X(s), Ve = (s) => ({
|
|
139
|
+
validate: (e, t) => !(t == null || t === ""),
|
|
140
|
+
errorMessage: s
|
|
141
|
+
}), Ee = (s) => C(s, "then") ? s : Promise.resolve(s), Oe = (s, e) => le(() => {
|
|
142
|
+
Ee(s()).then((t) => {
|
|
143
|
+
e.value = t;
|
|
144
|
+
});
|
|
145
|
+
}), De = (s, e) => ({
|
|
146
|
+
disabled: !1,
|
|
147
|
+
lazy: !1,
|
|
148
|
+
optional: !1,
|
|
149
|
+
readOnly: !1,
|
|
150
|
+
visible: !0,
|
|
151
|
+
wrapper: !0,
|
|
152
|
+
...e == null ? void 0 : e.config.fieldDefaults,
|
|
153
|
+
...s
|
|
154
|
+
});
|
|
155
|
+
class Fe {
|
|
156
|
+
// eslint-disable-next-line max-lines-per-function, complexity
|
|
157
|
+
constructor(e, t) {
|
|
158
|
+
a(this, "attributes");
|
|
159
|
+
a(this, "component");
|
|
160
|
+
a(this, "config");
|
|
161
|
+
a(this, "errors", f([]));
|
|
162
|
+
a(this, "errorsTarget");
|
|
163
|
+
a(this, "form");
|
|
164
|
+
a(this, "formattedErrors");
|
|
165
|
+
a(this, "hooks");
|
|
166
|
+
a(this, "isDirty", f(!1));
|
|
167
|
+
a(this, "isDisabled", f(!1));
|
|
168
|
+
a(this, "isOptional", f(!1));
|
|
169
|
+
a(this, "isReadOnly", f(!1));
|
|
170
|
+
a(this, "isSuspended", f(!1));
|
|
171
|
+
a(this, "isTouched", f(!1));
|
|
172
|
+
a(this, "isValid", f(!0));
|
|
173
|
+
a(this, "isVisible", f(!0));
|
|
174
|
+
a(this, "label");
|
|
175
|
+
a(this, "lazy", !1);
|
|
176
|
+
a(this, "name");
|
|
177
|
+
a(this, "props");
|
|
178
|
+
a(this, "ref");
|
|
179
|
+
a(this, "validators", f([]));
|
|
180
|
+
a(this, "wrapper");
|
|
181
|
+
a(this, "destroyHandles", f([]));
|
|
182
|
+
a(this, "initialValue");
|
|
183
|
+
a(this, "blur", async () => {
|
|
184
|
+
await this.hooks.execute("beforeBlur", this, o(this.ref)), this.setIsSuspended(!0), this.setValue(await this.hooks.execute("sanitize", this, o(this.ref)) ?? o(this.ref)), o(this.isDirty) && await this.validate(), this.setIsTouched(!0), this.setIsSuspended(!1), await this.hooks.execute("afterBlur", this, o(this.ref));
|
|
185
|
+
});
|
|
186
|
+
a(this, "focus", async (e, t) => {
|
|
187
|
+
await this.hooks.execute("beforeFocus", this, t), await this.hooks.execute("focus", this, t), await this.hooks.execute("afterFocus", this, t);
|
|
188
|
+
});
|
|
189
|
+
a(this, "reset", () => {
|
|
190
|
+
this.resetValidation(), this.setIsDirty(!1), this.setIsTouched(!1), this.setValue(this.initialValue);
|
|
191
|
+
});
|
|
192
|
+
a(this, "validate", async () => {
|
|
193
|
+
var h;
|
|
194
|
+
if (this.resetValidation(), await this.hooks.execute("beforeValidate", this, o(this.ref)), o(this.isDisabled))
|
|
195
|
+
return this.setValid(!0), o(this.isValid);
|
|
196
|
+
const e = async (n) => {
|
|
197
|
+
var d;
|
|
198
|
+
const u = await n.validate(this, o(this.ref));
|
|
199
|
+
if (!u && n.errorMessage)
|
|
200
|
+
if (this.errorsTarget) {
|
|
201
|
+
const b = o(this.form.fields).find((re) => re.name === this.errorsTarget);
|
|
202
|
+
(d = o(b == null ? void 0 : b.errors)) == null || d.push(n.errorMessage);
|
|
203
|
+
} else
|
|
204
|
+
o(this.errors).push(n.errorMessage);
|
|
205
|
+
return u;
|
|
206
|
+
}, t = o(this.validators).filter((n) => !n.precedence), i = o(this.validators).filter((n) => C(n, "precedence")).sort((n, u) => (n.precedence ?? 0) - (u.precedence ?? 0));
|
|
207
|
+
o(this.isOptional) || t.push(Ve(((h = this.form.config.validationMessages) == null ? void 0 : h.required) ?? ""));
|
|
208
|
+
const r = (await Promise.all(t.map(e))).every(Boolean), l = await ge(i, e);
|
|
209
|
+
return this.setValid(r && l), await this.hooks.execute("afterValidate", this, o(this.ref), o(this.isValid)), o(this.isValid);
|
|
210
|
+
});
|
|
211
|
+
var h, n, u;
|
|
212
|
+
const i = De(t, e);
|
|
213
|
+
this.config = i, this.ref = i.ref ?? f(), this.form = e, this.hooks = Z({
|
|
214
|
+
...i,
|
|
215
|
+
hookNames: [...ye, ...i.hookNames ?? []]
|
|
216
|
+
}), this.props = H(i.props ?? {}), this.attributes = H(i.attributes ?? {}), this.name = i.name, this.wrapper = i.wrapper, this.lazy = i.lazy, this.setVisible(i.visible ?? !0), this.setDisabled(i.disabled ?? !1), this.setOptional(i.optional ?? !1), this.setReadOnly(i.readOnly ?? !1), this.component = typeof i.component == "string" ? i.component : F(i.component), (h = e.config.initialValues) != null && h.hasOwnProperty(i.name) && this.setValue(e.config.initialValues[i.name]), this.initialValue = o(this.ref), this.errorsTarget = i.errorsTarget, this.label = i.label ? ((u = (n = e.config).renderLabel) == null ? void 0 : u.call(n, i.label)) ?? i.label : void 0, this.createValidators(t), this.formattedErrors = y(() => o(this.errors).map((d) => typeof d == "function" ? d() : d));
|
|
217
|
+
const r = () => {
|
|
218
|
+
this.initializeWatchers(t, i);
|
|
219
|
+
};
|
|
220
|
+
r(), e.on(m.AfterAddElement, r);
|
|
221
|
+
const l = L(this.ref, async (d, b) => {
|
|
222
|
+
await this.hooks.execute("beforeUpdate", this, d, b), this.setIsDirty(!0), await this.hooks.execute("afterUpdate", this, d, b);
|
|
223
|
+
});
|
|
224
|
+
this.destroyHandles.value.push(l);
|
|
225
|
+
}
|
|
226
|
+
addError(e) {
|
|
227
|
+
c(this.errors) ? this.errors.value.push(e) : this.errors.push(e);
|
|
228
|
+
}
|
|
229
|
+
async destroy() {
|
|
230
|
+
await Promise.all([
|
|
231
|
+
...this.destroyHandles.value.map((e) => e()),
|
|
232
|
+
...this.hooks.getRegisteredHooks().map((e) => this.hooks.unregister(e.name))
|
|
233
|
+
]);
|
|
234
|
+
}
|
|
235
|
+
resetValidation() {
|
|
236
|
+
this.setErrors([]);
|
|
237
|
+
}
|
|
238
|
+
setDisabled(e) {
|
|
239
|
+
c(this.isDisabled) ? this.isDisabled.value = e : this.isDisabled = e;
|
|
240
|
+
}
|
|
241
|
+
setInvalid() {
|
|
242
|
+
this.setValid(!1);
|
|
243
|
+
}
|
|
244
|
+
setOptional(e) {
|
|
245
|
+
c(this.isOptional) ? this.isOptional.value = e : this.isOptional = e;
|
|
246
|
+
}
|
|
247
|
+
setReadOnly(e) {
|
|
248
|
+
c(this.isReadOnly) ? this.isReadOnly.value = e : this.isReadOnly = e;
|
|
249
|
+
}
|
|
250
|
+
setValue(e) {
|
|
251
|
+
c(this.ref) ? this.ref.value = e : this.ref = e;
|
|
252
|
+
}
|
|
253
|
+
setVisible(e) {
|
|
254
|
+
c(this.isVisible) ? this.isVisible.value = e : this.isVisible = e;
|
|
255
|
+
}
|
|
256
|
+
createValidators(e) {
|
|
257
|
+
let t = [];
|
|
258
|
+
C(e, "validate") && (t = [
|
|
259
|
+
{
|
|
260
|
+
validate: e.validate,
|
|
261
|
+
errorMessage: e.errorMessage
|
|
262
|
+
}
|
|
263
|
+
]), C(e, "validators") && (t = e.validators ?? []), this.setValidators(t);
|
|
264
|
+
}
|
|
265
|
+
initializeWatchers(e, t) {
|
|
266
|
+
[
|
|
267
|
+
[e.visibleWhen, this.isVisible, t.visible],
|
|
268
|
+
[e.disabledWhen, this.isDisabled, t.disabled],
|
|
269
|
+
[e.optionalWhen, this.isOptional, t.optional],
|
|
270
|
+
[e.readOnlyWhen, this.isReadOnly, t.readOnly]
|
|
271
|
+
].forEach(([i, r, l]) => {
|
|
272
|
+
if (!pe(i))
|
|
273
|
+
return;
|
|
274
|
+
r.value = l;
|
|
275
|
+
const h = Oe(() => i(this), r);
|
|
276
|
+
this.destroyHandles.value.push(h);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
setErrors(e) {
|
|
280
|
+
c(this.errors) ? this.errors.value = e : this.errors = e;
|
|
281
|
+
}
|
|
282
|
+
setIsDirty(e) {
|
|
283
|
+
c(this.isDirty) ? this.isDirty.value = e : this.isDirty = e;
|
|
284
|
+
}
|
|
285
|
+
setIsSuspended(e) {
|
|
286
|
+
c(this.isSuspended) ? this.isSuspended.value = e : this.isSuspended = e;
|
|
287
|
+
}
|
|
288
|
+
setIsTouched(e) {
|
|
289
|
+
c(this.isTouched) ? this.isTouched.value = e : this.isTouched = e;
|
|
290
|
+
}
|
|
291
|
+
setValid(e) {
|
|
292
|
+
c(this.isValid) ? this.isValid.value = e : this.isValid = e;
|
|
293
|
+
}
|
|
294
|
+
setValidators(e) {
|
|
295
|
+
c(this.validators) ? this.validators.value = e : this.validators = e;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
class Re {
|
|
299
|
+
constructor(e, t) {
|
|
300
|
+
a(this, "config");
|
|
301
|
+
a(this, "fields", f([]));
|
|
302
|
+
a(this, "hooks");
|
|
303
|
+
a(this, "isDirty");
|
|
304
|
+
a(this, "isValid");
|
|
305
|
+
a(this, "model", {});
|
|
306
|
+
a(this, "name");
|
|
307
|
+
a(this, "off");
|
|
308
|
+
a(this, "on");
|
|
309
|
+
a(this, "values");
|
|
310
|
+
a(this, "stopHandles", f([]));
|
|
311
|
+
a(this, "getFieldMemoized", be((e) => o(this.fields).find((i) => i.name === e) ?? null));
|
|
312
|
+
t.hookNames = [...Q, ...t.hookNames ?? []], this.hooks = Z(t), this.on = this.hooks.register.bind(this.hooks), this.off = this.hooks.unregister.bind(this.hooks), this.name = e, this.config = t, this.values = H({}), V(this.config.field.wrapper), V(this.config.fieldDefaults.wrapper), this.isDirty = y(() => o(this.fields).some((i) => o(i.isDirty))), this.isValid = y(() => o(this.fields).every((i) => o(i.isValid)));
|
|
313
|
+
}
|
|
314
|
+
async destroy() {
|
|
315
|
+
o(this.stopHandles).forEach((e) => e()), await Promise.all(o(this.fields).map((e) => e.destroy()));
|
|
316
|
+
}
|
|
317
|
+
async addElement(e) {
|
|
318
|
+
await this.hooks.execute(m.BeforeAddElement, this, e);
|
|
319
|
+
const t = this.addFieldInstance(e, this);
|
|
320
|
+
return await this.hooks.execute(m.AfterAddElement, this, e), t;
|
|
321
|
+
}
|
|
322
|
+
getField(e) {
|
|
323
|
+
return this.getFieldMemoized(e);
|
|
324
|
+
}
|
|
325
|
+
/** @deprecated use computed values */
|
|
326
|
+
getValue(e) {
|
|
327
|
+
const t = this.ensureGetField(e);
|
|
328
|
+
return o(t.ref);
|
|
329
|
+
}
|
|
330
|
+
/** @deprecated use computed values */
|
|
331
|
+
getValues() {
|
|
332
|
+
return o(this.values);
|
|
333
|
+
}
|
|
334
|
+
async reset() {
|
|
335
|
+
await this.hooks.execute(m.BeforeReset, this), await Promise.all(o(this.fields).map((e) => e.reset())), await this.hooks.execute(m.AfterReset, this);
|
|
336
|
+
}
|
|
337
|
+
setValue(e, t) {
|
|
338
|
+
const i = this.getField(e);
|
|
339
|
+
i && i.setValue(t);
|
|
340
|
+
}
|
|
341
|
+
setValues(e) {
|
|
342
|
+
Object.entries(e).forEach(([t, i]) => this.setValue(t, i));
|
|
343
|
+
}
|
|
344
|
+
async submit() {
|
|
345
|
+
await this.hooks.execute(m.BeforeSubmit, this), await this.validate(), await this.hooks.execute(m.AfterSubmit, this);
|
|
346
|
+
}
|
|
347
|
+
async validate() {
|
|
348
|
+
await this.hooks.execute(m.BeforeValidate, this), await Promise.all(o(this.fields).map((e) => e.validate())), await this.hooks.execute(m.AfterValidate, this);
|
|
349
|
+
}
|
|
350
|
+
removeElement(e) {
|
|
351
|
+
const t = this.getField(e);
|
|
352
|
+
if (!t)
|
|
353
|
+
return;
|
|
354
|
+
const i = o(this.fields).indexOf(t);
|
|
355
|
+
i !== -1 && o(this.fields).splice(i, 1);
|
|
356
|
+
}
|
|
357
|
+
ensureGetField(e) {
|
|
358
|
+
const t = this.getField(e);
|
|
359
|
+
if (!t)
|
|
360
|
+
throw new Error(`Field ${e} not found in form ${this.name}`);
|
|
361
|
+
return t;
|
|
362
|
+
}
|
|
363
|
+
addFieldInstance(e, t) {
|
|
364
|
+
var h;
|
|
365
|
+
const i = {
|
|
366
|
+
...t.config.fieldDefaults,
|
|
367
|
+
...e,
|
|
368
|
+
attributes: {
|
|
369
|
+
...(h = t.config.fieldDefaults) == null ? void 0 : h.attributes,
|
|
370
|
+
...e.attributes
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
V(i.component), V(i.wrapper);
|
|
374
|
+
const r = new Fe(t, i), l = L(r.ref, async (n) => {
|
|
375
|
+
await this.hooks.execute(m.ElementChange, this, r, n), o(r.isDisabled) || (this.values[i.name] = n);
|
|
376
|
+
});
|
|
377
|
+
return this.stopHandles.value.push(l), o(r.isDisabled) || (this.values[i.name] = o(r.ref)), this.model[i.name] = r, o(this.fields).push(r), this.getFieldMemoized.delete(i.name), r;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
let R, O, S;
|
|
381
|
+
const Se = "register", ee = () => (R ?? (R = f({})), O ?? (O = f(ke())), S ?? (S = new X({
|
|
382
|
+
hookNames: [Se]
|
|
383
|
+
})), {
|
|
384
|
+
forms: R,
|
|
385
|
+
defaults: O,
|
|
386
|
+
on(s, e) {
|
|
387
|
+
S.register(s, e);
|
|
388
|
+
},
|
|
389
|
+
getForm(s) {
|
|
390
|
+
return o(R)[s];
|
|
391
|
+
},
|
|
392
|
+
// @ts-expect-error todo
|
|
393
|
+
register(s, e) {
|
|
394
|
+
S.execute("beforeRegister");
|
|
395
|
+
const t = new Re(s, W(o(O), { ...e }));
|
|
396
|
+
return o(R)[s] = t, S.execute("afterRegister", t), t;
|
|
397
|
+
},
|
|
398
|
+
setDefaults(s) {
|
|
399
|
+
O.value = W(o(O), s);
|
|
400
|
+
}
|
|
401
|
+
}), Ce = {
|
|
402
|
+
install(s, e) {
|
|
403
|
+
const t = ee();
|
|
404
|
+
s._context.config.globalProperties.$formBuilder = t, e && t.setDefaults(e);
|
|
405
|
+
}
|
|
406
|
+
}, Ke = (s) => ({
|
|
407
|
+
install: (e) => Ce.install(e, s)
|
|
408
|
+
}), te = Symbol("form"), se = Symbol("element"), $ = () => {
|
|
409
|
+
const s = U(te);
|
|
410
|
+
if (!s)
|
|
411
|
+
throw new Error("No form found.");
|
|
412
|
+
return s;
|
|
413
|
+
}, He = (s) => ({}), P = "onCreated", ie = () => ({
|
|
414
|
+
hooks: M,
|
|
415
|
+
register(s, ...e) {
|
|
416
|
+
s.has(P) && s.execute(P, ...e), s.getRegisteredHooks().filter(
|
|
417
|
+
(t) => M.includes(t.name) && t.name !== P
|
|
418
|
+
).forEach((t) => {
|
|
419
|
+
ne[t.name](async () => {
|
|
420
|
+
await s.execute(t.name, ...e);
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}), $e = (s) => {
|
|
425
|
+
ie().register(s.hooks, s);
|
|
426
|
+
}, _e = /* @__PURE__ */ w({
|
|
427
|
+
__name: "FormElement",
|
|
428
|
+
props: {
|
|
429
|
+
element: {}
|
|
430
|
+
},
|
|
431
|
+
setup(s) {
|
|
432
|
+
const e = s, t = K(e);
|
|
433
|
+
$e(t.element.value);
|
|
434
|
+
const i = y(() => {
|
|
435
|
+
var n, u;
|
|
436
|
+
const l = (u = (n = e.element.form) == null ? void 0 : n.config.field) == null ? void 0 : u.elementProp, h = {
|
|
437
|
+
...e.element.attributes
|
|
438
|
+
};
|
|
439
|
+
return typeof e.element.component != "string" && l === !0 && (h.element = e.element), l === "spread" && Object.keys(e.element).forEach((d) => {
|
|
440
|
+
const b = d;
|
|
441
|
+
ve.includes(b) && (h[d] = e.element[b]);
|
|
442
|
+
}), h;
|
|
443
|
+
}), r = y({
|
|
444
|
+
get() {
|
|
445
|
+
return v(e.element.ref);
|
|
446
|
+
},
|
|
447
|
+
set(l) {
|
|
448
|
+
c(e.element.ref) ? e.element.ref.value = l : e.element.ref = l;
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
return (l, h) => (p(), g(T(l.element.component), q({
|
|
452
|
+
modelValue: r.value,
|
|
453
|
+
"onUpdate:modelValue": h[0] || (h[0] = (n) => r.value = n)
|
|
454
|
+
}, i.value), ue({ _: 2 }, [
|
|
455
|
+
A(Object.keys(l.$slots), (n) => ({
|
|
456
|
+
name: n,
|
|
457
|
+
fn: k((u) => [
|
|
458
|
+
D(l.$slots, n, he(fe(u || {})))
|
|
459
|
+
])
|
|
460
|
+
}))
|
|
461
|
+
]), 1040, ["modelValue"]));
|
|
462
|
+
}
|
|
463
|
+
}), I = w({
|
|
464
|
+
name: "FormElementWrapper",
|
|
465
|
+
props: {
|
|
466
|
+
form: {
|
|
467
|
+
type: Object,
|
|
468
|
+
required: !0
|
|
469
|
+
},
|
|
470
|
+
element: {
|
|
471
|
+
type: Object,
|
|
472
|
+
required: !0
|
|
473
|
+
}
|
|
474
|
+
},
|
|
475
|
+
setup(s) {
|
|
476
|
+
G(se, s.element);
|
|
477
|
+
},
|
|
478
|
+
render() {
|
|
479
|
+
var i, r, l;
|
|
480
|
+
let s = E(
|
|
481
|
+
_e,
|
|
482
|
+
{
|
|
483
|
+
...this.$attrs,
|
|
484
|
+
...He(this.element),
|
|
485
|
+
element: this.element
|
|
486
|
+
},
|
|
487
|
+
this.$slots
|
|
488
|
+
);
|
|
489
|
+
const e = typeof this.element.wrapper != "boolean";
|
|
490
|
+
if (this.element.wrapper === !0 && ((i = this.form.config.field) == null ? void 0 : i.wrapper) || e) {
|
|
491
|
+
const h = s;
|
|
492
|
+
s = E(
|
|
493
|
+
e ? this.element.wrapper : (r = this.form.config.field) == null ? void 0 : r.wrapper,
|
|
494
|
+
{
|
|
495
|
+
...this.$attrs,
|
|
496
|
+
...(l = this.form.config.fieldDefaults) == null ? void 0 : l.attributes,
|
|
497
|
+
element: this.element
|
|
498
|
+
},
|
|
499
|
+
{ default: () => h }
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
return s;
|
|
503
|
+
}
|
|
504
|
+
}), Be = (s, e, t) => w({
|
|
505
|
+
setup() {
|
|
506
|
+
const i = $(), r = f();
|
|
507
|
+
return J(async () => {
|
|
508
|
+
r.value = await i.addElement(s);
|
|
509
|
+
}), de(() => {
|
|
510
|
+
r.value && (i.removeElement(r.value.name), r.value = void 0);
|
|
511
|
+
}), {
|
|
512
|
+
element: r,
|
|
513
|
+
form: i
|
|
514
|
+
};
|
|
515
|
+
},
|
|
516
|
+
emits: [
|
|
517
|
+
"afterBlur",
|
|
518
|
+
"afterSanitize",
|
|
519
|
+
"afterUpdate",
|
|
520
|
+
"afterValidate",
|
|
521
|
+
"beforeBlur",
|
|
522
|
+
"beforeSanitize",
|
|
523
|
+
"beforeUpdate",
|
|
524
|
+
"beforeValidate",
|
|
525
|
+
"blur"
|
|
526
|
+
],
|
|
527
|
+
render() {
|
|
528
|
+
return this.element && E(
|
|
529
|
+
I,
|
|
530
|
+
{
|
|
531
|
+
...e,
|
|
532
|
+
...this.$attrs,
|
|
533
|
+
form: this.form,
|
|
534
|
+
element: this.element
|
|
535
|
+
},
|
|
536
|
+
{
|
|
537
|
+
...this.$slots,
|
|
538
|
+
...t
|
|
539
|
+
}
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
}), Pe = (s) => w({
|
|
543
|
+
setup() {
|
|
544
|
+
const e = $();
|
|
545
|
+
return { element: y(() => s.name && e.getField(s.name)) };
|
|
546
|
+
},
|
|
547
|
+
render() {
|
|
548
|
+
return this.element && E("label", { for: this.element.attributes.id ?? this.element.name }, [this.element.label]);
|
|
549
|
+
}
|
|
550
|
+
}), Ae = (s) => w({
|
|
551
|
+
setup() {
|
|
552
|
+
const e = $();
|
|
553
|
+
return { element: y(() => s.name ? e.getField(s.name) : void 0) };
|
|
554
|
+
},
|
|
555
|
+
render() {
|
|
556
|
+
var e, t, i;
|
|
557
|
+
return o((e = this.element) == null ? void 0 : e.errors) && ((i = (t = this.$slots).default) == null ? void 0 : i.call(t, { errors: o(this.element.errors) }));
|
|
558
|
+
}
|
|
559
|
+
}), j = (s) => F(ce(() => Promise.resolve(s()))), qe = (s) => H({
|
|
560
|
+
field: s,
|
|
561
|
+
ref: s.ref ?? f(),
|
|
562
|
+
Component: F((e, t) => E(Be(s, t.attrs, t.slots))),
|
|
563
|
+
Errors: j(() => Ae(s)),
|
|
564
|
+
Label: j(() => Pe(s))
|
|
565
|
+
}), Me = /* @__PURE__ */ w({
|
|
566
|
+
__name: "RenderedFormContent",
|
|
567
|
+
setup(s) {
|
|
568
|
+
const e = f(!1), t = $();
|
|
569
|
+
if (!t)
|
|
570
|
+
throw new Error("Form not found");
|
|
571
|
+
const i = y(() => o(t.fields).filter((l) => !l.teleportSelector)), r = y(() => o(t.fields).filter((l) => !!l.teleportSelector));
|
|
572
|
+
return (l, h) => (p(), _(B, null, [
|
|
573
|
+
v(t) ? (p(), g(Y, {
|
|
574
|
+
key: 0,
|
|
575
|
+
onResolve: h[0] || (h[0] = (n) => e.value = !0)
|
|
576
|
+
}, {
|
|
577
|
+
default: k(() => [
|
|
578
|
+
(p(!0), _(B, null, A(i.value, (n, u) => (p(), g(v(I), {
|
|
579
|
+
key: `element--${n.name ?? "unnamed"}--${u}`,
|
|
580
|
+
element: n,
|
|
581
|
+
form: v(t)
|
|
582
|
+
}, null, 8, ["element", "form"]))), 128))
|
|
583
|
+
]),
|
|
584
|
+
_: 1
|
|
585
|
+
})) : z("", !0),
|
|
586
|
+
v(t) && e.value ? (p(!0), _(B, { key: 1 }, A(r.value, (n, u) => (p(), g(v(I), {
|
|
587
|
+
key: `element--${n.name ?? "unnamed"}--${u}`,
|
|
588
|
+
element: n,
|
|
589
|
+
form: v(t)
|
|
590
|
+
}, null, 8, ["element", "form"]))), 128)) : z("", !0)
|
|
591
|
+
], 64));
|
|
592
|
+
}
|
|
593
|
+
}), Ie = w({
|
|
594
|
+
name: "Fragment",
|
|
595
|
+
props: {
|
|
596
|
+
component: {
|
|
597
|
+
type: [String, Object],
|
|
598
|
+
default: null
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}), Te = (s, e) => {
|
|
602
|
+
const t = s.__vccOpts || s;
|
|
603
|
+
for (const [i, r] of e)
|
|
604
|
+
t[i] = r;
|
|
605
|
+
return t;
|
|
606
|
+
};
|
|
607
|
+
function ze(s, e, t, i, r, l) {
|
|
608
|
+
return s.component ? (p(), g(T(s.component), { key: 0 }, {
|
|
609
|
+
default: k(() => [
|
|
610
|
+
D(s.$slots, "default")
|
|
611
|
+
]),
|
|
612
|
+
_: 3
|
|
613
|
+
})) : D(s.$slots, "default", { key: 1 });
|
|
614
|
+
}
|
|
615
|
+
const xe = /* @__PURE__ */ Te(Ie, [["render", ze]]), Ne = /* @__PURE__ */ w({
|
|
616
|
+
__name: "MagicForm",
|
|
617
|
+
props: {
|
|
618
|
+
form: {}
|
|
619
|
+
},
|
|
620
|
+
setup(s, { emit: e }) {
|
|
621
|
+
const t = f(null), i = s, r = e, h = K(i).form.value;
|
|
622
|
+
return J(() => {
|
|
623
|
+
const u = o(t);
|
|
624
|
+
u && (i.form.element = u);
|
|
625
|
+
}), G(te, h), Q.forEach((u) => {
|
|
626
|
+
i.form.on(u, (d) => {
|
|
627
|
+
r(u, d);
|
|
628
|
+
});
|
|
629
|
+
}), ie().register(h.hooks, h), (u, d) => (p(), g(T(u.form.config.form.tag), q({
|
|
630
|
+
id: u.form.name,
|
|
631
|
+
ref_key: "formElement",
|
|
632
|
+
ref: t
|
|
633
|
+
}, u.form.config.form.attributes, {
|
|
634
|
+
onSubmit: d[0] || (d[0] = x(() => u.form.submit(), ["prevent"])),
|
|
635
|
+
onReset: d[1] || (d[1] = x(() => u.form.reset(), ["prevent"]))
|
|
636
|
+
}), {
|
|
637
|
+
default: k(() => [
|
|
638
|
+
me(xe, {
|
|
639
|
+
component: u.form.config.form.wrapper
|
|
640
|
+
}, {
|
|
641
|
+
default: k(() => [
|
|
642
|
+
u.$slots.default ? (p(), g(Y, { key: 0 }, {
|
|
643
|
+
default: k(() => [
|
|
644
|
+
D(u.$slots, "default")
|
|
645
|
+
]),
|
|
646
|
+
fallback: k(() => [
|
|
647
|
+
D(u.$slots, "loader")
|
|
648
|
+
]),
|
|
649
|
+
_: 3
|
|
650
|
+
})) : (p(), g(Me, { key: 1 }))
|
|
651
|
+
]),
|
|
652
|
+
_: 3
|
|
653
|
+
}, 8, ["component"])
|
|
654
|
+
]),
|
|
655
|
+
_: 3
|
|
656
|
+
}, 16, ["id"]));
|
|
657
|
+
}
|
|
658
|
+
}), We = (s, e) => ee().register(s, e), Ge = (s, e) => {
|
|
659
|
+
const t = We(s, e);
|
|
660
|
+
return {
|
|
661
|
+
Component: F((i, r) => E(Ne, { ...r.attrs, form: t }, r.slots)),
|
|
662
|
+
instance: t
|
|
663
|
+
};
|
|
664
|
+
}, Je = (s) => s, Ye = () => {
|
|
665
|
+
const s = U(se);
|
|
666
|
+
if (!s)
|
|
667
|
+
throw new Error("No element found.");
|
|
668
|
+
return s;
|
|
669
|
+
};
|
|
670
|
+
export {
|
|
671
|
+
Q as FORM_HOOKS,
|
|
672
|
+
m as FormHook,
|
|
673
|
+
Ce as MyParcelFormBuilderPlugin,
|
|
674
|
+
qe as createField,
|
|
675
|
+
Ge as createForm,
|
|
676
|
+
Ke as createMyParcelFormBuilderPlugin,
|
|
677
|
+
Ne as default,
|
|
678
|
+
Je as defineField,
|
|
679
|
+
We as defineForm,
|
|
680
|
+
Ye as useElement,
|
|
681
|
+
$ as useForm,
|
|
682
|
+
ee as useFormBuilder
|
|
683
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Plugin } from 'vue';
|
|
2
|
+
import { FormConfiguration } from '../types/form.types';
|
|
3
|
+
|
|
4
|
+
export type MyParcelFormBuilderPlugin = Plugin;
|
|
5
|
+
export type MyParcelFormBuilderPluginOptions = Partial<FormConfiguration>;
|
|
6
|
+
/**
|
|
7
|
+
* Plugin to register the form builder.
|
|
8
|
+
*/
|
|
9
|
+
export declare const MyParcelFormBuilderPlugin: MyParcelFormBuilderPlugin;
|
|
10
|
+
export declare const createMyParcelFormBuilderPlugin: (options?: MyParcelFormBuilderPluginOptions) => MyParcelFormBuilderPlugin;
|