@krate/runtime 0.5.0-alpha.2 → 0.5.0-alpha.3
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/action-context.d.ts +49 -0
- package/dist/action-context.js +37 -0
- package/dist/action-context.test.d.ts +1 -0
- package/dist/action-context.test.js +47 -0
- package/dist/actions.d.ts +120 -0
- package/dist/actions.js +533 -0
- package/dist/actions.test.d.ts +1 -0
- package/dist/actions.test.js +391 -0
- package/dist/boundary.d.ts +121 -0
- package/dist/boundary.js +429 -0
- package/dist/class-component.d.ts +66 -0
- package/dist/class-component.js +633 -0
- package/dist/class-component.test.d.ts +1 -0
- package/dist/class-component.test.js +411 -0
- package/dist/context.d.ts +13 -0
- package/dist/context.js +58 -32
- package/dist/context.test.d.ts +1 -0
- package/dist/context.test.js +57 -0
- package/dist/derived.test.d.ts +1 -0
- package/dist/derived.test.js +140 -0
- package/dist/discrete.test.d.ts +1 -0
- package/dist/discrete.test.js +100 -0
- package/dist/dom.d.ts +55 -0
- package/dist/dom.js +414 -63
- package/dist/effects.d.ts +88 -0
- package/dist/effects.js +719 -0
- package/dist/effects.test.d.ts +1 -0
- package/dist/effects.test.js +485 -0
- package/dist/events.d.ts +58 -0
- package/dist/events.js +503 -0
- package/dist/events.test.d.ts +1 -0
- package/dist/events.test.js +447 -0
- package/dist/index.d.ts +12 -2
- package/dist/index.js +8 -2
- package/dist/jsx-types.d.ts +2 -0
- package/dist/krate-hydrate-lite.js +7 -1
- package/dist/krate-hydrate.js +7 -1
- package/dist/krate-runtime.js +7 -1
- package/dist/krate-suspense.js +2 -0
- package/dist/lanes.test.d.ts +1 -0
- package/dist/lanes.test.js +59 -0
- package/dist/list.d.ts +20 -0
- package/dist/list.js +466 -0
- package/dist/list.test.d.ts +1 -0
- package/dist/list.test.js +344 -0
- package/dist/optimistic-lanes.test.d.ts +1 -0
- package/dist/optimistic-lanes.test.js +45 -0
- package/dist/portal.d.ts +43 -0
- package/dist/portal.js +178 -0
- package/dist/portal.test.d.ts +1 -0
- package/dist/portal.test.js +99 -0
- package/dist/react-effects.test.d.ts +1 -0
- package/dist/react-effects.test.js +96 -0
- package/dist/react-suspense.test.d.ts +1 -0
- package/dist/react-suspense.test.js +194 -0
- package/dist/react.d.ts +132 -32
- package/dist/react.js +335 -81
- package/dist/react.test.js +12 -2
- package/dist/reconcile.js +13 -0
- package/dist/refs.d.ts +33 -0
- package/dist/refs.js +142 -0
- package/dist/refs.test.d.ts +1 -0
- package/dist/refs.test.js +257 -0
- package/dist/resource.js +20 -1
- package/dist/server-renderer.js +39 -1
- package/dist/server.d.ts +4 -1
- package/dist/server.js +30 -1
- package/dist/server.test.js +20 -0
- package/dist/signal-lanes.test.d.ts +1 -0
- package/dist/signal-lanes.test.js +32 -0
- package/dist/signal.d.ts +150 -1
- package/dist/signal.js +750 -65
- package/dist/slots.d.ts +18 -0
- package/dist/slots.js +108 -0
- package/dist/slots.test.d.ts +1 -0
- package/dist/slots.test.js +169 -0
- package/dist/snapshots.test.d.ts +1 -0
- package/dist/snapshots.test.js +208 -0
- package/dist/suspense-actions.test.d.ts +1 -0
- package/dist/suspense-actions.test.js +72 -0
- package/dist/suspense.d.ts +220 -0
- package/dist/suspense.js +901 -0
- package/dist/suspense.test.d.ts +1 -0
- package/dist/suspense.test.js +518 -0
- package/package.json +1 -1
package/dist/events.js
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React-style event system and form-control semantics.
|
|
3
|
+
*
|
|
4
|
+
* Handlers live in a per-element table (`el.__kh`) keyed by the React event
|
|
5
|
+
* name (`click`, `change`, `focus`) plus `$` for the capture variant. Like
|
|
6
|
+
* React 17+, delegated events get one capture-phase and one bubble-phase
|
|
7
|
+
* listener on the root container (`#root`, else `<body>`), installed lazily the
|
|
8
|
+
* first time a handler for that native type is registered. The capture
|
|
9
|
+
* listener runs capture handlers top-down, the bubble listener runs bubble
|
|
10
|
+
* handlers bottom-up, so React handlers interleave with native listeners the
|
|
11
|
+
* same way they do in React: native listeners inside the root run before React
|
|
12
|
+
* bubble handlers, listeners on `document` run after them, and
|
|
13
|
+
* `stopPropagation()` stops both further React handlers and native propagation.
|
|
14
|
+
*
|
|
15
|
+
* Events React does not delegate (scroll, media, load/error, enter/leave, ...)
|
|
16
|
+
* get a listener on the element itself.
|
|
17
|
+
*
|
|
18
|
+
* onChange follows React's ChangeEventPlugin: text inputs and textareas fire on
|
|
19
|
+
* every `input`, checkboxes and radios on `click`, selects and file inputs on
|
|
20
|
+
* `change`, with a value tracker so a change fires once per actual change.
|
|
21
|
+
* After onChange handlers run, controlled inputs (bound with krateControl) are
|
|
22
|
+
* restored to their state value, so a handler that does not update state keeps
|
|
23
|
+
* the field unchanged, as in React.
|
|
24
|
+
*/
|
|
25
|
+
import { createEffect, krateFlushSync, krateRunEvent } from './signal.js';
|
|
26
|
+
// React event names whose native event has a different name.
|
|
27
|
+
const TO_NATIVE = { doubleclick: 'dblclick', focus: 'focusin', blur: 'focusout' };
|
|
28
|
+
const TO_REACT = { dblclick: 'doubleclick', focusin: 'focus', focusout: 'blur' };
|
|
29
|
+
// Synthetic event type for events whose React type differs from the native one.
|
|
30
|
+
const REACT_TYPE = { focusin: 'focus', focusout: 'blur' };
|
|
31
|
+
// Events React attaches to the element instead of the root (they do not bubble,
|
|
32
|
+
// or React emulates them per element).
|
|
33
|
+
const DIRECT = new Set(('scroll scrollend load error abort cancel close invalid toggle beforetoggle ' +
|
|
34
|
+
'mouseenter mouseleave pointerenter pointerleave ' +
|
|
35
|
+
'canplay canplaythrough durationchange emptied encrypted ended loadeddata ' +
|
|
36
|
+
'loadedmetadata loadstart pause play playing progress ratechange resize ' +
|
|
37
|
+
'seeked seeking stalled suspend timeupdate volumechange waiting').split(' '));
|
|
38
|
+
// Native events each plugin event listens to.
|
|
39
|
+
const PLUGIN_DEPS = {
|
|
40
|
+
change: ['change', 'input', 'click'],
|
|
41
|
+
beforeinput: ['beforeinput', 'compositionend'],
|
|
42
|
+
};
|
|
43
|
+
const proto = {
|
|
44
|
+
preventDefault() {
|
|
45
|
+
this.defaultPrevented = true;
|
|
46
|
+
this.nativeEvent.preventDefault();
|
|
47
|
+
},
|
|
48
|
+
stopPropagation() {
|
|
49
|
+
this._s = true;
|
|
50
|
+
this.nativeEvent.stopPropagation();
|
|
51
|
+
},
|
|
52
|
+
isDefaultPrevented() {
|
|
53
|
+
return this.defaultPrevented;
|
|
54
|
+
},
|
|
55
|
+
isPropagationStopped() {
|
|
56
|
+
return !!this._s;
|
|
57
|
+
},
|
|
58
|
+
persist() { },
|
|
59
|
+
isPersistent() {
|
|
60
|
+
return true;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
// Fields the synthetic event does not own (key, clientX, relatedTarget,
|
|
64
|
+
// deltaY, touches, dataTransfer, clipboardData, data, animationName, ...) are
|
|
65
|
+
// read from the native event, so every type-specific field is available.
|
|
66
|
+
const traps = {
|
|
67
|
+
get(t, k) {
|
|
68
|
+
if (k in t)
|
|
69
|
+
return t[k];
|
|
70
|
+
const n = t.nativeEvent;
|
|
71
|
+
const v = n[k];
|
|
72
|
+
return typeof v === 'function' ? v.bind(n) : v;
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
/** Wraps a native event in a React-style synthetic event. */
|
|
76
|
+
export function krateSynthetic(native, type, target) {
|
|
77
|
+
const e = Object.create(proto);
|
|
78
|
+
let t = target === undefined ? native.target : target;
|
|
79
|
+
// React reports the element for events targeted at text nodes.
|
|
80
|
+
if (t && t.nodeType === 3)
|
|
81
|
+
t = t.parentNode;
|
|
82
|
+
e.type = type || REACT_TYPE[native.type] || native.type;
|
|
83
|
+
e.target = t;
|
|
84
|
+
e.currentTarget = null;
|
|
85
|
+
e.nativeEvent = native;
|
|
86
|
+
e.defaultPrevented = native.defaultPrevented;
|
|
87
|
+
return new Proxy(e, traps);
|
|
88
|
+
}
|
|
89
|
+
// The first error thrown by a handler is rethrown after the dispatch finishes,
|
|
90
|
+
// so every handler still runs (React rethrows the same way).
|
|
91
|
+
let caught;
|
|
92
|
+
let hasCaught = false;
|
|
93
|
+
function call(h, e) {
|
|
94
|
+
try {
|
|
95
|
+
h(e);
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
if (!hasCaught) {
|
|
99
|
+
hasCaught = true;
|
|
100
|
+
caught = err;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function rethrow() {
|
|
105
|
+
if (hasCaught) {
|
|
106
|
+
const err = caught;
|
|
107
|
+
hasCaught = false;
|
|
108
|
+
caught = undefined;
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Phases for emit(): 1 = capture handlers, 2 = bubble handlers, 3 = both.
|
|
113
|
+
function emit(native, name, target, phases, type) {
|
|
114
|
+
const cap = [];
|
|
115
|
+
const bub = [];
|
|
116
|
+
// Portal content continues at its anchor, so events bubble through the
|
|
117
|
+
// React tree rather than the DOM container (as in React).
|
|
118
|
+
for (let n = target; n; n = (n.__kpp || n.parentNode)) {
|
|
119
|
+
const m = n.__kh;
|
|
120
|
+
if (!m)
|
|
121
|
+
continue;
|
|
122
|
+
let h;
|
|
123
|
+
if (phases & 1 && (h = m[name + '$']))
|
|
124
|
+
cap.unshift(n, h);
|
|
125
|
+
if (phases & 2 && (h = m[name]))
|
|
126
|
+
bub.push(n, h);
|
|
127
|
+
}
|
|
128
|
+
if (!cap.length && !bub.length)
|
|
129
|
+
return null;
|
|
130
|
+
const e = krateSynthetic(native, type, target);
|
|
131
|
+
const list = cap.concat(bub);
|
|
132
|
+
for (let i = 0; i < list.length; i += 2) {
|
|
133
|
+
if (e._s)
|
|
134
|
+
break;
|
|
135
|
+
e.currentTarget = list[i];
|
|
136
|
+
call(list[i + 1], e);
|
|
137
|
+
}
|
|
138
|
+
e.currentTarget = null;
|
|
139
|
+
return e;
|
|
140
|
+
}
|
|
141
|
+
// Root containers with their installed native types.
|
|
142
|
+
const rootTypes = new WeakMap();
|
|
143
|
+
const roots = new Set();
|
|
144
|
+
const allTypes = new Set();
|
|
145
|
+
function defaultRoot() {
|
|
146
|
+
if (typeof document === 'undefined')
|
|
147
|
+
return null;
|
|
148
|
+
return document.getElementById('root') || document.body;
|
|
149
|
+
}
|
|
150
|
+
function installOn(root, type) {
|
|
151
|
+
let set = rootTypes.get(root);
|
|
152
|
+
if (!set) {
|
|
153
|
+
set = new Set();
|
|
154
|
+
rootTypes.set(root, set);
|
|
155
|
+
}
|
|
156
|
+
if (set.has(type))
|
|
157
|
+
return;
|
|
158
|
+
set.add(type);
|
|
159
|
+
root.addEventListener(type, onRootCapture, true);
|
|
160
|
+
root.addEventListener(type, onRootBubble, false);
|
|
161
|
+
}
|
|
162
|
+
function ensure(type) {
|
|
163
|
+
allTypes.add(type);
|
|
164
|
+
const r = defaultRoot();
|
|
165
|
+
if (r) {
|
|
166
|
+
roots.add(r);
|
|
167
|
+
installOn(r, type);
|
|
168
|
+
}
|
|
169
|
+
for (const extra of roots) {
|
|
170
|
+
if (extra !== r && extra.isConnected)
|
|
171
|
+
installOn(extra, type);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Makes `node` an additional delegation root (portals moved outside `#root`).
|
|
176
|
+
* Every event type registered so far, and any registered later, is listened
|
|
177
|
+
* to on it.
|
|
178
|
+
*/
|
|
179
|
+
export function krateEventRoot(node) {
|
|
180
|
+
roots.add(node);
|
|
181
|
+
for (const t of allTypes)
|
|
182
|
+
installOn(node, t);
|
|
183
|
+
}
|
|
184
|
+
function onRootCapture(native) {
|
|
185
|
+
dispatchRoot(native, true);
|
|
186
|
+
}
|
|
187
|
+
function onRootBubble(native) {
|
|
188
|
+
dispatchRoot(native, false);
|
|
189
|
+
}
|
|
190
|
+
function dispatchRoot(native, capture) {
|
|
191
|
+
// Several roots can be on the path (a portal inside the root); the first
|
|
192
|
+
// listener to see the event handles the whole path.
|
|
193
|
+
const flag = capture ? '__kdc' : '__kdb';
|
|
194
|
+
const n = native;
|
|
195
|
+
if (n[flag])
|
|
196
|
+
return;
|
|
197
|
+
n[flag] = 1;
|
|
198
|
+
const t = native.type;
|
|
199
|
+
const target = native.target;
|
|
200
|
+
// The bubble listener is the end of the delegated dispatch: a discrete
|
|
201
|
+
// event's updates are flushed synchronously there, once per native event.
|
|
202
|
+
krateRunEvent(t, !capture, () => {
|
|
203
|
+
if (t !== 'change' && t !== 'beforeinput') {
|
|
204
|
+
emit(native, TO_REACT[t] || t, target, capture ? 1 : 2);
|
|
205
|
+
}
|
|
206
|
+
if (!capture) {
|
|
207
|
+
if (t === 'change' || t === 'input' || t === 'click')
|
|
208
|
+
changePlugin(native);
|
|
209
|
+
else if (t === 'beforeinput' || t === 'compositionend')
|
|
210
|
+
beforeInputPlugin(native);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
rethrow();
|
|
214
|
+
}
|
|
215
|
+
function onDirect(native) {
|
|
216
|
+
directDispatch(this, native, false);
|
|
217
|
+
}
|
|
218
|
+
function onDirectCapture(native) {
|
|
219
|
+
directDispatch(this, native, true);
|
|
220
|
+
}
|
|
221
|
+
function directDispatch(el, native, capture) {
|
|
222
|
+
const m = el.__kh;
|
|
223
|
+
const h = m && m[native.type + (capture ? '$' : '')];
|
|
224
|
+
if (!h)
|
|
225
|
+
return;
|
|
226
|
+
const e = krateSynthetic(native);
|
|
227
|
+
e.currentTarget = el;
|
|
228
|
+
krateRunEvent(native.type, true, () => call(h, e));
|
|
229
|
+
e.currentTarget = null;
|
|
230
|
+
rethrow();
|
|
231
|
+
}
|
|
232
|
+
/** Splits a React prop (`onClickCapture`) into its event name and phase. */
|
|
233
|
+
export function krateEventName(prop) {
|
|
234
|
+
let n = prop.slice(2);
|
|
235
|
+
let capture = false;
|
|
236
|
+
if (n.length > 7 && n.slice(-7) === 'Capture' && !/^(Got|Lost)PointerCapture$/.test(n)) {
|
|
237
|
+
n = n.slice(0, -7);
|
|
238
|
+
capture = true;
|
|
239
|
+
}
|
|
240
|
+
return [n.toLowerCase(), capture];
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Sets (or with a non-function `fn`, removes) the React event handler `prop`
|
|
244
|
+
* (`onClick`, `onChangeCapture`, ...) on `el`.
|
|
245
|
+
*/
|
|
246
|
+
export function krateOn(el, prop, fn) {
|
|
247
|
+
const [name, capture] = krateEventName(prop);
|
|
248
|
+
const node = el;
|
|
249
|
+
const table = node.__kh || (node.__kh = {});
|
|
250
|
+
const key = name + (capture ? '$' : '');
|
|
251
|
+
if (typeof fn !== 'function') {
|
|
252
|
+
table[key] = undefined;
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
table[key] = fn;
|
|
256
|
+
if (DIRECT.has(name)) {
|
|
257
|
+
const done = node.__kd || (node.__kd = {});
|
|
258
|
+
if (!done[key]) {
|
|
259
|
+
done[key] = 1;
|
|
260
|
+
el.addEventListener(name, capture ? onDirectCapture : onDirect, capture);
|
|
261
|
+
}
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const deps = PLUGIN_DEPS[name];
|
|
265
|
+
if (deps) {
|
|
266
|
+
for (const d of deps)
|
|
267
|
+
ensure(d);
|
|
268
|
+
if (name === 'change')
|
|
269
|
+
track(el);
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
ensure(TO_NATIVE[name] || name);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Mounts the text inputs under `root` the way React does: React sets an
|
|
277
|
+
* input's value through the DOM property, which leaves the caret at the end,
|
|
278
|
+
* while an input parsed from HTML keeps it at the start (so typing after
|
|
279
|
+
* focus() or autoFocus would land before the text). Re-assigning the value
|
|
280
|
+
* moves the caret; the user's value is kept. Textareas are left alone (React
|
|
281
|
+
* leaves their caret at the start too).
|
|
282
|
+
*/
|
|
283
|
+
export function krateMountFields(root) {
|
|
284
|
+
const list = root.querySelectorAll('input[value]');
|
|
285
|
+
for (let i = 0; i < list.length; i++) {
|
|
286
|
+
const f = list[i];
|
|
287
|
+
if (!TEXT_TYPES.has(f.type || 'text') || !f.value || f.ownerDocument.activeElement === f)
|
|
288
|
+
continue;
|
|
289
|
+
const v = f.value;
|
|
290
|
+
f.value = '';
|
|
291
|
+
f.value = v;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
// value tracking and onChange
|
|
295
|
+
const TEXT_TYPES = new Set('color date datetime datetime-local email month number password range search tel text time url week'.split(' '));
|
|
296
|
+
function isCheckable(el) {
|
|
297
|
+
return el.tagName === 'INPUT' && (el.type === 'checkbox' || el.type === 'radio');
|
|
298
|
+
}
|
|
299
|
+
function currentValue(el) {
|
|
300
|
+
return isCheckable(el) ? String(el.checked) : String(el.value);
|
|
301
|
+
}
|
|
302
|
+
function track(el) {
|
|
303
|
+
if (el.__kv === undefined && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA')) {
|
|
304
|
+
el.__kv = currentValue(el);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
function updateTracker(el) {
|
|
308
|
+
const v = currentValue(el);
|
|
309
|
+
if (el.__kv === v)
|
|
310
|
+
return false;
|
|
311
|
+
el.__kv = v;
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
function radioGroup(el) {
|
|
315
|
+
if (!el.name || !el.ownerDocument)
|
|
316
|
+
return [el];
|
|
317
|
+
const out = [];
|
|
318
|
+
const list = el.ownerDocument.getElementsByName(el.name);
|
|
319
|
+
for (let i = 0; i < list.length; i++) {
|
|
320
|
+
const o = list[i];
|
|
321
|
+
if (o.tagName === 'INPUT' && o.type === 'radio' && o.form === el.form)
|
|
322
|
+
out.push(o);
|
|
323
|
+
}
|
|
324
|
+
return out;
|
|
325
|
+
}
|
|
326
|
+
function changeTarget(native) {
|
|
327
|
+
const el = native.target;
|
|
328
|
+
if (!el || !el.tagName)
|
|
329
|
+
return null;
|
|
330
|
+
const t = native.type;
|
|
331
|
+
const tag = el.tagName;
|
|
332
|
+
if (tag === 'SELECT' || (tag === 'INPUT' && el.type === 'file'))
|
|
333
|
+
return t === 'change' ? el : null;
|
|
334
|
+
if (isCheckable(el))
|
|
335
|
+
return t === 'click' && updateTracker(el) ? el : null;
|
|
336
|
+
if (tag === 'TEXTAREA' || (tag === 'INPUT' && TEXT_TYPES.has(el.type || 'text'))) {
|
|
337
|
+
return t !== 'click' && updateTracker(el) ? el : null;
|
|
338
|
+
}
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
function changePlugin(native) {
|
|
342
|
+
const el = changeTarget(native);
|
|
343
|
+
if (!el)
|
|
344
|
+
return;
|
|
345
|
+
emit(native, 'change', el, 3, 'change');
|
|
346
|
+
if (el.type === 'radio') {
|
|
347
|
+
for (const o of radioGroup(el)) {
|
|
348
|
+
updateTracker(o);
|
|
349
|
+
restore(o);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
restore(el);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function beforeInputPlugin(native) {
|
|
357
|
+
const n = native;
|
|
358
|
+
if (native.type === 'beforeinput' && (n.isComposing || typeof n.data !== 'string' || n.data === ''))
|
|
359
|
+
return;
|
|
360
|
+
if (native.type === 'compositionend' && !n.data)
|
|
361
|
+
return;
|
|
362
|
+
emit(native, 'beforeinput', native.target, 3, 'beforeinput');
|
|
363
|
+
}
|
|
364
|
+
// controlled form controls
|
|
365
|
+
function warnOnce(el, msg) {
|
|
366
|
+
if (el.__kw)
|
|
367
|
+
return;
|
|
368
|
+
el.__kw = 1;
|
|
369
|
+
console.error(msg);
|
|
370
|
+
}
|
|
371
|
+
function setSelect(el, v, asDefault) {
|
|
372
|
+
const opts = el.options;
|
|
373
|
+
if (el.multiple) {
|
|
374
|
+
const want = new Set((Array.isArray(v) ? v : [v]).map(String));
|
|
375
|
+
for (let i = 0; i < opts.length; i++) {
|
|
376
|
+
const sel = want.has(opts[i].value);
|
|
377
|
+
if (asDefault)
|
|
378
|
+
opts[i].defaultSelected = sel;
|
|
379
|
+
else if (opts[i].selected !== sel)
|
|
380
|
+
opts[i].selected = sel;
|
|
381
|
+
}
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const s = String(v);
|
|
385
|
+
let first = null;
|
|
386
|
+
for (let i = 0; i < opts.length; i++) {
|
|
387
|
+
if (opts[i].value === s) {
|
|
388
|
+
if (asDefault)
|
|
389
|
+
opts[i].defaultSelected = true;
|
|
390
|
+
else
|
|
391
|
+
opts[i].selected = true;
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
if (!first && !opts[i].disabled)
|
|
395
|
+
first = opts[i];
|
|
396
|
+
}
|
|
397
|
+
// React selects the first enabled option when no option matches.
|
|
398
|
+
if (first && !asDefault)
|
|
399
|
+
first.selected = true;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Applies one value of a form-control property with React semantics.
|
|
403
|
+
* `value`/`checked` of null or undefined leave the DOM untouched (the control is
|
|
404
|
+
* uncontrolled); `defaultValue`/`defaultChecked` only change the default.
|
|
405
|
+
*/
|
|
406
|
+
export function krateSetControl(el, prop, v) {
|
|
407
|
+
const f = el;
|
|
408
|
+
switch (prop) {
|
|
409
|
+
case 'value': {
|
|
410
|
+
if (v == null)
|
|
411
|
+
return;
|
|
412
|
+
if (f.tagName === 'SELECT') {
|
|
413
|
+
setSelect(f, v, false);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
const s = String(v);
|
|
417
|
+
if (f.value !== s)
|
|
418
|
+
f.value = s;
|
|
419
|
+
// React keeps the value attribute in sync, so form.reset() restores the
|
|
420
|
+
// current controlled value. Skipped for a focused number input.
|
|
421
|
+
if (f.type !== 'file' && !(f.type === 'number' && f.ownerDocument && f.ownerDocument.activeElement === f)) {
|
|
422
|
+
if (f.defaultValue !== s)
|
|
423
|
+
f.defaultValue = s;
|
|
424
|
+
}
|
|
425
|
+
if (!isCheckable(f))
|
|
426
|
+
f.__kv = s;
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
case 'checked': {
|
|
430
|
+
if (v == null)
|
|
431
|
+
return;
|
|
432
|
+
const b = !!v;
|
|
433
|
+
if (f.checked !== b)
|
|
434
|
+
f.checked = b;
|
|
435
|
+
f.__kv = String(b);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
case 'defaultValue':
|
|
439
|
+
if (f.tagName === 'SELECT')
|
|
440
|
+
setSelect(f, v, true);
|
|
441
|
+
else
|
|
442
|
+
f.defaultValue = v == null ? '' : String(v);
|
|
443
|
+
return;
|
|
444
|
+
case 'defaultChecked':
|
|
445
|
+
f.defaultChecked = !!v;
|
|
446
|
+
return;
|
|
447
|
+
default:
|
|
448
|
+
f[prop] = !!v;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
function restore(el) {
|
|
452
|
+
const c = el.__kc;
|
|
453
|
+
if (!c)
|
|
454
|
+
return;
|
|
455
|
+
// React flushes the event's synchronous updates before restoring, so the
|
|
456
|
+
// control is compared with the newly committed state.
|
|
457
|
+
krateFlushSync();
|
|
458
|
+
if (c.value)
|
|
459
|
+
krateSetControl(el, 'value', c.value());
|
|
460
|
+
if (c.checked)
|
|
461
|
+
krateSetControl(el, 'checked', c.checked());
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Binds a reactive form-control property (`value`, `checked`, `defaultValue`,
|
|
465
|
+
* ...) to `get`. `value`/`checked` make the control controlled: after onChange
|
|
466
|
+
* the DOM is restored to `get()`. Switching between controlled (a defined
|
|
467
|
+
* value) and uncontrolled (undefined) logs React's warning. Returns the
|
|
468
|
+
* effect's dispose function.
|
|
469
|
+
*/
|
|
470
|
+
export function krateControl(el, prop, get) {
|
|
471
|
+
const f = el;
|
|
472
|
+
const controlProp = prop === 'value' || prop === 'checked';
|
|
473
|
+
if (controlProp) {
|
|
474
|
+
(f.__kc || (f.__kc = {}))[prop] = get;
|
|
475
|
+
// Restoring needs the change listeners even without an onChange handler.
|
|
476
|
+
for (const d of PLUGIN_DEPS.change)
|
|
477
|
+
ensure(d);
|
|
478
|
+
track(f);
|
|
479
|
+
}
|
|
480
|
+
let was;
|
|
481
|
+
let mounted = false;
|
|
482
|
+
return createEffect(() => {
|
|
483
|
+
const v = get();
|
|
484
|
+
if (controlProp) {
|
|
485
|
+
const now = v != null;
|
|
486
|
+
if (v === null && prop === 'value') {
|
|
487
|
+
warnOnce(f, '`value` prop on `' + f.tagName.toLowerCase() + '` should not be null. Use an empty string to clear it or `undefined` for an uncontrolled field.');
|
|
488
|
+
}
|
|
489
|
+
else if (was !== undefined && was !== now) {
|
|
490
|
+
warnOnce(f, 'A component is changing ' + (was ? 'a controlled' : 'an uncontrolled') + ' input to be ' +
|
|
491
|
+
(now ? 'controlled' : 'uncontrolled') +
|
|
492
|
+
'. Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://react.dev/link/controlled-components');
|
|
493
|
+
}
|
|
494
|
+
was = now;
|
|
495
|
+
}
|
|
496
|
+
krateSetControl(f, prop, v);
|
|
497
|
+
// React mounts a textarea with its caret at the start.
|
|
498
|
+
if (!mounted && prop === 'value' && f.tagName === 'TEXTAREA' && f.ownerDocument.activeElement !== f) {
|
|
499
|
+
f.setSelectionRange(0, 0);
|
|
500
|
+
}
|
|
501
|
+
mounted = true;
|
|
502
|
+
});
|
|
503
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|