@zag-js/number-input 0.16.0 → 0.18.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,5 +1,5 @@
1
1
  import * as _zag_js_anatomy from '@zag-js/anatomy';
2
- import { PropTypes, RequiredBy, DirectionProperty, CommonProperties, Context, NormalizeProps } from '@zag-js/types';
2
+ import { RequiredBy, PropTypes, DirectionProperty, CommonProperties, Context, NormalizeProps } from '@zag-js/types';
3
3
  import * as _zag_js_core from '@zag-js/core';
4
4
  import { StateMachine } from '@zag-js/core';
5
5
 
@@ -156,67 +156,6 @@ type PublicContext = DirectionProperty & CommonProperties & {
156
156
  */
157
157
  spinOnPress?: boolean;
158
158
  };
159
- type PublicApi<T extends PropTypes = PropTypes> = {
160
- /**
161
- * Whether the input is focused.
162
- */
163
- isFocused: boolean;
164
- /**
165
- * Whether the input is invalid.
166
- */
167
- isInvalid: boolean;
168
- /**
169
- * Whether the input value is empty.
170
- */
171
- isValueEmpty: boolean;
172
- /**
173
- * The formatted value of the input.
174
- */
175
- value: string;
176
- /**
177
- * The value of the input as a number.
178
- */
179
- valueAsNumber: number;
180
- /**
181
- * Function to set the value of the input.
182
- */
183
- setValue(value: string | number): void;
184
- /**
185
- * Function to clear the value of the input.
186
- */
187
- clearValue(): void;
188
- /**
189
- * Function to increment the value of the input by the step.
190
- */
191
- increment(): void;
192
- /**
193
- * Function to decrement the value of the input by the step.
194
- */
195
- decrement(): void;
196
- /**
197
- * Function to set the value of the input to the max.
198
- */
199
- setToMax(): void;
200
- /**
201
- * Function to set the value of the input to the min.
202
- */
203
- setToMin(): void;
204
- /**
205
- * Function to focus the input.
206
- */
207
- focus(): void;
208
- /**
209
- * Function to blur the input.
210
- */
211
- blur(): void;
212
- rootProps: T["element"];
213
- labelProps: T["label"];
214
- controlProps: T["element"];
215
- inputProps: T["input"];
216
- decrementTriggerProps: T["button"];
217
- incrementTriggerProps: T["button"];
218
- scrubberProps: T["element"];
219
- };
220
159
  type UserDefinedContext = RequiredBy<PublicContext, "id">;
221
160
  type ComputedContext = Readonly<{
222
161
  /**
@@ -278,9 +217,70 @@ type MachineState = {
278
217
  };
279
218
  type State = StateMachine.State<MachineContext, MachineState>;
280
219
  type Send = StateMachine.Send<StateMachine.AnyEventObject>;
220
+ type MachineApi<T extends PropTypes = PropTypes> = {
221
+ /**
222
+ * Whether the input is focused.
223
+ */
224
+ isFocused: boolean;
225
+ /**
226
+ * Whether the input is invalid.
227
+ */
228
+ isInvalid: boolean;
229
+ /**
230
+ * Whether the input value is empty.
231
+ */
232
+ isValueEmpty: boolean;
233
+ /**
234
+ * The formatted value of the input.
235
+ */
236
+ value: string;
237
+ /**
238
+ * The value of the input as a number.
239
+ */
240
+ valueAsNumber: number;
241
+ /**
242
+ * Function to set the value of the input.
243
+ */
244
+ setValue(value: string | number): void;
245
+ /**
246
+ * Function to clear the value of the input.
247
+ */
248
+ clearValue(): void;
249
+ /**
250
+ * Function to increment the value of the input by the step.
251
+ */
252
+ increment(): void;
253
+ /**
254
+ * Function to decrement the value of the input by the step.
255
+ */
256
+ decrement(): void;
257
+ /**
258
+ * Function to set the value of the input to the max.
259
+ */
260
+ setToMax(): void;
261
+ /**
262
+ * Function to set the value of the input to the min.
263
+ */
264
+ setToMin(): void;
265
+ /**
266
+ * Function to focus the input.
267
+ */
268
+ focus(): void;
269
+ /**
270
+ * Function to blur the input.
271
+ */
272
+ blur(): void;
273
+ rootProps: T["element"];
274
+ labelProps: T["label"];
275
+ controlProps: T["element"];
276
+ inputProps: T["input"];
277
+ decrementTriggerProps: T["button"];
278
+ incrementTriggerProps: T["button"];
279
+ scrubberProps: T["element"];
280
+ };
281
281
 
