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/dist/form.js
ADDED
|
@@ -0,0 +1,936 @@
|
|
|
1
|
+
// src/form.js
|
|
2
|
+
import { signal, computed, batch, effect, onCleanup, untrack } from "./reactive.js";
|
|
3
|
+
import { getCurrentComponent, _isEventProp, _markProps } from "./dom.js";
|
|
4
|
+
import { h } from "./h.js";
|
|
5
|
+
import { applyRef } from "./render.js";
|
|
6
|
+
import { report } from "./diagnostics.js";
|
|
7
|
+
function _composeHandlers(registered, callerHandler) {
|
|
8
|
+
if (typeof registered !== "function") return callerHandler;
|
|
9
|
+
if (typeof callerHandler !== "function") return registered;
|
|
10
|
+
return function composedHandler(...args) {
|
|
11
|
+
registered.apply(this, args);
|
|
12
|
+
return callerHandler.apply(this, args);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function _mergeRegistration(props, registered) {
|
|
16
|
+
const merged = { ...props };
|
|
17
|
+
const callerKeyByEvent = /* @__PURE__ */ new Map();
|
|
18
|
+
for (const key in merged) {
|
|
19
|
+
if (_isEventProp(key)) callerKeyByEvent.set(key.slice(2).toLowerCase(), key);
|
|
20
|
+
}
|
|
21
|
+
for (const key in registered) {
|
|
22
|
+
if (Object.getOwnPropertyDescriptor(registered, key)?.get) {
|
|
23
|
+
merged[key] = () => registered[key];
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const value = registered[key];
|
|
27
|
+
if (!_isEventProp(key)) {
|
|
28
|
+
merged[key] = value;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const callerKey = callerKeyByEvent.get(key.slice(2).toLowerCase());
|
|
32
|
+
if (callerKey === void 0) {
|
|
33
|
+
merged[key] = value;
|
|
34
|
+
callerKeyByEvent.set(key.slice(2).toLowerCase(), key);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
merged[callerKey] = _composeHandlers(value, merged[callerKey]);
|
|
38
|
+
}
|
|
39
|
+
return merged;
|
|
40
|
+
}
|
|
41
|
+
var isIndex = (segment) => /^\d+$/.test(segment);
|
|
42
|
+
function isContainer(value) {
|
|
43
|
+
return Array.isArray(value) || value !== null && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype;
|
|
44
|
+
}
|
|
45
|
+
function clone(value) {
|
|
46
|
+
if (Array.isArray(value)) return value.map(clone);
|
|
47
|
+
if (!isContainer(value)) return value;
|
|
48
|
+
const out = {};
|
|
49
|
+
for (const key in value) out[key] = clone(value[key]);
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
var map = () => /* @__PURE__ */ Object.create(null);
|
|
53
|
+
var own = (obj, key) => obj == null || !Object.hasOwn(obj, key) && key in Object.prototype ? void 0 : obj[key];
|
|
54
|
+
var getIn = (obj, path) => path.split(".").reduce(own, obj);
|
|
55
|
+
function setIn(obj, path, value) {
|
|
56
|
+
const segments = path.split(".");
|
|
57
|
+
if (segments.includes("__proto__")) return;
|
|
58
|
+
const last = segments.length - 1;
|
|
59
|
+
let cur = obj;
|
|
60
|
+
for (let i = 0; i < last; i++) {
|
|
61
|
+
const segment = segments[i];
|
|
62
|
+
if (typeof (cur[segment] ?? 0) !== "object") cur[segment] = isIndex(segments[i + 1]) ? [] : {};
|
|
63
|
+
cur = cur[segment];
|
|
64
|
+
}
|
|
65
|
+
cur[segments[last]] = value;
|
|
66
|
+
}
|
|
67
|
+
function unsetIn(obj, segments) {
|
|
68
|
+
const [segment, ...rest] = segments;
|
|
69
|
+
if (!isContainer(obj) || !Object.hasOwn(obj, segment)) return obj;
|
|
70
|
+
const out = Array.isArray(obj) ? [...obj] : { ...obj };
|
|
71
|
+
const inner = rest.length ? unsetIn(out[segment], rest) : void 0;
|
|
72
|
+
if (inner === void 0 || !Array.isArray(inner) && isContainer(inner) && !Object.keys(inner).length) delete out[segment];
|
|
73
|
+
else out[segment] = inner;
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
var KEEP_DEFAULT = { keepDefaultValue: true };
|
|
77
|
+
function hookStable(make) {
|
|
78
|
+
const ctx = getCurrentComponent();
|
|
79
|
+
if (!ctx) return make();
|
|
80
|
+
if (ctx.hooks === null) ctx.hooks = [];
|
|
81
|
+
const index = ctx.hookIndex++;
|
|
82
|
+
return ctx.hooks[index] ||= make();
|
|
83
|
+
}
|
|
84
|
+
function useForm(options = {}) {
|
|
85
|
+
return hookStable(() => createFormController(options));
|
|
86
|
+
}
|
|
87
|
+
function createFormController(options = {}) {
|
|
88
|
+
const {
|
|
89
|
+
mode = "onSubmit",
|
|
90
|
+
// 'onSubmit' | 'onChange' | 'onBlur'
|
|
91
|
+
reValidateMode = "onChange",
|
|
92
|
+
resolver,
|
|
93
|
+
shouldUnregister = false
|
|
94
|
+
} = options;
|
|
95
|
+
let defaultValues = options.defaultValues || {};
|
|
96
|
+
const arrays = map();
|
|
97
|
+
let ext;
|
|
98
|
+
let keyOf = (name) => name;
|
|
99
|
+
let nameOf = keyOf;
|
|
100
|
+
let defaultAt = getIn;
|
|
101
|
+
const fieldSignals = map();
|
|
102
|
+
const known = map();
|
|
103
|
+
const hidden = map();
|
|
104
|
+
const errorSignals = map();
|
|
105
|
+
const touchedSignals = map();
|
|
106
|
+
const errorsState = signal({});
|
|
107
|
+
const fieldCount = signal(0);
|
|
108
|
+
const counted = () => fieldCount.set((n) => n + 1);
|
|
109
|
+
const defaultOf = (key) => defaultAt(defaultValues, key) ?? "";
|
|
110
|
+
const signalOf = (key) => fieldSignals[key] ||= signal(defaultOf(key));
|
|
111
|
+
function getFieldSignal(key) {
|
|
112
|
+
const sig = signalOf(key);
|
|
113
|
+
if (!known[key]) {
|
|
114
|
+
known[key] = true;
|
|
115
|
+
delete hidden[key];
|
|
116
|
+
counted();
|
|
117
|
+
}
|
|
118
|
+
return sig;
|
|
119
|
+
}
|
|
120
|
+
const readKey = (key) => signalOf(key)();
|
|
121
|
+
function readName(key) {
|
|
122
|
+
if (!known[key] && (arrays[key] || isContainer(defaultOf(key)) || Object.keys(known).some((k) => k.startsWith(key + ".")))) {
|
|
123
|
+
const name = nameOf(key);
|
|
124
|
+
return name == null ? "" : getIn(getAllValues(), name) ?? "";
|
|
125
|
+
}
|
|
126
|
+
return readKey(key);
|
|
127
|
+
}
|
|
128
|
+
function getErrorSignal(key) {
|
|
129
|
+
return errorSignals[key] ||= signal(null);
|
|
130
|
+
}
|
|
131
|
+
function getTouchedSignal(key) {
|
|
132
|
+
if (!touchedSignals[key]) {
|
|
133
|
+
touchedSignals[key] = signal(false);
|
|
134
|
+
counted();
|
|
135
|
+
}
|
|
136
|
+
return touchedSignals[key];
|
|
137
|
+
}
|
|
138
|
+
const isDirty = signal(false);
|
|
139
|
+
const isSubmitting = signal(false);
|
|
140
|
+
const isSubmitted = signal(false);
|
|
141
|
+
const isValidating = signal(false);
|
|
142
|
+
const submitCount = signal(0);
|
|
143
|
+
function getAllValues() {
|
|
144
|
+
fieldCount();
|
|
145
|
+
let result = clone(defaultValues);
|
|
146
|
+
ext?.v(result);
|
|
147
|
+
for (const key in hidden) {
|
|
148
|
+
const name = nameOf(key);
|
|
149
|
+
if (name != null) result = unsetIn(result, name.split("."));
|
|
150
|
+
}
|
|
151
|
+
for (const key in known) {
|
|
152
|
+
const name = nameOf(key);
|
|
153
|
+
if (name != null) setIn(result, name, fieldSignals[key]());
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
function getAllErrors() {
|
|
158
|
+
const errors = errorsState();
|
|
159
|
+
return ext ? ext.e(errors) : errors;
|
|
160
|
+
}
|
|
161
|
+
function setFieldError(key, error) {
|
|
162
|
+
const nextError = error ?? null;
|
|
163
|
+
getErrorSignal(key).set(nextError);
|
|
164
|
+
errorsState.set((prev) => {
|
|
165
|
+
const prevError = prev[key];
|
|
166
|
+
if (prevError === nextError) return prev;
|
|
167
|
+
if (nextError == null) {
|
|
168
|
+
if (!Object.hasOwn(prev, key)) return prev;
|
|
169
|
+
const next = { ...prev };
|
|
170
|
+
delete next[key];
|
|
171
|
+
return next;
|
|
172
|
+
}
|
|
173
|
+
return { ...prev, [key]: nextError };
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
function replaceAllErrors(nextErrors) {
|
|
177
|
+
const normalized = nextErrors || {};
|
|
178
|
+
batch(() => {
|
|
179
|
+
for (const key in errorSignals) {
|
|
180
|
+
if (!Object.hasOwn(normalized, key)) {
|
|
181
|
+
errorSignals[key].set(null);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
for (const key in normalized) {
|
|
185
|
+
getErrorSignal(key).set(normalized[key] ?? null);
|
|
186
|
+
}
|
|
187
|
+
errorsState.set({ ...normalized });
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
const isValid = computed(() => Object.keys(getAllErrors()).length === 0);
|
|
191
|
+
const dirtyFields = computed(() => {
|
|
192
|
+
fieldCount();
|
|
193
|
+
const out = {};
|
|
194
|
+
for (const key in known) {
|
|
195
|
+
if (fieldSignals[key]() === defaultOf(key)) continue;
|
|
196
|
+
const name = nameOf(key);
|
|
197
|
+
if (name != null) out[name] = true;
|
|
198
|
+
}
|
|
199
|
+
return out;
|
|
200
|
+
});
|
|
201
|
+
let runCount = 0;
|
|
202
|
+
let formRun = 0;
|
|
203
|
+
const fieldRuns = map();
|
|
204
|
+
let inFlight = 0;
|
|
205
|
+
async function validate(fieldName) {
|
|
206
|
+
if (!resolver) return true;
|
|
207
|
+
const run = ++runCount;
|
|
208
|
+
const key = fieldName && keyOf(fieldName);
|
|
209
|
+
const byKey = ext ? ext.s() : (errors) => ({ ...errors });
|
|
210
|
+
if (fieldName) fieldRuns[key] = run;
|
|
211
|
+
else formRun = run;
|
|
212
|
+
if (inFlight++ === 0) isValidating.set(true);
|
|
213
|
+
try {
|
|
214
|
+
const result = await resolver(untrack(getAllValues), void 0, fieldName ? { names: [fieldName] } : {});
|
|
215
|
+
const nextErrors = result?.errors || {};
|
|
216
|
+
if (fieldName) {
|
|
217
|
+
const nextError = own(nextErrors, fieldName) ?? null;
|
|
218
|
+
if (fieldRuns[key] === run && formRun < run) setFieldError(key, nextError);
|
|
219
|
+
return !nextError;
|
|
220
|
+
}
|
|
221
|
+
if (formRun === run) {
|
|
222
|
+
const shown = byKey(nextErrors);
|
|
223
|
+
for (const at in fieldRuns) {
|
|
224
|
+
if (fieldRuns[at] < run) continue;
|
|
225
|
+
const latest = errorSignals[at]?.peek();
|
|
226
|
+
if (latest == null) delete shown[at];
|
|
227
|
+
else shown[at] = latest;
|
|
228
|
+
}
|
|
229
|
+
replaceAllErrors(shown);
|
|
230
|
+
}
|
|
231
|
+
return Object.keys(nextErrors).length === 0;
|
|
232
|
+
} finally {
|
|
233
|
+
if (--inFlight === 0) isValidating.set(false);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function forget(key, options2 = {}, gone) {
|
|
237
|
+
const each = (map2, fn) => {
|
|
238
|
+
for (const k in map2) {
|
|
239
|
+
if (gone ? !gone(k) : k !== key && !k.startsWith(key + ".")) continue;
|
|
240
|
+
fn(k, map2[k]);
|
|
241
|
+
if (gone) delete map2[k];
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
fieldRuns[key] = 0;
|
|
245
|
+
each(fieldRuns, (k) => fieldRuns[k] = ++runCount);
|
|
246
|
+
batch(() => {
|
|
247
|
+
if (!options2.keepValue) {
|
|
248
|
+
if (arrays[key]) ext.a(key, []);
|
|
249
|
+
if (!gone && (options2.keepDefaultValue || shouldUnregister)) {
|
|
250
|
+
if (!arrays[key]) hidden[key] = true;
|
|
251
|
+
} else if (!ext?.u(key)) defaultValues = unsetIn(defaultValues, key.split("."));
|
|
252
|
+
each(hidden, () => {
|
|
253
|
+
});
|
|
254
|
+
each(fieldSignals, (k, sig) => {
|
|
255
|
+
delete known[k];
|
|
256
|
+
sig.set(defaultOf(k));
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
if (!options2.keepError) each(errorSignals, (k) => setFieldError(k, null));
|
|
260
|
+
if (!options2.keepTouched) each(touchedSignals, (k, sig) => sig.set(false));
|
|
261
|
+
counted();
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
function unregister(names, options2) {
|
|
265
|
+
const keys = names == null ? Object.keys(known) : [].concat(names).map(keyOf);
|
|
266
|
+
batch(() => {
|
|
267
|
+
for (const key of keys) forget(key, options2);
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
function register(name, options2 = {}) {
|
|
271
|
+
const key = untrack(() => keyOf(name));
|
|
272
|
+
getFieldSignal(key);
|
|
273
|
+
const read = () => readKey(key);
|
|
274
|
+
if (options2.shouldUnregister ?? shouldUnregister) onCleanup(() => forget(key, KEEP_DEFAULT));
|
|
275
|
+
const isRadio = options2.type === "radio";
|
|
276
|
+
const isCheckbox = options2.type === "checkbox";
|
|
277
|
+
const hasRadioValue = isRadio && options2.value !== void 0;
|
|
278
|
+
function radioValueOf(el) {
|
|
279
|
+
if (hasRadioValue) return options2.value;
|
|
280
|
+
if (!el) return void 0;
|
|
281
|
+
if (typeof el.hasAttribute === "function") {
|
|
282
|
+
return el.hasAttribute("value") ? el.value : void 0;
|
|
283
|
+
}
|
|
284
|
+
return el.value;
|
|
285
|
+
}
|
|
286
|
+
let warnedValuelessRadio = false;
|
|
287
|
+
let warnedUntypedRadio = false;
|
|
288
|
+
const handler = (e) => {
|
|
289
|
+
const target = e && e.target;
|
|
290
|
+
if (!target) {
|
|
291
|
+
if ((process.env.NODE_ENV !== "production")) report("ERR_FORM_HANDLER_WITHOUT_EVENT", { data: { name } });
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const type = options2.type || target.type;
|
|
295
|
+
let value;
|
|
296
|
+
if (type === "radio") {
|
|
297
|
+
value = radioValueOf(target);
|
|
298
|
+
if (value === void 0) {
|
|
299
|
+
if ((process.env.NODE_ENV !== "production") && !warnedValuelessRadio) {
|
|
300
|
+
warnedValuelessRadio = true;
|
|
301
|
+
report("ERR_FORM_RADIO_WITHOUT_VALUE", { data: { name } });
|
|
302
|
+
}
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
} else if (type === "checkbox") {
|
|
306
|
+
value = target.checked;
|
|
307
|
+
} else if (type === "file") {
|
|
308
|
+
value = target.files;
|
|
309
|
+
} else {
|
|
310
|
+
value = type === "select-multiple" ? Array.from(target.selectedOptions, (option) => option.value) : target.value;
|
|
311
|
+
}
|
|
312
|
+
if (e.type === "change" && type !== "file" && JSON.stringify(value) === JSON.stringify(getFieldSignal(key).peek())) return;
|
|
313
|
+
setKey(key, value);
|
|
314
|
+
validateOn("onChange", key);
|
|
315
|
+
};
|
|
316
|
+
const result = {
|
|
317
|
+
name,
|
|
318
|
+
onBlur: () => {
|
|
319
|
+
getTouchedSignal(key).set(true);
|
|
320
|
+
validateOn("onBlur", key);
|
|
321
|
+
},
|
|
322
|
+
onFocus: () => {
|
|
323
|
+
},
|
|
324
|
+
ref: options2.ref,
|
|
325
|
+
onchange: handler
|
|
326
|
+
};
|
|
327
|
+
const moves = key !== name;
|
|
328
|
+
if (moves && !isRadio) Object.defineProperty(result, "name", { get: () => nameOf(key) ?? name, enumerable: true });
|
|
329
|
+
if (isRadio) {
|
|
330
|
+
const isChecked = hasRadioValue ? () => read() === options2.value : () => void 0;
|
|
331
|
+
if (hasRadioValue) result.value = options2.value;
|
|
332
|
+
result.checked = isChecked;
|
|
333
|
+
result.ref = (el) => {
|
|
334
|
+
applyRef(options2.ref, el);
|
|
335
|
+
if (!el || typeof el !== "object") return;
|
|
336
|
+
effect(() => {
|
|
337
|
+
if (moves) el.name = nameOf(key) ?? name;
|
|
338
|
+
const own2 = radioValueOf(el);
|
|
339
|
+
el.checked = own2 !== void 0 && read() === own2;
|
|
340
|
+
});
|
|
341
|
+
};
|
|
342
|
+
} else if (isCheckbox) {
|
|
343
|
+
Object.defineProperty(result, "checked", {
|
|
344
|
+
get() {
|
|
345
|
+
return !!read();
|
|
346
|
+
},
|
|
347
|
+
enumerable: true
|
|
348
|
+
});
|
|
349
|
+
} else {
|
|
350
|
+
Object.defineProperty(result, "value", {
|
|
351
|
+
get: read,
|
|
352
|
+
enumerable: true
|
|
353
|
+
});
|
|
354
|
+
Object.defineProperty(result, "oninput", { value: handler });
|
|
355
|
+
result.ref = (el) => {
|
|
356
|
+
applyRef(options2.ref, el);
|
|
357
|
+
if ((process.env.NODE_ENV !== "production") && !warnedUntypedRadio && el && el.type === "radio") {
|
|
358
|
+
warnedUntypedRadio = true;
|
|
359
|
+
report("ERR_FORM_RADIO_UNTYPED", { data: { name } });
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
return _markProps(result);
|
|
364
|
+
}
|
|
365
|
+
function validateOn(event, key) {
|
|
366
|
+
const name = nameOf(key);
|
|
367
|
+
if (name != null && (mode === event || isSubmitted.peek() && reValidateMode === event)) validate(name);
|
|
368
|
+
}
|
|
369
|
+
function setKey(key, value, shouldDirty = true) {
|
|
370
|
+
if (arrays[key]) ext.a(key, value);
|
|
371
|
+
else getFieldSignal(key).set(value);
|
|
372
|
+
if (shouldDirty) isDirty.set(true);
|
|
373
|
+
}
|
|
374
|
+
function setValue(name, value, options2 = {}) {
|
|
375
|
+
setKey(keyOf(name), value, options2.shouldDirty ?? true);
|
|
376
|
+
if (options2.shouldValidate) validate(name);
|
|
377
|
+
}
|
|
378
|
+
function getValue(name) {
|
|
379
|
+
return readName(keyOf(name));
|
|
380
|
+
}
|
|
381
|
+
function clearError(name) {
|
|
382
|
+
setFieldError(keyOf(name), null);
|
|
383
|
+
}
|
|
384
|
+
function clearErrors() {
|
|
385
|
+
replaceAllErrors({});
|
|
386
|
+
}
|
|
387
|
+
function reset(newValues) {
|
|
388
|
+
if (newValues !== void 0) defaultValues = newValues;
|
|
389
|
+
batch(() => {
|
|
390
|
+
ext?.r(defaultValues);
|
|
391
|
+
for (const key in fieldSignals) {
|
|
392
|
+
if (nameOf(key) != null) fieldSignals[key].set(defaultOf(key));
|
|
393
|
+
else {
|
|
394
|
+
delete fieldSignals[key];
|
|
395
|
+
delete known[key];
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
for (const key in hidden) delete hidden[key];
|
|
399
|
+
counted();
|
|
400
|
+
replaceAllErrors({});
|
|
401
|
+
for (const key in touchedSignals) touchedSignals[key].set(false);
|
|
402
|
+
isDirty.set(false);
|
|
403
|
+
isSubmitted.set(false);
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
function handleSubmit(onValid, onInvalid) {
|
|
407
|
+
return async (e) => {
|
|
408
|
+
if (e) e.preventDefault();
|
|
409
|
+
isSubmitting.set(true);
|
|
410
|
+
isSubmitted.set(true);
|
|
411
|
+
submitCount.set((n) => n + 1);
|
|
412
|
+
try {
|
|
413
|
+
const isFormValid = await validate();
|
|
414
|
+
if (isFormValid) {
|
|
415
|
+
await onValid(untrack(getAllValues));
|
|
416
|
+
} else if (onInvalid) {
|
|
417
|
+
onInvalid(untrack(getAllErrors));
|
|
418
|
+
}
|
|
419
|
+
} finally {
|
|
420
|
+
isSubmitting.set(false);
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
function watch(name) {
|
|
425
|
+
if (name) {
|
|
426
|
+
const key = keyOf(name);
|
|
427
|
+
return computed(() => readName(key));
|
|
428
|
+
}
|
|
429
|
+
return computed(getAllValues);
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
register,
|
|
433
|
+
unregister,
|
|
434
|
+
handleSubmit,
|
|
435
|
+
setValue,
|
|
436
|
+
getValue,
|
|
437
|
+
// Set error for a field
|
|
438
|
+
setError: (name, error) => setFieldError(keyOf(name), error),
|
|
439
|
+
clearError,
|
|
440
|
+
clearErrors,
|
|
441
|
+
reset,
|
|
442
|
+
watch,
|
|
443
|
+
validate,
|
|
444
|
+
// What useFieldArray works with: `x(make)` installs the field array
|
|
445
|
+
// support make() builds over this form's state, once.
|
|
446
|
+
control: {
|
|
447
|
+
x: (make) => ext ||= { k: keyOf, p: nameOf, d: defaultAt } = make(arrays, forget, getAllValues, isDirty, errorSignals, errorsState)
|
|
448
|
+
},
|
|
449
|
+
// Form state
|
|
450
|
+
formState: {
|
|
451
|
+
get values() {
|
|
452
|
+
return getAllValues();
|
|
453
|
+
},
|
|
454
|
+
get errors() {
|
|
455
|
+
return getAllErrors();
|
|
456
|
+
},
|
|
457
|
+
error: (name) => getErrorSignal(keyOf(name))(),
|
|
458
|
+
// The fields touched, by name.
|
|
459
|
+
get touched() {
|
|
460
|
+
fieldCount();
|
|
461
|
+
const out = {};
|
|
462
|
+
for (const key in touchedSignals) {
|
|
463
|
+
if (!touchedSignals[key]()) continue;
|
|
464
|
+
const name = nameOf(key);
|
|
465
|
+
if (name != null) out[name] = true;
|
|
466
|
+
}
|
|
467
|
+
return out;
|
|
468
|
+
},
|
|
469
|
+
isDirty: () => isDirty(),
|
|
470
|
+
isValid,
|
|
471
|
+
isValidating: () => isValidating(),
|
|
472
|
+
isSubmitting: () => isSubmitting(),
|
|
473
|
+
isSubmitted: () => isSubmitted(),
|
|
474
|
+
submitCount: () => submitCount(),
|
|
475
|
+
dirtyFields
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
var itemCount = 0;
|
|
480
|
+
function fieldArrays(arrays, forget, getAllValues, isDirty, errorSignals, errorsState) {
|
|
481
|
+
const bases = {};
|
|
482
|
+
const add = (value) => {
|
|
483
|
+
const segment = "\0" + (++itemCount).toString(36);
|
|
484
|
+
bases[segment] = value ?? {};
|
|
485
|
+
return segment;
|
|
486
|
+
};
|
|
487
|
+
const key = (name, items) => {
|
|
488
|
+
if (!name.includes(".")) return name;
|
|
489
|
+
let out = "";
|
|
490
|
+
for (const segment of name.split(".")) {
|
|
491
|
+
const list = out && (items ? items[out] : arrays[out]?.());
|
|
492
|
+
out += (out ? "." : "") + (list && isIndex(segment) && list[segment] !== void 0 ? list[segment] : segment);
|
|
493
|
+
}
|
|
494
|
+
return out;
|
|
495
|
+
};
|
|
496
|
+
const placeMaps = /* @__PURE__ */ new WeakMap();
|
|
497
|
+
const places = (k) => {
|
|
498
|
+
const list = arrays[k]?.();
|
|
499
|
+
if (list === void 0) return void 0;
|
|
500
|
+
let map2 = placeMaps.get(list);
|
|
501
|
+
if (map2 === void 0) placeMaps.set(list, map2 = new Map(list.map((segment, i) => [segment, i])));
|
|
502
|
+
return map2;
|
|
503
|
+
};
|
|
504
|
+
let names = /* @__PURE__ */ new Map();
|
|
505
|
+
const nameAt = (k) => {
|
|
506
|
+
if (!k.includes("\0")) return k;
|
|
507
|
+
let hit = names.get(k);
|
|
508
|
+
if (hit !== void 0) {
|
|
509
|
+
for (let i = 1; i < hit.length; i++) arrays[hit[i]]?.();
|
|
510
|
+
return hit[0];
|
|
511
|
+
}
|
|
512
|
+
names.set(k, hit = [null]);
|
|
513
|
+
let at = "";
|
|
514
|
+
let out = "";
|
|
515
|
+
for (const segment of k.split(".")) {
|
|
516
|
+
let part = segment;
|
|
517
|
+
if (segment[0] === "\0") {
|
|
518
|
+
hit.push(at);
|
|
519
|
+
part = places(at)?.get(segment);
|
|
520
|
+
if (part === void 0) return null;
|
|
521
|
+
}
|
|
522
|
+
at += (at ? "." : "") + segment;
|
|
523
|
+
out += (out ? "." : "") + part;
|
|
524
|
+
}
|
|
525
|
+
return hit[0] = out;
|
|
526
|
+
};
|
|
527
|
+
const drop = (k, segments) => {
|
|
528
|
+
if (!segments.length) return;
|
|
529
|
+
const removed = new Set(segments);
|
|
530
|
+
const gone = (key2) => key2.startsWith(k + ".") && removed.has(key2.slice(k.length + 1).split(".", 1)[0]);
|
|
531
|
+
const errors = { ...errorsState.peek() };
|
|
532
|
+
let cleared = false;
|
|
533
|
+
for (const key2 in errorSignals) {
|
|
534
|
+
if (!gone(key2)) continue;
|
|
535
|
+
errorSignals[key2].set(null);
|
|
536
|
+
delete errorSignals[key2];
|
|
537
|
+
if (Object.hasOwn(errors, key2)) cleared = delete errors[key2];
|
|
538
|
+
}
|
|
539
|
+
if (cleared) errorsState.set(errors);
|
|
540
|
+
forget(k + "." + segments[0], { keepError: true }, gone);
|
|
541
|
+
for (const segment of segments) delete bases[segment];
|
|
542
|
+
for (const inner in arrays) {
|
|
543
|
+
if (!gone(inner)) continue;
|
|
544
|
+
for (const segment of arrays[inner].peek()) delete bases[segment];
|
|
545
|
+
delete arrays[inner];
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
const seed = (k, items) => {
|
|
549
|
+
names = /* @__PURE__ */ new Map();
|
|
550
|
+
if (arrays[k]) return arrays[k].set(items.map(add));
|
|
551
|
+
arrays[k] = signal(items.map(add));
|
|
552
|
+
};
|
|
553
|
+
const lastItem = (segments) => {
|
|
554
|
+
let at = segments.length;
|
|
555
|
+
while (at-- > 0 && segments[at][0] !== "\0") ;
|
|
556
|
+
return at;
|
|
557
|
+
};
|
|
558
|
+
return {
|
|
559
|
+
k: key,
|
|
560
|
+
p: nameAt,
|
|
561
|
+
// A key's default: under an item, from the item's values.
|
|
562
|
+
d(defaults, k) {
|
|
563
|
+
const segments = k.split(".");
|
|
564
|
+
const at = lastItem(segments);
|
|
565
|
+
return segments.slice(at + 1).reduce(own, at < 0 ? defaults : bases[segments[at]]);
|
|
566
|
+
},
|
|
567
|
+
// Forget the default of a key under an item, in the item's values.
|
|
568
|
+
u(k) {
|
|
569
|
+
const segments = k.split(".");
|
|
570
|
+
const at = lastItem(segments);
|
|
571
|
+
if (at < 0) return false;
|
|
572
|
+
if (!(segments[at] in bases)) return true;
|
|
573
|
+
if (at === segments.length - 1) delete bases[segments[at]];
|
|
574
|
+
else bases[segments[at]] = unsetIn(bases[segments[at]], segments.slice(at + 1));
|
|
575
|
+
return true;
|
|
576
|
+
},
|
|
577
|
+
// Each array's items, outer arrays first, replace what the defaults hold.
|
|
578
|
+
v(result) {
|
|
579
|
+
for (const k of Object.keys(arrays).sort((a, b) => a.length - b.length)) {
|
|
580
|
+
const name = nameAt(k);
|
|
581
|
+
if (name != null) setIn(result, name, arrays[k]().map((segment) => clone(bases[segment])));
|
|
582
|
+
}
|
|
583
|
+
},
|
|
584
|
+
// Errors by name to errors by key, with the items in their places now;
|
|
585
|
+
// an error of an item removed by the time it lands goes.
|
|
586
|
+
s() {
|
|
587
|
+
const items = map();
|
|
588
|
+
for (const k in arrays) items[k] = arrays[k].peek();
|
|
589
|
+
return (errors) => {
|
|
590
|
+
const out = {};
|
|
591
|
+
for (const name in errors) {
|
|
592
|
+
const k = key(name, items);
|
|
593
|
+
if (nameAt(k) != null) out[k] = errors[name];
|
|
594
|
+
}
|
|
595
|
+
return out;
|
|
596
|
+
};
|
|
597
|
+
},
|
|
598
|
+
e(errors) {
|
|
599
|
+
const out = {};
|
|
600
|
+
for (const k in errors) {
|
|
601
|
+
const name = nameAt(k);
|
|
602
|
+
if (name != null) out[name] = errors[k];
|
|
603
|
+
}
|
|
604
|
+
return out;
|
|
605
|
+
},
|
|
606
|
+
// An array's items replaced by new ones with these values.
|
|
607
|
+
a(k, values) {
|
|
608
|
+
const old = arrays[k].peek();
|
|
609
|
+
batch(() => {
|
|
610
|
+
seed(k, values || []);
|
|
611
|
+
drop(k, old);
|
|
612
|
+
});
|
|
613
|
+
},
|
|
614
|
+
// A reset gives each array its items from the defaults, as new items;
|
|
615
|
+
// an array inside an item goes with the item.
|
|
616
|
+
r(defaults) {
|
|
617
|
+
for (const k in arrays) {
|
|
618
|
+
for (const segment of arrays[k].peek()) delete bases[segment];
|
|
619
|
+
if (!k.includes("\0")) seed(k, getIn(defaults, k) || []);
|
|
620
|
+
else delete arrays[k];
|
|
621
|
+
}
|
|
622
|
+
},
|
|
623
|
+
// One field array's controls.
|
|
624
|
+
array(name) {
|
|
625
|
+
const k = key(name);
|
|
626
|
+
if (!arrays[k]) seed(k, getIn(getAllValues(), name) || []);
|
|
627
|
+
const items = arrays[k];
|
|
628
|
+
let shown = {};
|
|
629
|
+
const change = (next, removed = []) => batch(() => {
|
|
630
|
+
names = /* @__PURE__ */ new Map();
|
|
631
|
+
items.set(next);
|
|
632
|
+
drop(k, removed);
|
|
633
|
+
isDirty.set(true);
|
|
634
|
+
});
|
|
635
|
+
const many = (value) => (Array.isArray(value) ? value : [value]).map(add);
|
|
636
|
+
const insert = (index, value) => {
|
|
637
|
+
const next = [...items.peek()];
|
|
638
|
+
next.splice(index, 0, ...many(value));
|
|
639
|
+
change(next);
|
|
640
|
+
};
|
|
641
|
+
return {
|
|
642
|
+
fields: computed(() => {
|
|
643
|
+
const now = {};
|
|
644
|
+
const list = items().map((segment) => now[segment] = shown[segment] || { ...bases[segment], id: segment.slice(1) });
|
|
645
|
+
shown = now;
|
|
646
|
+
return list;
|
|
647
|
+
}),
|
|
648
|
+
append: (value) => insert(items.peek().length, value),
|
|
649
|
+
prepend: (value) => insert(0, value),
|
|
650
|
+
insert,
|
|
651
|
+
remove(index) {
|
|
652
|
+
const list = items.peek();
|
|
653
|
+
const gone = index === void 0 ? list : [].concat(index).map((i) => list[i]);
|
|
654
|
+
change(list.filter((segment) => !gone.includes(segment)), gone);
|
|
655
|
+
},
|
|
656
|
+
move(from, to) {
|
|
657
|
+
const next = [...items.peek()];
|
|
658
|
+
next.splice(to, 0, next.splice(from, 1)[0]);
|
|
659
|
+
change(next);
|
|
660
|
+
},
|
|
661
|
+
swap(a, b) {
|
|
662
|
+
const next = [...items.peek()];
|
|
663
|
+
[next[a], next[b]] = [next[b], next[a]];
|
|
664
|
+
change(next);
|
|
665
|
+
},
|
|
666
|
+
// The item at `index` becomes a new one with these values, as React
|
|
667
|
+
// Hook Form's update remounts it.
|
|
668
|
+
update(index, value) {
|
|
669
|
+
const next = [...items.peek()];
|
|
670
|
+
const old = next[index];
|
|
671
|
+
next[index] = add(value);
|
|
672
|
+
change(next, [old]);
|
|
673
|
+
},
|
|
674
|
+
replace: (values) => change(many(values), items.peek())
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
function useFieldArray({ control, form, name }) {
|
|
680
|
+
return hookStable(() => untrack(() => (control || form.control).x(fieldArrays).array(name)));
|
|
681
|
+
}
|
|
682
|
+
function _zodIssues(err) {
|
|
683
|
+
if (!err || typeof err !== "object") return null;
|
|
684
|
+
if (Array.isArray(err.issues)) return err.issues;
|
|
685
|
+
if (Array.isArray(err.errors)) return err.errors;
|
|
686
|
+
return null;
|
|
687
|
+
}
|
|
688
|
+
function _issuePath(issue) {
|
|
689
|
+
const path = issue?.path;
|
|
690
|
+
if (path == null) return "";
|
|
691
|
+
if (!Array.isArray(path)) return String(path);
|
|
692
|
+
let out = "";
|
|
693
|
+
for (let i = 0; i < path.length; i++) {
|
|
694
|
+
const segment = path[i];
|
|
695
|
+
out += (i ? "." : "") + (typeof segment === "symbol" ? segment.toString() : String(segment));
|
|
696
|
+
}
|
|
697
|
+
return out;
|
|
698
|
+
}
|
|
699
|
+
function zodResolver(schema) {
|
|
700
|
+
return async (values) => {
|
|
701
|
+
try {
|
|
702
|
+
const result = await schema.parseAsync(values);
|
|
703
|
+
return { values: result, errors: {} };
|
|
704
|
+
} catch (e) {
|
|
705
|
+
const issues = _zodIssues(e);
|
|
706
|
+
if (!issues) throw e;
|
|
707
|
+
const errors = {};
|
|
708
|
+
for (const issue of issues) {
|
|
709
|
+
const path = _issuePath(issue);
|
|
710
|
+
if (!own(errors, path)) {
|
|
711
|
+
errors[path] = { type: issue.code ?? "validation", message: issue.message };
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
if (Object.keys(errors).length === 0) {
|
|
715
|
+
errors[""] = { type: "validation", message: e?.message || "Validation failed" };
|
|
716
|
+
}
|
|
717
|
+
return { values: {}, errors };
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
function _isYupValidationError(err) {
|
|
722
|
+
if (!err || typeof err !== "object") return false;
|
|
723
|
+
return err.name === "ValidationError" || Array.isArray(err.inner);
|
|
724
|
+
}
|
|
725
|
+
function yupResolver(schema) {
|
|
726
|
+
return async (values) => {
|
|
727
|
+
try {
|
|
728
|
+
const result = await schema.validate(values, { abortEarly: false });
|
|
729
|
+
return { values: result, errors: {} };
|
|
730
|
+
} catch (e) {
|
|
731
|
+
if (!_isYupValidationError(e)) throw e;
|
|
732
|
+
const errors = {};
|
|
733
|
+
const inner = Array.isArray(e.inner) && e.inner.length > 0 ? e.inner : [e];
|
|
734
|
+
for (const err of inner) {
|
|
735
|
+
const path = err.path ?? "";
|
|
736
|
+
if (!own(errors, path)) {
|
|
737
|
+
errors[path] = { type: err.type ?? "validation", message: err.message };
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
return { values: {}, errors };
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
function simpleResolver(rules2) {
|
|
745
|
+
return async (values, _context, options) => {
|
|
746
|
+
const errors = {};
|
|
747
|
+
const names = options?.names;
|
|
748
|
+
for (const [field, fieldRules] of Object.entries(rules2)) {
|
|
749
|
+
if (names && !names.includes(field)) continue;
|
|
750
|
+
const value = getIn(values, field);
|
|
751
|
+
for (const rule of fieldRules) {
|
|
752
|
+
const error = rule(value, values);
|
|
753
|
+
if (error) {
|
|
754
|
+
errors[field] = { type: "validation", message: error };
|
|
755
|
+
break;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
return { values, errors };
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
function numberOf(value) {
|
|
763
|
+
if (typeof value === "number") return value;
|
|
764
|
+
if (typeof value !== "string" || value.trim() === "") return void 0;
|
|
765
|
+
const n = Number(value);
|
|
766
|
+
return Number.isNaN(n) ? void 0 : n;
|
|
767
|
+
}
|
|
768
|
+
var rules = {
|
|
769
|
+
required: (message = "This field is required") => (value) => {
|
|
770
|
+
if (value === void 0 || value === null || value === "") {
|
|
771
|
+
return message;
|
|
772
|
+
}
|
|
773
|
+
},
|
|
774
|
+
minLength: (min, message) => (value) => {
|
|
775
|
+
if (typeof value === "string" && value.length < min) {
|
|
776
|
+
return message || `Must be at least ${min} characters`;
|
|
777
|
+
}
|
|
778
|
+
},
|
|
779
|
+
maxLength: (max, message) => (value) => {
|
|
780
|
+
if (typeof value === "string" && value.length > max) {
|
|
781
|
+
return message || `Must be at most ${max} characters`;
|
|
782
|
+
}
|
|
783
|
+
},
|
|
784
|
+
min: (min, message) => (value) => {
|
|
785
|
+
const n = numberOf(value);
|
|
786
|
+
if (n !== void 0 && n < min) {
|
|
787
|
+
return message || `Must be at least ${min}`;
|
|
788
|
+
}
|
|
789
|
+
},
|
|
790
|
+
max: (max, message) => (value) => {
|
|
791
|
+
const n = numberOf(value);
|
|
792
|
+
if (n !== void 0 && n > max) {
|
|
793
|
+
return message || `Must be at most ${max}`;
|
|
794
|
+
}
|
|
795
|
+
},
|
|
796
|
+
pattern: (regex, message = "Invalid format") => (value) => {
|
|
797
|
+
if (typeof value === "string" && !regex.test(value)) {
|
|
798
|
+
return message;
|
|
799
|
+
}
|
|
800
|
+
},
|
|
801
|
+
email: (message = "Invalid email address") => (value) => {
|
|
802
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
803
|
+
if (typeof value === "string" && !emailRegex.test(value)) {
|
|
804
|
+
return message;
|
|
805
|
+
}
|
|
806
|
+
},
|
|
807
|
+
url: (message = "Invalid URL") => (value) => {
|
|
808
|
+
try {
|
|
809
|
+
if (typeof value === "string" && value) {
|
|
810
|
+
new URL(value);
|
|
811
|
+
}
|
|
812
|
+
} catch {
|
|
813
|
+
return message;
|
|
814
|
+
}
|
|
815
|
+
},
|
|
816
|
+
match: (field, message) => (value, values) => {
|
|
817
|
+
if (value !== getIn(values, field)) {
|
|
818
|
+
return message || `Must match ${field}`;
|
|
819
|
+
}
|
|
820
|
+
},
|
|
821
|
+
custom: (validator) => validator
|
|
822
|
+
};
|
|
823
|
+
function useField(name, options = {}) {
|
|
824
|
+
const { validate: validateFn, defaultValue = "" } = options;
|
|
825
|
+
const value = signal(defaultValue);
|
|
826
|
+
const error = signal(null);
|
|
827
|
+
const isTouched = signal(false);
|
|
828
|
+
const isDirty = signal(false);
|
|
829
|
+
async function validate() {
|
|
830
|
+
if (!validateFn) return true;
|
|
831
|
+
const result = await validateFn(value.peek());
|
|
832
|
+
error.set(result || null);
|
|
833
|
+
return !result;
|
|
834
|
+
}
|
|
835
|
+
return {
|
|
836
|
+
name,
|
|
837
|
+
value: () => value(),
|
|
838
|
+
error: () => error(),
|
|
839
|
+
isTouched: () => isTouched(),
|
|
840
|
+
isDirty: () => isDirty(),
|
|
841
|
+
setValue: (v) => {
|
|
842
|
+
value.set(v);
|
|
843
|
+
isDirty.set(true);
|
|
844
|
+
},
|
|
845
|
+
setError: (e) => error.set(e),
|
|
846
|
+
validate,
|
|
847
|
+
reset: () => {
|
|
848
|
+
value.set(defaultValue);
|
|
849
|
+
error.set(null);
|
|
850
|
+
isTouched.set(false);
|
|
851
|
+
isDirty.set(false);
|
|
852
|
+
},
|
|
853
|
+
inputProps: () => ({
|
|
854
|
+
name,
|
|
855
|
+
value: value(),
|
|
856
|
+
onInput: (e) => {
|
|
857
|
+
value.set(e.target.value);
|
|
858
|
+
isDirty.set(true);
|
|
859
|
+
},
|
|
860
|
+
onBlur: () => {
|
|
861
|
+
isTouched.set(true);
|
|
862
|
+
validate();
|
|
863
|
+
}
|
|
864
|
+
})
|
|
865
|
+
};
|
|
866
|
+
}
|
|
867
|
+
function Input(props) {
|
|
868
|
+
const { register, error, ...rest } = props;
|
|
869
|
+
const registered = register ? register(props.name) : {};
|
|
870
|
+
return h("input", {
|
|
871
|
+
..._mergeRegistration(rest, registered),
|
|
872
|
+
"aria-invalid": error ? "true" : void 0
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
function Textarea(props) {
|
|
876
|
+
const { register, error, ...rest } = props;
|
|
877
|
+
const registered = register ? register(props.name) : {};
|
|
878
|
+
return h("textarea", {
|
|
879
|
+
..._mergeRegistration(rest, registered),
|
|
880
|
+
"aria-invalid": error ? "true" : void 0
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
var Select = /* @__PURE__ */ Object.assign(function Select2(props) {
|
|
884
|
+
const { register, error, children, ...rest } = props;
|
|
885
|
+
const registered = register ? register(props.name) : {};
|
|
886
|
+
return h("select", {
|
|
887
|
+
..._mergeRegistration(rest, registered),
|
|
888
|
+
"aria-invalid": error ? "true" : void 0
|
|
889
|
+
}, children);
|
|
890
|
+
}, { _deferChildren: true });
|
|
891
|
+
function Checkbox(props) {
|
|
892
|
+
const { register, ...rest } = props;
|
|
893
|
+
if (!register) return h("input", { type: "checkbox", ...rest });
|
|
894
|
+
return h("input", { type: "checkbox", ..._mergeRegistration(rest, register(props.name, { type: "checkbox" })) });
|
|
895
|
+
}
|
|
896
|
+
function Radio(props) {
|
|
897
|
+
const { register, value: radioValue, ...rest } = props;
|
|
898
|
+
if (radioValue === void 0) {
|
|
899
|
+
if ((process.env.NODE_ENV !== "production")) report("ERR_FORM_RADIO_WITHOUT_VALUE", { data: { name: props.name } });
|
|
900
|
+
return h("input", { type: "radio", ...rest });
|
|
901
|
+
}
|
|
902
|
+
const registered = register ? register(props.name, { type: "radio", value: radioValue, ref: rest.ref }) : {};
|
|
903
|
+
return h("input", _mergeRegistration({
|
|
904
|
+
type: "radio",
|
|
905
|
+
value: radioValue,
|
|
906
|
+
...rest
|
|
907
|
+
}, registered));
|
|
908
|
+
}
|
|
909
|
+
function ErrorMessage({ name, formState, errors, render }) {
|
|
910
|
+
return () => {
|
|
911
|
+
const fieldError = formState && typeof formState.error === "function" ? formState.error(name) : own(
|
|
912
|
+
formState?.errors != null ? formState.errors : typeof errors === "function" ? errors() : errors,
|
|
913
|
+
name
|
|
914
|
+
) || null;
|
|
915
|
+
if (!fieldError) return null;
|
|
916
|
+
if (render) {
|
|
917
|
+
return render({ message: fieldError.message, type: fieldError.type });
|
|
918
|
+
}
|
|
919
|
+
return h("span", { class: "what-error", role: "alert" }, fieldError.message);
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
export {
|
|
923
|
+
Checkbox,
|
|
924
|
+
ErrorMessage,
|
|
925
|
+
Input,
|
|
926
|
+
Radio,
|
|
927
|
+
Select,
|
|
928
|
+
Textarea,
|
|
929
|
+
rules,
|
|
930
|
+
simpleResolver,
|
|
931
|
+
useField,
|
|
932
|
+
useFieldArray,
|
|
933
|
+
useForm,
|
|
934
|
+
yupResolver,
|
|
935
|
+
zodResolver
|
|
936
|
+
};
|