@zag-js/number-input 0.70.0 → 0.72.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.js +75 -117
- package/dist/index.mjs +14 -31
- package/package.json +10 -11
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/src/cursor.ts +0 -57
- package/src/index.ts +0 -13
- package/src/number-input.anatomy.ts +0 -14
- package/src/number-input.connect.ts +0 -265
- package/src/number-input.dom.ts +0 -113
- package/src/number-input.machine.ts +0 -445
- package/src/number-input.props.ts +0 -34
- package/src/number-input.types.ts +0 -324
- package/src/number-input.utils.ts +0 -22
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
import type { NumberFormatter, NumberParser } from "@internationalized/number"
|
|
2
|
-
import type { Machine, StateMachine as S } from "@zag-js/core"
|
|
3
|
-
import type { CommonProperties, LocaleProperties, PropTypes, RequiredBy } from "@zag-js/types"
|
|
4
|
-
|
|
5
|
-
/* -----------------------------------------------------------------------------
|
|
6
|
-
* Callback details
|
|
7
|
-
* -----------------------------------------------------------------------------*/
|
|
8
|
-
|
|
9
|
-
export interface ValueChangeDetails {
|
|
10
|
-
value: string
|
|
11
|
-
valueAsNumber: number
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface FocusChangeDetails extends ValueChangeDetails {
|
|
15
|
-
focused: boolean
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type ValidityState = "rangeUnderflow" | "rangeOverflow"
|
|
19
|
-
|
|
20
|
-
export interface ValueInvalidDetails extends ValueChangeDetails {
|
|
21
|
-
reason: ValidityState
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/* -----------------------------------------------------------------------------
|
|
25
|
-
* Machine context
|
|
26
|
-
* -----------------------------------------------------------------------------*/
|
|
27
|
-
|
|
28
|
-
export type InputMode = "text" | "tel" | "numeric" | "decimal"
|
|
29
|
-
|
|
30
|
-
export type ElementIds = Partial<{
|
|
31
|
-
root: string
|
|
32
|
-
label: string
|
|
33
|
-
input: string
|
|
34
|
-
incrementTrigger: string
|
|
35
|
-
decrementTrigger: string
|
|
36
|
-
scrubber: string
|
|
37
|
-
}>
|
|
38
|
-
|
|
39
|
-
export type IntlTranslations = {
|
|
40
|
-
/**
|
|
41
|
-
* Function that returns the human-readable value.
|
|
42
|
-
* It is used to set the `aria-valuetext` property of the input
|
|
43
|
-
*/
|
|
44
|
-
valueText?: (value: string) => string
|
|
45
|
-
/**
|
|
46
|
-
* The label foe the increment button
|
|
47
|
-
*/
|
|
48
|
-
incrementLabel: string
|
|
49
|
-
/**
|
|
50
|
-
* The label for the decrement button
|
|
51
|
-
*/
|
|
52
|
-
decrementLabel: string
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
interface PublicContext extends LocaleProperties, CommonProperties {
|
|
56
|
-
/**
|
|
57
|
-
* The ids of the elements in the number input. Useful for composition.
|
|
58
|
-
*/
|
|
59
|
-
ids?: ElementIds
|
|
60
|
-
/**
|
|
61
|
-
* The name attribute of the number input. Useful for form submission.
|
|
62
|
-
*/
|
|
63
|
-
name?: string
|
|
64
|
-
/**
|
|
65
|
-
* The associate form of the input element.
|
|
66
|
-
*/
|
|
67
|
-
form?: string
|
|
68
|
-
/**
|
|
69
|
-
* Whether the number input is disabled.
|
|
70
|
-
*/
|
|
71
|
-
disabled?: boolean
|
|
72
|
-
/**
|
|
73
|
-
* Whether the number input is readonly
|
|
74
|
-
*/
|
|
75
|
-
readOnly?: boolean
|
|
76
|
-
/**
|
|
77
|
-
* Whether the number input value is invalid.
|
|
78
|
-
*/
|
|
79
|
-
invalid?: boolean
|
|
80
|
-
/**
|
|
81
|
-
* Whether the number input is required
|
|
82
|
-
*/
|
|
83
|
-
required?: boolean
|
|
84
|
-
/**
|
|
85
|
-
* The pattern used to check the <input> element's value against
|
|
86
|
-
*
|
|
87
|
-
* @default "[0-9]*(.[0-9]+)?"
|
|
88
|
-
*/
|
|
89
|
-
pattern: string
|
|
90
|
-
/**
|
|
91
|
-
* The value of the input
|
|
92
|
-
*/
|
|
93
|
-
value: string
|
|
94
|
-
/**
|
|
95
|
-
* The minimum value of the number input
|
|
96
|
-
* @default Number.MIN_SAFE_INTEGER
|
|
97
|
-
*/
|
|
98
|
-
min: number
|
|
99
|
-
/**
|
|
100
|
-
* The maximum value of the number input
|
|
101
|
-
* @default Number.MAX_SAFE_INTEGER
|
|
102
|
-
*/
|
|
103
|
-
max: number
|
|
104
|
-
/**
|
|
105
|
-
* The amount to increment or decrement the value by
|
|
106
|
-
* @default 1
|
|
107
|
-
*/
|
|
108
|
-
step: number
|
|
109
|
-
/**
|
|
110
|
-
* Whether to allow mouse wheel to change the value
|
|
111
|
-
*/
|
|
112
|
-
allowMouseWheel?: boolean
|
|
113
|
-
/**
|
|
114
|
-
* Whether to allow the value overflow the min/max range
|
|
115
|
-
* @default true
|
|
116
|
-
*/
|
|
117
|
-
allowOverflow: boolean
|
|
118
|
-
/**
|
|
119
|
-
* Whether to clamp the value when the input loses focus (blur)
|
|
120
|
-
* @default true
|
|
121
|
-
*/
|
|
122
|
-
clampValueOnBlur: boolean
|
|
123
|
-
/**
|
|
124
|
-
* Whether to focus input when the value changes
|
|
125
|
-
* @default true
|
|
126
|
-
*/
|
|
127
|
-
focusInputOnChange: boolean
|
|
128
|
-
/**
|
|
129
|
-
* Specifies the localized strings that identifies the accessibility elements and their states
|
|
130
|
-
*/
|
|
131
|
-
translations: IntlTranslations
|
|
132
|
-
/**
|
|
133
|
-
* The options to pass to the `Intl.NumberFormat` constructor
|
|
134
|
-
*/
|
|
135
|
-
formatOptions?: Intl.NumberFormatOptions
|
|
136
|
-
/**
|
|
137
|
-
* Hints at the type of data that might be entered by the user. It also determines
|
|
138
|
-
* the type of keyboard shown to the user on mobile devices
|
|
139
|
-
* @default "decimal"
|
|
140
|
-
*/
|
|
141
|
-
inputMode: InputMode
|
|
142
|
-
/**
|
|
143
|
-
* Function invoked when the value changes
|
|
144
|
-
*/
|
|
145
|
-
onValueChange?: (details: ValueChangeDetails) => void
|
|
146
|
-
/**
|
|
147
|
-
* Function invoked when the value overflows or underflows the min/max range
|
|
148
|
-
*/
|
|
149
|
-
onValueInvalid?: (details: ValueInvalidDetails) => void
|
|
150
|
-
/**
|
|
151
|
-
* Function invoked when the number input is focused
|
|
152
|
-
*/
|
|
153
|
-
onFocusChange?: (details: FocusChangeDetails) => void
|
|
154
|
-
/**
|
|
155
|
-
* Whether to spin the value when the increment/decrement button is pressed
|
|
156
|
-
* @default true
|
|
157
|
-
*/
|
|
158
|
-
spinOnPress?: boolean
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export type UserDefinedContext = RequiredBy<PublicContext, "id">
|
|
162
|
-
|
|
163
|
-
type ComputedContext = Readonly<{
|
|
164
|
-
/**
|
|
165
|
-
* @computed
|
|
166
|
-
* The value of the input as a number
|
|
167
|
-
*/
|
|
168
|
-
valueAsNumber: number
|
|
169
|
-
/**
|
|
170
|
-
* @computed
|
|
171
|
-
* Whether the value is at the min
|
|
172
|
-
*/
|
|
173
|
-
isAtMin: boolean
|
|
174
|
-
/**
|
|
175
|
-
* @computed
|
|
176
|
-
* Whether the value is at the max
|
|
177
|
-
*/
|
|
178
|
-
isAtMax: boolean
|
|
179
|
-
/**
|
|
180
|
-
* @computed
|
|
181
|
-
* Whether the value is out of the min/max range
|
|
182
|
-
*/
|
|
183
|
-
isOutOfRange: boolean
|
|
184
|
-
/**
|
|
185
|
-
* @computed
|
|
186
|
-
* Whether the value is empty
|
|
187
|
-
*/
|
|
188
|
-
isValueEmpty: boolean
|
|
189
|
-
/**
|
|
190
|
-
* @computed
|
|
191
|
-
* Whether the color picker is disabled
|
|
192
|
-
*/
|
|
193
|
-
isDisabled: boolean
|
|
194
|
-
/**
|
|
195
|
-
* @computed
|
|
196
|
-
* Whether the increment button is enabled
|
|
197
|
-
*/
|
|
198
|
-
canIncrement: boolean
|
|
199
|
-
/**
|
|
200
|
-
* @computed
|
|
201
|
-
* Whether the decrement button is enabled
|
|
202
|
-
*/
|
|
203
|
-
canDecrement: boolean
|
|
204
|
-
/**
|
|
205
|
-
* @computed
|
|
206
|
-
* The `aria-valuetext` attribute of the input
|
|
207
|
-
*/
|
|
208
|
-
valueText: string | undefined
|
|
209
|
-
/**
|
|
210
|
-
* @computed
|
|
211
|
-
* The formatted value of the input
|
|
212
|
-
*/
|
|
213
|
-
formattedValue: string
|
|
214
|
-
/**
|
|
215
|
-
* @computed
|
|
216
|
-
* Whether the writing direction is RTL
|
|
217
|
-
*/
|
|
218
|
-
isRtl: boolean
|
|
219
|
-
}>
|
|
220
|
-
|
|
221
|
-
interface PrivateContext {
|
|
222
|
-
/**
|
|
223
|
-
* @internal
|
|
224
|
-
* The hint that determines if we're incrementing or decrementing
|
|
225
|
-
*/
|
|
226
|
-
hint: "increment" | "decrement" | "set" | null
|
|
227
|
-
/**
|
|
228
|
-
* @internal
|
|
229
|
-
* The scrubber cursor position
|
|
230
|
-
*/
|
|
231
|
-
scrubberCursorPoint: { x: number; y: number } | null
|
|
232
|
-
/**
|
|
233
|
-
* @internal
|
|
234
|
-
* The number i18n formatter
|
|
235
|
-
*/
|
|
236
|
-
formatter: NumberFormatter
|
|
237
|
-
/**
|
|
238
|
-
* @internal
|
|
239
|
-
* The number i18n parser
|
|
240
|
-
*/
|
|
241
|
-
parser: NumberParser
|
|
242
|
-
/**
|
|
243
|
-
* @internal
|
|
244
|
-
* Whether the checkbox's fieldset is disabled
|
|
245
|
-
*/
|
|
246
|
-
fieldsetDisabled: boolean
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
export interface MachineContext extends PublicContext, PrivateContext, ComputedContext {}
|
|
250
|
-
|
|
251
|
-
export interface MachineState {
|
|
252
|
-
value: "idle" | "focused" | "spinning" | "before:spin" | "scrubbing"
|
|
253
|
-
tags: "focus"
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
export type State = S.State<MachineContext, MachineState>
|
|
257
|
-
|
|
258
|
-
export type Send = S.Send<S.AnyEventObject>
|
|
259
|
-
|
|
260
|
-
export type Service = Machine<MachineContext, MachineState, S.AnyEventObject>
|
|
261
|
-
|
|
262
|
-
/* -----------------------------------------------------------------------------
|
|
263
|
-
* Component API
|
|
264
|
-
* -----------------------------------------------------------------------------*/
|
|
265
|
-
|
|
266
|
-
export interface MachineApi<T extends PropTypes = PropTypes> {
|
|
267
|
-
/**
|
|
268
|
-
* Whether the input is focused.
|
|
269
|
-
*/
|
|
270
|
-
focused: boolean
|
|
271
|
-
/**
|
|
272
|
-
* Whether the input is invalid.
|
|
273
|
-
*/
|
|
274
|
-
invalid: boolean
|
|
275
|
-
/**
|
|
276
|
-
* Whether the input value is empty.
|
|
277
|
-
*/
|
|
278
|
-
empty: boolean
|
|
279
|
-
/**
|
|
280
|
-
* The formatted value of the input.
|
|
281
|
-
*/
|
|
282
|
-
value: string
|
|
283
|
-
/**
|
|
284
|
-
* The value of the input as a number.
|
|
285
|
-
*/
|
|
286
|
-
valueAsNumber: number
|
|
287
|
-
/**
|
|
288
|
-
* Function to set the value of the input.
|
|
289
|
-
*/
|
|
290
|
-
setValue(value: number): void
|
|
291
|
-
/**
|
|
292
|
-
* Function to clear the value of the input.
|
|
293
|
-
*/
|
|
294
|
-
clearValue(): void
|
|
295
|
-
/**
|
|
296
|
-
* Function to increment the value of the input by the step.
|
|
297
|
-
*/
|
|
298
|
-
increment(): void
|
|
299
|
-
/**
|
|
300
|
-
* Function to decrement the value of the input by the step.
|
|
301
|
-
*/
|
|
302
|
-
decrement(): void
|
|
303
|
-
/**
|
|
304
|
-
* Function to set the value of the input to the max.
|
|
305
|
-
*/
|
|
306
|
-
setToMax(): void
|
|
307
|
-
/**
|
|
308
|
-
* Function to set the value of the input to the min.
|
|
309
|
-
*/
|
|
310
|
-
setToMin(): void
|
|
311
|
-
/**
|
|
312
|
-
* Function to focus the input.
|
|
313
|
-
*/
|
|
314
|
-
focus(): void
|
|
315
|
-
|
|
316
|
-
getRootProps(): T["element"]
|
|
317
|
-
getLabelProps(): T["label"]
|
|
318
|
-
getControlProps(): T["element"]
|
|
319
|
-
getValueTextProps(): T["element"]
|
|
320
|
-
getInputProps(): T["input"]
|
|
321
|
-
getDecrementTriggerProps(): T["button"]
|
|
322
|
-
getIncrementTriggerProps(): T["button"]
|
|
323
|
-
getScrubberProps(): T["element"]
|
|
324
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { NumberParser } from "@internationalized/number"
|
|
2
|
-
import { ref } from "@zag-js/core"
|
|
3
|
-
import type { MachineContext } from "./number-input.types"
|
|
4
|
-
|
|
5
|
-
export const createFormatter = (locale: string, options: Intl.NumberFormatOptions = {}) => {
|
|
6
|
-
return ref(new Intl.NumberFormat(locale, options))
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const createParser = (locale: string, options: Intl.NumberFormatOptions = {}) => {
|
|
10
|
-
return ref(new NumberParser(locale, options))
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const parseValue = (ctx: MachineContext, value: string) => {
|
|
14
|
-
if (!ctx.formatOptions) return parseFloat(value)
|
|
15
|
-
return ctx.parser.parse(String(value))
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const formatValue = (ctx: MachineContext, value: number): string => {
|
|
19
|
-
if (Number.isNaN(value)) return ""
|
|
20
|
-
if (!ctx.formatOptions) return value.toString()
|
|
21
|
-
return ctx.formatter.format(value)
|
|
22
|
-
}
|