282
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): PublicApi<T>;
282
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
283
283
 
284
284
  declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
285
285
 
286
- export { UserDefinedContext as Context, PublicApi, anatomy, connect, machine };
286
+ export { MachineApi as Api, UserDefinedContext as Context, anatomy, connect, machine };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _zag_js_anatomy from '@zag-js/anatomy';
2
- import { PropTypes, RequiredBy, DirectionProperty, CommonProperties, Context, NormalizeProps } from '@zag-js/types';
2
+ import { RequiredBy, PropTypes, DirectionProperty, CommonProperties, Context, NormalizeProps } from '@zag-js/types';
3
3
  import * as _zag_js_core from '@zag-js/core';
4
4
  import { StateMachine } from '@zag-js/core';
5
5
 
@@ -156,67 +156,6 @@ type PublicContext = DirectionProperty & CommonProperties & {
156
156
  */
157
157
  spinOnPress?: boolean;
158
158
  };
159
- type PublicApi<T extends PropTypes = PropTypes> = {
160
- /**
161
- * Whether the input is focused.
162
- */
163
- isFocused: boolean;
164
- /**
165
- * Whether the input is invalid.
166
- */
167
- isInvalid: boolean;
168
- /**
169
- * Whether the input value is empty.
170
- */
171
- isValueEmpty: boolean;
172
- /**
173
- * The formatted value of the input.
174
- */
175
- value: string;
176
- /**
177
- * The value of the input as a number.
178
- */
179
- valueAsNumber: number;
180
- /**
181
- * Function to set the value of the input.
182
- */
183
- setValue(value: string | number): void;
184
- /**
185
- * Function to clear the value of the input.
186
- */
187
- clearValue(): void;
188
- /**
189
- * Function to increment the value of the input by the step.
190
- */
191
- increment(): void;
192
- /**
193
- * Function to decrement the value of the input by the step.
194
- */
195
- decrement(): void;
196
- /**
197
- * Function to set the value of the input to the max.
198
- */
199
- setToMax(): void;
200
- /**
201
- * Function to set the value of the input to the min.
202
- */
203
- setToMin(): void;
204
- /**
205
- * Function to focus the input.
206
- */
207
- focus(): void;
208
- /**
209
- * Function to blur the input.
210
- */
211
- blur(): void;
212
- rootProps: T["element"];
213
- labelProps: T["label"];
214
- controlProps: T["element"];
215
- inputProps: T["input"];
216
- decrementTriggerProps: T["button"];
217
- incrementTriggerProps: T["button"];
218
- scrubberProps: T["element"];
219
- };
220
159
  type UserDefinedContext = RequiredBy<PublicContext, "id">;
221
160
  type ComputedContext = Readonly<{
222
161
  /**
@@ -278,9 +217,70 @@ type MachineState = {
278
217
  };
279
218
  type State = StateMachine.State<MachineContext, MachineState>;
280
219
  type Send = StateMachine.Send<StateMachine.AnyEventObject>;
220
+ type MachineApi<T extends PropTypes = PropTypes> = {
221
+ /**
222
+ * Whether the input is focused.
223
+ */
224
+ isFocused: boolean;
225
+ /**
226
+ * Whether the input is invalid.
227
+ */
228
+ isInvalid: boolean;
229
+ /**
230
+ * Whether the input value is empty.
231
+ */
232
+ isValueEmpty: boolean;
233
+ /**
234
+ * The formatted value of the input.
235
+ */
236
+ value: string;
237
+ /**
238
+ * The value of the input as a number.
239
+ */
240
+ valueAsNumber: number;
241
+ /**
242
+ * Function to set the value of the input.
243
+ */
244
+ setValue(value: string | number): void;
245
+ /**
246
+ * Function to clear the value of the input.
247
+ */
248
+ clearValue(): void;
249
+ /**
250
+ * Function to increment the value of the input by the step.
251
+ */
252
+ increment(): void;
253
+ /**
254
+ * Function to decrement the value of the input by the step.
255
+ */
256
+ decrement(): void;
257
+ /**
258
+ * Function to set the value of the input to the max.
259
+ */
260
+ setToMax(): void;
261
+ /**
262
+ * Function to set the value of the input to the min.
263
+ */
264
+ setToMin(): void;
265
+ /**
266
+ * Function to focus the input.
267
+ */
268
+ focus(): void;
269
+ /**
270
+ * Function to blur the input.
271
+ */
272
+ blur(): void;
273
+ rootProps: T["element"];
274
+ labelProps: T["label"];
275
+ controlProps: T["element"];
276
+ inputProps: T["input"];
277
+ decrementTriggerProps: T["button"];
278
+ incrementTriggerProps: T["button"];
279
+ scrubberProps: T["element"];
280
+ };
281
281
 
