@zag-js/number-input 0.15.0 → 0.17.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
@@ -424,21 +424,15 @@ function machine(userContext) {
424
424
  formattedValue: (ctx2) => ctx2.format?.(ctx2.value).toString() ?? ctx2.value
425
425
  },
426
426
  watch: {
427
- value: ["invokeOnChange", "dispatchChangeEvent"],
427
+ value: ["syncInputElement"],
428
428
  isOutOfRange: ["invokeOnInvalid"],
429
429
  scrubberCursorPoint: ["setVirtualCursorPosition"]
430
430
  },
431
431
  entry: ["syncInputValue"],
432
432
  on: {
433
- SET_VALUE: [
434
- {
435
- guard: "clampOnBlur",
436
- actions: ["setValue", "clampValue", "setHintToSet"]
437
- },
438
- {
439
- actions: ["setValue", "setHintToSet"]
440
- }
441
- ],
433
+ SET_VALUE: {
434
+ actions: ["setValue", "clampValue", "setHintToSet"]
435
+ },
442
436
  CLEAR_VALUE: {
443
437
  actions: ["clearValue"]
444
438
  },
@@ -451,17 +445,19 @@ function machine(userContext) {
451
445
  },
452
446
  states: {
453
447
  idle: {
454
- exit: "invokeOnFocus",
455
448
  on: {
456
449
  PRESS_DOWN: {
457
450
  target: "before:spin",
458
- actions: ["focusInput", "setHint"]
451
+ actions: ["focusInput", "invokeOnFocus", "setHint"]
459
452
  },
460
453
  PRESS_DOWN_SCRUBBER: {
461
454
  target: "scrubbing",
462
- actions: ["focusInput", "setHint", "setCursorPoint"]
455
+ actions: ["focusInput", "invokeOnFocus", "setHint", "setCursorPoint"]
463
456
  },
464
- FOCUS: "focused"
457
+ FOCUS: {
458
+ target: "focused",
459
+ actions: ["focusInput", "invokeOnFocus"]
460
+ }
465
461
  }
466
462
  },
467
463
  focused: {
@@ -651,35 +647,38 @@ function machine(userContext) {
651
647
  focusInput(ctx2) {
652
648
  if (!ctx2.focusInputOnChange)
653
649
  return;
654
- const input = dom.getInputEl(ctx2);
655
- (0, import_dom_query3.raf)(() => input?.focus());
650
+ const inputEl = dom.getInputEl(ctx2);
651
+ (0, import_dom_query3.raf)(() => {
652
+ inputEl?.focus();
653
+ });
656
654
  },
657
655
  increment(ctx2, evt) {
658
- ctx2.value = utils.increment(ctx2, evt.step);
656
+ set.value(ctx2, utils.increment(ctx2, evt.step));
659
657
  },
660
658
  decrement(ctx2, evt) {
661
- ctx2.value = utils.decrement(ctx2, evt.step);
659
+ set.value(ctx2, utils.decrement(ctx2, evt.step));
662
660
  },
663
661
  clampValue(ctx2) {
664
- ctx2.value = utils.clamp(ctx2);
662
+ set.value(ctx2, utils.clamp(ctx2));
665
663
  },
666
664
  roundValue(ctx2) {
667
- if (ctx2.value !== "") {
668
- ctx2.value = utils.round(ctx2);
669
- }
665
+ if (ctx2.value === "")
666
+ return;
667
+ set.value(ctx2, utils.round(ctx2));
670
668
  },
671
669
  setValue(ctx2, evt) {
672
- const value = evt.target?.value ?? evt.value;
673
- ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
670
+ let value = evt.target?.value ?? evt.value;
671
+ value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
672
+ set.value(ctx2, value);
674
673
  },
675
674
  clearValue(ctx2) {
676
- ctx2.value = "";
675
+ set.value(ctx2, "");
677
676
  },
678
677
  setToMax(ctx2) {
679
- ctx2.value = ctx2.max.toString();
678
+ set.value(ctx2, ctx2.max.toString());
680
679
  },
681
680
  setToMin(ctx2) {
682
- ctx2.value = ctx2.min.toString();
681
+ set.value(ctx2, ctx2.min.toString());
683
682
  },
684
683
  setHint(ctx2, evt) {
685
684
  ctx2.hint = evt.hint;
@@ -690,12 +689,6 @@ function machine(userContext) {
690
689
  setHintToSet(ctx2) {
691
690
  ctx2.hint = "set";
692
691
  },
693
- invokeOnChange(ctx2) {
694
- ctx2.onChange?.({
695
- value: ctx2.value,
696
- valueAsNumber: ctx2.valueAsNumber
697
- });
698
- },
699
692
  invokeOnFocus(ctx2, evt) {
700
693
  let srcElement = null;
701
694
  if (evt.type === "PRESS_DOWN") {
@@ -712,10 +705,7 @@ function machine(userContext) {
712
705
  });
713
706
  },
714
707
  invokeOnBlur(ctx2) {
715
- ctx2.onBlur?.({
716
- value: ctx2.value,
717
- valueAsNumber: ctx2.valueAsNumber
718
- });
708
+ ctx2.onBlur?.({ value: ctx2.value, valueAsNumber: ctx2.valueAsNumber });
719
709
  },
720
710
  invokeOnInvalid(ctx2) {
721
711
  if (!ctx2.isOutOfRange)
@@ -727,13 +717,16 @@ function machine(userContext) {
727
717
  valueAsNumber: ctx2.valueAsNumber
728
718
  });
729
719
  },
730
- // sync input value, in event it was set from form libraries via `ref`, `bind:this`, etc.
720
+ syncInputElement(ctx2) {
721
+ const inputEl = dom.getInputEl(ctx2);
722
+ dom.setValue(inputEl, ctx2.formattedValue);
723
+ },
731
724
  syncInputValue(ctx2) {
732
- const input = dom.getInputEl(ctx2);
733
- if (!input || input.value == ctx2.value)
725
+ const inputEl = dom.getInputEl(ctx2);
726
+ if (!inputEl || inputEl.value == ctx2.value)
734
727
  return;
735
- const value = utils.parse(ctx2, input.value);
736
- ctx2.value = utils.sanitize(ctx2, value);
728
+ const value = utils.parse(ctx2, inputEl.value);
729
+ set.value(ctx2, utils.sanitize(ctx2, value));
737
730
  },
738
731
  setCursorPoint(ctx2, evt) {
739
732
  ctx2.scrubberCursorPoint = evt.point;
@@ -747,15 +740,24 @@ function machine(userContext) {
747
740
  return;
748
741
  const { x, y } = ctx2.scrubberCursorPoint;
749
742
  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
743
  }
755
744
  }
756
745
  }
757
746
  );
758
747
  }
748
+ var invoke = {
749
+ onChange: (ctx) => {
750
+ ctx.onChange?.({ value: ctx.value, valueAsNumber: ctx.valueAsNumber });
751
+ const inputEl = dom.getInputEl(ctx);
752
+ (0, import_form_utils.dispatchInputValueEvent)(inputEl, { value: ctx.formattedValue });
753
+ }
754
+ };
755
+ var set = {
756
+ value: (ctx, value) => {
757
+ ctx.value = value;
758
+ invoke.onChange(ctx);
759
+ }
760
+ };
759
761
  // Annotate the CommonJS export names for ESM import in node:
760
762
  0 && (module.exports = {
761
763
  anatomy,