@zag-js/number-input 0.0.0-dev-20230925133045 → 0.0.0-dev-20230926100120
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/index.d.mts +9 -26
- package/dist/index.d.ts +9 -26
- package/dist/index.js +254 -176
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +243 -165
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -9
- package/src/cursor.ts +51 -0
- package/src/number-input.anatomy.ts +1 -0
- package/src/number-input.connect.ts +72 -46
- package/src/number-input.machine.ts +138 -83
- package/src/number-input.types.ts +30 -26
- package/src/number-input.utils.ts +0 -42
package/dist/index.mjs
CHANGED
|
@@ -13,7 +13,7 @@ var parts = anatomy.build();
|
|
|
13
13
|
|
|
14
14
|
// src/number-input.connect.ts
|
|
15
15
|
import { getEventPoint, getEventStep, getNativeEvent, isLeftClick } from "@zag-js/dom-event";
|
|
16
|
-
import { ariaAttr, dataAttr } from "@zag-js/dom-query";
|
|
16
|
+
import { ariaAttr, dataAttr, isHTMLElement } from "@zag-js/dom-query";
|
|
17
17
|
import { roundToDevicePixel as roundToDevicePixel2 } from "@zag-js/number-utils";
|
|
18
18
|
|
|
19
19
|
// src/number-input.dom.ts
|
|
@@ -114,54 +114,15 @@ var dom = createScope({
|
|
|
114
114
|
}
|
|
115
115
|
});
|
|
116
116
|
|
|
117
|
-
// src/number-input.utils.ts
|
|
118
|
-
import { isModifiedEvent } from "@zag-js/dom-event";
|
|
119
|
-
import { clamp, decrement, formatDecimal, increment } from "@zag-js/number-utils";
|
|
120
|
-
var utils = {
|
|
121
|
-
isValidNumericEvent: (ctx, event) => {
|
|
122
|
-
if (event.key == null)
|
|
123
|
-
return true;
|
|
124
|
-
const isModifier = isModifiedEvent(event);
|
|
125
|
-
const isSingleKey = event.key.length === 1;
|
|
126
|
-
if (isModifier || !isSingleKey)
|
|
127
|
-
return true;
|
|
128
|
-
return ctx.validateCharacter?.(event.key) ?? utils.isFloatingPoint(event.key);
|
|
129
|
-
},
|
|
130
|
-
isFloatingPoint: (v) => /^[0-9+\-.]$/.test(v),
|
|
131
|
-
sanitize: (ctx, value) => {
|
|
132
|
-
return value.split("").filter(ctx.validateCharacter ?? utils.isFloatingPoint).join("");
|
|
133
|
-
},
|
|
134
|
-
increment: (ctx, step) => {
|
|
135
|
-
const value = increment(ctx.value, step ?? ctx.step);
|
|
136
|
-
return formatDecimal(clamp(value, ctx), ctx);
|
|
137
|
-
},
|
|
138
|
-
decrement: (ctx, step) => {
|
|
139
|
-
const value = decrement(ctx.value, step ?? ctx.step);
|
|
140
|
-
return formatDecimal(clamp(value, ctx), ctx);
|
|
141
|
-
},
|
|
142
|
-
clamp: (ctx) => {
|
|
143
|
-
return formatDecimal(clamp(ctx.value, ctx), ctx);
|
|
144
|
-
},
|
|
145
|
-
parse: (ctx, value) => {
|
|
146
|
-
return ctx.parse?.(value) ?? value;
|
|
147
|
-
},
|
|
148
|
-
format: (ctx, value) => {
|
|
149
|
-
const _val = value.toString();
|
|
150
|
-
return ctx.format?.(_val) ?? _val;
|
|
151
|
-
},
|
|
152
|
-
round: (ctx) => {
|
|
153
|
-
return formatDecimal(ctx.value, ctx);
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
|
|
157
117
|
// src/number-input.connect.ts
|
|
158
118
|
function connect(state, send, normalize) {
|
|
159
119
|
const isFocused = state.hasTag("focus");
|
|
160
120
|
const isInvalid = state.context.isOutOfRange || !!state.context.invalid;
|
|
161
|
-
const isDisabled = !!state.context.disabled;
|
|
162
121
|
const isValueEmpty = state.context.isValueEmpty;
|
|
163
|
-
const
|
|
164
|
-
const
|
|
122
|
+
const isDisabled = state.context.isDisabled;
|
|
123
|
+
const isReadOnly = state.context.readOnly;
|
|
124
|
+
const isIncrementDisabled = isDisabled || !state.context.canIncrement || isReadOnly;
|
|
125
|
+
const isDecrementDisabled = isDisabled || !state.context.canDecrement || isReadOnly;
|
|
165
126
|
const translations = state.context.translations;
|
|
166
127
|
return {
|
|
167
128
|
isFocused,
|
|
@@ -170,29 +131,26 @@ function connect(state, send, normalize) {
|
|
|
170
131
|
value: state.context.formattedValue,
|
|
171
132
|
valueAsNumber: state.context.valueAsNumber,
|
|
172
133
|
setValue(value) {
|
|
173
|
-
send({ type: "
|
|
134
|
+
send({ type: "VALUE.SET", value: value.toString() });
|
|
174
135
|
},
|
|
175
136
|
clearValue() {
|
|
176
|
-
send("
|
|
137
|
+
send("VALUE.CLEAR");
|
|
177
138
|
},
|
|
178
139
|
increment() {
|
|
179
|
-
send("INCREMENT");
|
|
140
|
+
send("VALUE.INCREMENT");
|
|
180
141
|
},
|
|
181
142
|
decrement() {
|
|
182
|
-
send("DECREMENT");
|
|
143
|
+
send("VALUE.DECREMENT");
|
|
183
144
|
},
|
|
184
145
|
setToMax() {
|
|
185
|
-
send({ type: "
|
|
146
|
+
send({ type: "VALUE.SET", value: state.context.max });
|
|
186
147
|
},
|
|
187
148
|
setToMin() {
|
|
188
|
-
send({ type: "
|
|
149
|
+
send({ type: "VALUE.SET", value: state.context.min });
|
|
189
150
|
},
|
|
190
151
|
focus() {
|
|
191
152
|
dom.getInputEl(state.context)?.focus();
|
|
192
153
|
},
|
|
193
|
-
blur() {
|
|
194
|
-
dom.getInputEl(state.context)?.blur();
|
|
195
|
-
},
|
|
196
154
|
rootProps: normalize.element({
|
|
197
155
|
id: dom.getRootId(state.context),
|
|
198
156
|
...parts.root.attrs,
|
|
@@ -242,44 +200,63 @@ function connect(state, send, normalize) {
|
|
|
242
200
|
"aria-roledescription": "numberfield",
|
|
243
201
|
"aria-valuemin": state.context.min,
|
|
244
202
|
"aria-valuemax": state.context.max,
|
|
245
|
-
"aria-valuenow": isNaN(state.context.valueAsNumber) ? void 0 : state.context.valueAsNumber,
|
|
203
|
+
"aria-valuenow": Number.isNaN(state.context.valueAsNumber) ? void 0 : state.context.valueAsNumber,
|
|
246
204
|
"aria-valuetext": state.context.valueText,
|
|
205
|
+
onCompositionStart() {
|
|
206
|
+
send("INPUT.COMPOSITION_START");
|
|
207
|
+
},
|
|
208
|
+
onCompositionEnd() {
|
|
209
|
+
send("INPUT.COMPOSITION_END");
|
|
210
|
+
},
|
|
247
211
|
onFocus() {
|
|
248
|
-
send("FOCUS");
|
|
212
|
+
send("INPUT.FOCUS");
|
|
249
213
|
},
|
|
250
214
|
onBlur() {
|
|
251
|
-
send("
|
|
215
|
+
send({ type: "INPUT.COMMIT", src: "blur" });
|
|
252
216
|
},
|
|
253
217
|
onChange(event) {
|
|
254
|
-
send({ type: "CHANGE", target: event.currentTarget, hint: "set" });
|
|
218
|
+
send({ type: "INPUT.CHANGE", target: event.currentTarget, hint: "set" });
|
|
219
|
+
},
|
|
220
|
+
onBeforeInput(event) {
|
|
221
|
+
try {
|
|
222
|
+
const { selectionStart, selectionEnd, value } = event.currentTarget;
|
|
223
|
+
const nextValue = value.slice(0, selectionStart) + event.data + value.slice(selectionEnd);
|
|
224
|
+
const isValid = state.context.parser.isValidPartialNumber(nextValue);
|
|
225
|
+
if (!isValid) {
|
|
226
|
+
event.preventDefault();
|
|
227
|
+
}
|
|
228
|
+
} catch {
|
|
229
|
+
}
|
|
255
230
|
},
|
|
256
231
|
onKeyDown(event) {
|
|
257
|
-
|
|
258
|
-
if (evt.isComposing)
|
|
232
|
+
if (state.context.readOnly)
|
|
259
233
|
return;
|
|
260
|
-
if (!utils.isValidNumericEvent(state.context, event)) {
|
|
261
|
-
event.preventDefault();
|
|
262
|
-
}
|
|
263
234
|
const step = getEventStep(event) * state.context.step;
|
|
264
235
|
const keyMap = {
|
|
265
236
|
ArrowUp() {
|
|
266
|
-
send({ type: "ARROW_UP", step });
|
|
237
|
+
send({ type: "INPUT.ARROW_UP", step });
|
|
238
|
+
event.preventDefault();
|
|
267
239
|
},
|
|
268
240
|
ArrowDown() {
|
|
269
|
-
send({ type: "ARROW_DOWN", step });
|
|
241
|
+
send({ type: "INPUT.ARROW_DOWN", step });
|
|
242
|
+
event.preventDefault();
|
|
270
243
|
},
|
|
271
244
|
Home() {
|
|
272
|
-
send("HOME");
|
|
245
|
+
send("INPUT.HOME");
|
|
246
|
+
event.preventDefault();
|
|
273
247
|
},
|
|
274
248
|
End() {
|
|
275
|
-
send("END");
|
|
249
|
+
send("INPUT.END");
|
|
250
|
+
event.preventDefault();
|
|
251
|
+
},
|
|
252
|
+
Enter() {
|
|
253
|
+
if (state.context.composing)
|
|
254
|
+
return;
|
|
255
|
+
send({ type: "INPUT.COMMIT", src: "enter" });
|
|
276
256
|
}
|
|
277
257
|
};
|
|
278
258
|
const exec = keyMap[event.key];
|
|
279
|
-
|
|
280
|
-
exec(event);
|
|
281
|
-
event.preventDefault();
|
|
282
|
-
}
|
|
259
|
+
exec?.(event);
|
|
283
260
|
}
|
|
284
261
|
}),
|
|
285
262
|
decrementTriggerProps: normalize.button({
|
|
@@ -293,18 +270,23 @@ function connect(state, send, normalize) {
|
|
|
293
270
|
tabIndex: -1,
|
|
294
271
|
"aria-controls": dom.getInputId(state.context),
|
|
295
272
|
onPointerDown(event) {
|
|
296
|
-
if (isDecrementDisabled)
|
|
273
|
+
if (isDecrementDisabled || !isLeftClick(event))
|
|
297
274
|
return;
|
|
298
|
-
|
|
299
|
-
|
|
275
|
+
if (event.pointerType === "mouse") {
|
|
276
|
+
send({ type: "TRIGGER.PRESS_DOWN", hint: "decrement" });
|
|
277
|
+
return event.preventDefault();
|
|
278
|
+
}
|
|
279
|
+
if (isHTMLElement(event.target)) {
|
|
280
|
+
event.target?.focus();
|
|
281
|
+
}
|
|
300
282
|
},
|
|
301
283
|
onPointerUp() {
|
|
302
|
-
send({ type: "PRESS_UP", hint: "decrement" });
|
|
284
|
+
send({ type: "TRIGGER.PRESS_UP", hint: "decrement" });
|
|
303
285
|
},
|
|
304
286
|
onPointerLeave() {
|
|
305
287
|
if (isDecrementDisabled)
|
|
306
288
|
return;
|
|
307
|
-
send({ type: "PRESS_UP", hint: "decrement" });
|
|
289
|
+
send({ type: "TRIGGER.PRESS_UP", hint: "decrement" });
|
|
308
290
|
}
|
|
309
291
|
}),
|
|
310
292
|
incrementTriggerProps: normalize.button({
|
|
@@ -318,16 +300,21 @@ function connect(state, send, normalize) {
|
|
|
318
300
|
tabIndex: -1,
|
|
319
301
|
"aria-controls": dom.getInputId(state.context),
|
|
320
302
|
onPointerDown(event) {
|
|
321
|
-
if (isIncrementDisabled)
|
|
303
|
+
if (isIncrementDisabled || !isLeftClick(event))
|
|
322
304
|
return;
|
|
323
|
-
|
|
324
|
-
|
|
305
|
+
if (event.pointerType === "mouse") {
|
|
306
|
+
send({ type: "TRIGGER.PRESS_DOWN", hint: "increment" });
|
|
307
|
+
return event.preventDefault();
|
|
308
|
+
}
|
|
309
|
+
if (isHTMLElement(event.target)) {
|
|
310
|
+
event.target?.focus();
|
|
311
|
+
}
|
|
325
312
|
},
|
|
326
313
|
onPointerUp() {
|
|
327
|
-
send({ type: "PRESS_UP", hint: "increment" });
|
|
314
|
+
send({ type: "TRIGGER.PRESS_UP", hint: "increment" });
|
|
328
315
|
},
|
|
329
316
|
onPointerLeave() {
|
|
330
|
-
send({ type: "PRESS_UP", hint: "increment" });
|
|
317
|
+
send({ type: "TRIGGER.PRESS_UP", hint: "increment" });
|
|
331
318
|
}
|
|
332
319
|
}),
|
|
333
320
|
scrubberProps: normalize.element({
|
|
@@ -342,7 +329,7 @@ function connect(state, send, normalize) {
|
|
|
342
329
|
const point = getEventPoint(evt);
|
|
343
330
|
point.x = point.x - roundToDevicePixel2(7.5);
|
|
344
331
|
point.y = point.y - roundToDevicePixel2(7.5);
|
|
345
|
-
send({ type: "
|
|
332
|
+
send({ type: "SCRUBBER.PRESS_DOWN", point });
|
|
346
333
|
event.preventDefault();
|
|
347
334
|
},
|
|
348
335
|
style: {
|
|
@@ -353,12 +340,57 @@ function connect(state, send, normalize) {
|
|
|
353
340
|
}
|
|
354
341
|
|
|
355
342
|
// src/number-input.machine.ts
|
|
356
|
-
import {
|
|
343
|
+
import { NumberParser } from "@internationalized/number";
|
|
344
|
+
import { choose, createMachine, guards, ref } from "@zag-js/core";
|
|
357
345
|
import { addDomEvent, requestPointerLock } from "@zag-js/dom-event";
|
|
358
346
|
import { isSafari as isSafari2, raf } from "@zag-js/dom-query";
|
|
347
|
+
import { trackFormControl } from "@zag-js/form-utils";
|
|
359
348
|
import { observeAttributes } from "@zag-js/mutation-observer";
|
|
360
|
-
import { isAtMax, isAtMin, isWithinRange
|
|
361
|
-
import { callAll, compact, isEqual
|
|
349
|
+
import { clamp, decrement, increment, isAtMax, isAtMin, isWithinRange } from "@zag-js/number-utils";
|
|
350
|
+
import { callAll, compact, isEqual } from "@zag-js/utils";
|
|
351
|
+
|
|
352
|
+
// src/cursor.ts
|
|
353
|
+
function recordCursor(inputEl) {
|
|
354
|
+
if (inputEl.ownerDocument.activeElement !== inputEl)
|
|
355
|
+
return;
|
|
356
|
+
try {
|
|
357
|
+
const { selectionStart: start, selectionEnd: end, value } = inputEl;
|
|
358
|
+
const beforeTxt = value.substring(0, start);
|
|
359
|
+
const afterTxt = value.substring(end);
|
|
360
|
+
return {
|
|
361
|
+
start,
|
|
362
|
+
end,
|
|
363
|
+
value,
|
|
364
|
+
beforeTxt,
|
|
365
|
+
afterTxt
|
|
366
|
+
};
|
|
367
|
+
} catch {
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
function restoreCursor(inputEl, selection) {
|
|
371
|
+
if (inputEl.ownerDocument.activeElement !== inputEl || !selection)
|
|
372
|
+
return;
|
|
373
|
+
try {
|
|
374
|
+
const { value } = inputEl;
|
|
375
|
+
const { beforeTxt = "", afterTxt = "", start } = selection;
|
|
376
|
+
let startPos = value.length;
|
|
377
|
+
if (value.endsWith(afterTxt)) {
|
|
378
|
+
startPos = value.length - afterTxt.length;
|
|
379
|
+
} else if (value.startsWith(beforeTxt)) {
|
|
380
|
+
startPos = beforeTxt.length;
|
|
381
|
+
} else if (start != null) {
|
|
382
|
+
const beforeLastChar = beforeTxt[start - 1];
|
|
383
|
+
const newIndex = value.indexOf(beforeLastChar, start - 1);
|
|
384
|
+
if (newIndex !== -1) {
|
|
385
|
+
startPos = newIndex + 1;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
inputEl.setSelectionRange(startPos, startPos);
|
|
389
|
+
} catch {
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// src/number-input.machine.ts
|
|
362
394
|
var { not, and } = guards;
|
|
363
395
|
function machine(userContext) {
|
|
364
396
|
const ctx = compact(userContext);
|
|
@@ -368,20 +400,25 @@ function machine(userContext) {
|
|
|
368
400
|
initial: "idle",
|
|
369
401
|
context: {
|
|
370
402
|
dir: "ltr",
|
|
403
|
+
locale: "en-US",
|
|
371
404
|
focusInputOnChange: true,
|
|
372
405
|
clampValueOnBlur: true,
|
|
373
406
|
allowOverflow: false,
|
|
374
407
|
inputMode: "decimal",
|
|
375
408
|
pattern: "[0-9]*(.[0-9]+)?",
|
|
376
|
-
hint: null,
|
|
377
409
|
value: "",
|
|
378
410
|
step: 1,
|
|
379
411
|
min: Number.MIN_SAFE_INTEGER,
|
|
380
412
|
max: Number.MAX_SAFE_INTEGER,
|
|
381
|
-
scrubberCursorPoint: null,
|
|
382
413
|
invalid: false,
|
|
383
414
|
spinOnPress: true,
|
|
384
415
|
...ctx,
|
|
416
|
+
hint: null,
|
|
417
|
+
scrubberCursorPoint: null,
|
|
418
|
+
composing: false,
|
|
419
|
+
fieldsetDisabled: false,
|
|
420
|
+
formatter: createFormatter(ctx.locale || "en-US", ctx.formatOptions),
|
|
421
|
+
parser: createParser(ctx.locale || "en-US", ctx.formatOptions),
|
|
385
422
|
translations: {
|
|
386
423
|
incrementLabel: "increment value",
|
|
387
424
|
decrementLabel: "decrease value",
|
|
@@ -390,48 +427,56 @@ function machine(userContext) {
|
|
|
390
427
|
},
|
|
391
428
|
computed: {
|
|
392
429
|
isRtl: (ctx2) => ctx2.dir === "rtl",
|
|
393
|
-
valueAsNumber: (ctx2) =>
|
|
430
|
+
valueAsNumber: (ctx2) => ctx2.parser.parse(String(ctx2.value)) ?? 0,
|
|
394
431
|
isAtMin: (ctx2) => isAtMin(ctx2.value, ctx2),
|
|
395
432
|
isAtMax: (ctx2) => isAtMax(ctx2.value, ctx2),
|
|
396
433
|
isOutOfRange: (ctx2) => !isWithinRange(ctx2.value, ctx2),
|
|
397
434
|
isValueEmpty: (ctx2) => ctx2.value === "",
|
|
435
|
+
isDisabled: (ctx2) => !!ctx2.disabled || ctx2.fieldsetDisabled,
|
|
398
436
|
canIncrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMax,
|
|
399
437
|
canDecrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMin,
|
|
400
438
|
valueText: (ctx2) => ctx2.translations.valueText?.(ctx2.value),
|
|
401
|
-
formattedValue: (ctx2) =>
|
|
439
|
+
formattedValue: (ctx2) => {
|
|
440
|
+
if (Number.isNaN(ctx2.valueAsNumber))
|
|
441
|
+
return ctx2.value;
|
|
442
|
+
return ctx2.formatter.format(ctx2.valueAsNumber);
|
|
443
|
+
}
|
|
402
444
|
},
|
|
403
445
|
watch: {
|
|
446
|
+
formatOptions: ["setFormatterAndParser", "syncInputElement"],
|
|
447
|
+
locale: ["setFormatterAndParser", "syncInputElement"],
|
|
404
448
|
value: ["syncInputElement"],
|
|
405
449
|
isOutOfRange: ["invokeOnInvalid"],
|
|
406
450
|
scrubberCursorPoint: ["setVirtualCursorPosition"]
|
|
407
451
|
},
|
|
408
452
|
entry: ["syncInputValue"],
|
|
453
|
+
activities: ["trackFormControl"],
|
|
409
454
|
on: {
|
|
410
|
-
|
|
455
|
+
"VALUE.SET": {
|
|
411
456
|
actions: ["setValue", "clampValue", "setHintToSet"]
|
|
412
457
|
},
|
|
413
|
-
|
|
458
|
+
"VALUE.CLEAR": {
|
|
414
459
|
actions: ["clearValue"]
|
|
415
460
|
},
|
|
416
|
-
INCREMENT: {
|
|
461
|
+
"VALUE.INCREMENT": {
|
|
417
462
|
actions: ["increment"]
|
|
418
463
|
},
|
|
419
|
-
DECREMENT: {
|
|
464
|
+
"VALUE.DECREMENT": {
|
|
420
465
|
actions: ["decrement"]
|
|
421
466
|
}
|
|
422
467
|
},
|
|
423
468
|
states: {
|
|
424
469
|
idle: {
|
|
425
470
|
on: {
|
|
426
|
-
PRESS_DOWN: {
|
|
471
|
+
"TRIGGER.PRESS_DOWN": {
|
|
427
472
|
target: "before:spin",
|
|
428
473
|
actions: ["focusInput", "invokeOnFocus", "setHint"]
|
|
429
474
|
},
|
|
430
|
-
|
|
475
|
+
"SCRUBBER.PRESS_DOWN": {
|
|
431
476
|
target: "scrubbing",
|
|
432
477
|
actions: ["focusInput", "invokeOnFocus", "setHint", "setCursorPoint"]
|
|
433
478
|
},
|
|
434
|
-
FOCUS: {
|
|
479
|
+
"INPUT.FOCUS": {
|
|
435
480
|
target: "focused",
|
|
436
481
|
actions: ["focusInput", "invokeOnFocus"]
|
|
437
482
|
}
|
|
@@ -442,45 +487,46 @@ function machine(userContext) {
|
|
|
442
487
|
entry: "focusInput",
|
|
443
488
|
activities: "attachWheelListener",
|
|
444
489
|
on: {
|
|
445
|
-
PRESS_DOWN: {
|
|
490
|
+
"TRIGGER.PRESS_DOWN": {
|
|
446
491
|
target: "before:spin",
|
|
447
492
|
actions: ["focusInput", "setHint"]
|
|
448
493
|
},
|
|
449
|
-
|
|
494
|
+
"SCRUBBER.PRESS_DOWN": {
|
|
450
495
|
target: "scrubbing",
|
|
451
496
|
actions: ["focusInput", "setHint", "setCursorPoint"]
|
|
452
497
|
},
|
|
453
|
-
ARROW_UP: {
|
|
498
|
+
"INPUT.ARROW_UP": {
|
|
454
499
|
actions: "increment"
|
|
455
500
|
},
|
|
456
|
-
ARROW_DOWN: {
|
|
501
|
+
"INPUT.ARROW_DOWN": {
|
|
457
502
|
actions: "decrement"
|
|
458
503
|
},
|
|
459
|
-
HOME: {
|
|
460
|
-
actions: "
|
|
504
|
+
"INPUT.HOME": {
|
|
505
|
+
actions: "decrementToMin"
|
|
461
506
|
},
|
|
462
|
-
END: {
|
|
463
|
-
actions: "
|
|
507
|
+
"INPUT.END": {
|
|
508
|
+
actions: "incrementToMax"
|
|
464
509
|
},
|
|
465
|
-
CHANGE: {
|
|
510
|
+
"INPUT.CHANGE": {
|
|
466
511
|
actions: ["setValue", "setHint"]
|
|
467
512
|
},
|
|
468
|
-
|
|
469
|
-
{
|
|
470
|
-
guard: "isInvalidExponential",
|
|
471
|
-
target: "idle",
|
|
472
|
-
actions: ["clearValue", "clearHint", "invokeOnBlur"]
|
|
473
|
-
},
|
|
513
|
+
"INPUT.COMMIT": [
|
|
474
514
|
{
|
|
475
|
-
guard: and("
|
|
515
|
+
guard: and("clampValueOnBlur", not("isInRange")),
|
|
476
516
|
target: "idle",
|
|
477
|
-
actions: ["clampValue", "clearHint", "invokeOnBlur"]
|
|
517
|
+
actions: ["clampValue", "syncInputElement", "clearHint", "invokeOnBlur"]
|
|
478
518
|
},
|
|
479
519
|
{
|
|
480
520
|
target: "idle",
|
|
481
|
-
actions: ["
|
|
521
|
+
actions: ["syncInputElement", "invokeOnBlur"]
|
|
482
522
|
}
|
|
483
|
-
]
|
|
523
|
+
],
|
|
524
|
+
"INPUT.COMPOSITION_START": {
|
|
525
|
+
actions: "setComposing"
|
|
526
|
+
},
|
|
527
|
+
"INPUT.COMPOSITION_END": {
|
|
528
|
+
actions: "clearComposing"
|
|
529
|
+
}
|
|
484
530
|
}
|
|
485
531
|
},
|
|
486
532
|
"before:spin": {
|
|
@@ -497,7 +543,7 @@ function machine(userContext) {
|
|
|
497
543
|
}
|
|
498
544
|
},
|
|
499
545
|
on: {
|
|
500
|
-
PRESS_UP: {
|
|
546
|
+
"TRIGGER.PRESS_UP": {
|
|
501
547
|
target: "focused",
|
|
502
548
|
actions: "clearHint"
|
|
503
549
|
}
|
|
@@ -519,7 +565,7 @@ function machine(userContext) {
|
|
|
519
565
|
}
|
|
520
566
|
],
|
|
521
567
|
on: {
|
|
522
|
-
PRESS_UP: {
|
|
568
|
+
"TRIGGER.PRESS_UP": {
|
|
523
569
|
target: "focused",
|
|
524
570
|
actions: "clearHint"
|
|
525
571
|
}
|
|
@@ -530,8 +576,8 @@ function machine(userContext) {
|
|
|
530
576
|
exit: "clearCursorPoint",
|
|
531
577
|
activities: ["activatePointerLock", "trackMousemove", "setupVirtualCursor", "preventTextSelection"],
|
|
532
578
|
on: {
|
|
533
|
-
|
|
534
|
-
|
|
579
|
+
"SCRUBBER.POINTER_UP": "focused",
|
|
580
|
+
"SCRUBBER.POINTER_MOVE": [
|
|
535
581
|
{
|
|
536
582
|
guard: "isIncrementHint",
|
|
537
583
|
actions: ["increment", "setCursorPoint"]
|
|
@@ -551,17 +597,26 @@ function machine(userContext) {
|
|
|
551
597
|
CHANGE_DELAY: 300
|
|
552
598
|
},
|
|
553
599
|
guards: {
|
|
554
|
-
|
|
600
|
+
clampValueOnBlur: (ctx2) => ctx2.clampValueOnBlur,
|
|
555
601
|
isAtMin: (ctx2) => ctx2.isAtMin,
|
|
556
602
|
spinOnPress: (ctx2) => !!ctx2.spinOnPress,
|
|
557
603
|
isAtMax: (ctx2) => ctx2.isAtMax,
|
|
558
604
|
isInRange: (ctx2) => !ctx2.isOutOfRange,
|
|
559
605
|
isDecrementHint: (ctx2, evt) => (evt.hint ?? ctx2.hint) === "decrement",
|
|
560
|
-
|
|
561
|
-
isIncrementHint: (ctx2, evt) => (evt.hint ?? ctx2.hint) === "increment",
|
|
562
|
-
isInvalidExponential: (ctx2) => ctx2.value.toString().startsWith("e")
|
|
606
|
+
isIncrementHint: (ctx2, evt) => (evt.hint ?? ctx2.hint) === "increment"
|
|
563
607
|
},
|
|
564
608
|
activities: {
|
|
609
|
+
trackFormControl(ctx2, _evt, { initialContext }) {
|
|
610
|
+
const inputEl = dom.getInputEl(ctx2);
|
|
611
|
+
return trackFormControl(inputEl, {
|
|
612
|
+
onFieldsetDisabledChange(disabled) {
|
|
613
|
+
ctx2.fieldsetDisabled = disabled;
|
|
614
|
+
},
|
|
615
|
+
onFormReset() {
|
|
616
|
+
set.value(ctx2, initialContext.value);
|
|
617
|
+
}
|
|
618
|
+
});
|
|
619
|
+
},
|
|
565
620
|
setupVirtualCursor(ctx2) {
|
|
566
621
|
return dom.setupVirtualCursor(ctx2);
|
|
567
622
|
},
|
|
@@ -571,26 +626,23 @@ function machine(userContext) {
|
|
|
571
626
|
trackButtonDisabled(ctx2, _evt, { send }) {
|
|
572
627
|
const btn = dom.getPressedTriggerEl(ctx2, ctx2.hint);
|
|
573
628
|
return observeAttributes(btn, ["disabled"], () => {
|
|
574
|
-
send("PRESS_UP");
|
|
629
|
+
send({ type: "TRIGGER.PRESS_UP", src: "attr" });
|
|
575
630
|
});
|
|
576
631
|
},
|
|
577
632
|
attachWheelListener(ctx2, _evt, { send }) {
|
|
578
|
-
const
|
|
579
|
-
if (!
|
|
633
|
+
const inputEl = dom.getInputEl(ctx2);
|
|
634
|
+
if (!inputEl || !dom.isActiveElement(ctx2, inputEl) || !ctx2.allowMouseWheel)
|
|
580
635
|
return;
|
|
581
636
|
function onWheel(event) {
|
|
582
|
-
const isInputFocused = dom.getDoc(ctx2).activeElement === input;
|
|
583
|
-
if (!ctx2.allowMouseWheel || !isInputFocused)
|
|
584
|
-
return;
|
|
585
637
|
event.preventDefault();
|
|
586
638
|
const dir = Math.sign(event.deltaY) * -1;
|
|
587
639
|
if (dir === 1) {
|
|
588
|
-
send("INCREMENT");
|
|
640
|
+
send("VALUE.INCREMENT");
|
|
589
641
|
} else if (dir === -1) {
|
|
590
|
-
send("DECREMENT");
|
|
642
|
+
send("VALUE.DECREMENT");
|
|
591
643
|
}
|
|
592
644
|
}
|
|
593
|
-
return addDomEvent(
|
|
645
|
+
return addDomEvent(inputEl, "wheel", onWheel, { passive: false });
|
|
594
646
|
},
|
|
595
647
|
activatePointerLock(ctx2) {
|
|
596
648
|
if (isSafari2())
|
|
@@ -606,13 +658,13 @@ function machine(userContext) {
|
|
|
606
658
|
if (!value.hint)
|
|
607
659
|
return;
|
|
608
660
|
send({
|
|
609
|
-
type: "
|
|
661
|
+
type: "SCRUBBER.POINTER_MOVE",
|
|
610
662
|
hint: value.hint,
|
|
611
663
|
point: value.point
|
|
612
664
|
});
|
|
613
665
|
}
|
|
614
666
|
function onMouseup() {
|
|
615
|
-
send("
|
|
667
|
+
send("SCRUBBER.POINTER_UP");
|
|
616
668
|
}
|
|
617
669
|
return callAll(
|
|
618
670
|
addDomEvent(doc, "mousemove", onMousemove, false),
|
|
@@ -625,37 +677,34 @@ function machine(userContext) {
|
|
|
625
677
|
if (!ctx2.focusInputOnChange)
|
|
626
678
|
return;
|
|
627
679
|
const inputEl = dom.getInputEl(ctx2);
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
});
|
|
680
|
+
if (dom.isActiveElement(ctx2, inputEl))
|
|
681
|
+
return;
|
|
682
|
+
raf(() => inputEl?.focus({ preventScroll: true }));
|
|
631
683
|
},
|
|
632
684
|
increment(ctx2, evt) {
|
|
633
|
-
|
|
685
|
+
const nextValue = increment(ctx2.value, evt.step ?? ctx2.step);
|
|
686
|
+
set.value(ctx2, String(clamp(nextValue, ctx2)));
|
|
634
687
|
},
|
|
635
688
|
decrement(ctx2, evt) {
|
|
636
|
-
|
|
689
|
+
const nextValue = decrement(ctx2.value, evt.step ?? ctx2.step);
|
|
690
|
+
set.value(ctx2, String(clamp(nextValue, ctx2)));
|
|
637
691
|
},
|
|
638
692
|
clampValue(ctx2) {
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
roundValue(ctx2) {
|
|
642
|
-
if (ctx2.value === "")
|
|
643
|
-
return;
|
|
644
|
-
set.value(ctx2, utils.round(ctx2));
|
|
693
|
+
const nextValue = clamp(ctx2.value, ctx2);
|
|
694
|
+
set.value(ctx2, String(nextValue));
|
|
645
695
|
},
|
|
646
696
|
setValue(ctx2, evt) {
|
|
647
|
-
|
|
648
|
-
value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
|
|
697
|
+
const value = evt.target?.value ?? evt.value;
|
|
649
698
|
set.value(ctx2, value);
|
|
650
699
|
},
|
|
651
700
|
clearValue(ctx2) {
|
|
652
701
|
set.value(ctx2, "");
|
|
653
702
|
},
|
|
654
|
-
|
|
655
|
-
set.value(ctx2, ctx2.max
|
|
703
|
+
incrementToMax(ctx2) {
|
|
704
|
+
set.value(ctx2, String(ctx2.max));
|
|
656
705
|
},
|
|
657
|
-
|
|
658
|
-
set.value(ctx2, ctx2.min
|
|
706
|
+
decrementToMin(ctx2) {
|
|
707
|
+
set.value(ctx2, String(ctx2.min));
|
|
659
708
|
},
|
|
660
709
|
setHint(ctx2, evt) {
|
|
661
710
|
ctx2.hint = evt.hint;
|
|
@@ -666,15 +715,9 @@ function machine(userContext) {
|
|
|
666
715
|
setHintToSet(ctx2) {
|
|
667
716
|
ctx2.hint = "set";
|
|
668
717
|
},
|
|
669
|
-
invokeOnFocus(ctx2
|
|
670
|
-
const srcElement = match(evt.type, {
|
|
671
|
-
PRESS_DOWN: dom.getPressedTriggerEl(ctx2, evt.hint),
|
|
672
|
-
FOCUS: dom.getInputEl(ctx2),
|
|
673
|
-
PRESS_DOWN_SCRUBBER: dom.getScrubberEl(ctx2)
|
|
674
|
-
});
|
|
718
|
+
invokeOnFocus(ctx2) {
|
|
675
719
|
ctx2.onFocusChange?.({
|
|
676
720
|
focused: true,
|
|
677
|
-
srcElement,
|
|
678
721
|
value: ctx2.formattedValue,
|
|
679
722
|
valueAsNumber: ctx2.valueAsNumber
|
|
680
723
|
});
|
|
@@ -696,16 +739,20 @@ function machine(userContext) {
|
|
|
696
739
|
valueAsNumber: ctx2.valueAsNumber
|
|
697
740
|
});
|
|
698
741
|
},
|
|
699
|
-
syncInputElement(ctx2) {
|
|
742
|
+
syncInputElement(ctx2, evt) {
|
|
700
743
|
const inputEl = dom.getInputEl(ctx2);
|
|
701
|
-
|
|
744
|
+
const inputValue = evt.type.endsWith("CHANGE") ? ctx2.value : ctx2.formattedValue;
|
|
745
|
+
const sel = recordCursor(inputEl);
|
|
746
|
+
dom.setValue(inputEl, inputValue);
|
|
747
|
+
raf(() => {
|
|
748
|
+
restoreCursor(inputEl, sel);
|
|
749
|
+
});
|
|
702
750
|
},
|
|
703
751
|
syncInputValue(ctx2) {
|
|
704
752
|
const inputEl = dom.getInputEl(ctx2);
|
|
705
753
|
if (!inputEl || inputEl.value == ctx2.value)
|
|
706
754
|
return;
|
|
707
|
-
|
|
708
|
-
set.value(ctx2, utils.sanitize(ctx2, value));
|
|
755
|
+
set.value(ctx2, ctx2.formattedValue);
|
|
709
756
|
},
|
|
710
757
|
setCursorPoint(ctx2, evt) {
|
|
711
758
|
ctx2.scrubberCursorPoint = evt.point;
|
|
@@ -714,19 +761,50 @@ function machine(userContext) {
|
|
|
714
761
|
ctx2.scrubberCursorPoint = null;
|
|
715
762
|
},
|
|
716
763
|
setVirtualCursorPosition(ctx2) {
|
|
717
|
-
const
|
|
718
|
-
if (!
|
|
764
|
+
const cursorEl = dom.getCursorEl(ctx2);
|
|
765
|
+
if (!cursorEl || !ctx2.scrubberCursorPoint)
|
|
719
766
|
return;
|
|
720
767
|
const { x, y } = ctx2.scrubberCursorPoint;
|
|
721
|
-
|
|
768
|
+
cursorEl.style.transform = `translate3d(${x}px, ${y}px, 0px)`;
|
|
769
|
+
},
|
|
770
|
+
setComposing(ctx2) {
|
|
771
|
+
ctx2.composing = true;
|
|
772
|
+
},
|
|
773
|
+
clearComposing(ctx2) {
|
|
774
|
+
ctx2.composing = false;
|
|
775
|
+
},
|
|
776
|
+
setFormatterAndParser(ctx2) {
|
|
777
|
+
if (!ctx2.locale)
|
|
778
|
+
return;
|
|
779
|
+
ctx2.formatter = createFormatter(ctx2.locale, ctx2.formatOptions);
|
|
780
|
+
ctx2.parser = createParser(ctx2.locale, ctx2.formatOptions);
|
|
722
781
|
}
|
|
782
|
+
},
|
|
783
|
+
compareFns: {
|
|
784
|
+
formatOptions: (a, b) => isEqual(a, b)
|
|
723
785
|
}
|
|
724
786
|
}
|
|
725
787
|
);
|
|
726
788
|
}
|
|
789
|
+
var defaultFormatOptions = {
|
|
790
|
+
style: "decimal",
|
|
791
|
+
minimumFractionDigits: 0,
|
|
792
|
+
maximumFractionDigits: 20
|
|
793
|
+
};
|
|
794
|
+
var createFormatter = (locale, options = {}) => {
|
|
795
|
+
const formatOptions = Object.assign({}, defaultFormatOptions, options);
|
|
796
|
+
return ref(new Intl.NumberFormat(locale, formatOptions));
|
|
797
|
+
};
|
|
798
|
+
var createParser = (locale, options = {}) => {
|
|
799
|
+
const formatOptions = Object.assign({}, defaultFormatOptions, options);
|
|
800
|
+
return ref(new NumberParser(locale, formatOptions));
|
|
801
|
+
};
|
|
727
802
|
var invoke = {
|
|
728
803
|
onChange: (ctx) => {
|
|
729
|
-
ctx.onValueChange?.({
|
|
804
|
+
ctx.onValueChange?.({
|
|
805
|
+
value: ctx.value,
|
|
806
|
+
valueAsNumber: ctx.valueAsNumber
|
|
807
|
+
});
|
|
730
808
|
}
|
|
731
809
|
};
|
|
732
810
|
var set = {
|