@zag-js/number-input 0.53.0 → 0.55.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.
@@ -47,194 +47,208 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
47
47
  dom.getInputEl(state.context)?.focus()
48
48
  },
49
49
 
50
- rootProps: normalize.element({
51
- id: dom.getRootId(state.context),
52
- ...parts.root.attrs,
53
- dir: state.context.dir,
54
- "data-disabled": dataAttr(disabled),
55
- "data-focus": dataAttr(focused),
56
- "data-invalid": dataAttr(invalid),
57
- }),
58
-
59
- labelProps: normalize.label({
60
- ...parts.label.attrs,
61
- dir: state.context.dir,
62
- "data-disabled": dataAttr(disabled),
63
- "data-focus": dataAttr(focused),
64
- "data-invalid": dataAttr(invalid),
65
- id: dom.getLabelId(state.context),
66
- htmlFor: dom.getInputId(state.context),
67
- }),
68
-
69
- controlProps: normalize.element({
70
- ...parts.control.attrs,
71
- dir: state.context.dir,
72
- role: "group",
73
- "aria-disabled": disabled,
74
- "data-focus": dataAttr(focused),
75
- "data-disabled": dataAttr(disabled),
76
- "data-invalid": dataAttr(invalid),
77
- "aria-invalid": ariaAttr(state.context.invalid),
78
- }),
79
-
80
- inputProps: normalize.input({
81
- ...parts.input.attrs,
82
- dir: state.context.dir,
83
- name: state.context.name,
84
- form: state.context.form,
85
- id: dom.getInputId(state.context),
86
- role: "spinbutton",
87
- defaultValue: state.context.formattedValue,
88
- pattern: state.context.pattern,
89
- inputMode: state.context.inputMode,
90
- "aria-invalid": ariaAttr(invalid),
91
- "data-invalid": dataAttr(invalid),
92
- disabled: disabled,
93
- "data-disabled": dataAttr(disabled),
94
- readOnly: !!state.context.readOnly,
95
- autoComplete: "off",
96
- autoCorrect: "off",
97
- spellCheck: "false",
98
- type: "text",
99
- "aria-roledescription": "numberfield",
100
- "aria-valuemin": state.context.min,
101
- "aria-valuemax": state.context.max,
102
- "aria-valuenow": Number.isNaN(state.context.valueAsNumber) ? undefined : state.context.valueAsNumber,
103
- "aria-valuetext": state.context.valueText,
104
- onFocus() {
105
- send("INPUT.FOCUS")
106
- },
107
- onBlur() {
108
- send({ type: "INPUT.COMMIT", src: "blur" })
109
- },
110
- onChange(event) {
111
- send({ type: "INPUT.CHANGE", target: event.currentTarget, hint: "set" })
112
- },
113
- onBeforeInput(event) {
114
- try {
115
- const { selectionStart, selectionEnd, value } = event.currentTarget
116
-
117
- const nextValue = value.slice(0, selectionStart!) + ((event as any).data ?? "") + value.slice(selectionEnd!)
118
- const isValid = state.context.parser.isValidPartialNumber(nextValue)
119
-
120
- if (!isValid) {
121
- event.preventDefault()
50
+ getRootProps() {
51
+ return normalize.element({
52
+ id: dom.getRootId(state.context),
53
+ ...parts.root.attrs,
54
+ dir: state.context.dir,
55
+ "data-disabled": dataAttr(disabled),
56
+ "data-focus": dataAttr(focused),
57
+ "data-invalid": dataAttr(invalid),
58
+ })
59
+ },
60
+
61
+ getLabelProps() {
62
+ return normalize.label({
63
+ ...parts.label.attrs,
64
+ dir: state.context.dir,
65
+ "data-disabled": dataAttr(disabled),
66
+ "data-focus": dataAttr(focused),
67
+ "data-invalid": dataAttr(invalid),
68
+ id: dom.getLabelId(state.context),
69
+ htmlFor: dom.getInputId(state.context),
70
+ })
71
+ },
72
+
73
+ getControlProps() {
74
+ return normalize.element({
75
+ ...parts.control.attrs,
76
+ dir: state.context.dir,
77
+ role: "group",
78
+ "aria-disabled": disabled,
79
+ "data-focus": dataAttr(focused),
80
+ "data-disabled": dataAttr(disabled),
81
+ "data-invalid": dataAttr(invalid),
82
+ "aria-invalid": ariaAttr(state.context.invalid),
83
+ })
84
+ },
85
+
86
+ getInputProps() {
87
+ return normalize.input({
88
+ ...parts.input.attrs,
89
+ dir: state.context.dir,
90
+ name: state.context.name,
91
+ form: state.context.form,
92
+ id: dom.getInputId(state.context),
93
+ role: "spinbutton",
94
+ defaultValue: state.context.formattedValue,
95
+ pattern: state.context.pattern,
96
+ inputMode: state.context.inputMode,
97
+ "aria-invalid": ariaAttr(invalid),
98
+ "data-invalid": dataAttr(invalid),
99
+ disabled: disabled,
100
+ "data-disabled": dataAttr(disabled),
101
+ readOnly: !!state.context.readOnly,
102
+ autoComplete: "off",
103
+ autoCorrect: "off",
104
+ spellCheck: "false",
105
+ type: "text",
106
+ "aria-roledescription": "numberfield",
107
+ "aria-valuemin": state.context.min,
108
+ "aria-valuemax": state.context.max,
109
+ "aria-valuenow": Number.isNaN(state.context.valueAsNumber) ? undefined : state.context.valueAsNumber,
110
+ "aria-valuetext": state.context.valueText,
111
+ onFocus() {
112
+ send("INPUT.FOCUS")
113
+ },
114
+ onBlur() {
115
+ send({ type: "INPUT.COMMIT", src: "blur" })
116
+ },
117
+ onChange(event) {
118
+ send({ type: "INPUT.CHANGE", target: event.currentTarget, hint: "set" })
119
+ },
120
+ onBeforeInput(event) {
121
+ try {
122
+ const { selectionStart, selectionEnd, value } = event.currentTarget
123
+
124
+ const nextValue = value.slice(0, selectionStart!) + ((event as any).data ?? "") + value.slice(selectionEnd!)
125
+ const isValid = state.context.parser.isValidPartialNumber(nextValue)
126
+
127
+ if (!isValid) {
128
+ event.preventDefault()
129
+ }
130
+ } catch {
131
+ // noop
122
132
  }
123
- } catch {
124
- // noop
125
- }
126
- },
127
- onKeyDown(event) {
128
- if (event.defaultPrevented) return
129
- if (readOnly) return
130
- if (isComposingEvent(event)) return
131
-
132
- const step = getEventStep(event) * state.context.step
133
-
134
- const keyMap: EventKeyMap = {
135
- ArrowUp() {
136
- send({ type: "INPUT.ARROW_UP", step })
137
- event.preventDefault()
138
- },
139
- ArrowDown() {
140
- send({ type: "INPUT.ARROW_DOWN", step })
141
- event.preventDefault()
142
- },
143
- Home() {
144
- send("INPUT.HOME")
133
+ },
134
+ onKeyDown(event) {
135
+ if (event.defaultPrevented) return
136
+ if (readOnly) return
137
+ if (isComposingEvent(event)) return
138
+
139
+ const step = getEventStep(event) * state.context.step
140
+
141
+ const keyMap: EventKeyMap = {
142
+ ArrowUp() {
143
+ send({ type: "INPUT.ARROW_UP", step })
144
+ event.preventDefault()
145
+ },
146
+ ArrowDown() {
147
+ send({ type: "INPUT.ARROW_DOWN", step })
148
+ event.preventDefault()
149
+ },
150
+ Home() {
151
+ send("INPUT.HOME")
152
+ event.preventDefault()
153
+ },
154
+ End() {
155
+ send("INPUT.END")
156
+ event.preventDefault()
157
+ },
158
+ Enter() {
159
+ send({ type: "INPUT.COMMIT", src: "enter" })
160
+ },
161
+ }
162
+
163
+ const exec = keyMap[event.key]
164
+ exec?.(event)
165
+ },
166
+ })
167
+ },
168
+
169
+ getDecrementTriggerProps() {
170
+ return normalize.button({
171
+ ...parts.decrementTrigger.attrs,
172
+ dir: state.context.dir,
173
+ id: dom.getDecrementTriggerId(state.context),
174
+ disabled: isDecrementDisabled,
175
+ "data-disabled": dataAttr(isDecrementDisabled),
176
+ "aria-label": translations.decrementLabel,
177
+ type: "button",
178
+ tabIndex: -1,
179
+ "aria-controls": dom.getInputId(state.context),
180
+ onPointerDown(event) {
181
+ if (isDecrementDisabled || !isLeftClick(event)) return
182
+ send({ type: "TRIGGER.PRESS_DOWN", hint: "decrement", pointerType: event.pointerType })
183
+ if (event.pointerType === "mouse") {
145
184
  event.preventDefault()
146
- },
147
- End() {
148
- send("INPUT.END")
185
+ }
186
+ if (event.pointerType === "touch") {
187
+ event.currentTarget?.focus({ preventScroll: true })
188
+ }
189
+ },
190
+ onPointerUp(event) {
191
+ send({ type: "TRIGGER.PRESS_UP", hint: "decrement", pointerType: event.pointerType })
192
+ },
193
+ onPointerLeave() {
194
+ if (isDecrementDisabled) return
195
+ send({ type: "TRIGGER.PRESS_UP", hint: "decrement" })
196
+ },
197
+ })
198
+ },
199
+
200
+ getIncrementTriggerProps() {
201
+ return normalize.button({
202
+ ...parts.incrementTrigger.attrs,
203
+ dir: state.context.dir,
204
+ id: dom.getIncrementTriggerId(state.context),
205
+ disabled: isIncrementDisabled,
206
+ "data-disabled": dataAttr(isIncrementDisabled),
207
+ "aria-label": translations.incrementLabel,
208
+ type: "button",
209
+ tabIndex: -1,
210
+ "aria-controls": dom.getInputId(state.context),
211
+ onPointerDown(event) {
212
+ if (isIncrementDisabled || !isLeftClick(event)) return
213
+ send({ type: "TRIGGER.PRESS_DOWN", hint: "increment", pointerType: event.pointerType })
214
+ if (event.pointerType === "mouse") {
149
215
  event.preventDefault()
150
- },
151
- Enter() {
152
- send({ type: "INPUT.COMMIT", src: "enter" })
153
- },
154
- }
155
-
156
- const exec = keyMap[event.key]
157
- exec?.(event)
158
- },
159
- }),
160
-
161
- decrementTriggerProps: normalize.button({
162
- ...parts.decrementTrigger.attrs,
163
- dir: state.context.dir,
164
- id: dom.getDecrementTriggerId(state.context),
165
- disabled: isDecrementDisabled,
166
- "data-disabled": dataAttr(isDecrementDisabled),
167
- "aria-label": translations.decrementLabel,
168
- type: "button",
169
- tabIndex: -1,
170
- "aria-controls": dom.getInputId(state.context),
171
- onPointerDown(event) {
172
- if (isDecrementDisabled || !isLeftClick(event)) return
173
- send({ type: "TRIGGER.PRESS_DOWN", hint: "decrement", pointerType: event.pointerType })
174
- if (event.pointerType === "mouse") {
175
- event.preventDefault()
176
- }
177
- if (event.pointerType === "touch") {
178
- event.currentTarget?.focus({ preventScroll: true })
179
- }
180
- },
181
- onPointerUp(event) {
182
- send({ type: "TRIGGER.PRESS_UP", hint: "decrement", pointerType: event.pointerType })
183
- },
184
- onPointerLeave() {
185
- if (isDecrementDisabled) return
186
- send({ type: "TRIGGER.PRESS_UP", hint: "decrement" })
187
- },
188
- }),
189
-
190
- incrementTriggerProps: normalize.button({
191
- ...parts.incrementTrigger.attrs,
192
- dir: state.context.dir,
193
- id: dom.getIncrementTriggerId(state.context),
194
- disabled: isIncrementDisabled,
195
- "data-disabled": dataAttr(isIncrementDisabled),
196
- "aria-label": translations.incrementLabel,
197
- type: "button",
198
- tabIndex: -1,
199
- "aria-controls": dom.getInputId(state.context),
200
- onPointerDown(event) {
201
- if (isIncrementDisabled || !isLeftClick(event)) return
202
- send({ type: "TRIGGER.PRESS_DOWN", hint: "increment", pointerType: event.pointerType })
203
- if (event.pointerType === "mouse") {
216
+ }
217
+ if (event.pointerType === "touch") {
218
+ event.currentTarget?.focus({ preventScroll: true })
219
+ }
220
+ },
221
+ onPointerUp(event) {
222
+ send({ type: "TRIGGER.PRESS_UP", hint: "increment", pointerType: event.pointerType })
223
+ },
224
+ onPointerLeave(event) {
225
+ send({ type: "TRIGGER.PRESS_UP", hint: "increment", pointerType: event.pointerType })
226
+ },
227
+ })
228
+ },
229
+
230
+ getScrubberProps() {
231
+ return normalize.element({
232
+ ...parts.scrubber.attrs,
233
+ dir: state.context.dir,
234
+ "data-disabled": dataAttr(disabled),
235
+ id: dom.getScrubberId(state.context),
236
+ role: "presentation",
237
+ onMouseDown(event) {
238
+ if (disabled) return
239
+
240
+ const point = getEventPoint(event)
241
+
242
+ point.x = point.x - roundToDevicePixel(7.5)
243
+ point.y = point.y - roundToDevicePixel(7.5)
244
+
245
+ send({ type: "SCRUBBER.PRESS_DOWN", point })
204
246
  event.preventDefault()
205
- }
206
- if (event.pointerType === "touch") {
207
- event.currentTarget?.focus({ preventScroll: true })
208
- }
209
- },
210
- onPointerUp(event) {
211
- send({ type: "TRIGGER.PRESS_UP", hint: "increment", pointerType: event.pointerType })
212
- },
213
- onPointerLeave(event) {
214
- send({ type: "TRIGGER.PRESS_UP", hint: "increment", pointerType: event.pointerType })
215
- },
216
- }),
217
-
218
- scrubberProps: normalize.element({
219
- ...parts.scrubber.attrs,
220
- dir: state.context.dir,
221
- "data-disabled": dataAttr(disabled),
222
- id: dom.getScrubberId(state.context),
223
- role: "presentation",
224
- onMouseDown(event) {
225
- if (disabled) return
226
-
227
- const point = getEventPoint(event)
228
-
229
- point.x = point.x - roundToDevicePixel(7.5)
230
- point.y = point.y - roundToDevicePixel(7.5)
231
-
232
- send({ type: "SCRUBBER.PRESS_DOWN", point })
233
- event.preventDefault()
234
- },
235
- style: {
236
- cursor: disabled ? undefined : "ew-resize",
237
- },
238
- }),
247
+ },
248
+ style: {
249
+ cursor: disabled ? undefined : "ew-resize",
250
+ },
251
+ })
252
+ },
239
253
  }
240
254
  }
@@ -307,11 +307,11 @@ export interface MachineApi<T extends PropTypes = PropTypes> {
307
307
  */
308
308
  focus(): void
309
309
 
310
- rootProps: T["element"]
311
- labelProps: T["label"]
312
- controlProps: T["element"]
313
- inputProps: T["input"]
314
- decrementTriggerProps: T["button"]
315
- incrementTriggerProps: T["button"]
316
- scrubberProps: T["element"]
310
+ getRootProps(): T["element"]
311
+ getLabelProps(): T["label"]
312
+ getControlProps(): T["element"]
313
+ getInputProps(): T["input"]
314
+ getDecrementTriggerProps(): T["button"]
315
+ getIncrementTriggerProps(): T["button"]
316
+ getScrubberProps(): T["element"]
317
317
  }