282
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): PublicApi<T>;
282
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
283
283
 
284
284
  declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
285
285
 
286
- export { UserDefinedContext as Context, PublicApi, anatomy, connect, machine };
286
+ export { MachineApi as Api, UserDefinedContext as Context, anatomy, connect, machine };
package/dist/index.js CHANGED
@@ -378,7 +378,6 @@ function connect(state, send, normalize) {
378
378
  var import_core = require("@zag-js/core");
379
379
  var import_dom_event3 = require("@zag-js/dom-event");
380
380
  var import_dom_query3 = require("@zag-js/dom-query");
381
- var import_form_utils = require("@zag-js/form-utils");
382
381
  var import_mutation_observer = require("@zag-js/mutation-observer");
383
382
  var import_number_utils4 = require("@zag-js/number-utils");
384
383
  var import_utils = require("@zag-js/utils");
@@ -424,21 +423,15 @@ function machine(userContext) {
424
423
  formattedValue: (ctx2) => ctx2.format?.(ctx2.value).toString() ?? ctx2.value
425
424
  },
426
425
  watch: {
427
- value: ["invokeOnChange", "dispatchChangeEvent"],
426
+ value: ["syncInputElement"],
428
427
  isOutOfRange: ["invokeOnInvalid"],
429
428
  scrubberCursorPoint: ["setVirtualCursorPosition"]
430
429
  },
431
430
  entry: ["syncInputValue"],
432
431
  on: {
433
- SET_VALUE: [
434
- {
435
- guard: "clampOnBlur",
436
- actions: ["setValue", "clampValue", "setHintToSet"]
437
- },
438
- {
439
- actions: ["setValue", "setHintToSet"]
440
- }
441
- ],
432
+ SET_VALUE: {
433
+ actions: ["setValue", "clampValue", "setHintToSet"]
434
+ },
442
435
  CLEAR_VALUE: {
443
436
  actions: ["clearValue"]
444
437
  },
@@ -451,17 +444,19 @@ function machine(userContext) {
451
444
  },
452
445
  states: {
453
446
  idle: {
454
- exit: "invokeOnFocus",
455
447
  on: {
456
448
  PRESS_DOWN: {
457
449
  target: "before:spin",
458
- actions: ["focusInput", "setHint"]
450
+ actions: ["focusInput", "invokeOnFocus", "setHint"]
459
451
  },
460
452
  PRESS_DOWN_SCRUBBER: {
461
453
  target: "scrubbing",
462
- actions: ["focusInput", "setHint", "setCursorPoint"]
454
+ actions: ["focusInput", "invokeOnFocus", "setHint", "setCursorPoint"]
463
455
  },
464
- FOCUS: "focused"
456
+ FOCUS: {
457
+ target: "focused",
458
+ actions: ["focusInput", "invokeOnFocus"]
459
+ }
465
460
  }
466
461
  },
467
462
  focused: {
@@ -651,35 +646,38 @@ function machine(userContext) {
651
646
  focusInput(ctx2) {
652
647
  if (!ctx2.focusInputOnChange)
653
648
  return;
654
- const input = dom.getInputEl(ctx2);
655
- (0, import_dom_query3.raf)(() => input?.focus());
649
+ const inputEl = dom.getInputEl(ctx2);
650
+ (0, import_dom_query3.raf)(() => {
651
+ inputEl?.focus();
652
+ });
656
653
  },
657
654
  increment(ctx2, evt) {
658
- ctx2.value = utils.increment(ctx2, evt.step);
655
+ set.value(ctx2, utils.increment(ctx2, evt.step));
659
656
  },
660
657
  decrement(ctx2, evt) {
661
- ctx2.value = utils.decrement(ctx2, evt.step);
658
+ set.value(ctx2, utils.decrement(ctx2, evt.step));
662
659
  },
663
660
  clampValue(ctx2) {
664
- ctx2.value = utils.clamp(ctx2);
661
+ set.value(ctx2, utils.clamp(ctx2));
665
662
  },
666
663
  roundValue(ctx2) {
667
- if (ctx2.value !== "") {
668
- ctx2.value = utils.round(ctx2);
669
- }
664
+ if (ctx2.value === "")
665
+ return;
666
+ set.value(ctx2, utils.round(ctx2));
670
667
  },
671
668
  setValue(ctx2, evt) {
672
- const value = evt.target?.value ?? evt.value;
673
- ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
669
+ let value = evt.target?.value ?? evt.value;
670
+ value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
671
+ set.value(ctx2, value);
674
672
  },
675
673
  clearValue(ctx2) {
676
- ctx2.value = "";
674
+ set.value(ctx2, "");
677
675
  },
678
676
  setToMax(ctx2) {
679
- ctx2.value = ctx2.max.toString();
677
+ set.value(ctx2, ctx2.max.toString());
680
678
  },
681
679
  setToMin(ctx2) {
682
- ctx2.value = ctx2.min.toString();
680
+ set.value(ctx2, ctx2.min.toString());
683
681
  },
684
682
  setHint(ctx2, evt) {
685
683
  ctx2.hint = evt.hint;
@@ -690,12 +688,6 @@ function machine(userContext) {
690
688
  setHintToSet(ctx2) {
691
689
  ctx2.hint = "set";
692
690
  },
693
- invokeOnChange(ctx2) {
694
- ctx2.onChange?.({
695
- value: ctx2.value,
696
- valueAsNumber: ctx2.valueAsNumber
697
- });
698
- },
699
691
  invokeOnFocus(ctx2, evt) {
700
692
  let srcElement = null;
701
693
  if (evt.type === "PRESS_DOWN") {
@@ -712,10 +704,7 @@ function machine(userContext) {
712
704
  });
713
705
  },
714
706
  invokeOnBlur(ctx2) {
715
- ctx2.onBlur?.({
716
- value: ctx2.value,
717
- valueAsNumber: ctx2.valueAsNumber
718
- });
707
+ ctx2.onBlur?.({ value: ctx2.value, valueAsNumber: ctx2.valueAsNumber });
719
708
  },
720
709
  invokeOnInvalid(ctx2) {
721
710
  if (!ctx2.isOutOfRange)
@@ -727,13 +716,16 @@ function machine(userContext) {
727
716
  valueAsNumber: ctx2.valueAsNumber
728
717
  });
729
718
  },
730
- // sync input value, in event it was set from form libraries via `ref`, `bind:this`, etc.
719
+ syncInputElement(ctx2) {
720
+ const inputEl = dom.getInputEl(ctx2);
721
+ dom.setValue(inputEl, ctx2.formattedValue);
722
+ },
731
723
  syncInputValue(ctx2) {
732
- const input = dom.getInputEl(ctx2);
733
- if (!input || input.value == ctx2.value)
724
+ const inputEl = dom.getInputEl(ctx2);
725
+ if (!inputEl || inputEl.value == ctx2.value)
734
726
  return;
735
- const value = utils.parse(ctx2, input.value);
736
- ctx2.value = utils.sanitize(ctx2, value);
727
+ const value = utils.parse(ctx2, inputEl.value);
728
+ set.value(ctx2, utils.sanitize(ctx2, value));
737
729
  },
738
730
  setCursorPoint(ctx2, evt) {
739
731
  ctx2.scrubberCursorPoint = evt.point;
@@ -747,15 +739,22 @@ function machine(userContext) {
747
739
  return;
748
740
  const { x, y } = ctx2.scrubberCursorPoint;
749
741
  cursor.style.transform = `translate3d(${x}px, ${y}px, 0px)`;
750
- },
751
- dispatchChangeEvent(ctx2) {
752
- const inputEl = dom.getInputEl(ctx2);
753
- (0, import_form_utils.dispatchInputValueEvent)(inputEl, { value: ctx2.formattedValue });
754
742
  }
755
743
  }
756
744
  }
757
745
  );
758
746
  }
747
+ var invoke = {
748
+ onChange: (ctx) => {
749
+ ctx.onChange?.({ value: ctx.value, valueAsNumber: ctx.valueAsNumber });
750
+ }
751
+ };
752
+ var set = {
753
+ value: (ctx, value) => {
754
+ ctx.value = value;
755
+ invoke.onChange(ctx);
756
+ }
757
+ };
759
758
  // Annotate the CommonJS export names for ESM import in node:
760
759
  0 && (module.exports = {
761
760
  anatomy,