@zag-js/number-input 1.2.0 → 1.3.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/index.d.mts CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as _zag_js_anatomy from '@zag-js/anatomy';
2
- import { LocaleProperties, CommonProperties, PropTypes, RequiredBy, NormalizeProps } from '@zag-js/types';
2
+ import { RequiredBy, LocaleProperties, CommonProperties, PropTypes, NormalizeProps } from '@zag-js/types';
3
3
  import { NumberFormatter, NumberParser } from '@internationalized/number';
4
4
  import * as _zag_js_core from '@zag-js/core';
5
- import { Service, Machine, EventObject } from '@zag-js/core';
5
+ import { Service, EventObject, Machine } from '@zag-js/core';
6
6
 
7
7
  declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "label" | "input" | "control" | "valueText" | "incrementTrigger" | "decrementTrigger" | "scrubber">;
8
8
 
@@ -206,7 +206,7 @@ type ComputedContext = Readonly<{
206
206
  */
207
207
  parser: NumberParser;
208
208
  }>;
209
- type HintValue = "increment" | "decrement" | "set";
209
+ type HintValue = "increment" | "decrement";
210
210
  interface PrivateContext {
211
211
  /**
212
212
  * The hint that determines if we're incrementing or decrementing
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as _zag_js_anatomy from '@zag-js/anatomy';
2
- import { LocaleProperties, CommonProperties, PropTypes, RequiredBy, NormalizeProps } from '@zag-js/types';
2
+ import { RequiredBy, LocaleProperties, CommonProperties, PropTypes, NormalizeProps } from '@zag-js/types';
3
3
  import { NumberFormatter, NumberParser } from '@internationalized/number';
4
4
  import * as _zag_js_core from '@zag-js/core';
5
- import { Service, Machine, EventObject } from '@zag-js/core';
5
+ import { Service, EventObject, Machine } from '@zag-js/core';
6
6
 
7
7
  declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "label" | "input" | "control" | "valueText" | "incrementTrigger" | "decrementTrigger" | "scrubber">;
8
8
 
@@ -206,7 +206,7 @@ type ComputedContext = Readonly<{
206
206
  */
207
207
  parser: NumberParser;
208
208
  }>;
