@zag-js/number-input 0.82.2 → 1.0.1

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,7 +1,8 @@
1
1
  import * as _zag_js_anatomy from '@zag-js/anatomy';
2
- import { RequiredBy, PropTypes, LocaleProperties, CommonProperties, NormalizeProps } from '@zag-js/types';
2
+ import { LocaleProperties, CommonProperties, PropTypes, RequiredBy, NormalizeProps } from '@zag-js/types';
3
+ import { NumberFormatter, NumberParser } from '@internationalized/number';
3
4
  import * as _zag_js_core from '@zag-js/core';
4
- import { Machine, StateMachine } from '@zag-js/core';
5
+ import { Service, Machine, EventObject } from '@zag-js/core';
5
6
 
6
7
  declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "label" | "input" | "control" | "valueText" | "incrementTrigger" | "decrementTrigger" | "scrubber">;
7
8
 
@@ -25,22 +26,22 @@ type ElementIds = Partial<{
25
26
  decrementTrigger: string;
26
27
  scrubber: string;
27
28
  }>;
28
- type IntlTranslations = {
29
+ interface IntlTranslations {
29
30
  /**
30
31
  * Function that returns the human-readable value.
31
32
  * It is used to set the `aria-valuetext` property of the input
32
33
  */
33
- valueText?: (value: string) => string;
34
+ valueText?: ((value: string) => string) | undefined;
34
35
  /**
35
36
  * The label foe the increment button
36
37
  */
37
- incrementLabel: string;
38
+ incrementLabel?: string | undefined;
38
39
  /**
39
40
  * The label for the decrement button
40
41
  */
41
- decrementLabel: string;
42
- };
43
- interface PublicContext extends LocaleProperties, CommonProperties {
42
+ decrementLabel?: string | undefined;
43
+ }
44
+ interface NumberInputProps extends LocaleProperties, CommonProperties {
44
45
  /**
45
46
  * The ids of the elements in the number input. Useful for composition.
46
47
  */
@@ -74,26 +75,31 @@ interface PublicContext extends LocaleProperties, CommonProperties {
74
75
  *
75
76
  * @default "[0-9]*(.[0-9]+)?"
76
77
  */
77
- pattern: string;
78
+ pattern?: string | undefined;
78
79
  /**
79
- * The value of the input
80
+ * The controlled value of the input
80
81
  */
81
- value: string;
82
+ value?: string | undefined;
83
+ /**
84
+ * The initial value of the input when rendered.
85
+ * Use when you don't need to control the value of the input.
86
+ */
87
+ defaultValue?: string | undefined;
82
88
  /**
83
89
  * The minimum value of the number input
84
90
  * @default Number.MIN_SAFE_INTEGER
85
91
  */
86
- min: number;
92
+ min?: number | undefined;
87
93
  /**
88
94
  * The maximum value of the number input
89
95
  * @default Number.MAX_SAFE_INTEGER
90
96
  */
91
- max: number;
97
+ max?: number | undefined;
92
98
  /**
93
99
  * The amount to increment or decrement the value by
94
100
  * @default 1
95
101
  */
96
- step: number;
102
+ step?: number | undefined;
97
103
  /**
98
104
  * Whether to allow mouse wheel to change the value
99
105
  */
@@ -102,21 +108,21 @@ interface PublicContext extends LocaleProperties, CommonProperties {
102
108
  * Whether to allow the value overflow the min/max range
103
109
  * @default true
104
110
  */
105
- allowOverflow: boolean;
111
+ allowOverflow?: boolean | undefined;
106
112
  /**
107
113
  * Whether to clamp the value when the input loses focus (blur)
108
114
  * @default true
109
115
  */
110
- clampValueOnBlur: boolean;
116
+ clampValueOnBlur?: boolean | undefined;
111
117
  /**
112
118
  * Whether to focus input when the value changes
113
119
  * @default true
114
120
  */
115
- focusInputOnChange: boolean;
121
+ focusInputOnChange?: boolean | undefined;
116
122
  /**
117
123
  * Specifies the localized strings that identifies the accessibility elements and their states
118
124
  */
119
- translations: IntlTranslations;
125
+ translations?: IntlTranslations | undefined;
120
126
  /**
121
127
  * The options to pass to the `Intl.NumberFormat` constructor
122
128
  */
@@ -126,7 +132,7 @@ interface PublicContext extends LocaleProperties, CommonProperties {
126
132
  * the type of keyboard shown to the user on mobile devices
127
133
  * @default "decimal"
128
134
  */
129
- inputMode: InputMode;
135
+ inputMode?: InputMode | undefined;
130
136
  /**
131
137
  * Function invoked when the value changes
132
138
  */
@@ -145,76 +151,97 @@ interface PublicContext extends LocaleProperties, CommonProperties {
145
151
  */
146
152
  spinOnPress?: boolean | undefined;
147
153
  }
148
- type UserDefinedContext = RequiredBy<PublicContext, "id">;
154
+ type PropsWithDefault = "dir" | "locale" | "focusInputOnChange" | "clampValueOnBlur" | "allowOverflow" | "inputMode" | "pattern" | "translations" | "step" | "spinOnPress" | "min" | "max" | "step" | "translations";
149
155
  type ComputedContext = Readonly<{
150
156
  /**
151
- * @computed
152
157
  * The value of the input as a number
153
158
  */
154
159
  valueAsNumber: number;
155
160
  /**
156
- * @computed
157
161
  * Whether the value is at the min
158
162
  */
159
163
  isAtMin: boolean;
160
164
  /**
161
- * @computed
162
165
  * Whether the value is at the max
163
166
  */
164
167
  isAtMax: boolean;
165
168
  /**
166
- * @computed
167
169
  * Whether the value is out of the min/max range
168
170
  */
169
171
  isOutOfRange: boolean;
170
172
  /**
171
- * @computed
172
173
  * Whether the value is empty
173
174
  */
174
175
  isValueEmpty: boolean;
175
176
  /**
176
- * @computed
177
177
  * Whether the color picker is disabled
178
178
  */
179
179
  isDisabled: boolean;
180
180
  /**
181
- * @computed
182
181
  * Whether the increment button is enabled
183
182
  */
184
183
  canIncrement: boolean;
185
184
  /**
186
- * @computed
187
185
  * Whether the decrement button is enabled
188
186
  */
189
187
  canDecrement: boolean;
190
188
  /**
191
- * @computed
192
189
  * The `aria-valuetext` attribute of the input
193
190
  */
194
191
  valueText: string | undefined;
195
192
  /**
196
- * @computed
197
193
  * The formatted value of the input
198
194
  */
199
195
  formattedValue: string;
200
196
  /**
201
- * @computed
202
197
  * Whether the writing direction is RTL
203
198
  */
204
199
  isRtl: boolean;
200
+ /**
201
+ * The number i18n formatter
202
+ */
203
+ formatter: NumberFormatter;
204
+ /**
205
+ * The number i18n parser
206
+ */
207
+ parser: NumberParser;
205
208
  }>;
209
+ type HintValue = "increment" | "decrement" | "set";
206
210
  interface PrivateContext {
211
+ /**
212
+ * The hint that determines if we're incrementing or decrementing
213
+ */
214
+ hint: HintValue | null;
215
+ /**
216
+ * The scrubber cursor position
217
+ */
218
+ scrubberCursorPoint: {
219
+ x: number;
220
+ y: number;
221
+ } | null;
222
+ /**
223
+ * Whether the checkbox's fieldset is disabled
224
+ */
225
+ fieldsetDisabled: boolean;
226
+ /**
227
+ * The value of the input
228
+ */
229
+ value: string;
207
230
  }
208
- interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
209
- }
210
- interface MachineState {
211
- value: "idle" | "focused" | "spinning" | "before:spin" | "scrubbing";
212
- tags: "focus";
231
+ interface NumberInputSchema {
232
+ state: "idle" | "focused" | "spinning" | "before:spin" | "scrubbing";
233
+ tag: "focus";
234
+ props: RequiredBy<NumberInputProps, PropsWithDefault>;
235
+ context: PrivateContext;
236
+ computed: ComputedContext;
237
+ action: string;
238
+ event: EventObject;
239
+ guard: string;
240
+ effect: string;
213
241
  }
214
- type State = StateMachine.State<MachineContext, MachineState>;
215
- type Send = StateMachine.Send<StateMachine.AnyEventObject>;
216
- type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
217
- interface MachineApi<T extends PropTypes = PropTypes> {
242
+ type NumberInputService = Service<NumberInputSchema>;
243
+ type NumberInputMachine = Machine<NumberInputSchema>;
244
+ interface NumberInputApi<T extends PropTypes = PropTypes> {
218
245
  /**
219
246
  * Whether the input is focused.
220
247
  */
@@ -273,8 +300,11 @@ interface MachineApi<T extends PropTypes = PropTypes> {
273
300
  getScrubberProps(): T["element"];
274
301
  }
275
302
 
276
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
303
+ declare function connect<T extends PropTypes>(service: NumberInputService, normalize: NormalizeProps<T>): NumberInputApi<T>;
304
+
305
+ declare const machine: _zag_js_core.Machine<NumberInputSchema>;
277
306
 
278
- declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
307
+ declare const props: (keyof NumberInputProps)[];
308
+ declare const splitProps: <Props extends Partial<NumberInputProps>>(props: Props) => [Partial<NumberInputProps>, Omit<Props, keyof NumberInputProps>];
279
309
 
280
- export { type MachineApi as Api, type UserDefinedContext as Context, type ElementIds, type FocusChangeDetails, type IntlTranslations, type Service, type ValueChangeDetails, type ValueInvalidDetails, anatomy, connect, machine };
310
+ export { type NumberInputApi as Api, type ElementIds, type FocusChangeDetails, type IntlTranslations, type NumberInputMachine as Machine, type NumberInputProps as Props, type NumberInputService as Service, type ValueChangeDetails, type ValueInvalidDetails, anatomy, connect, machine, props, splitProps };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import * as _zag_js_anatomy from '@zag-js/anatomy';
2
- import { RequiredBy, PropTypes, LocaleProperties, CommonProperties, NormalizeProps } from '@zag-js/types';
2
+ import { LocaleProperties, CommonProperties, PropTypes, RequiredBy, NormalizeProps } from '@zag-js/types';
3
+ import { NumberFormatter, NumberParser } from '@internationalized/number';
3
4
  import * as _zag_js_core from '@zag-js/core';
4
- import { Machine, StateMachine } from '@zag-js/core';
5
+ import { Service, Machine, EventObject } from '@zag-js/core';
5
6
 
6
7
  declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "label" | "input" | "control" | "valueText" | "incrementTrigger" | "decrementTrigger" | "scrubber">;
7
8
 
@@ -25,22 +26,22 @@ type ElementIds = Partial<{
25
26
  decrementTrigger: string;
26
27
  scrubber: string;
27
28
  }>;
28
- type IntlTranslations = {
29
+ interface IntlTranslations {
29
30
  /**
30
31
  * Function that returns the human-readable value.
31
32
  * It is used to set the `aria-valuetext` property of the input
32
33
  */
33
- valueText?: (value: string) => string;
34
+ valueText?: ((value: string) => string) | undefined;
34
35
  /**
35
36
  * The label foe the increment button
36
37
  */
37
- incrementLabel: string;
38
+ incrementLabel?: string | undefined;
38
39
  /**
39
40
  * The label for the decrement button
40
41
  */
41
- decrementLabel: string;
42
- };
43
- interface PublicContext extends LocaleProperties, CommonProperties {
42
+ decrementLabel?: string | undefined;
43
+ }
44
+ interface NumberInputProps extends LocaleProperties, CommonProperties {
44
45
  /**
45
46
  * The ids of the elements in the number input. Useful for composition.
46
47
  */
@@ -74,26 +75,31 @@ interface PublicContext extends LocaleProperties, CommonProperties {
74
75
  *
75
76
  * @default "[0-9]*(.[0-9]+)?"
76
77
  */
77
- pattern: string;
78
+ pattern?: string | undefined;
78
79
  /**
79
- * The value of the input
80
+ * The controlled value of the input
80
81
  */
81
- value: string;
82
+ value?: string | undefined;
83
+ /**
84
+ * The initial value of the input when rendered.
85
+ * Use when you don't need to control the value of the input.
86
+ */
87
+ defaultValue?: string | undefined;
82
88
  /**
83
89
  * The minimum value of the number input
84
90
  * @default Number.MIN_SAFE_INTEGER
85
91
  */
86
- min: number;
92
+ min?: number | undefined;
87
93
  /**
88
94
  * The maximum value of the number input
89
95
  * @default Number.MAX_SAFE_INTEGER
90
96
  */
91
- max: number;
97
+ max?: number | undefined;
92
98
  /**
93
99
  * The amount to increment or decrement the value by
94
100
  * @default 1
95
101
  */
96
- step: number;
102
+ step?: number | undefined;
97
103
  /**
98
104
  * Whether to allow mouse wheel to change the value
99
105
  */
@@ -102,21 +108,21 @@ interface PublicContext extends LocaleProperties, CommonProperties {
102
108
  * Whether to allow the value overflow the min/max range
103
109
  * @default true
104
110
  */
105
- allowOverflow: boolean;
111
+ allowOverflow?: boolean | undefined;
106
112
  /**
107
113
  * Whether to clamp the value when the input loses focus (blur)
108
114
  * @default true
109
115
  */
110
- clampValueOnBlur: boolean;
116
+ clampValueOnBlur?: boolean | undefined;
111
117
  /**
112
118
  * Whether to focus input when the value changes
113
119
  * @default true
114
120
  */
115
- focusInputOnChange: boolean;
121
+ focusInputOnChange?: boolean | undefined;
116
122
  /**
117
123
  * Specifies the localized strings that identifies the accessibility elements and their states
118
124
  */
119
- translations: IntlTranslations;
125
+ translations?: IntlTranslations | undefined;
120
126
  /**
121
127
  * The options to pass to the `Intl.NumberFormat` constructor
122
128
  */
@@ -126,7 +132,7 @@ interface PublicContext extends LocaleProperties, CommonProperties {
126
132
  * the type of keyboard shown to the user on mobile devices
127
133
  * @default "decimal"
128
134
  */
129
- inputMode: InputMode;
135
+ inputMode?: InputMode | undefined;
130
136
  /**
131
137
  * Function invoked when the value changes
132
138
  */
@@ -145,76 +151,97 @@ interface PublicContext extends LocaleProperties, CommonProperties {
145
151
  */
146
152
  spinOnPress?: boolean | undefined;
147
153
  }
148
- type UserDefinedContext = RequiredBy<PublicContext, "id">;
154
+ type PropsWithDefault = "dir" | "locale" | "focusInputOnChange" | "clampValueOnBlur" | "allowOverflow" | "inputMode" | "pattern" | "translations" | "step" | "spinOnPress" | "min" | "max" | "step" | "translations";
149
155
  type ComputedContext = Readonly<{
150
156
  /**
151
- * @computed
152
157
  * The value of the input as a number
153
158
  */
154
159
  valueAsNumber: number;
155
160
  /**
156
- * @computed
157
161
  * Whether the value is at the min
158
162
  */
159
163
  isAtMin: boolean;
160
164
  /**
161
- * @computed
162
165
  * Whether the value is at the max
163
166
  */
164
167
  isAtMax: boolean;
165
168
  /**
166
- * @computed
167
169
  * Whether the value is out of the min/max range
168
170
  */
169
171
  isOutOfRange: boolean;
170
172
  /**
171
- * @computed
172
173
  * Whether the value is empty
173
174
  */
174
175
  isValueEmpty: boolean;
175
176
  /**
176
- * @computed
177
177
  * Whether the color picker is disabled
178
178
  */
179
179
  isDisabled: boolean;
180
180
  /**
181
- * @computed
182
181
  * Whether the increment button is enabled
183
182
  */
184
183
  canIncrement: boolean;
185
184
  /**
186
- * @computed
187
185
  * Whether the decrement button is enabled
188
186
  */
189
187
  canDecrement: boolean;
190
188
  /**
191
- * @computed
192
189
  * The `aria-valuetext` attribute of the input
193
190
  */
194
191
  valueText: string | undefined;
195
192
  /**
196
- * @computed
197
193
  * The formatted value of the input
198
194
  */
199
195
  formattedValue: string;
200
196
  /**
201
- * @computed
202
197
  * Whether the writing direction is RTL
203
198
  */
204
199
  isRtl: boolean;
200
+ /**
201
+ * The number i18n formatter
202
+ */
203
+ formatter: NumberFormatter;
204
+ /**
205
+ * The number i18n parser
206
+ */
207
+ parser: NumberParser;
205
208
  }>;
209
+ type HintValue = "increment" | "decrement" | "set";
206
210
  interface PrivateContext {
211
+ /**
212
+ * The hint that determines if we're incrementing or decrementing
213
+ */
214
+ hint: HintValue | null;
215
+ /**
216
+ * The scrubber cursor position
217
+ */
218
+ scrubberCursorPoint: {
219
+ x: number;
220
+ y: number;
221
+ } | null;
222
+ /**
223
+ * Whether the checkbox's fieldset is disabled
224
+ */
225
+ fieldsetDisabled: boolean;
226
+ /**
227
+ * The value of the input
228
+ */
229
+ value: string;
207
230
  }
208
- interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
209
- }
210
- interface MachineState {
211
- value: "idle" | "focused" | "spinning" | "before:spin" | "scrubbing";
212
- tags: "focus";
231
+ interface NumberInputSchema {
232
+ state: "idle" | "focused" | "spinning" | "before:spin" | "scrubbing";
233
+ tag: "focus";
234
+ props: RequiredBy<NumberInputProps, PropsWithDefault>;
235
+ context: PrivateContext;
236
+ computed: ComputedContext;
237
+ action: string;
238
+ event: EventObject;
239
+ guard: string;
240
+ effect: string;
213
241
  }
214
- type State = StateMachine.State<MachineContext, MachineState>;
215
- type Send = StateMachine.Send<StateMachine.AnyEventObject>;
216
- type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
217
- interface MachineApi<T extends PropTypes = PropTypes> {
242
+ type NumberInputService = Service<NumberInputSchema>;
243
+ type NumberInputMachine = Machine<NumberInputSchema>;
244
+ interface NumberInputApi<T extends PropTypes = PropTypes> {
218
245
  /**
219
246
  * Whether the input is focused.
220
247
  */
@@ -273,8 +300,11 @@ interface MachineApi<T extends PropTypes = PropTypes> {
273
300
  getScrubberProps(): T["element"];
274
301
  }
275
302
 
276
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
303
+ declare function connect<T extends PropTypes>(service: NumberInputService, normalize: NormalizeProps<T>): NumberInputApi<T>;
304
+
305
+ declare const machine: _zag_js_core.Machine<NumberInputSchema>;
277
306
 
278
- declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
307
+ declare const props: (keyof NumberInputProps)[];
308
+ declare const splitProps: <Props extends Partial<NumberInputProps>>(props: Props) => [Partial<NumberInputProps>, Omit<Props, keyof NumberInputProps>];
279
309
 
280
- export { type MachineApi as Api, type UserDefinedContext as Context, type ElementIds, type FocusChangeDetails, type IntlTranslations, type Service, type ValueChangeDetails, type ValueInvalidDetails, anatomy, connect, machine };
310
+ export { type NumberInputApi as Api, type ElementIds, type FocusChangeDetails, type IntlTranslations, type NumberInputMachine as Machine, type NumberInputProps as Props, type NumberInputService as Service, type ValueChangeDetails, type ValueInvalidDetails, anatomy, connect, machine, props, splitProps };