clarusjs 0.8.1 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/check/generate.d.ts +20 -4
- package/dist/check/generate.js +157 -21
- package/dist/cli/check.d.ts +13 -0
- package/dist/cli/check.js +65 -18
- package/dist/cli/index.js +9 -3
- package/dist/cli/mapper.d.ts +25 -0
- package/dist/cli/mapper.js +65 -0
- package/dist/cli/project.d.ts +5 -0
- package/dist/cli/project.js +21 -0
- package/dist/compiler/emit-client.js +33 -9
- package/dist/compiler/emit-server.js +3 -2
- package/dist/compiler/errors.d.ts +14 -1
- package/dist/compiler/html-expr.d.ts +21 -0
- package/dist/compiler/html-expr.js +29 -3
- package/dist/compiler/index.d.ts +28 -3
- package/dist/compiler/index.js +36 -24
- package/dist/compiler/ir.d.ts +19 -1
- package/dist/compiler/ir.js +261 -11
- package/dist/compiler/parse.d.ts +52 -1
- package/dist/compiler/parse.js +145 -6
- package/dist/compiler/script.d.ts +22 -1
- package/dist/compiler/script.js +108 -5
- package/dist/fetch/index.d.ts +149 -0
- package/dist/fetch/index.js +312 -0
- package/dist/forms/index.d.ts +26 -0
- package/dist/forms/index.js +374 -0
- package/dist/forms/types.d.ts +111 -0
- package/dist/forms/types.js +1 -0
- package/dist/runtime/attrs.js +10 -2
- package/dist/runtime/dom.d.ts +26 -11
- package/dist/runtime/dom.js +102 -14
- package/dist/runtime/env.d.ts +14 -0
- package/dist/runtime/env.js +14 -0
- package/dist/runtime/html.d.ts +9 -7
- package/dist/runtime/html.js +12 -7
- package/dist/runtime/index.d.ts +3 -2
- package/dist/runtime/index.js +3 -2
- package/dist/runtime/loader.d.ts +1 -1
- package/dist/runtime/loader.js +36 -5
- package/dist/runtime/navigate.d.ts +5 -0
- package/dist/runtime/navigation.d.ts +85 -13
- package/dist/runtime/navigation.js +77 -11
- package/dist/runtime/router.js +10 -1
- package/dist/runtime/serialize.d.ts +1 -1
- package/dist/runtime/serialize.js +1 -1
- package/dist/runtime/server.d.ts +9 -2
- package/dist/runtime/server.js +23 -5
- package/dist/runtime/signals.d.ts +98 -3
- package/dist/runtime/signals.js +112 -6
- package/dist/server/handler.d.ts +14 -1
- package/dist/server/handler.js +2 -2
- package/dist/server/head.d.ts +4 -1
- package/dist/server/manifest.d.ts +14 -1
- package/dist/server/views.d.ts +64 -6
- package/dist/server/views.js +46 -0
- package/dist/vite/build.d.ts +1 -1
- package/dist/vite/manifest.d.ts +12 -0
- package/dist/vite/plugin.d.ts +30 -2
- package/dist/vite/plugin.js +17 -2
- package/dist/vite/sitemap.d.ts +1 -0
- package/package.json +19 -1
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { effect, onDispose, state, untrack } from '../runtime/index.js';
|
|
2
|
+
import { marked } from '../runtime/dom.js';
|
|
3
|
+
import { browser } from '../runtime/env.js';
|
|
4
|
+
const CONTAINERS = 'label, [data-cl-field], fieldset, cl-island';
|
|
5
|
+
const FIELDS = 'input[data-cl-path],select[data-cl-path],textarea[data-cl-path]';
|
|
6
|
+
const FLAGS = ['valueMissing', 'typeMismatch', 'patternMismatch', 'tooLong', 'tooShort', 'rangeUnderflow', 'rangeOverflow', 'stepMismatch', 'badInput'];
|
|
7
|
+
let ids = 0;
|
|
8
|
+
const read = (value, path) => path.split('.').reduce((v, k) => (v == null ? v : v[k]), value);
|
|
9
|
+
const dotted = (path = []) => path.map((p) => String(typeof p === 'object' ? p.key : p)).join('.');
|
|
10
|
+
function pathOf(field, root) {
|
|
11
|
+
const parts = [];
|
|
12
|
+
for (let el = field; el && el !== root.parentElement; el = el.parentElement) {
|
|
13
|
+
const here = el;
|
|
14
|
+
let part = here.getAttribute('data-cl-path');
|
|
15
|
+
if (part?.startsWith('@')) {
|
|
16
|
+
const [model, ...rest] = part.slice(1).split('.');
|
|
17
|
+
const island = here.parentElement?.closest(`[data-cl-path-${model}]`);
|
|
18
|
+
if (!island)
|
|
19
|
+
return '';
|
|
20
|
+
part = [island.getAttribute(`data-cl-path-${model}`), ...rest].filter(Boolean).join('.');
|
|
21
|
+
el = island;
|
|
22
|
+
}
|
|
23
|
+
if (part)
|
|
24
|
+
parts.unshift(part);
|
|
25
|
+
}
|
|
26
|
+
return parts.join('.');
|
|
27
|
+
}
|
|
28
|
+
export function form(values, options = {}) {
|
|
29
|
+
if (!browser || !values || typeof values.get !== 'function')
|
|
30
|
+
return inert();
|
|
31
|
+
const signal = values;
|
|
32
|
+
let initial = untrack(() => signal.get());
|
|
33
|
+
const show = options.show ?? 'blur';
|
|
34
|
+
const version = state(0);
|
|
35
|
+
const pending = state(false);
|
|
36
|
+
let touched = new WeakSet();
|
|
37
|
+
let dirty = new WeakSet();
|
|
38
|
+
let checked = new WeakSet();
|
|
39
|
+
const shownAll = { on: false };
|
|
40
|
+
const forced = new Set();
|
|
41
|
+
let server = {};
|
|
42
|
+
let schemaIssues = {};
|
|
43
|
+
let errorsNow = {};
|
|
44
|
+
const roots = new Map();
|
|
45
|
+
let checkSeq = 0;
|
|
46
|
+
let latest = Promise.resolve();
|
|
47
|
+
let revision = 0;
|
|
48
|
+
let failed = false;
|
|
49
|
+
let timer;
|
|
50
|
+
let last = initial;
|
|
51
|
+
const current = state('');
|
|
52
|
+
const submitting = state(false);
|
|
53
|
+
const passed = new Set();
|
|
54
|
+
const now = () => untrack(() => current.get());
|
|
55
|
+
const boxOf = (el, path, root) => {
|
|
56
|
+
for (let c = el.closest(CONTAINERS); c; c = c.parentElement?.closest(CONTAINERS) ?? null) {
|
|
57
|
+
const box = c.querySelector('[data-cl-message]');
|
|
58
|
+
if (!box)
|
|
59
|
+
continue;
|
|
60
|
+
return [...c.querySelectorAll(FIELDS)].every((x) => x === el || pathOf(x, root) === path) ? box : undefined;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const eventField = (e) => {
|
|
64
|
+
const field = e.target.closest?.(FIELDS);
|
|
65
|
+
return field && [...roots.keys()].some((r) => r.contains(field)) ? field : undefined;
|
|
66
|
+
};
|
|
67
|
+
const fields = () => [...roots.keys()].flatMap((root) => [...root.querySelectorAll(FIELDS)].map((el) => ({ el: el, path: pathOf(el, root), root })));
|
|
68
|
+
const shownField = (el, path) => shownAll.on || forced.has(path) || checked.has(el) || (show === 'input' ? touched.has(el) || dirty.has(el) : show === 'blur' && touched.has(el));
|
|
69
|
+
const shown = (path) => shownAll.on || forced.has(path) || fields().some((f) => f.path === path && shownField(f.el, path));
|
|
70
|
+
function refresh() {
|
|
71
|
+
const next = {};
|
|
72
|
+
for (const { el, path } of fields()) {
|
|
73
|
+
el.setCustomValidity('');
|
|
74
|
+
let native = '';
|
|
75
|
+
if (!el.validity.valid) {
|
|
76
|
+
const flag = FLAGS.find((f) => el.validity[f]);
|
|
77
|
+
native = options.messages?.[path] ?? (flag && options.messages?.[flag]) ?? el.validationMessage;
|
|
78
|
+
}
|
|
79
|
+
const message = server[path] || schemaIssues[path] || native || next[path] || '';
|
|
80
|
+
if (message)
|
|
81
|
+
next[path] = message;
|
|
82
|
+
if (server[path] || schemaIssues[path])
|
|
83
|
+
el.setCustomValidity(message);
|
|
84
|
+
}
|
|
85
|
+
for (const path of new Set([...Object.keys(schemaIssues), ...Object.keys(server)]))
|
|
86
|
+
next[path] ??= server[path] || schemaIssues[path];
|
|
87
|
+
if (failed)
|
|
88
|
+
next[''] ??= options.messages?.schema ?? "The form couldn't be checked. Try again.";
|
|
89
|
+
errorsNow = next;
|
|
90
|
+
for (const { el, path, root } of fields())
|
|
91
|
+
paint(el, boxOf(el, path, root), shown(path) ? errorsNow[path] ?? '' : '');
|
|
92
|
+
version.set(untrack(() => version.get()) + 1);
|
|
93
|
+
}
|
|
94
|
+
function paint(el, box, message) {
|
|
95
|
+
if (box) {
|
|
96
|
+
if (!box.id)
|
|
97
|
+
box.id = `cl-msg-${++ids}`;
|
|
98
|
+
const described = (el.getAttribute('aria-describedby') ?? '').split(/\s+/).filter(Boolean);
|
|
99
|
+
if (!described.includes(box.id))
|
|
100
|
+
el.setAttribute('aria-describedby', [...described, box.id].join(' '));
|
|
101
|
+
box.textContent = message;
|
|
102
|
+
box.hidden = !message;
|
|
103
|
+
}
|
|
104
|
+
if (message)
|
|
105
|
+
el.setAttribute('aria-invalid', 'true');
|
|
106
|
+
else
|
|
107
|
+
el.removeAttribute('aria-invalid');
|
|
108
|
+
}
|
|
109
|
+
function check() {
|
|
110
|
+
const schema = options.schema;
|
|
111
|
+
if (!schema)
|
|
112
|
+
return (latest = Promise.resolve());
|
|
113
|
+
const seq = ++checkSeq;
|
|
114
|
+
const apply = (result) => {
|
|
115
|
+
if (seq !== checkSeq)
|
|
116
|
+
return;
|
|
117
|
+
schemaIssues = {};
|
|
118
|
+
failed = false;
|
|
119
|
+
for (const issue of result.issues ?? []) {
|
|
120
|
+
const path = dotted(issue.path);
|
|
121
|
+
schemaIssues[path] ??= issue.message;
|
|
122
|
+
}
|
|
123
|
+
pending.set(false);
|
|
124
|
+
refresh();
|
|
125
|
+
};
|
|
126
|
+
const result = schema['~standard'].validate(untrack(() => signal.get()));
|
|
127
|
+
if (!(result instanceof Promise))
|
|
128
|
+
return (latest = Promise.resolve(apply(result)));
|
|
129
|
+
pending.set(true);
|
|
130
|
+
return (latest = result.then(apply, (err) => {
|
|
131
|
+
if (seq === checkSeq) {
|
|
132
|
+
pending.set(false);
|
|
133
|
+
failed = true;
|
|
134
|
+
refresh();
|
|
135
|
+
}
|
|
136
|
+
console.error('[clarusjs] the form schema failed', err);
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
async function settle() {
|
|
140
|
+
for (;;) {
|
|
141
|
+
clearTimeout(timer);
|
|
142
|
+
const start = revision;
|
|
143
|
+
const p = check();
|
|
144
|
+
await p;
|
|
145
|
+
if (p === latest && revision === start)
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const steps = () => [...roots.keys()].flatMap((root) => [...root.querySelectorAll('[data-cl-step][id]')]);
|
|
150
|
+
function showStep(id, push) {
|
|
151
|
+
current.set(id);
|
|
152
|
+
for (const step of steps()) {
|
|
153
|
+
step.hidden = step.id !== id;
|
|
154
|
+
step.toggleAttribute('data-cl-current', step.id === id);
|
|
155
|
+
}
|
|
156
|
+
if (push && location.hash !== `#${id}`)
|
|
157
|
+
history.pushState(null, '', `#${id}`);
|
|
158
|
+
refresh();
|
|
159
|
+
}
|
|
160
|
+
const hashStep = () => {
|
|
161
|
+
let id = '';
|
|
162
|
+
try {
|
|
163
|
+
id = decodeURIComponent(location.hash.slice(1));
|
|
164
|
+
}
|
|
165
|
+
catch { }
|
|
166
|
+
return steps().find((s) => s.id === id)?.id;
|
|
167
|
+
};
|
|
168
|
+
const onHash = () => {
|
|
169
|
+
const id = hashStep() ?? (location.hash.length > 1 ? undefined : steps()[0]?.id);
|
|
170
|
+
if (id && id !== now())
|
|
171
|
+
showStep(id, false);
|
|
172
|
+
};
|
|
173
|
+
const go = (id) => {
|
|
174
|
+
showStep(id, true);
|
|
175
|
+
const step = steps().find((s) => s.id === id);
|
|
176
|
+
step?.querySelector('input, select, textarea, button, [tabindex]')?.focus();
|
|
177
|
+
};
|
|
178
|
+
const errorsIn = (step) => new Set(fields().filter((f) => step.contains(f.el) && shown(f.path) && errorsNow[f.path]).map((f) => f.path)).size;
|
|
179
|
+
const checkSoon = () => {
|
|
180
|
+
clearTimeout(timer);
|
|
181
|
+
timer = setTimeout(check, 300);
|
|
182
|
+
};
|
|
183
|
+
effect(() => {
|
|
184
|
+
const value = signal.get();
|
|
185
|
+
untrack(() => {
|
|
186
|
+
for (const path of Object.keys(server))
|
|
187
|
+
if (read(value, path) !== read(last, path))
|
|
188
|
+
delete server[path];
|
|
189
|
+
last = value;
|
|
190
|
+
revision++;
|
|
191
|
+
if (roots.size) {
|
|
192
|
+
refresh();
|
|
193
|
+
checkSoon();
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
const onInput = (e) => {
|
|
198
|
+
const field = eventField(e);
|
|
199
|
+
if (!field)
|
|
200
|
+
return;
|
|
201
|
+
dirty.add(field);
|
|
202
|
+
revision++;
|
|
203
|
+
refresh();
|
|
204
|
+
checkSoon();
|
|
205
|
+
};
|
|
206
|
+
const onLeave = (e) => {
|
|
207
|
+
const field = eventField(e);
|
|
208
|
+
if (!field)
|
|
209
|
+
return;
|
|
210
|
+
touched.add(field);
|
|
211
|
+
refresh();
|
|
212
|
+
check();
|
|
213
|
+
};
|
|
214
|
+
const onSubmit = (e) => { e.preventDefault(); void engine.submit(); };
|
|
215
|
+
const listeners = [['input', onInput], ['change', onInput], ['focusout', onLeave], ['submit', onSubmit]];
|
|
216
|
+
const toFirstError = () => {
|
|
217
|
+
const first = fields().find((f) => errorsNow[f.path]);
|
|
218
|
+
if (first)
|
|
219
|
+
engine.focus(first.path);
|
|
220
|
+
return !Object.keys(errorsNow).length;
|
|
221
|
+
};
|
|
222
|
+
const urlEvents = ['popstate', 'hashchange'];
|
|
223
|
+
const engine = {
|
|
224
|
+
error(path) { version.get(); return shown(path) ? errorsNow[path] ?? '' : ''; },
|
|
225
|
+
get errors() {
|
|
226
|
+
version.get();
|
|
227
|
+
const out = [];
|
|
228
|
+
const all = fields();
|
|
229
|
+
for (const { el, path, root } of all) {
|
|
230
|
+
if (out.some((e) => e.path === path) || !shown(path) || !errorsNow[path])
|
|
231
|
+
continue;
|
|
232
|
+
out.push({ path, message: errorsNow[path], id: boxOf(el, path, root)?.id || el.id, step: el.closest('[data-cl-step]')?.id ?? null });
|
|
233
|
+
}
|
|
234
|
+
for (const [path, message] of Object.entries(errorsNow)) {
|
|
235
|
+
if (!all.some((f) => f.path === path) && (shownAll.on || forced.has(path)))
|
|
236
|
+
out.push({ path, message, id: '', step: null });
|
|
237
|
+
}
|
|
238
|
+
return out;
|
|
239
|
+
},
|
|
240
|
+
touched(path) { version.get(); return fields().some((f) => f.path === path && touched.has(f.el)); },
|
|
241
|
+
changed(path) { return read(signal.get(), path) !== read(initial, path); },
|
|
242
|
+
get valid() { version.get(); return !Object.keys(errorsNow).length; },
|
|
243
|
+
get pending() { return pending.get(); },
|
|
244
|
+
setErrors(errors) { server = { ...errors }; for (const path of Object.keys(errors))
|
|
245
|
+
forced.add(path); refresh(); },
|
|
246
|
+
focus(path) {
|
|
247
|
+
const group = fields().filter((f) => f.path === path).map((f) => f.el);
|
|
248
|
+
const el = group.find((x) => x.checked) ?? group[0];
|
|
249
|
+
const step = el?.closest('[data-cl-step][id]');
|
|
250
|
+
if (step?.hidden)
|
|
251
|
+
showStep(step.id, true);
|
|
252
|
+
el?.focus();
|
|
253
|
+
},
|
|
254
|
+
get step() { return current.get(); },
|
|
255
|
+
get steps() {
|
|
256
|
+
version.get();
|
|
257
|
+
const shownStep = current.get();
|
|
258
|
+
return steps().map((s) => { const errors = errorsIn(s); return { id: s.id, current: s.id === shownStep, done: passed.has(s.id) && !errors, errors }; });
|
|
259
|
+
},
|
|
260
|
+
async next() {
|
|
261
|
+
const list = steps();
|
|
262
|
+
const at = list.findIndex((s) => s.id === now());
|
|
263
|
+
const step = list[at];
|
|
264
|
+
if (!step)
|
|
265
|
+
return false;
|
|
266
|
+
for (const f of fields())
|
|
267
|
+
if (step.contains(f.el))
|
|
268
|
+
checked.add(f.el);
|
|
269
|
+
await settle();
|
|
270
|
+
refresh();
|
|
271
|
+
const bad = fields().find((f) => step.contains(f.el) && errorsNow[f.path]);
|
|
272
|
+
if (bad) {
|
|
273
|
+
bad.el.focus();
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
passed.add(step.id);
|
|
277
|
+
if (list[at + 1])
|
|
278
|
+
go(list[at + 1].id);
|
|
279
|
+
return true;
|
|
280
|
+
},
|
|
281
|
+
back() {
|
|
282
|
+
const list = steps();
|
|
283
|
+
const prev = list[list.findIndex((s) => s.id === now()) - 1];
|
|
284
|
+
if (prev)
|
|
285
|
+
go(prev.id);
|
|
286
|
+
},
|
|
287
|
+
go,
|
|
288
|
+
get submitting() { return submitting.get(); },
|
|
289
|
+
async submit() {
|
|
290
|
+
if (untrack(() => submitting.get()))
|
|
291
|
+
return false;
|
|
292
|
+
submitting.set(true);
|
|
293
|
+
try {
|
|
294
|
+
shownAll.on = true;
|
|
295
|
+
await settle();
|
|
296
|
+
refresh();
|
|
297
|
+
if (!toFirstError())
|
|
298
|
+
return false;
|
|
299
|
+
const sent = untrack(() => signal.get());
|
|
300
|
+
const answer = await options.submit?.(sent);
|
|
301
|
+
const now = untrack(() => signal.get());
|
|
302
|
+
const errors = Object.entries(answer?.errors ?? {}).filter(([path]) => !path || read(now, path) === read(sent, path));
|
|
303
|
+
if (errors.length) {
|
|
304
|
+
engine.setErrors(Object.fromEntries(errors));
|
|
305
|
+
return toFirstError();
|
|
306
|
+
}
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
finally {
|
|
310
|
+
submitting.set(false);
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
reset(next) {
|
|
314
|
+
if (next !== undefined)
|
|
315
|
+
initial = next;
|
|
316
|
+
touched = new WeakSet();
|
|
317
|
+
dirty = new WeakSet();
|
|
318
|
+
checked = new WeakSet();
|
|
319
|
+
forced.clear();
|
|
320
|
+
server = {};
|
|
321
|
+
shownAll.on = false;
|
|
322
|
+
signal.set(initial);
|
|
323
|
+
refresh();
|
|
324
|
+
},
|
|
325
|
+
connect(el) {
|
|
326
|
+
const root = el.closest('form') ?? el;
|
|
327
|
+
const count = roots.get(root) ?? 0;
|
|
328
|
+
roots.set(root, count + 1);
|
|
329
|
+
if (!count) {
|
|
330
|
+
if (root instanceof HTMLFormElement)
|
|
331
|
+
root.noValidate = true;
|
|
332
|
+
for (const [type, fn] of listeners)
|
|
333
|
+
root.addEventListener(type, fn);
|
|
334
|
+
if (roots.size === 1)
|
|
335
|
+
for (const type of urlEvents)
|
|
336
|
+
addEventListener(type, onHash);
|
|
337
|
+
const list = steps();
|
|
338
|
+
if (list.length) {
|
|
339
|
+
root.setAttribute('data-cl-steps', '');
|
|
340
|
+
showStep(hashStep() || now() || list[0].id, false);
|
|
341
|
+
}
|
|
342
|
+
refresh();
|
|
343
|
+
check();
|
|
344
|
+
}
|
|
345
|
+
return () => {
|
|
346
|
+
const left = (roots.get(root) ?? 1) - 1;
|
|
347
|
+
if (left)
|
|
348
|
+
return void roots.set(root, left);
|
|
349
|
+
roots.delete(root);
|
|
350
|
+
for (const [type, fn] of listeners)
|
|
351
|
+
root.removeEventListener(type, fn);
|
|
352
|
+
if (!roots.size) {
|
|
353
|
+
for (const type of urlEvents)
|
|
354
|
+
removeEventListener(type, onHash);
|
|
355
|
+
clearTimeout(timer);
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
},
|
|
359
|
+
};
|
|
360
|
+
return engine;
|
|
361
|
+
}
|
|
362
|
+
export function connect(ctx, index, get) {
|
|
363
|
+
const el = marked(ctx, `[data-cl-form="${index}"]`);
|
|
364
|
+
const engine = get();
|
|
365
|
+
if (el && typeof engine.connect === 'function')
|
|
366
|
+
onDispose(engine.connect(el));
|
|
367
|
+
}
|
|
368
|
+
function inert() {
|
|
369
|
+
return {
|
|
370
|
+
error: () => '', errors: [], touched: () => false, changed: () => false, valid: true, pending: false,
|
|
371
|
+
setErrors() { }, focus() { }, reset() { }, step: '', steps: [], next: async () => false, back() { }, go() { },
|
|
372
|
+
submitting: false, submit: async () => false,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Standard Schema (https://standardschema.dev): Zod, Valibot, ArkType… Declared here, so there's no dependency.
|
|
3
|
+
* @see https://clarusjs.com/reference/forms#standardschema
|
|
4
|
+
*/
|
|
5
|
+
export type StandardSchema<T = unknown> = {
|
|
6
|
+
readonly '~standard': {
|
|
7
|
+
readonly version: 1;
|
|
8
|
+
readonly vendor: string;
|
|
9
|
+
validate(value: unknown): StandardResult<T> | Promise<StandardResult<T>>;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export type StandardIssue = {
|
|
13
|
+
readonly message: string;
|
|
14
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
15
|
+
readonly key: PropertyKey;
|
|
16
|
+
}>;
|
|
17
|
+
};
|
|
18
|
+
export type StandardResult<T> = {
|
|
19
|
+
readonly value: T;
|
|
20
|
+
readonly issues?: undefined;
|
|
21
|
+
} | {
|
|
22
|
+
readonly issues: ReadonlyArray<StandardIssue>;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The dotted paths of a form's values: `customer.email`, `lines.0.item` (any string for values typed `any`).
|
|
26
|
+
* @see https://clarusjs.com/reference/forms#path
|
|
27
|
+
*/
|
|
28
|
+
export type Path<T> = 0 extends 1 & T ? string : PathOf<T, 6>;
|
|
29
|
+
type Prev = [never, 0, 1, 2, 3, 4, 5];
|
|
30
|
+
type PathOf<T, D extends number> = [D] extends [never] ? never : T extends readonly (infer U)[] ? `${number}` | `${number}.${PathOf<U, Prev[D]>}` : T extends object ? {
|
|
31
|
+
[K in keyof T & string]: K | `${K}.${PathOf<T[K], Prev[D]>}`;
|
|
32
|
+
}[keyof T & string] : never;
|
|
33
|
+
/**
|
|
34
|
+
* `form()`'s options: a schema, when errors show, messages, and the submit callback.
|
|
35
|
+
* @see https://clarusjs.com/reference/forms#form
|
|
36
|
+
*/
|
|
37
|
+
export type FormOptions<T> = {
|
|
38
|
+
/** Validates the whole object; its issues land on their fields by path. Sync or async. */
|
|
39
|
+
schema?: StandardSchema<T>;
|
|
40
|
+
/** When a field's error appears: after it's been left once ('blur', the default), on input, or on submit. */
|
|
41
|
+
show?: 'blur' | 'input' | 'submit';
|
|
42
|
+
/** Messages for native constraints, by path (`'customer.email'`) or by constraint (`valueMissing`, `typeMismatch`…). */
|
|
43
|
+
messages?: Record<string, string>;
|
|
44
|
+
/** Called with the values once everything is valid; may return server errors by path. */
|
|
45
|
+
submit?(values: T): void | {
|
|
46
|
+
errors?: Record<string, string>;
|
|
47
|
+
} | Promise<void | {
|
|
48
|
+
errors?: Record<string, string>;
|
|
49
|
+
}>;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* An error being shown, for a summary: its path, message, its message element's id, and its step.
|
|
53
|
+
* @see https://clarusjs.com/reference/forms#formerror
|
|
54
|
+
*/
|
|
55
|
+
export type FormError<T = any> = {
|
|
56
|
+
path: Path<T>;
|
|
57
|
+
message: string;
|
|
58
|
+
id: string;
|
|
59
|
+
step: string | null;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* A form's engine, as `form()` returns it: errors, touched and changed fields, steps, and submit.
|
|
63
|
+
* @see https://clarusjs.com/reference/forms#form
|
|
64
|
+
*/
|
|
65
|
+
export type Form<T> = {
|
|
66
|
+
/** The message shown for a field now: '' when there is none, or it isn't shown yet. */
|
|
67
|
+
error(path: Path<T>): string;
|
|
68
|
+
/** Every error being shown, in document order. */
|
|
69
|
+
readonly errors: FormError<T>[];
|
|
70
|
+
/** Left at least once. */
|
|
71
|
+
touched(path: Path<T>): boolean;
|
|
72
|
+
/** Differs from the starting value. */
|
|
73
|
+
changed(path: Path<T>): boolean;
|
|
74
|
+
/** No errors at all (shown or not). */
|
|
75
|
+
readonly valid: boolean;
|
|
76
|
+
/** An async schema check is running. */
|
|
77
|
+
readonly pending: boolean;
|
|
78
|
+
/** Server errors by path (they show until that field's value changes). */
|
|
79
|
+
setErrors(errors: Record<string, string>): void;
|
|
80
|
+
/** Focus a field (the checked radio of a group, else the first). */
|
|
81
|
+
focus(path: Path<T>): void;
|
|
82
|
+
/**
|
|
83
|
+
* Back to the starting values, with touched and errors cleared. With `values` (a record loaded after the form was
|
|
84
|
+
* made, or saved), those become the values and the new starting point.
|
|
85
|
+
*/
|
|
86
|
+
reset(values?: T): void;
|
|
87
|
+
/** Multi-step forms: the current step's id. */
|
|
88
|
+
readonly step: string;
|
|
89
|
+
/** Every step, in document order: current, done (left with next() and now without errors), errors shown in it. */
|
|
90
|
+
readonly steps: {
|
|
91
|
+
id: string;
|
|
92
|
+
current: boolean;
|
|
93
|
+
done: boolean;
|
|
94
|
+
errors: number;
|
|
95
|
+
}[];
|
|
96
|
+
/** Check the current step; with errors, stay and focus the first (false); else go to the next step (true). */
|
|
97
|
+
next(): Promise<boolean>;
|
|
98
|
+
/** The previous step, without checks. */
|
|
99
|
+
back(): void;
|
|
100
|
+
/** Go to a step by id. */
|
|
101
|
+
go(id: string): void;
|
|
102
|
+
/** The submit callback is running. */
|
|
103
|
+
readonly submitting: boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Check everything; with errors, go to the first (its step, its field) and resolve false. Else call `submit` with
|
|
106
|
+
* the values: server errors it returns land the same way (false); otherwise true. A connected <form> runs this on
|
|
107
|
+
* submit. One at a time.
|
|
108
|
+
*/
|
|
109
|
+
submit(): Promise<boolean>;
|
|
110
|
+
};
|
|
111
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/runtime/attrs.js
CHANGED
|
@@ -25,16 +25,24 @@ function safeUrl(url, image) {
|
|
|
25
25
|
return true;
|
|
26
26
|
return image && RASTER_DATA.test(compact);
|
|
27
27
|
}
|
|
28
|
+
// false is a value here, not a missing attribute: enumerated attributes (removing one means its default), and the
|
|
29
|
+
// ARIA states whose absence means "doesn't apply" (no aria-expanded: nothing expands). Other aria-* (aria-current:
|
|
30
|
+
// `active ? 'page' : false`) keep false = removed.
|
|
31
|
+
const ENUMERATED = new Set(['contenteditable', 'draggable', 'spellcheck', 'translate', 'aria-expanded', 'aria-pressed', 'aria-checked', 'aria-selected']);
|
|
28
32
|
/**
|
|
29
33
|
* What an attribute should become: `null` removes it, `true` makes it a bare boolean attribute, a string is its value.
|
|
30
34
|
*/
|
|
31
35
|
export function attrValue(name, value, tag) {
|
|
32
|
-
if (value
|
|
36
|
+
if (value == null)
|
|
37
|
+
return null;
|
|
38
|
+
const n = name.toLowerCase();
|
|
39
|
+
if (typeof value === 'boolean' && ENUMERATED.has(n))
|
|
40
|
+
return n === 'translate' ? (value ? 'yes' : 'no') : String(value); // translate: yes/no
|
|
41
|
+
if (value === false)
|
|
33
42
|
return null;
|
|
34
43
|
if (value === true)
|
|
35
44
|
return true;
|
|
36
45
|
const s = String(value);
|
|
37
|
-
const n = name.toLowerCase();
|
|
38
46
|
// List-valued URL attributes: every entry must be safe, not just the first.
|
|
39
47
|
if (n === 'srcset') {
|
|
40
48
|
// "url 1x, url 2x"
|
package/dist/runtime/dom.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { type Signal } from './signals.ts';
|
|
2
|
+
/** @internal Compiled components call this; apps don't. */
|
|
2
3
|
export type Hole = {
|
|
3
4
|
start: Comment;
|
|
4
5
|
end: Comment;
|
|
5
6
|
};
|
|
7
|
+
/** @internal Compiled components call this; apps don't. */
|
|
6
8
|
export type Block = {
|
|
7
9
|
start: Comment;
|
|
8
10
|
end: Comment;
|
|
@@ -19,52 +21,65 @@ export type For = Range & {
|
|
|
19
21
|
/**
|
|
20
22
|
* Where wiring looks for its markers: a whole island, or one @for row inside it. A row template repeats its marker
|
|
21
23
|
* numbers once per row, so code wiring a row only looks between that row's markers.
|
|
24
|
+
* @internal Compiled components call this; apps don't.
|
|
22
25
|
*/
|
|
23
26
|
export type Ctx = Element | {
|
|
24
27
|
root: Element;
|
|
25
28
|
start: Comment;
|
|
26
29
|
end: Comment;
|
|
27
30
|
};
|
|
28
|
-
/** Hole markers in `ctx`, by index. */
|
|
31
|
+
/** Hole markers in `ctx`, by index. @internal */
|
|
29
32
|
export declare function holes(ctx: Ctx): Hole[];
|
|
30
|
-
/** @if block markers in `ctx`, by index. */
|
|
33
|
+
/** @if block markers in `ctx`, by index. @internal */
|
|
31
34
|
export declare function blocks(ctx: Ctx): Block[];
|
|
32
35
|
/**
|
|
33
36
|
* An @if block. `which()` picks the branch (-1 for none). The server-rendered branch is wired in place; a change
|
|
34
37
|
* removes the old branch (disposing its effects and handlers), inserts the new branch's HTML and wires it.
|
|
38
|
+
* @internal Compiled components call this; apps don't.
|
|
35
39
|
*/
|
|
36
40
|
export declare function block(ctx: Ctx, index: number, which: () => number, branches: {
|
|
37
41
|
html: () => string;
|
|
38
42
|
wire: () => void;
|
|
39
43
|
}[]): void;
|
|
40
|
-
/**
|
|
41
|
-
* A child component inside an island whose props or content read state. `html()` renders it with the current
|
|
42
|
-
* values; when anything it read changes, the old HTML is disposed and replaced, and `wire()` wires the island's own
|
|
43
|
-
* holes and events in the child's slot content again. A stateful child is remounted with its new props.
|
|
44
|
-
*/
|
|
44
|
+
/** @internal Compiled components call this; apps don't. */
|
|
45
45
|
export declare function region(ctx: Ctx, index: number, html: () => string, wire: () => void): void;
|
|
46
46
|
/**
|
|
47
47
|
* A @for list. Rows the server rendered are adopted in place. When the list changes, rows are matched by key:
|
|
48
48
|
* kept rows keep their DOM nodes (moved only when out of order) and get the new item and index through their
|
|
49
49
|
* signals; new rows are rendered and wired; removed rows are disposed and removed. `@empty` content is shown while
|
|
50
50
|
* the list is empty.
|
|
51
|
+
* @internal Compiled components call this; apps don't.
|
|
51
52
|
*/
|
|
52
53
|
export declare function forBlock(ctx: Ctx, index: number, list: () => unknown, keyOf: (item: any, i: number) => unknown, row: {
|
|
53
|
-
html: (item: any, i: number) => string;
|
|
54
|
-
wire: (ctx: Ctx, item: Signal<any>, index: Signal<number>) => void;
|
|
54
|
+
html: (item: any, i: number, list: unknown[]) => string;
|
|
55
|
+
wire: (ctx: Ctx, item: Signal<any>, index: Signal<number>, count: Signal<number>) => void;
|
|
55
56
|
}, empty: {
|
|
56
57
|
html: () => string;
|
|
57
58
|
wire: (ctx: Ctx) => void;
|
|
58
59
|
} | null): void;
|
|
59
|
-
/** Keep exactly one text node between the hole's markers, set to `fn()`. */
|
|
60
|
+
/** Keep exactly one text node between the hole's markers, set to `fn()`. @internal */
|
|
60
61
|
export declare function text(hole: Hole, fn: () => unknown): void;
|
|
61
62
|
/**
|
|
62
63
|
* Register a handler for elements marked `data-cl-e="index"` inside `ctx`.
|
|
63
64
|
* One listener per island per event type; it walks from the target up to the island root, running handlers
|
|
64
65
|
* innermost first and stopping when one calls `stopPropagation()`. A handler registered for a @for row only fires
|
|
65
66
|
* for elements in that row.
|
|
67
|
+
* @internal Compiled components call this; apps don't.
|
|
66
68
|
*/
|
|
67
69
|
export declare function on(ctx: Ctx, index: number, type: string, fn: (e: Event) => void): void;
|
|
68
|
-
/**
|
|
70
|
+
/** The element matching `selector` in `ctx`: this island's (not a nested island's), and in a row, that row's. */
|
|
71
|
+
export declare function marked(ctx: Ctx, selector: string): Element | undefined;
|
|
72
|
+
/** Keep attribute `name` of the element marked `data-cl-a="index"` in `ctx` in sync with `fn()`. @internal */
|
|
69
73
|
export declare function attr(ctx: Ctx, index: number, name: string, fn: () => unknown, tag?: string): void;
|
|
74
|
+
/**
|
|
75
|
+
* A @model input: writing it asks the parent island to update its state (a bubbling, cancelable cl:model event from
|
|
76
|
+
* the island); when no parent takes it (a top-level island), the input keeps the value itself. New props from the
|
|
77
|
+
* parent set it with own().
|
|
78
|
+
* @internal Compiled components call this; apps don't.
|
|
79
|
+
*/
|
|
80
|
+
export declare function modelInput<T>(root: Element, name: string, initial: T): Signal<T> & {
|
|
81
|
+
own(value: T): void;
|
|
82
|
+
};
|
|
83
|
+
/** [(name)]="…" on a child island in region `index`: the child's writes to its @model name arrive here. @internal */
|
|
84
|
+
export declare function model(ctx: Ctx, index: number, name: string, set: (value: unknown) => void): void;
|
|
70
85
|
export {};
|