Skip to content

Commit 15fd93e

Browse files
committed
refactor(number-input): add value commit callback
1 parent aac4769 commit 15fd93e

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zag-js/number-input": minor
3+
---
4+
5+
Add `onValueCommit` callback that fires when the input loses focus or Enter is pressed

packages/machines/number-input/src/number-input.machine.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,20 @@ export const machine = createMachine({
183183
{
184184
guard: and("clampValueOnBlur", not("isInRange")),
185185
target: "idle",
186-
actions: ["setClampedValue", "clearHint", "invokeOnBlur"],
186+
actions: ["setClampedValue", "clearHint", "invokeOnBlur", "invokeOnValueCommit"],
187187
},
188188
{
189189
guard: not("isInRange"),
190190
target: "idle",
191-
actions: ["setFormattedValue", "clearHint", "invokeOnBlur", "invokeOnInvalid"],
191+
actions: ["setFormattedValue", "clearHint", "invokeOnBlur", "invokeOnInvalid", "invokeOnValueCommit"],
192192
},
193193
{
194194
target: "idle",
195-
actions: ["setFormattedValue", "clearHint", "invokeOnBlur"],
195+
actions: ["setFormattedValue", "clearHint", "invokeOnBlur", "invokeOnValueCommit"],
196196
},
197197
],
198198
"INPUT.ENTER": {
199-
actions: ["setFormattedValue", "clearHint", "invokeOnBlur"],
199+
actions: ["setFormattedValue", "clearHint", "invokeOnBlur", "invokeOnValueCommit"],
200200
},
201201
},
202202
},
@@ -429,6 +429,12 @@ export const machine = createMachine({
429429
valueAsNumber: computed("valueAsNumber"),
430430
})
431431
},
432+
invokeOnValueCommit({ computed, prop }) {
433+
prop("onValueCommit")?.({
434+
value: computed("formattedValue"),
435+
valueAsNumber: computed("valueAsNumber"),
436+
})
437+
},
432438
syncInputElement({ context, event, computed, scope }) {
433439
const value = event.type.endsWith("CHANGE") ? context.get("value") : computed("formattedValue")
434440
const inputEl = dom.getInputEl(scope)

packages/machines/number-input/src/number-input.props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const props = createProps<NumberInputProps>()([
2222
"name",
2323
"onFocusChange",
2424
"onValueChange",
25+
"onValueCommit",
2526
"onValueInvalid",
2627
"pattern",
2728
"required",

packages/machines/number-input/src/number-input.types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ export interface NumberInputProps extends LocaleProperties, CommonProperties {
156156
* Function invoked when the number input is focused
157157
*/
158158
onFocusChange?: ((details: FocusChangeDetails) => void) | undefined
159+
/**
160+
* Function invoked when the value is committed (when the input is blurred or the Enter key is pressed)
161+
*/
162+
onValueCommit?: ((details: ValueChangeDetails) => void) | undefined
159163
/**
160164
* Whether to spin the value when the increment/decrement button is pressed
161165
* @default true

0 commit comments

Comments
 (0)