@vizejs/composable 0.809.0 → 0.811.0
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/dist/abort-signal.mjs +0 -62
- package/dist/active-element.mjs +0 -11
- package/dist/animate.mjs +0 -19
- package/dist/async-resource.mjs +0 -17
- package/dist/battery.mjs +0 -13
- package/dist/breakpoints.mjs +0 -24
- package/dist/capability.mjs +0 -12
- package/dist/{catalog-BRX-Wg8B.mjs → catalog-CcpCnLjb.mjs} +0 -14
- package/dist/catalog.mjs +1 -1
- package/dist/color-mode.mjs +0 -23
- package/dist/computed-with-control.mjs +0 -22
- package/dist/create-event-hook.mjs +0 -27
- package/dist/create-global-state.mjs +0 -23
- package/dist/create-shared-composable.mjs +0 -23
- package/dist/device-motion.mjs +0 -11
- package/dist/device-orientation.mjs +0 -23
- package/dist/device-pixel-ratio.mjs +0 -11
- package/dist/disposal-scope.mjs +0 -25
- package/dist/document-visibility.mjs +0 -14
- package/dist/draggable.mjs +0 -14
- package/dist/element-bounding.mjs +0 -14
- package/dist/element-by-point.mjs +0 -10
- package/dist/element-hover.mjs +0 -13
- package/dist/element-target.mjs +0 -30
- package/dist/focus.mjs +0 -23
- package/dist/fps.mjs +0 -12
- package/dist/geolocation.mjs +0 -14
- package/dist/i18n.mjs +0 -86
- package/dist/icu-message.mjs +0 -58
- package/dist/idle.mjs +0 -16
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/infinite-scroll.mjs +0 -16
- package/dist/intersection-observer.mjs +0 -27
- package/dist/locale.mjs +0 -20
- package/dist/magic-keys.mjs +0 -35
- package/dist/math.mjs +0 -37
- package/dist/media-query.mjs +0 -29
- package/dist/mouse-in-element.mjs +0 -12
- package/dist/mouse.mjs +0 -12
- package/dist/mutation-observer.mjs +0 -15
- package/dist/network.mjs +0 -24
- package/dist/on-click-outside.mjs +0 -16
- package/dist/on-key-stroke.mjs +0 -33
- package/dist/on-long-press.mjs +0 -14
- package/dist/on-start-typing.mjs +0 -23
- package/dist/page-leave.mjs +0 -11
- package/dist/parent-element.mjs +0 -9
- package/dist/pinch.mjs +0 -14
- package/dist/pointer-lock.mjs +0 -23
- package/dist/pointer.mjs +0 -18
- package/dist/preferences.mjs +0 -31
- package/dist/preferred-languages.mjs +0 -13
- package/dist/ref-auto-reset.mjs +0 -24
- package/dist/ref-debounced.mjs +0 -22
- package/dist/ref-default.mjs +0 -21
- package/dist/resize-observer.mjs +0 -31
- package/dist/resolver.mjs +1 -58
- package/dist/retry-async.mjs +0 -25
- package/dist/retry-delay.mjs +0 -23
- package/dist/scope.mjs +0 -14
- package/dist/screen-orientation.mjs +0 -15
- package/dist/scroll-lock.mjs +0 -14
- package/dist/scroll.mjs +0 -27
- package/dist/standard-schema.mjs +0 -31
- package/dist/swipe.mjs +0 -36
- package/dist/temporal.mjs +0 -30
- package/dist/text-selection.mjs +0 -15
- package/dist/textarea-autosize.mjs +0 -13
- package/dist/to-reactive.mjs +0 -20
- package/dist/transition.mjs +0 -7
- package/dist/until.mjs +0 -25
- package/dist/use-array.mjs +0 -52
- package/dist/use-barcode-detector.mjs +0 -25
- package/dist/use-bluetooth.mjs +0 -27
- package/dist/use-broadcast-channel.mjs +0 -30
- package/dist/use-builtin-ai.mjs +0 -68
- package/dist/use-clipboard.mjs +0 -27
- package/dist/use-compression-stream.mjs +0 -78
- package/dist/use-contact-picker.mjs +0 -22
- package/dist/use-cookie.mjs +0 -63
- package/dist/use-countdown.mjs +0 -22
- package/dist/use-counter.mjs +0 -29
- package/dist/use-css-var.mjs +0 -28
- package/dist/use-date-format.mjs +0 -48
- package/dist/use-date-time-format.mjs +0 -22
- package/dist/use-debounced.mjs +0 -39
- package/dist/use-display-media.mjs +0 -22
- package/dist/use-display-names.mjs +0 -19
- package/dist/use-document-title.mjs +0 -25
- package/dist/use-drop-zone.mjs +0 -36
- package/dist/use-event-source.mjs +0 -25
- package/dist/use-eye-dropper.mjs +0 -19
- package/dist/use-favicon.mjs +0 -23
- package/dist/use-fetch.mjs +0 -27
- package/dist/use-field.mjs +0 -27
- package/dist/use-file-dialog.mjs +0 -21
- package/dist/{use-form-C2JXZ0i7.mjs → use-form-BZqwMs6k.mjs} +143 -70
- package/dist/use-form-field-props.mjs +0 -57
- package/dist/use-form.d.mts +123 -18
- package/dist/use-form.mjs +1 -1
- package/dist/use-fullscreen.mjs +0 -23
- package/dist/use-gamepad.mjs +0 -50
- package/dist/use-hid.mjs +0 -25
- package/dist/use-history.mjs +0 -43
- package/dist/use-id-generator.mjs +0 -21
- package/dist/use-image.mjs +0 -22
- package/dist/use-indexed-db.mjs +0 -39
- package/dist/use-interval.mjs +0 -28
- package/dist/use-list-format.mjs +0 -18
- package/dist/use-machine.mjs +0 -34
- package/dist/use-map.mjs +0 -28
- package/dist/use-media-controls.mjs +0 -28
- package/dist/use-mounted.mjs +0 -18
- package/dist/use-navigation-api.mjs +0 -27
- package/dist/use-notification.mjs +0 -24
- package/dist/use-number-format.mjs +0 -23
- package/dist/use-object-url.mjs +0 -22
- package/dist/use-offset-pagination.mjs +0 -18
- package/dist/use-payment-request.mjs +0 -25
- package/dist/use-performance-observer.mjs +0 -27
- package/dist/use-permission.mjs +0 -26
- package/dist/use-plural-rules.mjs +0 -21
- package/dist/use-push-subscription.mjs +0 -28
- package/dist/use-queue.mjs +0 -29
- package/dist/use-raf-fn.mjs +0 -23
- package/dist/use-relative-time-format.mjs +0 -35
- package/dist/use-request-idle-callback.mjs +0 -42
- package/dist/use-route-query.mjs +0 -17
- package/dist/use-scheduler-post-task.mjs +0 -27
- package/dist/use-screen-details.mjs +0 -42
- package/dist/use-script-tag.mjs +0 -28
- package/dist/use-segmenter.mjs +0 -20
- package/dist/use-serial.mjs +0 -24
- package/dist/use-service-worker.mjs +0 -27
- package/dist/use-set.mjs +0 -23
- package/dist/use-share.mjs +0 -23
- package/dist/use-sorted-locale.mjs +0 -20
- package/dist/use-speech-recognition.mjs +0 -24
- package/dist/use-speech-synthesis.mjs +0 -26
- package/dist/use-stack.mjs +0 -27
- package/dist/use-storage-estimate.mjs +0 -41
- package/dist/use-storage.mjs +0 -68
- package/dist/use-style-tag.mjs +0 -22
- package/dist/use-supported.mjs +0 -17
- package/dist/use-throttled.mjs +0 -46
- package/dist/use-time-ago.mjs +0 -22
- package/dist/use-toggle.mjs +0 -19
- package/dist/use-url-hash.mjs +0 -28
- package/dist/use-url-search-params.mjs +0 -99
- package/dist/use-usb.mjs +0 -23
- package/dist/use-user-activation.mjs +0 -22
- package/dist/use-user-media.mjs +0 -50
- package/dist/use-vibrate.mjs +0 -21
- package/dist/use-view-transition.mjs +0 -22
- package/dist/use-visual-viewport.mjs +0 -25
- package/dist/use-wake-lock.mjs +0 -21
- package/dist/use-web-authn.mjs +0 -61
- package/dist/use-web-locks.mjs +0 -25
- package/dist/use-web-midi.mjs +0 -22
- package/dist/use-web-vitals.mjs +0 -41
- package/dist/use-websocket.mjs +0 -27
- package/dist/use-worker-fn.mjs +0 -37
- package/dist/virtual-list.mjs +0 -14
- package/dist/whenever.mjs +0 -21
- package/dist/window-size.mjs +0 -12
- package/package.json +2 -2
- package/registry/files/form-control-types.ts +260 -0
- package/registry/files/form-dirty.ts +131 -0
- package/registry/files/form-options.ts +156 -0
- package/registry/files/form-path-types.ts +135 -0
- package/registry/files/use-form.ts +102 -449
- package/registry/registry.json +28 -4
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
import { tryOnScopeDispose } from "./scope.mjs";
|
|
2
2
|
import { validateStandardSchema } from "./standard-schema.mjs";
|
|
3
|
-
import { computed, ref, shallowRef, toRaw, triggerRef } from "vue";
|
|
3
|
+
import { computed, ref, shallowRef, toRaw, toValue, triggerRef } from "vue";
|
|
4
4
|
//#region src/form-value-paths.ts
|
|
5
|
-
/** Whether a value is a plain object or an array that a path may descend into. */
|
|
6
5
|
function isContainer(value) {
|
|
7
6
|
if (Array.isArray(value)) return true;
|
|
8
7
|
if (typeof value !== "object" || value === null) return false;
|
|
9
8
|
const prototype = Object.getPrototypeOf(value);
|
|
10
9
|
return prototype === Object.prototype || prototype === null;
|
|
11
10
|
}
|
|
12
|
-
/** Keys that reach an object's prototype chain instead of its own data. */
|
|
13
11
|
const prototypeKeys = new Set([
|
|
14
12
|
"__proto__",
|
|
15
13
|
"constructor",
|
|
16
14
|
"prototype"
|
|
17
15
|
]);
|
|
18
|
-
/** Split a dotted path; undefined when a segment names the prototype chain. */
|
|
19
16
|
function splitPath(path) {
|
|
20
17
|
if (path === "") return [];
|
|
21
18
|
const keys = path.split(".");
|
|
@@ -30,7 +27,6 @@ function writeChild(container, key, value) {
|
|
|
30
27
|
if (Array.isArray(container)) container[Number(key)] = value;
|
|
31
28
|
else container[key] = value;
|
|
32
29
|
}
|
|
33
|
-
/** Read a dotted path. */
|
|
34
30
|
function readPath(root, path) {
|
|
35
31
|
const keys = splitPath(path);
|
|
36
32
|
if (keys === void 0) return void 0;
|
|
@@ -41,7 +37,6 @@ function readPath(root, path) {
|
|
|
41
37
|
}
|
|
42
38
|
return current;
|
|
43
39
|
}
|
|
44
|
-
/** Write a dotted path, creating intermediate containers. */
|
|
45
40
|
function writePath(root, path, value) {
|
|
46
41
|
const keys = splitPath(path);
|
|
47
42
|
const last = keys?.at(-1);
|
|
@@ -60,21 +55,96 @@ function writePath(root, path, value) {
|
|
|
60
55
|
writeChild(current, last, value);
|
|
61
56
|
}
|
|
62
57
|
//#endregion
|
|
63
|
-
//#region src/
|
|
64
|
-
function
|
|
65
|
-
return structuredClone(toRaw(value));
|
|
66
|
-
}
|
|
67
|
-
function deepEqual(left, right) {
|
|
58
|
+
//#region src/form-dirty.ts
|
|
59
|
+
function valuesEqual(left, right) {
|
|
68
60
|
if (Object.is(left, right)) return true;
|
|
69
61
|
if (left instanceof Date && right instanceof Date) return left.getTime() === right.getTime();
|
|
70
|
-
if (Array.isArray(left) && Array.isArray(right)) return left.length === right.length && left.every((item, index) =>
|
|
62
|
+
if (Array.isArray(left) && Array.isArray(right)) return left.length === right.length && left.every((item, index) => valuesEqual(item, right[index]));
|
|
71
63
|
if (isContainer(left) && isContainer(right) && !Array.isArray(left) && !Array.isArray(right)) {
|
|
72
64
|
const keys = new Set([...Object.keys(left), ...Object.keys(right)]);
|
|
73
|
-
for (const key of keys) if (!
|
|
65
|
+
for (const key of keys) if (!valuesEqual(left[key], right[key])) return false;
|
|
74
66
|
return true;
|
|
75
67
|
}
|
|
76
68
|
return false;
|
|
77
69
|
}
|
|
70
|
+
function blankDirty(value) {
|
|
71
|
+
if (value === void 0) return true;
|
|
72
|
+
if (Array.isArray(value)) return value.every((item) => blankDirty(item));
|
|
73
|
+
if (isContainer(value)) return Object.keys(value).length === 0;
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
function dirtyFieldsOf(current, previous) {
|
|
77
|
+
if (valuesEqual(current, previous)) {
|
|
78
|
+
if (Array.isArray(current)) return [];
|
|
79
|
+
if (isContainer(current)) return {};
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (Array.isArray(current) || Array.isArray(previous)) {
|
|
83
|
+
const left = Array.isArray(current) ? current : [];
|
|
84
|
+
const right = Array.isArray(previous) ? previous : [];
|
|
85
|
+
const length = Math.max(left.length, right.length);
|
|
86
|
+
return Array.from({ length }, (_, index) => {
|
|
87
|
+
if (index >= left.length || index >= right.length) return true;
|
|
88
|
+
const child = dirtyFieldsOf(left[index], right[index]);
|
|
89
|
+
return blankDirty(child) ? void 0 : child;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (isContainer(current) && isContainer(previous) && !Array.isArray(current) && !Array.isArray(previous)) {
|
|
93
|
+
const result = {};
|
|
94
|
+
const keys = new Set([...Object.keys(current), ...Object.keys(previous)]);
|
|
95
|
+
for (const key of keys) {
|
|
96
|
+
const child = dirtyFieldsOf(Object.hasOwn(current, key) ? current[key] : void 0, Object.hasOwn(previous, key) ? previous[key] : void 0);
|
|
97
|
+
if (blankDirty(child)) continue;
|
|
98
|
+
result[key] = child;
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
function own(record, key) {
|
|
105
|
+
if (!isContainer(record) || Array.isArray(record)) return void 0;
|
|
106
|
+
return Object.hasOwn(record, key) ? record[key] : void 0;
|
|
107
|
+
}
|
|
108
|
+
function mergeKept(base, current, previous) {
|
|
109
|
+
if (valuesEqual(current, previous)) return base;
|
|
110
|
+
if (Array.isArray(current) && Array.isArray(previous)) return current.map((item, index) => {
|
|
111
|
+
if (index >= previous.length) return item;
|
|
112
|
+
return mergeKept(Array.isArray(base) ? base[index] : void 0, item, previous[index]);
|
|
113
|
+
});
|
|
114
|
+
if (isContainer(current) && isContainer(previous) && !Array.isArray(current) && !Array.isArray(previous)) {
|
|
115
|
+
const baseObject = isContainer(base) && !Array.isArray(base) ? base : {};
|
|
116
|
+
const keys = new Set([
|
|
117
|
+
...Object.keys(baseObject),
|
|
118
|
+
...Object.keys(current),
|
|
119
|
+
...Object.keys(previous)
|
|
120
|
+
]);
|
|
121
|
+
const result = {};
|
|
122
|
+
for (const key of keys) {
|
|
123
|
+
const inCurrent = Object.hasOwn(current, key);
|
|
124
|
+
const inPrevious = Object.hasOwn(previous, key);
|
|
125
|
+
if (!inCurrent && inPrevious) continue;
|
|
126
|
+
if (inCurrent && !inPrevious) {
|
|
127
|
+
result[key] = current[key];
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (!inCurrent) {
|
|
131
|
+
if (Object.hasOwn(baseObject, key)) result[key] = baseObject[key];
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
result[key] = mergeKept(own(baseObject, key), current[key], previous[key]);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
return current;
|
|
139
|
+
}
|
|
140
|
+
function valuesKeepingDirty(base, current, previous) {
|
|
141
|
+
return mergeKept(base, current, previous);
|
|
142
|
+
}
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/use-form.ts
|
|
145
|
+
function cloneValue(value) {
|
|
146
|
+
return structuredClone(toRaw(value));
|
|
147
|
+
}
|
|
78
148
|
function toMessages(result) {
|
|
79
149
|
if (result === void 0 || result === null || result === "") return [];
|
|
80
150
|
return typeof result === "string" ? [result] : result.filter((message) => message !== "");
|
|
@@ -84,7 +154,6 @@ function unknownMessages(result) {
|
|
|
84
154
|
if (!Array.isArray(result)) return [];
|
|
85
155
|
return result.filter((message) => typeof message === "string" && message !== "");
|
|
86
156
|
}
|
|
87
|
-
/** Rewrite `${arrayPath}.${index}…` keys through an index mapping (undefined drops). */
|
|
88
157
|
function remapKeys(entries, arrayPath, mapIndex) {
|
|
89
158
|
const prefix = `${arrayPath}.`;
|
|
90
159
|
const result = [];
|
|
@@ -106,43 +175,6 @@ function remapKeys(entries, arrayPath, mapIndex) {
|
|
|
106
175
|
}
|
|
107
176
|
return result;
|
|
108
177
|
}
|
|
109
|
-
/**
|
|
110
|
-
* Fully typed form state with validation, field arrays, and submission.
|
|
111
|
-
*
|
|
112
|
-
* The value type is inferred from `initialValues`; every path is a
|
|
113
|
-
* template-literal key, so `field("address.city")` returns controls whose
|
|
114
|
-
* `value` is typed as the city's type and misspelled paths do not compile.
|
|
115
|
-
* Validation combines a Standard Schema v1 validator (Zod, Valibot, ArkType,
|
|
116
|
-
* … via the `~standard` protocol), a whole-form validator, and per-field
|
|
117
|
-
* validators; all may be asynchronous. A newer validation aborts older ones
|
|
118
|
-
* through their `signal` and their results are discarded, so a slow check
|
|
119
|
-
* can never overwrite fresher errors.
|
|
120
|
-
*
|
|
121
|
-
* This composable owns the *values*. `@vizejs/ui/form` owns accessible
|
|
122
|
-
* error presentation (error summaries, focus management, native constraint
|
|
123
|
-
* validation); feed {@link FormControls.errors} into it to render them.
|
|
124
|
-
*
|
|
125
|
-
* Server rendering: pure state, no host access; validation never runs
|
|
126
|
-
* automatically during setup, so server and client render the same markup.
|
|
127
|
-
* Pending validations are aborted when the owning scope stops.
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* const form = useForm({
|
|
132
|
-
* initialValues: { name: "", address: { city: "" }, tags: [] as string[] },
|
|
133
|
-
* validators: { name: (name) => (name ? undefined : "Required") },
|
|
134
|
-
* onSubmit: (values) => api.save(values),
|
|
135
|
-
* });
|
|
136
|
-
* const city = form.field("address.city"); // city.value: Ref<string>
|
|
137
|
-
* const tags = form.fieldArray("tags");
|
|
138
|
-
* tags.append("vue");
|
|
139
|
-
* ```
|
|
140
|
-
*
|
|
141
|
-
* @typeParam Values Form values, inferred from `initialValues`.
|
|
142
|
-
* @typeParam Output Submitted value, inferred from `schema` (defaults to `Values`).
|
|
143
|
-
* @param options Initial values, validators, triggers, and submit handler.
|
|
144
|
-
* @returns Form state and actions.
|
|
145
|
-
*/
|
|
146
178
|
function useForm(options) {
|
|
147
179
|
const createInitial = () => typeof options.initialValues === "function" ? options.initialValues() : cloneValue(options.initialValues);
|
|
148
180
|
let initial = createInitial();
|
|
@@ -152,6 +184,8 @@ function useForm(options) {
|
|
|
152
184
|
const pending = ref(/* @__PURE__ */ new Map());
|
|
153
185
|
const submitting = ref(false);
|
|
154
186
|
const submitCount = ref(0);
|
|
187
|
+
const isSubmitSuccessful = ref(false);
|
|
188
|
+
const disabled = computed(() => toValue(options.disabled) === true);
|
|
155
189
|
const arrayKeys = /* @__PURE__ */ new Map();
|
|
156
190
|
let nextKey = 0;
|
|
157
191
|
let formController;
|
|
@@ -182,13 +216,14 @@ function useForm(options) {
|
|
|
182
216
|
errors.value = next;
|
|
183
217
|
};
|
|
184
218
|
const readFieldValidator = (path) => validators.find(([candidate]) => candidate === path)?.[1];
|
|
185
|
-
/** Collect every error source; resolves to errors keyed by path. */
|
|
186
219
|
const collectErrors = async (signal) => {
|
|
187
220
|
const snapshot = values.value;
|
|
188
|
-
const
|
|
189
|
-
const
|
|
221
|
+
const schemaMessages = Object.create(null);
|
|
222
|
+
const formMessages = Object.create(null);
|
|
223
|
+
const fieldMessages = Object.create(null);
|
|
224
|
+
const add = (bucket, path, messages) => {
|
|
190
225
|
if (messages.length === 0) return;
|
|
191
|
-
(
|
|
226
|
+
(bucket[path] ??= []).push(...messages);
|
|
192
227
|
};
|
|
193
228
|
let output = snapshot;
|
|
194
229
|
const tasks = [];
|
|
@@ -196,18 +231,29 @@ function useForm(options) {
|
|
|
196
231
|
const schema = options.schema;
|
|
197
232
|
tasks.push(validateStandardSchema(schema, cloneValue(snapshot)).then((result) => {
|
|
198
233
|
if (result.status === "valid") output = result.value;
|
|
199
|
-
else for (const issue of result.issues) add(issue.path, [issue.message]);
|
|
234
|
+
else for (const issue of result.issues) add(schemaMessages, issue.path, [issue.message]);
|
|
200
235
|
}));
|
|
201
236
|
}
|
|
202
237
|
if (options.validate) {
|
|
203
238
|
const validate = options.validate;
|
|
204
239
|
tasks.push(Promise.resolve(validate(snapshot, { signal })).then((result) => {
|
|
205
240
|
if (!result) return;
|
|
206
|
-
for (const [path, messages] of Object.entries(result)) if (messages !== void 0) add(path, toMessages(messages));
|
|
241
|
+
for (const [path, messages] of Object.entries(result)) if (messages !== void 0) add(formMessages, path, toMessages(messages));
|
|
207
242
|
}));
|
|
208
243
|
}
|
|
209
|
-
for (const [path, validator] of validators) tasks.push(runValidator(validator, path, snapshot, signal).then((messages) => add(path, messages)));
|
|
244
|
+
for (const [path, validator] of validators) tasks.push(runValidator(validator, path, snapshot, signal).then((messages) => add(fieldMessages, path, messages)));
|
|
210
245
|
await Promise.all(tasks);
|
|
246
|
+
const collected = Object.create(null);
|
|
247
|
+
const firstOnly = options.criteriaMode === "first";
|
|
248
|
+
for (const bucket of [
|
|
249
|
+
schemaMessages,
|
|
250
|
+
formMessages,
|
|
251
|
+
fieldMessages
|
|
252
|
+
]) for (const [path, messages] of Object.entries(bucket)) {
|
|
253
|
+
const target = collected[path] ??= [];
|
|
254
|
+
if (firstOnly && target.length > 0) continue;
|
|
255
|
+
target.push(...firstOnly ? messages.slice(0, 1) : messages);
|
|
256
|
+
}
|
|
211
257
|
return {
|
|
212
258
|
errors: { ...collected },
|
|
213
259
|
output
|
|
@@ -260,11 +306,12 @@ function useForm(options) {
|
|
|
260
306
|
}) : Promise.resolve([])
|
|
261
307
|
]);
|
|
262
308
|
if (controller.signal.aborted) return false;
|
|
263
|
-
const
|
|
309
|
+
const combined = [
|
|
264
310
|
...fromSchema,
|
|
265
311
|
...fromForm,
|
|
266
312
|
...own
|
|
267
313
|
];
|
|
314
|
+
const messages = options.criteriaMode === "first" ? combined.slice(0, 1) : combined;
|
|
268
315
|
setErrorsFor(path, messages);
|
|
269
316
|
return messages.length === 0;
|
|
270
317
|
} finally {
|
|
@@ -279,6 +326,7 @@ function useForm(options) {
|
|
|
279
326
|
touched.value = next;
|
|
280
327
|
};
|
|
281
328
|
const setValueAt = (path, value, setOptions = {}) => {
|
|
329
|
+
if (disabled.value) return;
|
|
282
330
|
writePath(values.value, path, value);
|
|
283
331
|
if (setOptions.touch) touch(path);
|
|
284
332
|
if (setOptions.validate ?? trigger() === "change") validateField(path);
|
|
@@ -320,7 +368,7 @@ function useForm(options) {
|
|
|
320
368
|
}),
|
|
321
369
|
errors: computed(() => errors.value[key] ?? []),
|
|
322
370
|
error: computed(() => errors.value[key]?.[0]),
|
|
323
|
-
dirty: computed(() => !
|
|
371
|
+
dirty: computed(() => !valuesEqual(readPath(values.value, key), readPath(initial, key))),
|
|
324
372
|
touched: computed(() => touched.value.has(key)),
|
|
325
373
|
validating: computed(() => pending.value.has(key) || pending.value.has("")),
|
|
326
374
|
onBlur: () => {
|
|
@@ -329,6 +377,7 @@ function useForm(options) {
|
|
|
329
377
|
},
|
|
330
378
|
validate: () => validateField(key),
|
|
331
379
|
reset: () => {
|
|
380
|
+
if (disabled.value) return;
|
|
332
381
|
writePath(values.value, key, cloneValue(readPath(initial, key)));
|
|
333
382
|
setErrorsFor(key, []);
|
|
334
383
|
const next = new Set(touched.value);
|
|
@@ -344,6 +393,7 @@ function useForm(options) {
|
|
|
344
393
|
if (trigger() === "change") validateField(key);
|
|
345
394
|
};
|
|
346
395
|
const insertAt = (index, items) => {
|
|
396
|
+
if (disabled.value) return;
|
|
347
397
|
const array = readArray(key);
|
|
348
398
|
const keys = syncKeys(key, array.length);
|
|
349
399
|
const at = Math.max(0, Math.min(index, array.length));
|
|
@@ -369,6 +419,7 @@ function useForm(options) {
|
|
|
369
419
|
prepend: (...items) => insertAt(0, items),
|
|
370
420
|
insert: (index, item) => insertAt(index, [item]),
|
|
371
421
|
remove: (index) => {
|
|
422
|
+
if (disabled.value) return;
|
|
372
423
|
const array = readArray(key);
|
|
373
424
|
if (index < 0 || index >= array.length) return;
|
|
374
425
|
const keys = syncKeys(key, array.length);
|
|
@@ -378,6 +429,7 @@ function useForm(options) {
|
|
|
378
429
|
afterChange();
|
|
379
430
|
},
|
|
380
431
|
move: (from, to) => {
|
|
432
|
+
if (disabled.value) return;
|
|
381
433
|
const array = readArray(key);
|
|
382
434
|
if (from < 0 || from >= array.length || to < 0 || to >= array.length || from === to) return;
|
|
383
435
|
const keys = syncKeys(key, array.length);
|
|
@@ -394,6 +446,7 @@ function useForm(options) {
|
|
|
394
446
|
afterChange();
|
|
395
447
|
},
|
|
396
448
|
swap: (first, second) => {
|
|
449
|
+
if (disabled.value) return;
|
|
397
450
|
const array = readArray(key);
|
|
398
451
|
if (first < 0 || second < 0 || first >= array.length || second >= array.length) return;
|
|
399
452
|
const keys = syncKeys(key, array.length);
|
|
@@ -407,6 +460,7 @@ function useForm(options) {
|
|
|
407
460
|
afterChange();
|
|
408
461
|
},
|
|
409
462
|
replace: (items) => {
|
|
463
|
+
if (disabled.value) return;
|
|
410
464
|
remapState(key, () => void 0);
|
|
411
465
|
writePath(values.value, key, [...items]);
|
|
412
466
|
arrayKeys.delete(key);
|
|
@@ -419,15 +473,25 @@ function useForm(options) {
|
|
|
419
473
|
else setErrorsFor(path, []);
|
|
420
474
|
};
|
|
421
475
|
const submitWith = async (handler) => {
|
|
476
|
+
if (disabled.value) return {
|
|
477
|
+
status: "invalid",
|
|
478
|
+
errors: {}
|
|
479
|
+
};
|
|
422
480
|
submitCount.value += 1;
|
|
423
481
|
submitting.value = true;
|
|
424
482
|
try {
|
|
425
483
|
const result = await validate();
|
|
426
484
|
const signal = formController?.signal ?? new AbortController().signal;
|
|
427
|
-
if (result.status === "valid") {
|
|
485
|
+
if (result.status === "valid") try {
|
|
428
486
|
const onSubmit = handler ?? options.onSubmit;
|
|
429
487
|
if (onSubmit) await onSubmit(result.value, { signal });
|
|
430
|
-
|
|
488
|
+
isSubmitSuccessful.value = true;
|
|
489
|
+
} catch (error) {
|
|
490
|
+
isSubmitSuccessful.value = false;
|
|
491
|
+
throw error;
|
|
492
|
+
}
|
|
493
|
+
else if (result.status === "invalid") {
|
|
494
|
+
isSubmitSuccessful.value = false;
|
|
431
495
|
const next = new Set(touched.value);
|
|
432
496
|
for (const path of Object.keys(result.errors)) if (path !== "") next.add(path);
|
|
433
497
|
touched.value = next;
|
|
@@ -437,17 +501,20 @@ function useForm(options) {
|
|
|
437
501
|
submitting.value = false;
|
|
438
502
|
}
|
|
439
503
|
};
|
|
440
|
-
const reset = (next) => {
|
|
504
|
+
const reset = (next, keep = {}) => {
|
|
441
505
|
formController?.abort();
|
|
442
506
|
for (const controller of fieldControllers.values()) controller.abort();
|
|
443
507
|
fieldControllers.clear();
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
508
|
+
const previousInitial = initial;
|
|
509
|
+
const base = next === void 0 ? createInitial() : cloneValue(next);
|
|
510
|
+
initial = cloneValue(base);
|
|
511
|
+
values.value = keep.keepDirtyValues ? cloneValue(valuesKeepingDirty(base, cloneValue(values.value), previousInitial)) : cloneValue(initial);
|
|
512
|
+
if (!keep.keepErrors) errors.value = {};
|
|
513
|
+
if (!keep.keepTouched) touched.value = /* @__PURE__ */ new Set();
|
|
448
514
|
pending.value.clear();
|
|
449
|
-
submitCount.value = 0;
|
|
450
|
-
|
|
515
|
+
if (!keep.keepSubmitCount) submitCount.value = 0;
|
|
516
|
+
if (!keep.keepIsSubmitSuccessful) isSubmitSuccessful.value = false;
|
|
517
|
+
if (!keep.keepDirtyValues) arrayKeys.clear();
|
|
451
518
|
bumpKeys();
|
|
452
519
|
};
|
|
453
520
|
tryOnScopeDispose(() => {
|
|
@@ -458,15 +525,20 @@ function useForm(options) {
|
|
|
458
525
|
values,
|
|
459
526
|
errors,
|
|
460
527
|
valid: computed(() => Object.keys(errors.value).length === 0),
|
|
461
|
-
dirty: computed(() => !
|
|
528
|
+
dirty: computed(() => !valuesEqual(values.value, initial)),
|
|
529
|
+
dirtyFields: computed(() => dirtyFieldsOf(values.value, initial)),
|
|
462
530
|
validating: computed(() => pending.value.size > 0),
|
|
463
531
|
submitting,
|
|
464
532
|
submitCount,
|
|
533
|
+
isSubmitSuccessful,
|
|
534
|
+
disabled,
|
|
465
535
|
field,
|
|
466
536
|
fieldArray,
|
|
467
537
|
getValue: (path) => readPath(values.value, path),
|
|
538
|
+
watch: (path) => computed(() => readPath(values.value, path)),
|
|
468
539
|
setValue: (path, value, setOptions) => setValueAt(path, value, setOptions),
|
|
469
540
|
setValues: (next, setOptions = {}) => {
|
|
541
|
+
if (disabled.value) return;
|
|
470
542
|
if (setOptions.merge) values.value = {
|
|
471
543
|
...values.value,
|
|
472
544
|
...cloneValue(next)
|
|
@@ -484,6 +556,7 @@ function useForm(options) {
|
|
|
484
556
|
for (const [path, messages] of Object.entries(next)) if (messages !== void 0 && messages.length > 0) normalized[path] = messages;
|
|
485
557
|
errors.value = normalized;
|
|
486
558
|
},
|
|
559
|
+
setError: (path, message) => setErrorsFor(path, toMessages(message)),
|
|
487
560
|
clearErrors,
|
|
488
561
|
validate,
|
|
489
562
|
submit: submitWith,
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { computed, toValue } from "vue";
|
|
2
2
|
//#region src/use-form-field-props.ts
|
|
3
|
-
/** Derive the default control id for a path. */
|
|
4
3
|
function formFieldId(path, idPrefix = "field") {
|
|
5
4
|
const segment = path.replace(/[^A-Za-z0-9_-]+/g, "-");
|
|
6
5
|
return segment.length === 0 ? idPrefix : `${idPrefix}-${segment}`;
|
|
@@ -9,37 +8,6 @@ function readFlag(source, fallback) {
|
|
|
9
8
|
const value = source === void 0 ? void 0 : toValue(source);
|
|
10
9
|
return typeof value === "boolean" ? value : fallback;
|
|
11
10
|
}
|
|
12
|
-
/**
|
|
13
|
-
* Bind one `useForm` field to accessible field markup and any `v-model` control.
|
|
14
|
-
*
|
|
15
|
-
* Returns ids, ARIA relations (`fieldProps`), label/description/error props,
|
|
16
|
-
* and `modelProps` whose shapes match `@vizejs/ui`'s Field contracts
|
|
17
|
-
* structurally, so the bridge lives here without a runtime dependency on the
|
|
18
|
-
* UI package. Ids are derived from the path, so server and client agree
|
|
19
|
-
* without any instance-local id sequence.
|
|
20
|
-
*
|
|
21
|
-
* Server rendering: pure derived state, no host access. Cleanup: none needed
|
|
22
|
-
* beyond the owning form.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```vue
|
|
26
|
-
* <script setup lang="ts">
|
|
27
|
-
* const form = useForm({ initialValues: { address: { city: "" } } });
|
|
28
|
-
* const city = useFormFieldProps(form, "address.city");
|
|
29
|
-
* <\/script>
|
|
30
|
-
* <template>
|
|
31
|
-
* <label v-bind="city.labelProps.value">City</label>
|
|
32
|
-
* <TextInput v-bind="city.controlProps.value" />
|
|
33
|
-
* <p v-if="city.invalid.value" v-bind="city.errorMessageProps.value">{{ city.errorMessage.value }}</p>
|
|
34
|
-
* </template>
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* @param form Controls returned by `useForm`.
|
|
38
|
-
* @param path Typed dotted path.
|
|
39
|
-
* @param options Id, relation, and error-visibility options.
|
|
40
|
-
* @default options {}
|
|
41
|
-
* @returns Typed bindings for the field.
|
|
42
|
-
*/
|
|
43
11
|
function useFormFieldProps(form, path, options = {}) {
|
|
44
12
|
const field = form.field(path);
|
|
45
13
|
const showErrors = options.showErrors ?? "touched";
|
|
@@ -93,13 +61,6 @@ function useFormFieldProps(form, path, options = {}) {
|
|
|
93
61
|
}))
|
|
94
62
|
};
|
|
95
63
|
}
|
|
96
|
-
/**
|
|
97
|
-
* Convert form errors into ui `FormFieldError` entries (`{ name, message, path }`),
|
|
98
|
-
* one per message, so `@vizejs/ui` `Field` components match them by `name`.
|
|
99
|
-
*
|
|
100
|
-
* @param errors `form.errors.value`.
|
|
101
|
-
* @returns Entries in record order; the form-level `""` path has an empty `path`.
|
|
102
|
-
*/
|
|
103
64
|
function toFormFieldErrors(errors) {
|
|
104
65
|
return Object.entries(errors).flatMap(([name, messages]) => {
|
|
105
66
|
const path = name.length === 0 ? [] : name.split(".").map((segment) => /^\d+$/.test(segment) ? Number(segment) : segment);
|
|
@@ -110,24 +71,6 @@ function toFormFieldErrors(errors) {
|
|
|
110
71
|
}));
|
|
111
72
|
});
|
|
112
73
|
}
|
|
113
|
-
/**
|
|
114
|
-
* Derive `@vizejs/ui` `ErrorSummary` fields from a `useForm` instance.
|
|
115
|
-
*
|
|
116
|
-
* Each invalid path becomes `{ id, message, label? }` pointing at the control
|
|
117
|
-
* id that {@link useFormFieldProps} assigns, in `order` then error order.
|
|
118
|
-
*
|
|
119
|
-
* Server rendering: pure derived state. Cleanup: none.
|
|
120
|
-
*
|
|
121
|
-
* @example
|
|
122
|
-
* ```vue
|
|
123
|
-
* <ErrorSummary :fields="summary" heading="Fix these fields" />
|
|
124
|
-
* ```
|
|
125
|
-
*
|
|
126
|
-
* @param form Controls returned by `useForm`.
|
|
127
|
-
* @param options Id, label, ordering, and visibility options.
|
|
128
|
-
* @default options {}
|
|
129
|
-
* @returns Summary fields, empty until errors should be shown.
|
|
130
|
-
*/
|
|
131
74
|
function useFormErrorSummaryFields(form, options = {}) {
|
|
132
75
|
const order = options.order ?? [];
|
|
133
76
|
const ids = options.ids ?? {};
|