209
- type HintValue = "increment" | "decrement" | "set";
209
+ type HintValue = "increment" | "decrement";
210
210
  interface PrivateContext {
211
211
  /**
212
212
  * The hint that determines if we're incrementing or decrementing
package/dist/index.js CHANGED
@@ -418,7 +418,7 @@ var machine = createMachine({
418
418
  dir: "ltr",
419
419
  locale: "en-US",
420
420
  focusInputOnChange: true,
421
- clampValueOnBlur: true,
421
+ clampValueOnBlur: !props2.allowOverflow,
422
422
  allowOverflow: false,
423
423
  inputMode: "decimal",
424
424
  pattern: "[0-9]*(.[0-9]+)?",
@@ -493,7 +493,7 @@ var machine = createMachine({
493
493
  effects: ["trackFormControl"],
494
494
  on: {
495
495
  "VALUE.SET": {
496
- actions: ["setRawValue", "setHintToSet"]
496
+ actions: ["setRawValue"]
497
497
  },
498
498
  "VALUE.CLEAR": {
499
499
  actions: ["clearValue"]
@@ -558,6 +558,11 @@ var machine = createMachine({
558
558
  target: "idle",
559
559
  actions: ["setClampedValue", "clearHint", "invokeOnBlur"]
560
560
  },
561
+ {
562
+ guard: not("isInRange"),
563
+ target: "idle",
564
+ actions: ["setFormattedValue", "clearHint", "invokeOnBlur", "invokeOnInvalid"]
565
+ },
561
566
  {
562
567
  target: "idle",
563
568
  actions: ["setFormattedValue", "clearHint", "invokeOnBlur"]
@@ -592,11 +597,11 @@ var machine = createMachine({
592
597
  on: {
593
598
  SPIN: [
594
599
  {
595
- guard: and(not("isAtMin"), "isIncrementHint"),
600
+ guard: "isIncrementHint",
596
601
  actions: ["increment"]
597
602
  },
598
603
  {
599
- guard: and(not("isAtMax"), "isDecrementHint"),
604
+ guard: "isDecrementHint",
600
605
  actions: ["decrement"]
601
606
  }
602
607
  ],
@@ -630,9 +635,7 @@ var machine = createMachine({
630
635
  implementations: {
631
636
  guards: {
632
637
  clampValueOnBlur: ({ prop }) => prop("clampValueOnBlur"),
633
- isAtMin: ({ computed }) => computed("isAtMin"),
634
638
  spinOnPress: ({ prop }) => !!prop("spinOnPress"),
635
- isAtMax: ({ computed }) => computed("isAtMax"),
636
639
  isInRange: ({ computed }) => !computed("isOutOfRange"),
637
640
  isDecrementHint: ({ context, event }) => (event.hint ?? context.get("hint")) === "decrement",
638
641
  isIncrementHint: ({ context, event }) => (event.hint ?? context.get("hint")) === "increment",
@@ -724,23 +727,23 @@ var machine = createMachine({
724
727
  domQuery.raf(() => inputEl?.focus({ preventScroll: true }));
725
728
  },
726
729
  increment({ context, event, prop, computed }) {
727
- const nextValue = utils.incrementValue(computed("valueAsNumber"), event.step ?? prop("step"));
728
- const value = formatValue(utils.clampValue(nextValue, prop("min"), prop("max")), { computed, prop });
729
- context.set("value", value);
730
+ let nextValue = utils.incrementValue(computed("valueAsNumber"), event.step ?? prop("step"));
731
+ if (!prop("allowOverflow")) nextValue = utils.clampValue(nextValue, prop("min"), prop("max"));
732
+ context.set("value", formatValue(nextValue, { computed, prop }));
730
733
  },
731
734
  decrement({ context, event, prop, computed }) {
732
- const nextValue = utils.decrementValue(computed("valueAsNumber"), event.step ?? prop("step"));
733
- const value = formatValue(utils.clampValue(nextValue, prop("min"), prop("max")), { computed, prop });
734
- context.set("value", value);
735
+ let nextValue = utils.decrementValue(computed("valueAsNumber"), event.step ?? prop("step"));
736
+ if (!prop("allowOverflow")) nextValue = utils.clampValue(nextValue, prop("min"), prop("max"));
737
+ context.set("value", formatValue(nextValue, { computed, prop }));
735
738
  },
736
739
  setClampedValue({ context, prop, computed }) {
737
740
  const nextValue = utils.clampValue(computed("valueAsNumber"), prop("min"), prop("max"));
738
741
  context.set("value", formatValue(nextValue, { computed, prop }));
739
742
  },
740
743
  setRawValue({ context, event, prop, computed }) {
741
- const parsedValue = parseValue(event.value, { computed, prop });
742
- const value = formatValue(utils.clampValue(parsedValue, prop("min"), prop("max")), { computed, prop });
743
- context.set("value", value);
744
+ let nextValue = parseValue(event.value, { computed, prop });
745
+ if (!prop("allowOverflow")) nextValue = utils.clampValue(nextValue, prop("min"), prop("max"));
746
+ context.set("value", formatValue(nextValue, { computed, prop }));
744
747
  },
745
748
  setValue({ context, event }) {
746
749
  const value = event.target?.value ?? event.value;
@@ -763,9 +766,6 @@ var machine = createMachine({
763
766
  clearHint({ context }) {
764
767
  context.set("hint", null);
765
768
  },
766
- setHintToSet({ context }) {
767
- context.set("hint", "set");
768
- },
769
769
  invokeOnFocus({ computed, prop }) {
770
770
  prop("onFocusChange")?.({
771
771
  focused: true,
@@ -780,8 +780,8 @@ var machine = createMachine({
780
780
  valueAsNumber: computed("valueAsNumber")
781
781
  });
782
782
  },
783
- invokeOnInvalid({ computed, prop }) {
784
- if (!computed("isOutOfRange")) return;
783
+ invokeOnInvalid({ computed, prop, event }) {
784
+ if (event.type === "INPUT.CHANGE") return;
785
785
  const reason = computed("valueAsNumber") > prop("max") ? "rangeOverflow" : "rangeUnderflow";
786
786
  prop("onValueInvalid")?.({
787
787
  reason,
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createAnatomy } from '@zag-js/anatomy';
2
- import { trackFormControl, observeAttributes, addDomEvent, isSafari, requestPointerLock, raf, setElementValue, MAX_Z_INDEX, dataAttr, ariaAttr, isComposingEvent, getEventStep, isLeftClick, getEventPoint, getWindow, isModifierKey } from '@zag-js/dom-query';
3
- import { isValueAtMin, isValueAtMax, isValueWithinRange, callAll, incrementValue, clampValue, decrementValue, createSplitProps, roundToDpr, wrap } from '@zag-js/utils';
2
+ import { raf, setElementValue, addDomEvent, isSafari, requestPointerLock, observeAttributes, trackFormControl, MAX_Z_INDEX, dataAttr, getEventPoint, getWindow, isLeftClick, ariaAttr, isComposingEvent, getEventStep, isModifierKey } from '@zag-js/dom-query';
3
+ import { isValueAtMax, isValueAtMin, clampValue, decrementValue, incrementValue, callAll, isValueWithinRange, createSplitProps, roundToDpr, wrap } from '@zag-js/utils';
4
4
  import { setup, memo } from '@zag-js/core';
5
5
  import { NumberParser } from '@internationalized/number';
6
6
  import { createProps } from '@zag-js/types';
@@ -416,7 +416,7 @@ var machine = createMachine({
416
416
  dir: "ltr",
417
417
  locale: "en-US",
418
418
  focusInputOnChange: true,
419
- clampValueOnBlur: true,
419
+ clampValueOnBlur: !props2.allowOverflow,
420
420
  allowOverflow: false,
421
421
  inputMode: "decimal",
422
422
  pattern: "[0-9]*(.[0-9]+)?",
@@ -491,7 +491,7 @@ var machine = createMachine({
491
491
  effects: ["trackFormControl"],
492
492
  on: {
493
493
  "VALUE.SET": {
494
- actions: ["setRawValue", "setHintToSet"]
494
+ actions: ["setRawValue"]
495
495
  },
496
496
  "VALUE.CLEAR": {
497
497
  actions: ["clearValue"]
@@ -556,6 +556,11 @@ var machine = createMachine({
556
556
  target: "idle",
557
557
  actions: ["setClampedValue", "clearHint", "invokeOnBlur"]
558
558
  },
559
+ {
560
+ guard: not("isInRange"),
561
+ target: "idle",
562
+ actions: ["setFormattedValue", "clearHint", "invokeOnBlur", "invokeOnInvalid"]
563
+ },
559
564
  {
560
565
  target: "idle",
561
566
  actions: ["setFormattedValue", "clearHint", "invokeOnBlur"]
@@ -590,11 +595,11 @@ var machine = createMachine({
590
595
  on: {
591
596
  SPIN: [
592
597
  {
593
- guard: and(not("isAtMin"), "isIncrementHint"),
598
+ guard: "isIncrementHint",
594
599
  actions: ["increment"]
595
600
  },
596
601
  {
597
- guard: and(not("isAtMax"), "isDecrementHint"),
602
+ guard: "isDecrementHint",
598
603
  actions: ["decrement"]
599
604
  }
600
605
  ],
@@ -628,9 +633,7 @@ var machine = createMachine({
628
633
  implementations: {
629
634
  guards: {
630
635
  clampValueOnBlur: ({ prop }) => prop("clampValueOnBlur"),
631
- isAtMin: ({ computed }) => computed("isAtMin"),
632
636
  spinOnPress: ({ prop }) => !!prop("spinOnPress"),
633
- isAtMax: ({ computed }) => computed("isAtMax"),
634
637
  isInRange: ({ computed }) => !computed("isOutOfRange"),
635
638
  isDecrementHint: ({ context, event }) => (event.hint ?? context.get("hint")) === "decrement",
636
639
  isIncrementHint: ({ context, event }) => (event.hint ?? context.get("hint")) === "increment",
@@ -722,23 +725,23 @@ var machine = createMachine({
722
725
  raf(() => inputEl?.focus({ preventScroll: true }));
723
726
  },
724
727
  increment({ context, event, prop, computed }) {
725
- const nextValue = incrementValue(computed("valueAsNumber"), event.step ?? prop("step"));
726
- const value = formatValue(clampValue(nextValue, prop("min"), prop("max")), { computed, prop });
727
- context.set("value", value);
728
+ let nextValue = incrementValue(computed("valueAsNumber"), event.step ?? prop("step"));
729
+ if (!prop("allowOverflow")) nextValue = clampValue(nextValue, prop("min"), prop("max"));
730
+ context.set("value", formatValue(nextValue, { computed, prop }));
728
731
  },
729
732
  decrement({ context, event, prop, computed }) {
730
- const nextValue = decrementValue(computed("valueAsNumber"), event.step ?? prop("step"));
731
- const value = formatValue(clampValue(nextValue, prop("min"), prop("max")), { computed, prop });
732
- context.set("value", value);
733
+ let nextValue = decrementValue(computed("valueAsNumber"), event.step ?? prop("step"));
734
+ if (!prop("allowOverflow")) nextValue = clampValue(nextValue, prop("min"), prop("max"));
735
+ context.set("value", formatValue(nextValue, { computed, prop }));
733
736
  },
734
737
  setClampedValue({ context, prop, computed }) {
735
738
  const nextValue = clampValue(computed("valueAsNumber"), prop("min"), prop("max"));
736
739
  context.set("value", formatValue(nextValue, { computed, prop }));
737
740
  },
738
741
  setRawValue({ context, event, prop, computed }) {
739
- const parsedValue = parseValue(event.value, { computed, prop });
740
- const value = formatValue(clampValue(parsedValue, prop("min"), prop("max")), { computed, prop });
741
- context.set("value", value);
742
+ let nextValue = parseValue(event.value, { computed, prop });
743
+ if (!prop("allowOverflow")) nextValue = clampValue(nextValue, prop("min"), prop("max"));
744
+ context.set("value", formatValue(nextValue, { computed, prop }));
742
745
  },
743
746
  setValue({ context, event }) {
744
747
  const value = event.target?.value ?? event.value;
@@ -761,9 +764,6 @@ var machine = createMachine({
761
764
  clearHint({ context }) {
762
765
  context.set("hint", null);
763
766
  },
764
- setHintToSet({ context }) {
765
- context.set("hint", "set");
766
- },
767
767
  invokeOnFocus({ computed, prop }) {
768
768
  prop("onFocusChange")?.({
769
769
  focused: true,
@@ -778,8 +778,8 @@ var machine = createMachine({
778
778
  valueAsNumber: computed("valueAsNumber")
779
779
  });
780
780
  },
781
- invokeOnInvalid({ computed, prop }) {
782
- if (!computed("isOutOfRange")) return;
781
+ invokeOnInvalid({ computed, prop, event }) {
782
+ if (event.type === "INPUT.CHANGE") return;
783
783
  const reason = computed("valueAsNumber") > prop("max") ? "rangeOverflow" : "rangeUnderflow";
784
784
  prop("onValueInvalid")?.({
785
785
  reason,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/number-input",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Core logic for the number-input widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -27,11 +27,11 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@internationalized/number": "3.6.0",
30
- "@zag-js/anatomy": "1.2.0",
31
- "@zag-js/core": "1.2.0",
32
- "@zag-js/dom-query": "1.2.0",
33
- "@zag-js/types": "1.2.0",
34
- "@zag-js/utils": "1.2.0"
30
+ "@zag-js/anatomy": "1.3.0",
31
+ "@zag-js/core": "1.3.0",
32
+ "@zag-js/dom-query": "1.3.0",
33
+ "@zag-js/types": "1.3.0",
34
+ "@zag-js/utils": "1.3.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "clean-package": "2.2.0"