Form-9sZcpyMB.mjs
13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { computed, useId, useTemplateRef, inject, provide, ref, reactive, readonly, createVNode, resolveDynamicComponent, unref, mergeProps, withCtx, renderSlot, useSSRContext } from 'vue';
import { ssrRenderVNode, ssrRenderSlot } from 'vue/server-renderer';
import { k as useAppConfig, l as useComponentUI, t as tv, G as formBusInjectionKey, H as formStateInjectionKey, I as formErrorsInjectionKey, J as formInputsInjectionKey, K as formLoadingInjectionKey, L as formOptionsInjectionKey, M as useEventBus } from './server.mjs';
function isSuperStructSchema(schema) {
return "schema" in schema && typeof schema.coercer === "function" && typeof schema.validator === "function" && typeof schema.refiner === "function";
}
function isStandardSchema(schema) {
return "~standard" in schema;
}
async function validateStandardSchema(state, schema) {
const result = await schema["~standard"].validate(state);
if (result.issues) {
return {
errors: result.issues?.map((issue) => ({
name: issue.path?.map((item) => typeof item === "object" ? item.key : item).join(".") || "",
message: issue.message
})) || [],
result: null
};
}
return {
errors: null,
result: result.value
};
}
async function validateSuperstructSchema(state, schema) {
const [err, result] = schema.validate(state);
if (err) {
const errors = err.failures().map((error) => ({
message: error.message,
name: error.path.join(".")
}));
return {
errors,
result: null
};
}
return {
errors: null,
result
};
}
function validateSchema(state, schema) {
if (isStandardSchema(schema)) {
return validateStandardSchema(state, schema);
} else if (isSuperStructSchema(schema)) {
return validateSuperstructSchema(state, schema);
} else {
throw new Error("Form validation failed: Unsupported form schema");
}
}
function getAtPath(data, path) {
if (!path) return data;
const value = path.split(".").reduce(
(value2, key) => value2?.[key],
data
);
return value;
}
function setAtPath(data, path, value) {
if (!path) return Object.assign(data, value);
if (!data) return data;
const keys = path.split(".");
let current = data;
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
if (current[key] === void 0 || current[key] === null) {
if (i + 1 < keys.length && !Number.isNaN(Number(keys[i + 1]))) {
current[key] = [];
} else {
current[key] = {};
}
}
current = current[key];
}
const lastKey = keys[keys.length - 1];
current[lastKey] = value;
return data;
}
class FormValidationException extends Error {
formId;
errors;
constructor(formId, errors) {
super("Form validation exception");
this.formId = formId;
this.errors = errors;
Object.setPrototypeOf(this, FormValidationException.prototype);
}
}
const theme = {
"base": ""
};
const _sfc_main = {
__name: "UForm",
__ssrInlineRender: true,
props: {
id: { type: [String, Number], required: false },
schema: { type: null, required: false },
state: { type: null, required: false },
validate: { type: Function, required: false },
validateOn: { type: Array, required: false, default() {
return ["input", "blur", "change"];
} },
disabled: { type: Boolean, required: false },
name: { type: null, required: false },
validateOnInputDelay: { type: Number, required: false, default: 300 },
transform: { type: null, required: false, default: () => true },
nested: { type: Boolean, required: false },
loadingAuto: { type: Boolean, required: false, default: true },
class: { type: null, required: false },
ui: { type: Object, required: false },
onSubmit: { type: Function, required: false }
},
emits: ["submit", "error"],
setup(__props, { expose: __expose, emit: __emit }) {
const props = __props;
const emits = __emit;
const appConfig = useAppConfig();
const uiProp = useComponentUI("form", props);
const ui = computed(() => tv({ extend: tv(theme), ...appConfig.ui?.form || {} }));
const formId = props.id ?? useId();
const formRef = useTemplateRef("formRef");
const bus = useEventBus(`form-${formId}`);
const parentBus = props.nested === true && inject(
formBusInjectionKey,
void 0
);
const parentState = props.nested === true ? inject(formStateInjectionKey, void 0) : void 0;
const state = computed(() => {
if (parentState?.value) {
return props.name ? getAtPath(parentState.value, props.name) : parentState.value;
}
return props.state;
});
provide(formBusInjectionKey, bus);
provide(formStateInjectionKey, state);
const nestedForms = ref(/* @__PURE__ */ new Map());
const errors = ref([]);
provide(formErrorsInjectionKey, errors);
const inputs = ref({});
provide(formInputsInjectionKey, inputs);
const dirtyFields = reactive(/* @__PURE__ */ new Set());
const touchedFields = reactive(/* @__PURE__ */ new Set());
const blurredFields = reactive(/* @__PURE__ */ new Set());
function resolveErrorIds(errs) {
return errs.map((err) => ({
...err,
id: err?.name ? inputs.value[err.name]?.id : void 0
}));
}
const transformedState = ref(null);
async function getErrors() {
let errs = props.validate ? await props.validate(state.value) ?? [] : [];
if (props.schema) {
const { errors: errors2, result } = await validateSchema(state.value, props.schema);
if (errors2) {
errs = errs.concat(errors2);
} else {
transformedState.value = result;
}
}
return resolveErrorIds(errs);
}
async function _validate(opts = { silent: false, nested: false, transform: false }) {
const names = opts.name && !Array.isArray(opts.name) ? [opts.name] : opts.name;
let nestedResults = [];
let nestedErrors = [];
if (!names && opts.nested) {
const validations = Array.from(nestedForms.value.values()).map(
(form) => validateNestedForm(form, opts)
);
const results = await Promise.all(validations);
nestedErrors = results.filter((r) => r.error).flatMap((r) => r.error.errors.map((e) => addFormPath(e, r.name)));
nestedResults = results.filter((r) => r.output !== void 0);
}
const currentErrors = await getErrors();
const allErrors = [...currentErrors, ...nestedErrors];
if (names) {
errors.value = filterErrorsByNames(allErrors, names);
} else {
errors.value = allErrors;
}
if (errors.value?.length) {
if (opts.silent) return false;
throw new FormValidationException(formId, errors.value);
}
if (opts.transform) {
nestedResults.forEach((result) => {
if (result.name) {
setAtPath(transformedState.value, result.name, result.output);
} else {
Object.assign(transformedState.value, result.output);
}
});
return transformedState.value ?? state.value;
}
return state.value;
}
const loading = ref(false);
provide(formLoadingInjectionKey, readonly(loading));
async function onSubmitWrapper(payload) {
loading.value = props.loadingAuto && true;
const event = payload;
try {
event.data = await _validate({ nested: true, transform: props.transform });
await props.onSubmit?.(event);
dirtyFields.clear();
} catch (error) {
if (!(error instanceof FormValidationException)) {
throw error;
}
const errorEvent = {
...event,
errors: error.errors
};
emits("error", errorEvent);
} finally {
loading.value = false;
}
}
const disabled = computed(() => props.disabled || loading.value);
provide(formOptionsInjectionKey, computed(() => ({
disabled: disabled.value,
validateOnInputDelay: props.validateOnInputDelay
})));
async function validateNestedForm(form, opts) {
try {
const result = await form.validate({ ...opts, silent: false });
return { name: form.name, output: result };
} catch (error) {
if (!(error instanceof FormValidationException)) throw error;
return { name: form.name, error };
}
}
function addFormPath(error, formPath) {
if (!formPath || !error.name) return error;
return { ...error, name: formPath + "." + error.name };
}
function stripFormPath(error, formPath) {
const prefix = formPath + ".";
const name = error?.name?.startsWith(prefix) ? error.name.substring(prefix.length) : error.name;
return { ...error, name };
}
function filterFormErrors(errors2, formPath) {
if (!formPath) return errors2;
return errors2.filter((e) => e?.name?.startsWith(formPath + ".")).map((e) => stripFormPath(e, formPath));
}
function getFormErrors(form) {
return form.api.getErrors().map(
(e) => form.name ? { ...e, name: form.name + "." + e.name } : e
);
}
function matchesTarget(target, path) {
if (!target || !path) return true;
if (target instanceof RegExp) return target.test(path);
return path === target || typeof target === "string" && target.startsWith(path + ".");
}
function getNestedTarget(target, formPath) {
if (!target || target instanceof RegExp) return target;
if (formPath === target) return void 0;
if (typeof target === "string" && target.startsWith(formPath + ".")) {
return target.substring(formPath.length + 1);
}
return target;
}
function filterErrorsByNames(allErrors, names) {
const nameSet = new Set(names);
const patterns = names.map((name) => inputs.value?.[name]?.pattern).filter(Boolean);
const matchesNames = (error) => {
if (!error.name) return false;
if (nameSet.has(error.name)) return true;
return patterns.some((pattern) => pattern.test(error.name));
};
const keepErrors = errors.value.filter((error) => !matchesNames(error));
const newErrors = allErrors.filter(matchesNames);
return [...keepErrors, ...newErrors];
}
function filterErrorsByTarget(currentErrors, target) {
return currentErrors.filter(
(err) => target instanceof RegExp ? !(err.name && target.test(err.name)) : !err.name || err.name !== target
);
}
function isLocalError(error) {
return !error.name || !!inputs.value[error.name];
}
const api = {
validate: _validate,
errors,
setErrors(errs, name) {
const localErrors = resolveErrorIds(errs.filter(isLocalError));
const nestedErrors = [];
for (const form of nestedForms.value.values()) {
if (matchesTarget(name, form.name)) {
const formErrors = filterFormErrors(errs, form.name);
form.api.setErrors(formErrors, getNestedTarget(name, form.name || ""));
nestedErrors.push(...getFormErrors(form));
}
}
if (name) {
const keepErrors = filterErrorsByTarget(errors.value, name);
errors.value = [...keepErrors, ...localErrors, ...nestedErrors];
} else {
errors.value = [...localErrors, ...nestedErrors];
}
},
async submit() {
if (formRef.value instanceof HTMLFormElement && formRef.value.reportValidity() === false) {
return;
}
await onSubmitWrapper(new Event("submit"));
},
getErrors(name) {
if (!name) return errors.value;
return errors.value.filter(
(err) => name instanceof RegExp ? err.name && name.test(err.name) : err.name === name
);
},
clear(name) {
const localErrors = name ? errors.value.filter(
(err) => isLocalError(err) && (name instanceof RegExp ? !(err.name && name.test(err.name)) : err.name !== name)
) : [];
const nestedErrors = [];
for (const form of nestedForms.value.values()) {
if (matchesTarget(name, form.name)) form.api.clear();
nestedErrors.push(...getFormErrors(form));
}
errors.value = [...localErrors, ...nestedErrors];
},
disabled,
loading,
dirty: computed(() => !!dirtyFields.size),
dirtyFields: readonly(dirtyFields),
blurredFields: readonly(blurredFields),
touchedFields: readonly(touchedFields)
};
__expose(api);
return (_ctx, _push, _parent, _attrs) => {
ssrRenderVNode(_push, createVNode(resolveDynamicComponent(unref(parentBus) ? "div" : "form"), mergeProps({
id: unref(formId),
ref_key: "formRef",
ref: formRef,
class: ui.value({ class: [unref(uiProp)?.base, props.class] }),
onSubmit: onSubmitWrapper
}, _attrs), {
default: withCtx((_, _push2, _parent2, _scopeId) => {
if (_push2) {
ssrRenderSlot(_ctx.$slots, "default", {
errors: errors.value,
loading: loading.value
}, null, _push2, _parent2, _scopeId);
} else {
return [
renderSlot(_ctx.$slots, "default", {
errors: errors.value,
loading: loading.value
})
];
}
}),
_: 3
}), _parent);
};
}
};
const _sfc_setup = _sfc_main.setup;
_sfc_main.setup = (props, ctx) => {
const ssrContext = useSSRContext();
(ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("../node_modules/.pnpm/@nuxt+ui@4.7.1_@internationalized+date@3.12.1_@internationalized+number@3.6.6_@tiptap+e_90a56fc914ccb75fcd2d7dd88cfc0d9f/node_modules/@nuxt/ui/dist/runtime/components/Form.vue");
return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};
export { _sfc_main as _ };
//# sourceMappingURL=Form-9sZcpyMB.mjs.map