-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind.ts
More file actions
274 lines (252 loc) · 8.73 KB
/
Copy pathbind.ts
File metadata and controls
274 lines (252 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Bridge between reactive {@link Form} / {@link FormField} objects and the DOM.
*
* Provides `bindField()` to two-way-bind a single field to an input element,
* and `bindForm()` to wire an entire `<form>` element to a {@link Form}.
*
* @module bquery/forms
*/
import { effect } from '../reactive/index';
import type { BindFieldOptions, BindFormOptions, Form, FormField } from './types';
const isInput = (el: Element): el is HTMLInputElement => el.tagName === 'INPUT';
const isTextarea = (el: Element): el is HTMLTextAreaElement => el.tagName === 'TEXTAREA';
const isSelect = (el: Element): el is HTMLSelectElement => el.tagName === 'SELECT';
const isButton = (el: Element): el is HTMLButtonElement => el.tagName === 'BUTTON';
const defaultGetValue = (element: Element): unknown => {
if (isInput(element)) {
if (element.type === 'checkbox') return element.checked;
if (element.type === 'radio') return element.checked ? element.value : undefined;
if (element.type === 'number' || element.type === 'range') {
return element.value === '' ? '' : Number(element.value);
}
if (element.type === 'file') return element.files;
if (element.type === 'date' || element.type === 'datetime-local' || element.type === 'time') {
return element.value;
}
return element.value;
}
if (isTextarea(element)) {
return element.value;
}
if (isSelect(element)) {
if (element.multiple) {
return Array.from(element.selectedOptions).map((o) => o.value);
}
return element.value;
}
if ((element as HTMLElement).isContentEditable) {
return (element as HTMLElement).textContent ?? '';
}
const anyEl = element as unknown as { value?: unknown };
return anyEl.value;
};
const writeValue = (element: Element, value: unknown): void => {
if (isInput(element)) {
if (element.type === 'checkbox') {
element.checked = Boolean(value);
return;
}
if (element.type === 'radio') {
element.checked = element.value === String(value);
return;
}
if (element.type === 'file') return;
element.value = value == null ? '' : String(value);
return;
}
if (isTextarea(element)) {
element.value = value == null ? '' : String(value);
return;
}
if (isSelect(element)) {
if (element.multiple && Array.isArray(value)) {
const set = new Set(value.map((v) => String(v)));
for (const option of Array.from(element.options)) {
option.selected = set.has(option.value);
}
return;
}
element.value = value == null ? '' : String(value);
return;
}
if ((element as HTMLElement).isContentEditable) {
(element as HTMLElement).textContent = value == null ? '' : String(value);
return;
}
try {
(element as unknown as { value: unknown }).value = value;
} catch {
element.setAttribute('value', value == null ? '' : String(value));
}
};
/**
* Two-way bind a reactive {@link FormField} to a DOM input element.
*
* Supports `<input>` (text, number, checkbox, radio, file, date), `<textarea>`,
* `<select>` (including `multiple`), `[contenteditable]` elements, and custom
* Web Components that expose a `.value` property (e.g. `<bq-input>`).
*
* Returns a cleanup function that detaches the listeners and the reactive
* effect that mirrors the field's value into the element.
*
* @param field - Reactive field
* @param element - Target DOM element
* @param options - Optional binding overrides
* @returns A cleanup function
*/
export const bindField = <T>(
field: FormField<T>,
element: Element,
options: BindFieldOptions = {}
): (() => void) => {
const getValue = options.getValue ?? defaultGetValue;
let suppressEcho = false;
const debounceMs = Math.max(0, options.debounceMs ?? 0);
let debounceTimer: ReturnType<typeof setTimeout> | undefined;
const onInput = (): void => {
const raw = getValue(element);
if (raw === undefined && isInput(element) && element.type === 'radio') {
// unchecked radio — ignore so other group members can set the value
return;
}
if (debounceMs > 0) {
if (debounceTimer !== undefined) clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
debounceTimer = undefined;
suppressEcho = true;
field.value.value = raw as T;
}, debounceMs);
} else {
suppressEcho = true;
field.value.value = raw as T;
}
};
const onChange = (): void => {
onInput();
};
const onFocus = (): void => {
field.focus();
};
const onBlur = (): void => {
field.blur();
};
element.addEventListener('input', onInput);
element.addEventListener('change', onChange);
element.addEventListener('focus', onFocus, true);
element.addEventListener('blur', onBlur, true);
const stopEffect = effect(() => {
const next = field.value.value;
if (suppressEcho) {
suppressEcho = false;
return;
}
writeValue(element, next);
});
const stopErrorEffect = effect(() => {
const err = field.error.value;
if (err) element.setAttribute('aria-invalid', 'true');
else element.removeAttribute('aria-invalid');
});
const stopDisabledEffect = effect(() => {
const disabled = field.disabled.value;
if (isInput(element) || isTextarea(element) || isSelect(element) || isButton(element)) {
(
element as HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | HTMLButtonElement
).disabled = disabled;
} else {
if (disabled) element.setAttribute('aria-disabled', 'true');
else element.removeAttribute('aria-disabled');
}
});
return () => {
element.removeEventListener('input', onInput);
element.removeEventListener('change', onChange);
element.removeEventListener('focus', onFocus, true);
element.removeEventListener('blur', onBlur, true);
if (debounceTimer !== undefined) clearTimeout(debounceTimer);
stopEffect();
stopErrorEffect();
stopDisabledEffect();
};
};
const findInputsByName = (root: HTMLElement): Map<string, Element[]> => {
const map = new Map<string, Element[]>();
const elements = root.querySelectorAll<HTMLElement>('[name]');
for (const el of Array.from(elements)) {
const name = el.getAttribute('name');
if (!name) continue;
const list = map.get(name);
if (list) list.push(el);
else map.set(name, [el]);
}
return map;
};
/**
* Wire a {@link Form} to a `<form>` (or any container) element.
*
* The binding:
* - Auto-discovers `[name]` inputs inside the container and calls
* {@link bindField} for each matching form field.
* - Hooks the container's `submit` event to call `form.handleSubmit()`
* (preventing the default browser submit).
* - Mirrors per-field error messages into `[data-bq-error-for="<name>"]`
* elements when present, or a custom `errorSlot` lookup function.
*
* Returns a cleanup function that detaches every listener and reactive effect.
*
* @example
* ```html
* <form id="register">
* <input name="email" type="email" />
* <span data-bq-error-for="email"></span>
* <button type="submit">Save</button>
* </form>
* ```
* ```ts
* const cleanup = bindForm(form, document.getElementById('register')!);
* ```
*/
export const bindForm = <T extends Record<string, unknown>>(
form: Form<T>,
formElement: HTMLElement,
options: BindFormOptions = {}
): (() => void) => {
const cleanups: Array<() => void> = [];
const inputs = findInputsByName(formElement);
const fieldMap = options.fieldMap ?? {};
for (const [name, elements] of inputs.entries()) {
const fieldKey = fieldMap[name] ?? name;
const field = (form.fields as Record<string, FormField | undefined>)[fieldKey];
if (!field) continue;
// Group radio inputs and selects act as a single field via per-element bind.
for (const element of elements) {
cleanups.push(bindField(field, element));
}
// Error slot wiring
const cssEscape = (globalThis as { CSS?: { escape?: (value: string) => string } }).CSS?.escape;
const escapeAttr = (value: string): string =>
cssEscape ? cssEscape(value) : value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
const slotLookup =
options.errorSlot ??
((name: string, root: HTMLElement): HTMLElement | null =>
root.querySelector<HTMLElement>(`[data-bq-error-for="${escapeAttr(name)}"]`));
const slot = slotLookup(name, formElement);
if (slot) {
const stop = effect(() => {
const err = field.error.value;
slot.textContent = err;
});
cleanups.push(stop);
}
}
const onSubmit = (event: Event): void => {
event.preventDefault();
void form.handleSubmit();
};
formElement.addEventListener('submit', onSubmit);
cleanups.push(() => formElement.removeEventListener('submit', onSubmit));
return () => {
for (const cleanup of cleanups) cleanup();
cleanups.length = 0;
};
};