what-core 0.14.0 → 0.15.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/LICENSE +21 -0
- package/README.md +64 -38
- package/a11y.d.ts +128 -0
- package/data.d.ts +294 -0
- package/dist/a11y.js +510 -0
- package/dist/a11y.min.js +413 -0
- package/dist/actions.js +158 -0
- package/dist/actions.min.js +112 -0
- package/dist/animation.js +481 -0
- package/dist/animation.min.js +295 -0
- package/dist/async.js +180 -0
- package/dist/async.min.js +134 -0
- package/dist/components.js +577 -0
- package/dist/components.min.js +413 -0
- package/dist/data.js +1212 -0
- package/dist/data.min.js +918 -0
- package/dist/diagnostics.js +187 -0
- package/dist/diagnostics.min.js +134 -0
- package/dist/dom.js +796 -0
- package/dist/dom.min.js +603 -0
- package/dist/entries/a11y.js +43 -0
- package/dist/entries/a11y.min.js +43 -0
- package/dist/entries/data.js +29 -0
- package/dist/entries/data.min.js +29 -0
- package/dist/entries/dom.js +45 -0
- package/dist/entries/dom.min.js +45 -0
- package/dist/entries/form.js +31 -0
- package/dist/entries/form.min.js +31 -0
- package/dist/entries/internal.js +123 -0
- package/dist/entries/internal.min.js +123 -0
- package/dist/entries/motion.js +23 -0
- package/dist/entries/motion.min.js +23 -0
- package/dist/entries/ui.js +25 -0
- package/dist/entries/ui.min.js +25 -0
- package/dist/env.js +5 -0
- package/dist/env.min.js +5 -0
- package/dist/errors.js +1518 -0
- package/dist/errors.min.js +548 -0
- package/dist/form.js +936 -0
- package/dist/form.min.js +703 -0
- package/dist/guardrails.js +84 -0
- package/dist/guardrails.min.js +55 -0
- package/dist/h.js +232 -0
- package/dist/h.min.js +171 -0
- package/dist/head.js +241 -0
- package/dist/head.min.js +207 -0
- package/dist/helpers.js +126 -0
- package/dist/helpers.min.js +98 -0
- package/dist/hooks.js +255 -0
- package/dist/hooks.min.js +153 -0
- package/dist/hot.js +215 -0
- package/dist/hot.min.js +24 -0
- package/dist/hydration-data.js +495 -0
- package/dist/hydration-data.min.js +371 -0
- package/dist/import-changes.js +273 -0
- package/dist/import-changes.min.js +272 -0
- package/dist/index.js +95 -0
- package/dist/index.min.js +95 -227
- package/dist/jsx-dev-runtime.js +10 -0
- package/dist/jsx-dev-runtime.min.js +10 -1
- package/dist/jsx-runtime.js +27 -0
- package/dist/jsx-runtime.min.js +15 -1
- package/dist/live.js +54 -0
- package/dist/live.min.js +49 -0
- package/dist/markup-rules.js +250 -0
- package/dist/markup-rules.min.js +213 -0
- package/dist/reactive.js +3334 -0
- package/dist/reactive.min.js +2033 -0
- package/dist/render.js +4746 -0
- package/dist/render.min.js +2797 -1
- package/dist/scheduler.js +225 -0
- package/dist/scheduler.min.js +162 -0
- package/dist/selector.js +11 -0
- package/dist/selector.min.js +10 -0
- package/dist/server-context.js +148 -0
- package/dist/server-context.min.js +121 -0
- package/dist/skeleton.js +342 -0
- package/dist/skeleton.min.js +310 -0
- package/dist/store.js +72 -0
- package/dist/store.min.js +50 -0
- package/dist/testing.js +720 -0
- package/dist/testing.min.js +458 -1
- package/dist/vnode-adoption.js +19 -0
- package/dist/vnode-adoption.min.js +19 -0
- package/dist/vnode-renderer.js +61 -0
- package/dist/vnode-renderer.min.js +47 -0
- package/dom.d.ts +44 -0
- package/form.d.ts +218 -0
- package/hot.d.ts +35 -0
- package/index.d.ts +421 -736
- package/internal.d.ts +284 -0
- package/jsx-runtime.d.ts +160 -137
- package/markup-rules.d.ts +83 -0
- package/motion.d.ts +156 -0
- package/package.json +117 -3
- package/render.d.ts +278 -29
- package/src/a11y.js +64 -59
- package/src/actions.js +226 -0
- package/src/animation.js +105 -57
- package/src/async.js +259 -0
- package/src/components.js +495 -144
- package/src/data.js +1410 -1159
- package/src/diagnostics.js +263 -0
- package/src/dom.js +1159 -872
- package/src/entries/a11y.js +24 -0
- package/src/entries/data.js +17 -0
- package/src/entries/dom.js +29 -0
- package/src/entries/form.js +16 -0
- package/src/entries/internal.js +63 -0
- package/src/entries/motion.js +13 -0
- package/src/entries/ui.js +13 -0
- package/src/env-development.js +12 -0
- package/src/env.js +33 -0
- package/src/errors.js +1144 -168
- package/src/form.js +723 -225
- package/src/guardrails.js +38 -126
- package/src/h.js +227 -152
- package/src/head.js +189 -19
- package/src/helpers.js +47 -75
- package/src/hooks.js +233 -248
- package/src/hot.js +316 -0
- package/src/hydration-data.js +859 -16
- package/src/import-changes.js +156 -0
- package/src/index.js +38 -188
- package/src/jsx-dev-runtime.js +7 -14
- package/src/jsx-runtime.js +25 -9
- package/src/live.js +80 -0
- package/src/markup-rules.js +387 -0
- package/src/reactive.js +4400 -862
- package/src/render.js +6502 -2570
- package/src/scheduler.js +56 -21
- package/src/selector.js +31 -0
- package/src/server-context.js +252 -22
- package/src/skeleton.js +22 -12
- package/src/store.js +17 -31
- package/src/testing.js +389 -63
- package/src/vnode-adoption.js +48 -0
- package/src/vnode-renderer.js +114 -0
- package/testing.d.ts +81 -1
- package/ui.d.ts +20 -0
- package/dist/chunk-6YOUZ6T4.min.js +0 -1
- package/dist/chunk-FN6HXME3.min.js +0 -11
- package/dist/chunk-VKZY7JPN.min.js +0 -1
- package/src/agent-context.js +0 -127
- package/src/warnings.js +0 -193
package/src/form.js
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
// What Framework - Form Utilities
|
|
2
2
|
// Controlled inputs, validation, and form state management
|
|
3
3
|
|
|
4
|
-
import { signal, computed, batch, effect, __DEV__ } from './reactive.js';
|
|
5
|
-
import { getCurrentComponent, _isEventProp } from './dom.js';
|
|
4
|
+
import { signal, computed, batch, effect, onCleanup, untrack, __DEV__ } from './reactive.js';
|
|
5
|
+
import { getCurrentComponent, _isEventProp, _markProps } from './dom.js';
|
|
6
6
|
import { h } from './h.js';
|
|
7
|
+
import { applyRef } from './render.js';
|
|
8
|
+
import { report } from './diagnostics.js';
|
|
7
9
|
|
|
8
10
|
// --- Registration plumbing ---
|
|
9
11
|
|
|
10
|
-
// A ref can be a callback or a { current } box; both spellings are public API,
|
|
11
|
-
// so anything that wants to install its OWN ref has to forward the caller's.
|
|
12
|
-
function _applyRef(ref, el) {
|
|
13
|
-
if (typeof ref === 'function') ref(el);
|
|
14
|
-
else if (ref && typeof ref === 'object') ref.current = el;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
12
|
// Run BOTH handlers when a registration and its caller want the same DOM event.
|
|
18
13
|
// Registration first, caller second, so the caller's handler observes form state
|
|
19
14
|
// that is already up to date (React Hook Form orders its `register(name, {
|
|
@@ -50,6 +45,13 @@ function _mergeRegistration(props, registered) {
|
|
|
50
45
|
}
|
|
51
46
|
|
|
52
47
|
for (const key in registered) {
|
|
48
|
+
// A registration's `value` and `checked` are getters over the field; read
|
|
49
|
+
// here they would be its value at render, so they cross as functions,
|
|
50
|
+
// which the renderer binds live (a later setValue or reset shows).
|
|
51
|
+
if (Object.getOwnPropertyDescriptor(registered, key)?.get) {
|
|
52
|
+
merged[key] = () => registered[key];
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
53
55
|
const value = registered[key];
|
|
54
56
|
if (!_isEventProp(key)) {
|
|
55
57
|
merged[key] = value;
|
|
@@ -68,57 +70,174 @@ function _mergeRegistration(props, registered) {
|
|
|
68
70
|
return merged;
|
|
69
71
|
}
|
|
70
72
|
|
|
73
|
+
// --- Paths ---
|
|
74
|
+
// A field's name is a path, as in React Hook Form: `vehicles.0.vin` is
|
|
75
|
+
// `values.vehicles[0].vin`. A name without a dot is a key of its own.
|
|
76
|
+
|
|
77
|
+
const isIndex = (segment) => /^\d+$/.test(segment);
|
|
78
|
+
|
|
79
|
+
function isContainer(value) {
|
|
80
|
+
return Array.isArray(value) || (value !== null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Plain objects and arrays copied all the way down; anything else (a File,
|
|
84
|
+
// a Date, a FileList) kept as it is.
|
|
85
|
+
function clone(value) {
|
|
86
|
+
if (Array.isArray(value)) return value.map(clone);
|
|
87
|
+
if (!isContainer(value)) return value;
|
|
88
|
+
const out = {};
|
|
89
|
+
for (const key in value) out[key] = clone(value[key]);
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// A map with no prototype, so a key named `constructor` or `__proto__` is
|
|
94
|
+
// a key like any other.
|
|
95
|
+
const map = () => Object.create(null);
|
|
96
|
+
|
|
97
|
+
// obj[key], except a member obj inherits from Object.prototype, which is no
|
|
98
|
+
// entry of its own: a field named `toString` with no default has none.
|
|
99
|
+
const own = (obj, key) => (obj == null || (!Object.hasOwn(obj, key) && key in Object.prototype) ? undefined : obj[key]);
|
|
100
|
+
|
|
101
|
+
const getIn = (obj, path) => path.split('.').reduce(own, obj);
|
|
102
|
+
|
|
103
|
+
// Writes into an object getAllValues built, making an array where the next
|
|
104
|
+
// segment is an index and an object anywhere else. A path through
|
|
105
|
+
// `__proto__` writes nothing: it would write into Object.prototype, the
|
|
106
|
+
// prototype of every object (React Hook Form's set refuses it too).
|
|
107
|
+
function setIn(obj, path, value) {
|
|
108
|
+
const segments = path.split('.');
|
|
109
|
+
if (segments.includes('__proto__')) return;
|
|
110
|
+
const last = segments.length - 1;
|
|
111
|
+
let cur = obj;
|
|
112
|
+
for (let i = 0; i < last; i++) {
|
|
113
|
+
const segment = segments[i];
|
|
114
|
+
if (typeof (cur[segment] ?? 0) !== 'object') cur[segment] = isIndex(segments[i + 1]) ? [] : {};
|
|
115
|
+
cur = cur[segment];
|
|
116
|
+
}
|
|
117
|
+
cur[segments[last]] = value;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// A copy of `obj` without the path, copying only the plain objects and arrays
|
|
121
|
+
// on it: every other value keeps its identity, which dirtyFields compares a
|
|
122
|
+
// field's value with. An object the removal leaves empty goes too, as React
|
|
123
|
+
// Hook Form's unset does.
|
|
124
|
+
function unsetIn(obj, segments) {
|
|
125
|
+
const [segment, ...rest] = segments;
|
|
126
|
+
if (!isContainer(obj) || !Object.hasOwn(obj, segment)) return obj;
|
|
127
|
+
const out = Array.isArray(obj) ? [...obj] : { ...obj };
|
|
128
|
+
const inner = rest.length ? unsetIn(out[segment], rest) : undefined;
|
|
129
|
+
if (inner === undefined || (!Array.isArray(inner) && isContainer(inner) && !Object.keys(inner).length)) delete out[segment];
|
|
130
|
+
else out[segment] = inner;
|
|
131
|
+
return out;
|
|
132
|
+
}
|
|
133
|
+
|
|
71
134
|
// --- useForm Hook ---
|
|
72
135
|
// Complete form state management with validation
|
|
73
136
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
137
|
+
const KEEP_DEFAULT = { keepDefaultValue: true };
|
|
138
|
+
|
|
139
|
+
// One instance per call site of a component, made on its first run.
|
|
140
|
+
function hookStable(make) {
|
|
141
|
+
const ctx = getCurrentComponent();
|
|
142
|
+
if (!ctx) return make();
|
|
143
|
+
if (ctx.hooks === null) ctx.hooks = [];
|
|
144
|
+
const index = ctx.hookIndex++;
|
|
145
|
+
return ctx.hooks[index] ||= make();
|
|
146
|
+
}
|
|
84
147
|
|
|
85
|
-
|
|
86
|
-
return createFormController(options);
|
|
148
|
+
export function useForm(options = {}) {
|
|
149
|
+
return hookStable(() => createFormController(options));
|
|
87
150
|
}
|
|
88
151
|
|
|
89
152
|
function createFormController(options = {}) {
|
|
90
153
|
const {
|
|
91
|
-
defaultValues = {},
|
|
92
154
|
mode = 'onSubmit', // 'onSubmit' | 'onChange' | 'onBlur'
|
|
93
155
|
reValidateMode = 'onChange',
|
|
94
156
|
resolver,
|
|
157
|
+
shouldUnregister = false,
|
|
95
158
|
} = options;
|
|
96
|
-
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
159
|
+
// The values a field starts from and dirtyFields measures from: useForm's
|
|
160
|
+
// defaultValues, then the values a reset() was last given (React Hook
|
|
161
|
+
// Form's reset makes them the defaults).
|
|
162
|
+
let defaultValues = options.defaultValues || {};
|
|
163
|
+
|
|
164
|
+
// State is kept by key. A key is the field's name, except under a field
|
|
165
|
+
// array (useFieldArray), where an item's index is replaced by the item's
|
|
166
|
+
// own segment (`vehicles.<item>.vin`): its value, error and touched state
|
|
167
|
+
// stay with the item when it moves. `arrays` holds each field array's
|
|
168
|
+
// items. `ext`, the field array support, is installed by the first
|
|
169
|
+
// useFieldArray and replaces keyOf, nameOf and defaultAt with its own;
|
|
170
|
+
// until then a key is its name.
|
|
171
|
+
/** @type {Record<string, any>} */
|
|
172
|
+
const arrays = map();
|
|
173
|
+
/** @type {any} */
|
|
174
|
+
let ext;
|
|
175
|
+
let keyOf = (name) => name;
|
|
176
|
+
// The name a key has now, or null for a key of an item that was removed.
|
|
177
|
+
let nameOf = keyOf;
|
|
178
|
+
// The default at a key in the defaults, or in the item's values for a key
|
|
179
|
+
// under a field array item.
|
|
180
|
+
let defaultAt = getIn;
|
|
181
|
+
|
|
182
|
+
// Per-field signals for granular reactivity (avoids full-form re-renders
|
|
183
|
+
// on each keystroke), made by the first read or write of a field.
|
|
184
|
+
const fieldSignals = map();
|
|
185
|
+
// The fields written, which the values hold. A reader of a field nothing
|
|
186
|
+
// wrote yet (or that was unregistered) follows its signal, and the field
|
|
187
|
+
// joins the values when something writes it.
|
|
188
|
+
const known = map();
|
|
189
|
+
// Fields forgotten with their defaults kept: left out of the values until
|
|
190
|
+
// something writes them again.
|
|
191
|
+
const hidden = map();
|
|
192
|
+
const errorSignals = map();
|
|
193
|
+
const touchedSignals = map();
|
|
101
194
|
const errorsState = signal({});
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
195
|
+
// Counts the field and touched signals made and forgotten so far. A reader
|
|
196
|
+
// of the whole form (values, watch(), dirtyFields, touched) reads it, so
|
|
197
|
+
// one that ran before a field existed (a summary rendered above the
|
|
198
|
+
// fields) runs again when the field comes into being and subscribes to it
|
|
199
|
+
// then. Only what writes a field (register, setValue, a blur) makes one
|
|
200
|
+
// known; a read of a field nothing wrote yet sees its default either way.
|
|
201
|
+
const fieldCount = signal(0);
|
|
202
|
+
const counted = () => fieldCount.set((n) => n + 1);
|
|
203
|
+
|
|
204
|
+
const defaultOf = (key) => defaultAt(defaultValues, key) ?? '';
|
|
205
|
+
|
|
206
|
+
const signalOf = (key) => (fieldSignals[key] ||= signal(defaultOf(key)));
|
|
207
|
+
|
|
208
|
+
// A field's signal, the field written.
|
|
209
|
+
function getFieldSignal(key) {
|
|
210
|
+
const sig = signalOf(key);
|
|
211
|
+
if (!known[key]) {
|
|
212
|
+
known[key] = true;
|
|
213
|
+
delete hidden[key];
|
|
214
|
+
counted();
|
|
106
215
|
}
|
|
107
|
-
return
|
|
216
|
+
return sig;
|
|
108
217
|
}
|
|
109
218
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
219
|
+
const readKey = (key) => signalOf(key)();
|
|
220
|
+
|
|
221
|
+
// A name that holds other fields (a field array, an object of fields) is
|
|
222
|
+
// read from the whole values; a field's own name from its signal.
|
|
223
|
+
function readName(key) {
|
|
224
|
+
if (!known[key] && (arrays[key] || isContainer(defaultOf(key)) || Object.keys(known).some((k) => k.startsWith(key + '.')))) {
|
|
225
|
+
const name = nameOf(key);
|
|
226
|
+
return name == null ? '' : getIn(getAllValues(), name) ?? '';
|
|
113
227
|
}
|
|
114
|
-
return
|
|
228
|
+
return readKey(key);
|
|
115
229
|
}
|
|
116
230
|
|
|
117
|
-
function
|
|
118
|
-
|
|
119
|
-
|
|
231
|
+
function getErrorSignal(key) {
|
|
232
|
+
return errorSignals[key] ||= signal(null);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function getTouchedSignal(key) {
|
|
236
|
+
if (!touchedSignals[key]) {
|
|
237
|
+
touchedSignals[key] = signal(false);
|
|
238
|
+
counted();
|
|
120
239
|
}
|
|
121
|
-
return touchedSignals[
|
|
240
|
+
return touchedSignals[key];
|
|
122
241
|
}
|
|
123
242
|
|
|
124
243
|
// Aggregate signals for bulk operations
|
|
@@ -128,91 +247,186 @@ function createFormController(options = {}) {
|
|
|
128
247
|
const isValidating = signal(false);
|
|
129
248
|
const submitCount = signal(0);
|
|
130
249
|
|
|
131
|
-
//
|
|
132
|
-
// tracked
|
|
133
|
-
function getAllValues(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
250
|
+
// All current values as an object, nested by the fields' paths, read
|
|
251
|
+
// tracked: a snapshot reads them through untrack().
|
|
252
|
+
function getAllValues() {
|
|
253
|
+
fieldCount();
|
|
254
|
+
let result = clone(defaultValues);
|
|
255
|
+
ext?.v(result);
|
|
256
|
+
for (const key in hidden) {
|
|
257
|
+
const name = nameOf(key);
|
|
258
|
+
if (name != null) result = unsetIn(result, name.split('.'));
|
|
259
|
+
}
|
|
260
|
+
for (const key in known) {
|
|
261
|
+
const name = nameOf(key);
|
|
262
|
+
if (name != null) setIn(result, name, fieldSignals[key]());
|
|
137
263
|
}
|
|
138
264
|
return result;
|
|
139
265
|
}
|
|
140
266
|
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return
|
|
267
|
+
// All current errors as an object keyed by field name, read tracked.
|
|
268
|
+
function getAllErrors() {
|
|
269
|
+
const errors = errorsState();
|
|
270
|
+
return ext ? ext.e(errors) : errors;
|
|
145
271
|
}
|
|
146
272
|
|
|
147
|
-
function setFieldError(
|
|
273
|
+
function setFieldError(key, error) {
|
|
148
274
|
const nextError = error ?? null;
|
|
149
|
-
getErrorSignal(
|
|
275
|
+
getErrorSignal(key).set(nextError);
|
|
150
276
|
errorsState.set((prev) => {
|
|
151
|
-
const prevError = prev[
|
|
277
|
+
const prevError = prev[key];
|
|
152
278
|
if (prevError === nextError) return prev;
|
|
153
279
|
if (nextError == null) {
|
|
154
|
-
if (!Object.
|
|
280
|
+
if (!Object.hasOwn(prev, key)) return prev;
|
|
155
281
|
const next = { ...prev };
|
|
156
|
-
delete next[
|
|
282
|
+
delete next[key];
|
|
157
283
|
return next;
|
|
158
284
|
}
|
|
159
|
-
return { ...prev, [
|
|
285
|
+
return { ...prev, [key]: nextError };
|
|
160
286
|
});
|
|
161
287
|
}
|
|
162
288
|
|
|
163
|
-
function replaceAllErrors(nextErrors
|
|
289
|
+
function replaceAllErrors(nextErrors) {
|
|
164
290
|
const normalized = nextErrors || {};
|
|
165
291
|
batch(() => {
|
|
166
|
-
for (const
|
|
167
|
-
if (!Object.
|
|
168
|
-
|
|
292
|
+
for (const key in errorSignals) {
|
|
293
|
+
if (!Object.hasOwn(normalized, key)) {
|
|
294
|
+
errorSignals[key].set(null);
|
|
169
295
|
}
|
|
170
296
|
}
|
|
171
|
-
for (const
|
|
172
|
-
getErrorSignal(
|
|
297
|
+
for (const key in normalized) {
|
|
298
|
+
getErrorSignal(key).set(normalized[key] ?? null);
|
|
173
299
|
}
|
|
174
300
|
errorsState.set({ ...normalized });
|
|
175
301
|
});
|
|
176
302
|
}
|
|
177
303
|
|
|
178
304
|
// Computed states
|
|
179
|
-
const isValid = computed(() => Object.keys(getAllErrors(
|
|
305
|
+
const isValid = computed(() => Object.keys(getAllErrors()).length === 0);
|
|
180
306
|
|
|
307
|
+
// The fields whose value is not their default, by name.
|
|
181
308
|
const dirtyFields = computed(() => {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
309
|
+
fieldCount();
|
|
310
|
+
const out = {};
|
|
311
|
+
for (const key in known) {
|
|
312
|
+
if (fieldSignals[key]() === defaultOf(key)) continue;
|
|
313
|
+
const name = nameOf(key);
|
|
314
|
+
if (name != null) out[name] = true;
|
|
187
315
|
}
|
|
188
|
-
return
|
|
316
|
+
return out;
|
|
189
317
|
});
|
|
190
318
|
|
|
191
|
-
// Validation
|
|
319
|
+
// Validation. The resolver is handed every value, and, for a field's own
|
|
320
|
+
// validation, `{ names: [field] }` (React Hook Form's resolver options), so
|
|
321
|
+
// one that can (simpleResolver) checks only that field: a keystroke in a
|
|
322
|
+
// 300-field form runs one field's rules.
|
|
323
|
+
// Runs are numbered as they start, because an async resolver (a lookup on
|
|
324
|
+
// a server) answers them in any order: a field's answer is shown only when
|
|
325
|
+
// no later run of that field or of the whole form started before it
|
|
326
|
+
// landed, and a whole-form answer leaves alone a field validated since it
|
|
327
|
+
// started. A field unregistered or removed counts as validated when it
|
|
328
|
+
// goes, so no answer started before lands on it. A field's last number
|
|
329
|
+
// is kept: numbers only grow, so it never outranks a run that starts
|
|
330
|
+
// later. isValidating holds until the last run in flight lands.
|
|
331
|
+
let runCount = 0;
|
|
332
|
+
let formRun = 0;
|
|
333
|
+
const fieldRuns = map();
|
|
334
|
+
let inFlight = 0;
|
|
335
|
+
|
|
192
336
|
async function validate(fieldName) {
|
|
193
337
|
if (!resolver) return true;
|
|
194
338
|
|
|
195
|
-
|
|
339
|
+
const run = ++runCount;
|
|
340
|
+
const key = fieldName && keyOf(fieldName);
|
|
341
|
+
// Errors by name to errors by key, with the keys the names have now: the
|
|
342
|
+
// answer is about the items in the places they had when the run started.
|
|
343
|
+
const byKey = ext ? ext.s() : (errors) => ({ ...errors });
|
|
344
|
+
if (fieldName) fieldRuns[key] = run;
|
|
345
|
+
else formRun = run;
|
|
346
|
+
if (inFlight++ === 0) isValidating.set(true);
|
|
196
347
|
try {
|
|
197
|
-
const result = await resolver(getAllValues
|
|
348
|
+
const result = await resolver(untrack(getAllValues), undefined, fieldName ? { names: [fieldName] } : {});
|
|
198
349
|
const nextErrors = result?.errors || {};
|
|
199
350
|
|
|
200
351
|
if (fieldName) {
|
|
201
|
-
const nextError = nextErrors
|
|
202
|
-
setFieldError(
|
|
352
|
+
const nextError = own(nextErrors, fieldName) ?? null;
|
|
353
|
+
if (fieldRuns[key] === run && formRun < run) setFieldError(key, nextError);
|
|
203
354
|
return !nextError;
|
|
204
|
-
} else {
|
|
205
|
-
replaceAllErrors(nextErrors);
|
|
206
|
-
return Object.keys(nextErrors).length === 0;
|
|
207
355
|
}
|
|
356
|
+
if (formRun === run) {
|
|
357
|
+
const shown = byKey(nextErrors);
|
|
358
|
+
for (const at in fieldRuns) {
|
|
359
|
+
if (fieldRuns[at] < run) continue;
|
|
360
|
+
const latest = errorSignals[at]?.peek();
|
|
361
|
+
if (latest == null) delete shown[at];
|
|
362
|
+
else shown[at] = latest;
|
|
363
|
+
}
|
|
364
|
+
replaceAllErrors(shown);
|
|
365
|
+
}
|
|
366
|
+
return Object.keys(nextErrors).length === 0;
|
|
208
367
|
} finally {
|
|
209
|
-
isValidating.set(false);
|
|
368
|
+
if (--inFlight === 0) isValidating.set(false);
|
|
210
369
|
}
|
|
211
370
|
}
|
|
212
371
|
|
|
372
|
+
// Forget the field at `key` and every field under it: its value (and its
|
|
373
|
+
// default, unless options.keepDefaultValue or the form's shouldUnregister
|
|
374
|
+
// keeps it, so a field shown again starts from it), its error and its
|
|
375
|
+
// touched state, each unless options keeps it (React Hook Form's
|
|
376
|
+
// unregister options). A reader of the field (a mounted input, watch())
|
|
377
|
+
// keeps its signal, which shows the field's default now, and an edit in
|
|
378
|
+
// the input brings the field back. `gone` tells the keys of removed items
|
|
379
|
+
// (one of them `key`), which nothing can name again: their signals are
|
|
380
|
+
// dropped.
|
|
381
|
+
function forget(key, options = {}, gone) {
|
|
382
|
+
// Gives `fn` each key at or under `key` in `map`, or each key `gone`
|
|
383
|
+
// tells, and its entry; the entry goes from the map when the key is gone.
|
|
384
|
+
const each = (map, fn) => {
|
|
385
|
+
for (const k in map) {
|
|
386
|
+
if (gone ? !gone(k) : k !== key && !k.startsWith(key + '.')) continue;
|
|
387
|
+
fn(k, map[k]);
|
|
388
|
+
if (gone) delete map[k];
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
// Each field counts as validated now, so no answer in flight lands on it.
|
|
392
|
+
fieldRuns[key] = 0;
|
|
393
|
+
each(fieldRuns, (k) => (fieldRuns[k] = ++runCount));
|
|
394
|
+
batch(() => {
|
|
395
|
+
if (!options.keepValue) {
|
|
396
|
+
// A field array's name: its items go, each with its state.
|
|
397
|
+
if (arrays[key]) ext.a(key, []);
|
|
398
|
+
if (!gone && (options.keepDefaultValue || shouldUnregister)) {
|
|
399
|
+
if (!arrays[key]) hidden[key] = true;
|
|
400
|
+
} else if (!ext?.u(key)) defaultValues = unsetIn(defaultValues, key.split('.'));
|
|
401
|
+
each(hidden, () => {});
|
|
402
|
+
// Every field written has a signal, so one walk unmarks the fields
|
|
403
|
+
// and gives their signals the default.
|
|
404
|
+
each(fieldSignals, (k, sig) => {
|
|
405
|
+
delete known[k];
|
|
406
|
+
sig.set(defaultOf(k));
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
if (!options.keepError) each(errorSignals, (k) => setFieldError(k, null));
|
|
410
|
+
if (!options.keepTouched) each(touchedSignals, (k, sig) => sig.set(false));
|
|
411
|
+
counted();
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Forget fields by name (React Hook Form's unregister), or every field.
|
|
416
|
+
function unregister(names, options) {
|
|
417
|
+
const keys = names == null ? Object.keys(known) : [].concat(names).map(keyOf);
|
|
418
|
+
batch(() => { for (const key of keys) forget(key, options); });
|
|
419
|
+
}
|
|
420
|
+
|
|
213
421
|
// Register a field — only subscribes to THIS field's signal
|
|
214
422
|
function register(name, options = {}) {
|
|
215
|
-
|
|
423
|
+
// Read untracked: compiled JSX reads `{...form.register(name)}` again
|
|
424
|
+
// when what the call read changes, and a move of the items must not
|
|
425
|
+
// hand the row the field of the item now at its first index.
|
|
426
|
+
const key = untrack(() => keyOf(name));
|
|
427
|
+
getFieldSignal(key);
|
|
428
|
+
const read = () => readKey(key);
|
|
429
|
+
if (options.shouldUnregister ?? shouldUnregister) onCleanup(() => forget(key, KEEP_DEFAULT));
|
|
216
430
|
const isRadio = options.type === 'radio';
|
|
217
431
|
const isCheckbox = options.type === 'checkbox';
|
|
218
432
|
// A radio does NOT store its own checkedness: every radio in a group shares
|
|
@@ -247,16 +461,12 @@ function createFormController(options = {}) {
|
|
|
247
461
|
|
|
248
462
|
const handler = (e) => {
|
|
249
463
|
const target = e && e.target;
|
|
250
|
-
// NOT `|| {}`: a missing target
|
|
251
|
-
// undefined, so calling the handler without an event quietly
|
|
252
|
-
// field with undefined. Refuse instead — there is no value
|
|
464
|
+
// NOT `|| {}`: a missing target would read as `{}`, whose `.value` is
|
|
465
|
+
// undefined, so calling the handler without an event would quietly
|
|
466
|
+
// overwrite the field with undefined. Refuse instead — there is no value
|
|
467
|
+
// to apply.
|
|
253
468
|
if (!target) {
|
|
254
|
-
if (__DEV__) {
|
|
255
|
-
console.warn(
|
|
256
|
-
`[what] register("${name}") change handler was called without an event. ` +
|
|
257
|
-
'Pass the DOM event through (or call form.setValue() directly). Ignoring.'
|
|
258
|
-
);
|
|
259
|
-
}
|
|
469
|
+
if (__DEV__) report('ERR_FORM_HANDLER_WITHOUT_EVENT', { data: { name } });
|
|
260
470
|
return;
|
|
261
471
|
}
|
|
262
472
|
// options.type is the declared intent; target.type covers a bare
|
|
@@ -272,47 +482,58 @@ function createFormController(options = {}) {
|
|
|
272
482
|
// Nothing identifies this radio, so any write would be a guess.
|
|
273
483
|
if (__DEV__ && !warnedValuelessRadio) {
|
|
274
484
|
warnedValuelessRadio = true;
|
|
275
|
-
|
|
276
|
-
`[what] register("${name}", { type: 'radio' }) cannot tell which value this radio ` +
|
|
277
|
-
'contributes: pass { value } or give the <input> a value attribute. Field left unchanged.'
|
|
278
|
-
);
|
|
485
|
+
report('ERR_FORM_RADIO_WITHOUT_VALUE', { data: { name } });
|
|
279
486
|
}
|
|
280
487
|
return;
|
|
281
488
|
}
|
|
282
489
|
} else if (type === 'checkbox') {
|
|
283
490
|
value = target.checked;
|
|
491
|
+
} else if (type === 'file') {
|
|
492
|
+
// A file input's value is a fake path ("C:\fakepath\photo.png") that
|
|
493
|
+
// carries no file: the field holds its FileList, as React Hook Form
|
|
494
|
+
// registers one. Each choice is a new list, so it is always news.
|
|
495
|
+
value = target.files;
|
|
284
496
|
} else {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
setValue(name, value);
|
|
288
|
-
|
|
289
|
-
if (mode === 'onChange' || (isSubmitted.peek() && reValidateMode === 'onChange')) {
|
|
290
|
-
validate(name);
|
|
497
|
+
// A multiple select holds the values of its selected options.
|
|
498
|
+
value = type === 'select-multiple' ? Array.from(target.selectedOptions, (option) => option.value) : target.value;
|
|
291
499
|
}
|
|
500
|
+
// A `change` that brings what the form already holds is no news: a
|
|
501
|
+
// field registered as a value (below) takes its edit from `input`, and
|
|
502
|
+
// from `change` only what that did not bring (a listbox, a multiple
|
|
503
|
+
// select or one with a size, fires only `change` for a key typed).
|
|
504
|
+
if (e.type === 'change' && type !== 'file' && JSON.stringify(value) === JSON.stringify(getFieldSignal(key).peek())) return;
|
|
505
|
+
setKey(key, value);
|
|
506
|
+
|
|
507
|
+
validateOn('onChange', key);
|
|
292
508
|
};
|
|
293
509
|
|
|
294
510
|
const result = {
|
|
295
511
|
name,
|
|
296
512
|
onBlur: () => {
|
|
297
|
-
getTouchedSignal(
|
|
513
|
+
getTouchedSignal(key).set(true);
|
|
298
514
|
|
|
299
|
-
|
|
300
|
-
validate(name);
|
|
301
|
-
}
|
|
515
|
+
validateOn('onBlur', key);
|
|
302
516
|
},
|
|
303
517
|
onFocus: () => {},
|
|
304
518
|
ref: options.ref,
|
|
519
|
+
onchange: handler,
|
|
305
520
|
};
|
|
521
|
+
// The name of a field of a field array item follows the item's place (a
|
|
522
|
+
// radio's is written by its ref, below).
|
|
523
|
+
const moves = key !== name;
|
|
524
|
+
if (moves && !isRadio) Object.defineProperty(result, 'name', { get: () => nameOf(key) ?? name, enumerable: true });
|
|
306
525
|
|
|
307
526
|
if (isRadio) {
|
|
308
|
-
// Without a declared value this thunk must report
|
|
309
|
-
// the contribution can only come from the element, which a
|
|
310
|
-
// not have, and `!!field` is true for every radio in the
|
|
311
|
-
// picking any option rendered ALL of them checked.
|
|
312
|
-
//
|
|
527
|
+
// Without a declared value this thunk must report UNDEFINED, never
|
|
528
|
+
// `!!field`: the contribution can only come from the element, which a
|
|
529
|
+
// prop thunk does not have, and `!!field` is true for every radio in the
|
|
530
|
+
// group at once, so picking any option rendered ALL of them checked. Not
|
|
531
|
+
// false either: a false `checked` controls the radio unchecked, and the
|
|
532
|
+
// ref below is what checks it once it has the element. A server, which
|
|
533
|
+
// has no element, writes it unchecked.
|
|
313
534
|
const isChecked = hasRadioValue
|
|
314
|
-
? () =>
|
|
315
|
-
: () =>
|
|
535
|
+
? () => read() === options.value
|
|
536
|
+
: () => undefined;
|
|
316
537
|
|
|
317
538
|
if (hasRadioValue) result.value = options.value;
|
|
318
539
|
// A FUNCTION, not the getter the checkbox branch uses: every consumer
|
|
@@ -322,9 +543,10 @@ function createFormController(options = {}) {
|
|
|
322
543
|
// reactive-binding protocol — setProp() wraps it in an effect, and the SSR
|
|
323
544
|
// serializer calls it — so one thunk covers both renderers.
|
|
324
545
|
result.checked = isChecked;
|
|
325
|
-
result.onchange = handler;
|
|
326
546
|
result.ref = (el) => {
|
|
327
|
-
|
|
547
|
+
// The caller's ref, a callback or a { current } box, is applied as
|
|
548
|
+
// the renderer applies a ref.
|
|
549
|
+
applyRef(options.ref, el);
|
|
328
550
|
if (!el || typeof el !== 'object') return;
|
|
329
551
|
|
|
330
552
|
// The reactive `checked` prop above only stamps the checked CONTENT
|
|
@@ -335,40 +557,43 @@ function createFormController(options = {}) {
|
|
|
335
557
|
// screen. Writing the checked PROPERTY is what a controlled radio needs
|
|
336
558
|
// (React and Solid do the same) and it also runs the DOM's radio-group
|
|
337
559
|
// invariant that unchecks the siblings.
|
|
338
|
-
//
|
|
339
|
-
//
|
|
340
|
-
//
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
//
|
|
347
|
-
//
|
|
348
|
-
//
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
el._propEffects[key] = effect(() => {
|
|
560
|
+
// The effect belongs to the owner the ref runs under, like every
|
|
561
|
+
// binding of the element. radioValueOf(el) is re-read on every run
|
|
562
|
+
// rather than captured, so a registration without a declared value
|
|
563
|
+
// still binds correctly once the element's own value attribute is in
|
|
564
|
+
// place, and an unidentifiable radio is simply never checked instead of
|
|
565
|
+
// being checked alongside its siblings.
|
|
566
|
+
// A radio of a field array item takes its item's new place as its
|
|
567
|
+
// name here, then its checkedness: taking a name joins that radio
|
|
568
|
+
// group, which unchecks the group's checked radio (the radio of the
|
|
569
|
+
// item that held the place), and that radio's own run checks it
|
|
570
|
+
// again once it has its new name.
|
|
571
|
+
effect(() => {
|
|
572
|
+
if (moves) el.name = nameOf(key) ?? name;
|
|
352
573
|
const own = radioValueOf(el);
|
|
353
|
-
el.checked = own !== undefined &&
|
|
574
|
+
el.checked = own !== undefined && read() === own;
|
|
354
575
|
});
|
|
355
576
|
};
|
|
356
577
|
} else if (isCheckbox) {
|
|
357
578
|
// Checkbox: use checked prop + onchange event
|
|
358
579
|
Object.defineProperty(result, 'checked', {
|
|
359
|
-
get() { return !!
|
|
580
|
+
get() { return !!read(); },
|
|
360
581
|
enumerable: true,
|
|
361
582
|
});
|
|
362
|
-
result.onchange = handler;
|
|
363
583
|
} else {
|
|
364
|
-
// Text/select/textarea: use value prop +
|
|
584
|
+
// Text/select/textarea: use value prop + onchange, React's change event,
|
|
585
|
+
// which a text field fires as the user types and a select when its
|
|
586
|
+
// choice changes (render.js listen).
|
|
365
587
|
Object.defineProperty(result, 'value', {
|
|
366
|
-
get
|
|
588
|
+
get: read,
|
|
367
589
|
enumerable: true,
|
|
368
590
|
});
|
|
369
|
-
|
|
591
|
+
// 0.14 code wires `oninput: register('x').oninput` by hand: it keeps the
|
|
592
|
+
// handler, not enumerable, so a spread attaches only `onchange` and
|
|
593
|
+
// the field is not handled twice per keystroke.
|
|
594
|
+
Object.defineProperty(result, 'oninput', { value: handler });
|
|
370
595
|
result.ref = (el) => {
|
|
371
|
-
|
|
596
|
+
applyRef(options.ref, el);
|
|
372
597
|
// An UNTYPED registration spread onto a radio cannot be rescued, and it
|
|
373
598
|
// fails silently, so say so at mount. The `value` binding above resolves
|
|
374
599
|
// during the spread and overwrites the input's own value with the FIELD's
|
|
@@ -387,45 +612,45 @@ function createFormController(options = {}) {
|
|
|
387
612
|
// a radio's value.
|
|
388
613
|
if (__DEV__ && !warnedUntypedRadio && el && el.type === 'radio') {
|
|
389
614
|
warnedUntypedRadio = true;
|
|
390
|
-
|
|
391
|
-
`[what] register("${name}") was spread onto <input type="radio"> without a type. ` +
|
|
392
|
-
'Radios in a group share one field holding the SELECTED option, so the registration ' +
|
|
393
|
-
`must be told this radio's value: register("${name}", { type: 'radio', value: ... }), ` +
|
|
394
|
-
'or use <Radio name value register />. As written, the registration overwrote the ' +
|
|
395
|
-
"input's own value and the group cannot record a choice."
|
|
396
|
-
);
|
|
615
|
+
report('ERR_FORM_RADIO_UNTYPED', { data: { name } });
|
|
397
616
|
}
|
|
398
617
|
};
|
|
399
618
|
}
|
|
400
619
|
|
|
401
|
-
|
|
620
|
+
// Marked as a props object of What's (dom.js PROPS): a spread binds its
|
|
621
|
+
// `value` and `checked` getters live, so `<input {...register('x')} />`
|
|
622
|
+
// shows setValue and reset however the call is written.
|
|
623
|
+
return _markProps(result);
|
|
402
624
|
}
|
|
403
625
|
|
|
404
|
-
//
|
|
405
|
-
|
|
406
|
-
|
|
626
|
+
// A field's own validation, by the name its key has now, when the event
|
|
627
|
+
// is the one the form validates on (reValidateMode's once submitted).
|
|
628
|
+
function validateOn(event, key) {
|
|
629
|
+
const name = nameOf(key);
|
|
630
|
+
if (name != null && (mode === event || (isSubmitted.peek() && reValidateMode === event))) validate(name);
|
|
631
|
+
}
|
|
407
632
|
|
|
408
|
-
|
|
633
|
+
// A field array's name takes new items, as its replace() gives them.
|
|
634
|
+
function setKey(key, value, shouldDirty = true) {
|
|
635
|
+
if (arrays[key]) ext.a(key, value);
|
|
636
|
+
else getFieldSignal(key).set(value);
|
|
409
637
|
if (shouldDirty) isDirty.set(true);
|
|
638
|
+
}
|
|
410
639
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
640
|
+
// Set single field value — only triggers re-render for components reading this field
|
|
641
|
+
function setValue(name, value, options = {}) {
|
|
642
|
+
setKey(keyOf(name), value, options.shouldDirty ?? true);
|
|
643
|
+
if (options.shouldValidate) validate(name);
|
|
414
644
|
}
|
|
415
645
|
|
|
416
646
|
// Get single field value
|
|
417
647
|
function getValue(name) {
|
|
418
|
-
return
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// Set error for a field
|
|
422
|
-
function setError(name, error) {
|
|
423
|
-
setFieldError(name, error);
|
|
648
|
+
return readName(keyOf(name));
|
|
424
649
|
}
|
|
425
650
|
|
|
426
651
|
// Clear error for a field
|
|
427
652
|
function clearError(name) {
|
|
428
|
-
setFieldError(name, null);
|
|
653
|
+
setFieldError(keyOf(name), null);
|
|
429
654
|
}
|
|
430
655
|
|
|
431
656
|
// Clear all errors
|
|
@@ -433,19 +658,25 @@ function createFormController(options = {}) {
|
|
|
433
658
|
replaceAllErrors({});
|
|
434
659
|
}
|
|
435
660
|
|
|
436
|
-
// Reset form
|
|
437
|
-
|
|
661
|
+
// Reset form: to the values given, which become the defaults, or to the
|
|
662
|
+
// defaults. The count moves so a whole-form reader (dirtyFields) measures
|
|
663
|
+
// again, a field that held its new default already included. A field
|
|
664
|
+
// array takes its items from the defaults again, each a new item.
|
|
665
|
+
function reset(newValues) {
|
|
666
|
+
if (newValues !== undefined) defaultValues = newValues;
|
|
438
667
|
batch(() => {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
for (const sig of Object.values(touchedSignals)) {
|
|
447
|
-
sig.set(false);
|
|
668
|
+
ext?.r(defaultValues);
|
|
669
|
+
for (const key in fieldSignals) {
|
|
670
|
+
if (nameOf(key) != null) fieldSignals[key].set(defaultOf(key));
|
|
671
|
+
else {
|
|
672
|
+
delete fieldSignals[key];
|
|
673
|
+
delete known[key];
|
|
674
|
+
}
|
|
448
675
|
}
|
|
676
|
+
for (const key in hidden) delete hidden[key];
|
|
677
|
+
counted();
|
|
678
|
+
replaceAllErrors({});
|
|
679
|
+
for (const key in touchedSignals) touchedSignals[key].set(false);
|
|
449
680
|
isDirty.set(false);
|
|
450
681
|
isSubmitted.set(false);
|
|
451
682
|
});
|
|
@@ -458,7 +689,7 @@ function createFormController(options = {}) {
|
|
|
458
689
|
|
|
459
690
|
isSubmitting.set(true);
|
|
460
691
|
isSubmitted.set(true);
|
|
461
|
-
submitCount.set(
|
|
692
|
+
submitCount.set((n) => n + 1);
|
|
462
693
|
|
|
463
694
|
// try/finally, because anything that throws between here and the release
|
|
464
695
|
// leaves isSubmitting stuck true forever, which in practice means a submit
|
|
@@ -469,9 +700,9 @@ function createFormController(options = {}) {
|
|
|
469
700
|
const isFormValid = await validate();
|
|
470
701
|
|
|
471
702
|
if (isFormValid) {
|
|
472
|
-
await onValid(getAllValues
|
|
703
|
+
await onValid(untrack(getAllValues));
|
|
473
704
|
} else if (onInvalid) {
|
|
474
|
-
onInvalid(getAllErrors
|
|
705
|
+
onInvalid(untrack(getAllErrors));
|
|
475
706
|
}
|
|
476
707
|
} finally {
|
|
477
708
|
isSubmitting.set(false);
|
|
@@ -479,37 +710,50 @@ function createFormController(options = {}) {
|
|
|
479
710
|
};
|
|
480
711
|
}
|
|
481
712
|
|
|
482
|
-
// Watch a field — returns a computed that subscribes only to this field
|
|
713
|
+
// Watch a field — returns a computed that subscribes only to this field.
|
|
714
|
+
// The name is taken now: a field of a field array item stays that item's.
|
|
483
715
|
function watch(name) {
|
|
484
716
|
if (name) {
|
|
485
|
-
|
|
717
|
+
const key = keyOf(name);
|
|
718
|
+
return computed(() => readName(key));
|
|
486
719
|
}
|
|
487
720
|
// Watch all: return a computed that reads all field signals
|
|
488
|
-
return computed(
|
|
721
|
+
return computed(getAllValues);
|
|
489
722
|
}
|
|
490
723
|
|
|
491
724
|
return {
|
|
492
725
|
register,
|
|
726
|
+
unregister,
|
|
493
727
|
handleSubmit,
|
|
494
728
|
setValue,
|
|
495
729
|
getValue,
|
|
496
|
-
|
|
730
|
+
// Set error for a field
|
|
731
|
+
setError: (name, error) => setFieldError(keyOf(name), error),
|
|
497
732
|
clearError,
|
|
498
733
|
clearErrors,
|
|
499
734
|
reset,
|
|
500
735
|
watch,
|
|
501
736
|
validate,
|
|
737
|
+
// What useFieldArray works with: `x(make)` installs the field array
|
|
738
|
+
// support make() builds over this form's state, once.
|
|
739
|
+
control: {
|
|
740
|
+
x: (make) => ext ||= ({ k: keyOf, p: nameOf, d: defaultAt } = make(arrays, forget, getAllValues, isDirty, errorSignals, errorsState)),
|
|
741
|
+
},
|
|
502
742
|
// Form state
|
|
503
743
|
formState: {
|
|
504
|
-
get values() { return getAllValues(
|
|
505
|
-
get errors() { return getAllErrors(
|
|
506
|
-
error: (name) => getErrorSignal(name)(),
|
|
744
|
+
get values() { return getAllValues(); },
|
|
745
|
+
get errors() { return getAllErrors(); },
|
|
746
|
+
error: (name) => getErrorSignal(keyOf(name))(),
|
|
747
|
+
// The fields touched, by name.
|
|
507
748
|
get touched() {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
749
|
+
fieldCount();
|
|
750
|
+
const out = {};
|
|
751
|
+
for (const key in touchedSignals) {
|
|
752
|
+
if (!touchedSignals[key]()) continue;
|
|
753
|
+
const name = nameOf(key);
|
|
754
|
+
if (name != null) out[name] = true;
|
|
511
755
|
}
|
|
512
|
-
return
|
|
756
|
+
return out;
|
|
513
757
|
},
|
|
514
758
|
isDirty: () => isDirty(),
|
|
515
759
|
isValid,
|
|
@@ -522,6 +766,241 @@ function createFormController(options = {}) {
|
|
|
522
766
|
};
|
|
523
767
|
}
|
|
524
768
|
|
|
769
|
+
// --- useFieldArray Hook ---
|
|
770
|
+
// The items of an array field, as React Hook Form's useFieldArray has them:
|
|
771
|
+
// `fields()` lists them with an id each for the row's key, and append,
|
|
772
|
+
// prepend, insert, remove, move, swap, update and replace change the list.
|
|
773
|
+
// An item's values, errors and touched state are kept under the item, not
|
|
774
|
+
// its index, so they move with it; a removed item's state goes with it, and
|
|
775
|
+
// a validation of it still in flight never lands.
|
|
776
|
+
|
|
777
|
+
let itemCount = 0;
|
|
778
|
+
|
|
779
|
+
// Field array support for one form, installed by its first useFieldArray.
|
|
780
|
+
// An item's segment is '\0' and its id: no name a person writes has it.
|
|
781
|
+
function fieldArrays(arrays, forget, getAllValues, isDirty, errorSignals, errorsState) {
|
|
782
|
+
// Each item's values as it was added, by its segment.
|
|
783
|
+
/** @type {Record<string, any>} */
|
|
784
|
+
const bases = {};
|
|
785
|
+
const add = (value) => {
|
|
786
|
+
const segment = '\0' + (++itemCount).toString(36);
|
|
787
|
+
bases[segment] = value ?? {};
|
|
788
|
+
return segment;
|
|
789
|
+
};
|
|
790
|
+
// The key of a name, taking an array's items from `items` (an array's key
|
|
791
|
+
// to its items), the arrays' current items by default.
|
|
792
|
+
const key = (name, items) => {
|
|
793
|
+
if (!name.includes('.')) return name;
|
|
794
|
+
let out = '';
|
|
795
|
+
for (const segment of name.split('.')) {
|
|
796
|
+
const list = out && (items ? items[out] : arrays[out]?.());
|
|
797
|
+
out += (out ? '.' : '') + (list && isIndex(segment) && list[segment] !== undefined ? list[segment] : segment);
|
|
798
|
+
}
|
|
799
|
+
return out;
|
|
800
|
+
};
|
|
801
|
+
// Each array's items by their place, so a key's name is found without
|
|
802
|
+
// searching the items: made once for each list the array holds, from the
|
|
803
|
+
// list as a read of it finds it. Not a computed, which would belong to the
|
|
804
|
+
// component that first used the array and go with it, while the array
|
|
805
|
+
// lives as long as the form.
|
|
806
|
+
const placeMaps = new WeakMap();
|
|
807
|
+
const places = (k) => {
|
|
808
|
+
const list = arrays[k]?.();
|
|
809
|
+
if (list === undefined) return undefined;
|
|
810
|
+
let map = placeMaps.get(list);
|
|
811
|
+
if (map === undefined) placeMaps.set(list, map = new Map(list.map((segment, i) => [segment, i])));
|
|
812
|
+
return map;
|
|
813
|
+
};
|
|
814
|
+
// Each key's name, and the arrays on its path, found once until an
|
|
815
|
+
// array's items change (seed and change start a new map). A read of a
|
|
816
|
+
// kept name still reads those arrays, so it follows them as before.
|
|
817
|
+
/** @type {Map<string, any[]>} */
|
|
818
|
+
let names = new Map();
|
|
819
|
+
const nameAt = (k) => {
|
|
820
|
+
if (!k.includes('\0')) return k;
|
|
821
|
+
let hit = names.get(k);
|
|
822
|
+
if (hit !== undefined) {
|
|
823
|
+
for (let i = 1; i < hit.length; i++) arrays[hit[i]]?.();
|
|
824
|
+
return hit[0];
|
|
825
|
+
}
|
|
826
|
+
names.set(k, hit = [null]);
|
|
827
|
+
let at = '';
|
|
828
|
+
let out = '';
|
|
829
|
+
for (const segment of k.split('.')) {
|
|
830
|
+
let part = segment;
|
|
831
|
+
if (segment[0] === '\0') {
|
|
832
|
+
hit.push(at);
|
|
833
|
+
part = places(at)?.get(segment);
|
|
834
|
+
if (part === undefined) return null;
|
|
835
|
+
}
|
|
836
|
+
at += (at ? '.' : '') + segment;
|
|
837
|
+
out += (out ? '.' : '') + part;
|
|
838
|
+
}
|
|
839
|
+
return (hit[0] = out);
|
|
840
|
+
};
|
|
841
|
+
// Forget the items of array `k` with these segments, and the field arrays
|
|
842
|
+
// inside them, in one pass over the form's fields; their errors leave the
|
|
843
|
+
// form's errors in one write.
|
|
844
|
+
const drop = (k, segments) => {
|
|
845
|
+
if (!segments.length) return;
|
|
846
|
+
const removed = new Set(segments);
|
|
847
|
+
const gone = (key) => key.startsWith(k + '.') && removed.has(key.slice(k.length + 1).split('.', 1)[0]);
|
|
848
|
+
const errors = { ...errorsState.peek() };
|
|
849
|
+
let cleared = false;
|
|
850
|
+
for (const key in errorSignals) {
|
|
851
|
+
if (!gone(key)) continue;
|
|
852
|
+
errorSignals[key].set(null);
|
|
853
|
+
delete errorSignals[key];
|
|
854
|
+
if (Object.hasOwn(errors, key)) cleared = delete errors[key];
|
|
855
|
+
}
|
|
856
|
+
if (cleared) errorsState.set(errors);
|
|
857
|
+
forget(k + '.' + segments[0], { keepError: true }, gone);
|
|
858
|
+
for (const segment of segments) delete bases[segment];
|
|
859
|
+
for (const inner in arrays) {
|
|
860
|
+
if (!gone(inner)) continue;
|
|
861
|
+
for (const segment of arrays[inner].peek()) delete bases[segment];
|
|
862
|
+
delete arrays[inner];
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
const seed = (k, items) => {
|
|
866
|
+
names = new Map();
|
|
867
|
+
if (arrays[k]) return arrays[k].set(items.map(add));
|
|
868
|
+
arrays[k] = signal(items.map(add));
|
|
869
|
+
};
|
|
870
|
+
// The last item segment of a key, and where it is.
|
|
871
|
+
const lastItem = (segments) => {
|
|
872
|
+
let at = segments.length;
|
|
873
|
+
while (at-- > 0 && segments[at][0] !== '\0');
|
|
874
|
+
return at;
|
|
875
|
+
};
|
|
876
|
+
return {
|
|
877
|
+
k: key,
|
|
878
|
+
p: nameAt,
|
|
879
|
+
// A key's default: under an item, from the item's values.
|
|
880
|
+
d(defaults, k) {
|
|
881
|
+
const segments = k.split('.');
|
|
882
|
+
const at = lastItem(segments);
|
|
883
|
+
return segments.slice(at + 1).reduce(own, at < 0 ? defaults : bases[segments[at]]);
|
|
884
|
+
},
|
|
885
|
+
// Forget the default of a key under an item, in the item's values.
|
|
886
|
+
u(k) {
|
|
887
|
+
const segments = k.split('.');
|
|
888
|
+
const at = lastItem(segments);
|
|
889
|
+
if (at < 0) return false;
|
|
890
|
+
if (!(segments[at] in bases)) return true;
|
|
891
|
+
if (at === segments.length - 1) delete bases[segments[at]];
|
|
892
|
+
else bases[segments[at]] = unsetIn(bases[segments[at]], segments.slice(at + 1));
|
|
893
|
+
return true;
|
|
894
|
+
},
|
|
895
|
+
// Each array's items, outer arrays first, replace what the defaults hold.
|
|
896
|
+
v(result) {
|
|
897
|
+
for (const k of Object.keys(arrays).sort((a, b) => a.length - b.length)) {
|
|
898
|
+
const name = nameAt(k);
|
|
899
|
+
if (name != null) setIn(result, name, arrays[k]().map((segment) => clone(bases[segment])));
|
|
900
|
+
}
|
|
901
|
+
},
|
|
902
|
+
// Errors by name to errors by key, with the items in their places now;
|
|
903
|
+
// an error of an item removed by the time it lands goes.
|
|
904
|
+
s() {
|
|
905
|
+
const items = map();
|
|
906
|
+
for (const k in arrays) items[k] = arrays[k].peek();
|
|
907
|
+
return (errors) => {
|
|
908
|
+
const out = {};
|
|
909
|
+
for (const name in errors) {
|
|
910
|
+
const k = key(name, items);
|
|
911
|
+
if (nameAt(k) != null) out[k] = errors[name];
|
|
912
|
+
}
|
|
913
|
+
return out;
|
|
914
|
+
};
|
|
915
|
+
},
|
|
916
|
+
e(errors) {
|
|
917
|
+
const out = {};
|
|
918
|
+
for (const k in errors) {
|
|
919
|
+
const name = nameAt(k);
|
|
920
|
+
if (name != null) out[name] = errors[k];
|
|
921
|
+
}
|
|
922
|
+
return out;
|
|
923
|
+
},
|
|
924
|
+
// An array's items replaced by new ones with these values.
|
|
925
|
+
a(k, values) {
|
|
926
|
+
const old = arrays[k].peek();
|
|
927
|
+
batch(() => {
|
|
928
|
+
seed(k, values || []);
|
|
929
|
+
drop(k, old);
|
|
930
|
+
});
|
|
931
|
+
},
|
|
932
|
+
// A reset gives each array its items from the defaults, as new items;
|
|
933
|
+
// an array inside an item goes with the item.
|
|
934
|
+
r(defaults) {
|
|
935
|
+
for (const k in arrays) {
|
|
936
|
+
for (const segment of arrays[k].peek()) delete bases[segment];
|
|
937
|
+
if (!k.includes('\0')) seed(k, getIn(defaults, k) || []);
|
|
938
|
+
else delete arrays[k];
|
|
939
|
+
}
|
|
940
|
+
},
|
|
941
|
+
// One field array's controls.
|
|
942
|
+
array(name) {
|
|
943
|
+
const k = key(name);
|
|
944
|
+
if (!arrays[k]) seed(k, getIn(getAllValues(), name) || []);
|
|
945
|
+
const items = arrays[k];
|
|
946
|
+
// One object per item for as long as it is in the list: a keyed row
|
|
947
|
+
// that took its item as a value is built again for a new one.
|
|
948
|
+
let shown = {};
|
|
949
|
+
const change = (next, removed = []) => batch(() => {
|
|
950
|
+
names = new Map();
|
|
951
|
+
items.set(next);
|
|
952
|
+
drop(k, removed);
|
|
953
|
+
isDirty.set(true);
|
|
954
|
+
});
|
|
955
|
+
const many = (value) => (Array.isArray(value) ? value : [value]).map(add);
|
|
956
|
+
const insert = (index, value) => {
|
|
957
|
+
const next = [...items.peek()];
|
|
958
|
+
next.splice(index, 0, ...many(value));
|
|
959
|
+
change(next);
|
|
960
|
+
};
|
|
961
|
+
return {
|
|
962
|
+
fields: computed(() => {
|
|
963
|
+
const now = {};
|
|
964
|
+
const list = items().map((segment) => (now[segment] = shown[segment] || { ...bases[segment], id: segment.slice(1) }));
|
|
965
|
+
shown = now;
|
|
966
|
+
return list;
|
|
967
|
+
}),
|
|
968
|
+
append: (value) => insert(items.peek().length, value),
|
|
969
|
+
prepend: (value) => insert(0, value),
|
|
970
|
+
insert,
|
|
971
|
+
remove(index) {
|
|
972
|
+
const list = items.peek();
|
|
973
|
+
const gone = index === undefined ? list : [].concat(index).map((i) => list[i]);
|
|
974
|
+
change(list.filter((segment) => !gone.includes(segment)), gone);
|
|
975
|
+
},
|
|
976
|
+
move(from, to) {
|
|
977
|
+
const next = [...items.peek()];
|
|
978
|
+
next.splice(to, 0, next.splice(from, 1)[0]);
|
|
979
|
+
change(next);
|
|
980
|
+
},
|
|
981
|
+
swap(a, b) {
|
|
982
|
+
const next = [...items.peek()];
|
|
983
|
+
[next[a], next[b]] = [next[b], next[a]];
|
|
984
|
+
change(next);
|
|
985
|
+
},
|
|
986
|
+
// The item at `index` becomes a new one with these values, as React
|
|
987
|
+
// Hook Form's update remounts it.
|
|
988
|
+
update(index, value) {
|
|
989
|
+
const next = [...items.peek()];
|
|
990
|
+
const old = next[index];
|
|
991
|
+
next[index] = add(value);
|
|
992
|
+
change(next, [old]);
|
|
993
|
+
},
|
|
994
|
+
replace: (values) => change(many(values), items.peek()),
|
|
995
|
+
};
|
|
996
|
+
},
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
export function useFieldArray({ control, form, name }) {
|
|
1001
|
+
return hookStable(() => untrack(() => (control || form.control).x(fieldArrays).array(name)));
|
|
1002
|
+
}
|
|
1003
|
+
|
|
525
1004
|
// --- Validation Resolvers ---
|
|
526
1005
|
|
|
527
1006
|
// A resolver that cannot read its library's error shape must never answer "no
|
|
@@ -572,7 +1051,7 @@ export function zodResolver(schema) {
|
|
|
572
1051
|
const errors = {};
|
|
573
1052
|
for (const issue of issues) {
|
|
574
1053
|
const path = _issuePath(issue);
|
|
575
|
-
if (!errors
|
|
1054
|
+
if (!own(errors, path)) {
|
|
576
1055
|
errors[path] = { type: issue.code ?? 'validation', message: issue.message };
|
|
577
1056
|
}
|
|
578
1057
|
}
|
|
@@ -610,7 +1089,7 @@ export function yupResolver(schema) {
|
|
|
610
1089
|
const inner = Array.isArray(e.inner) && e.inner.length > 0 ? e.inner : [e];
|
|
611
1090
|
for (const err of inner) {
|
|
612
1091
|
const path = err.path ?? '';
|
|
613
|
-
if (!errors
|
|
1092
|
+
if (!own(errors, path)) {
|
|
614
1093
|
errors[path] = { type: err.type ?? 'validation', message: err.message };
|
|
615
1094
|
}
|
|
616
1095
|
}
|
|
@@ -619,13 +1098,17 @@ export function yupResolver(schema) {
|
|
|
619
1098
|
};
|
|
620
1099
|
}
|
|
621
1100
|
|
|
622
|
-
// Simple validation resolver
|
|
1101
|
+
// Simple validation resolver. Given `{ names }` (a field's own validation),
|
|
1102
|
+
// it runs the rules of those fields only; every rule still reads all values.
|
|
1103
|
+
// A rule's field is a name, so `'address.city'` reads `values.address.city`.
|
|
623
1104
|
export function simpleResolver(rules) {
|
|
624
|
-
return async (values) => {
|
|
1105
|
+
return async (values, _context, options) => {
|
|
625
1106
|
const errors = {};
|
|
1107
|
+
const names = options?.names;
|
|
626
1108
|
|
|
627
1109
|
for (const [field, fieldRules] of Object.entries(rules)) {
|
|
628
|
-
|
|
1110
|
+
if (names && !names.includes(field)) continue;
|
|
1111
|
+
const value = getIn(values, field);
|
|
629
1112
|
|
|
630
1113
|
for (const rule of fieldRules) {
|
|
631
1114
|
const error = rule(value, values);
|
|
@@ -640,6 +1123,17 @@ export function simpleResolver(rules) {
|
|
|
640
1123
|
};
|
|
641
1124
|
}
|
|
642
1125
|
|
|
1126
|
+
// The number a value stands for, for min and max: a number as it is, and a
|
|
1127
|
+
// numeric string as the number it spells, because a registered
|
|
1128
|
+
// <input type="number"> gives the form its value as a string. A blank or
|
|
1129
|
+
// non-numeric string stands for none; required and pattern judge those.
|
|
1130
|
+
function numberOf(value) {
|
|
1131
|
+
if (typeof value === 'number') return value;
|
|
1132
|
+
if (typeof value !== 'string' || value.trim() === '') return undefined;
|
|
1133
|
+
const n = Number(value);
|
|
1134
|
+
return Number.isNaN(n) ? undefined : n;
|
|
1135
|
+
}
|
|
1136
|
+
|
|
643
1137
|
// Built-in validation rules
|
|
644
1138
|
export const rules = {
|
|
645
1139
|
required: (message = 'This field is required') => (value) => {
|
|
@@ -661,13 +1155,15 @@ export const rules = {
|
|
|
661
1155
|
},
|
|
662
1156
|
|
|
663
1157
|
min: (min, message) => (value) => {
|
|
664
|
-
|
|
1158
|
+
const n = numberOf(value);
|
|
1159
|
+
if (n !== undefined && n < min) {
|
|
665
1160
|
return message || `Must be at least ${min}`;
|
|
666
1161
|
}
|
|
667
1162
|
},
|
|
668
1163
|
|
|
669
1164
|
max: (max, message) => (value) => {
|
|
670
|
-
|
|
1165
|
+
const n = numberOf(value);
|
|
1166
|
+
if (n !== undefined && n > max) {
|
|
671
1167
|
return message || `Must be at most ${max}`;
|
|
672
1168
|
}
|
|
673
1169
|
},
|
|
@@ -696,7 +1192,7 @@ export const rules = {
|
|
|
696
1192
|
},
|
|
697
1193
|
|
|
698
1194
|
match: (field, message) => (value, values) => {
|
|
699
|
-
if (value !== values
|
|
1195
|
+
if (value !== getIn(values, field)) {
|
|
700
1196
|
return message || `Must match ${field}`;
|
|
701
1197
|
}
|
|
702
1198
|
},
|
|
@@ -757,13 +1253,15 @@ export function useField(name, options = {}) {
|
|
|
757
1253
|
|
|
758
1254
|
// --- Controlled Input Components ---
|
|
759
1255
|
|
|
1256
|
+
// The registration is merged into the caller's props (_mergeRegistration), so
|
|
1257
|
+
// a handler of the caller's for an event the registration listens for runs
|
|
1258
|
+
// too: an `onChange` of <Select> with the registration's `onchange`.
|
|
760
1259
|
export function Input(props) {
|
|
761
1260
|
const { register, error, ...rest } = props;
|
|
762
1261
|
const registered = register ? register(props.name) : {};
|
|
763
1262
|
|
|
764
1263
|
return h('input', {
|
|
765
|
-
...rest,
|
|
766
|
-
...registered,
|
|
1264
|
+
..._mergeRegistration(rest, registered),
|
|
767
1265
|
'aria-invalid': error ? 'true' : undefined,
|
|
768
1266
|
});
|
|
769
1267
|
}
|
|
@@ -773,33 +1271,33 @@ export function Textarea(props) {
|
|
|
773
1271
|
const registered = register ? register(props.name) : {};
|
|
774
1272
|
|
|
775
1273
|
return h('textarea', {
|
|
776
|
-
...rest,
|
|
777
|
-
...registered,
|
|
1274
|
+
..._mergeRegistration(rest, registered),
|
|
778
1275
|
'aria-invalid': error ? 'true' : undefined,
|
|
779
1276
|
});
|
|
780
1277
|
}
|
|
781
1278
|
|
|
782
|
-
|
|
1279
|
+
// Compiled options arrive unbuilt (`_deferChildren`, dom.js
|
|
1280
|
+
// _installLazyChildren) and are built inside the <select>: while hydrating,
|
|
1281
|
+
// options built while Select runs would claim the server's nodes before the
|
|
1282
|
+
// select does.
|
|
1283
|
+
export const Select = /* @__PURE__ */ Object.assign(function Select(props) {
|
|
783
1284
|
const { register, error, children, ...rest } = props;
|
|
784
1285
|
const registered = register ? register(props.name) : {};
|
|
785
1286
|
|
|
786
1287
|
return h('select', {
|
|
787
|
-
...rest,
|
|
788
|
-
...registered,
|
|
1288
|
+
..._mergeRegistration(rest, registered),
|
|
789
1289
|
'aria-invalid': error ? 'true' : undefined,
|
|
790
1290
|
}, children);
|
|
791
|
-
}
|
|
1291
|
+
}, { _deferChildren: true });
|
|
792
1292
|
|
|
1293
|
+
// Registered as a checkbox, so the field holds its checkedness: `checked`
|
|
1294
|
+
// follows the field (_mergeRegistration) and no `value` attribute is written,
|
|
1295
|
+
// so the form submits "on". An unregistered Checkbox takes `checked` from its
|
|
1296
|
+
// props.
|
|
793
1297
|
export function Checkbox(props) {
|
|
794
1298
|
const { register, ...rest } = props;
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
return h('input', {
|
|
798
|
-
type: 'checkbox',
|
|
799
|
-
...rest,
|
|
800
|
-
...registered,
|
|
801
|
-
checked: registered.value,
|
|
802
|
-
});
|
|
1299
|
+
if (!register) return h('input', { type: 'checkbox', ...rest });
|
|
1300
|
+
return h('input', { type: 'checkbox', ..._mergeRegistration(rest, register(props.name, { type: 'checkbox' })) });
|
|
803
1301
|
}
|
|
804
1302
|
|
|
805
1303
|
export function Radio(props) {
|
|
@@ -811,13 +1309,7 @@ export function Radio(props) {
|
|
|
811
1309
|
// form state and then check every radio in the group that reads it back.
|
|
812
1310
|
// Render a plain unregistered input instead: inert, but honest.
|
|
813
1311
|
if (radioValue === undefined) {
|
|
814
|
-
if (__DEV__) {
|
|
815
|
-
console.warn(
|
|
816
|
-
`[what] <Radio name="${props.name}"> is missing a \`value\` prop. Radios in a group share ` +
|
|
817
|
-
"one field holding the selected option's value, so a valueless radio has nothing to " +
|
|
818
|
-
'contribute and cannot be checked. Rendering it unregistered.'
|
|
819
|
-
);
|
|
820
|
-
}
|
|
1312
|
+
if (__DEV__) report('ERR_FORM_RADIO_WITHOUT_VALUE', { data: { name: props.name } });
|
|
821
1313
|
return h('input', { type: 'radio', ...rest });
|
|
822
1314
|
}
|
|
823
1315
|
|
|
@@ -847,21 +1339,27 @@ export function Radio(props) {
|
|
|
847
1339
|
|
|
848
1340
|
// --- Form Error Display ---
|
|
849
1341
|
|
|
1342
|
+
// The component runs once, so what it renders is a reactive function: the
|
|
1343
|
+
// field's error is read inside it, and the message appears, changes and goes
|
|
1344
|
+
// away with the error.
|
|
850
1345
|
export function ErrorMessage({ name, formState, errors, render }) {
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
(
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
return render({ message: error.message, type: error.type });
|
|
864
|
-
}
|
|
1346
|
+
return () => {
|
|
1347
|
+
const fieldError = formState && typeof formState.error === 'function'
|
|
1348
|
+
? formState.error(name)
|
|
1349
|
+
: (
|
|
1350
|
+
own(
|
|
1351
|
+
formState?.errors != null
|
|
1352
|
+
? formState.errors
|
|
1353
|
+
: (typeof errors === 'function' ? errors() : errors),
|
|
1354
|
+
name,
|
|
1355
|
+
) || null
|
|
1356
|
+
);
|
|
1357
|
+
if (!fieldError) return null;
|
|
865
1358
|
|
|
866
|
-
|
|
1359
|
+
if (render) {
|
|
1360
|
+
return render({ message: fieldError.message, type: fieldError.type });
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
return h('span', { class: 'what-error', role: 'alert' }, fieldError.message);
|
|
1364
|
+
};
|
|
867
1365
|
}
|