Skip to content

Commit c59c574

Browse files
committed
fix: number input cursor
1 parent 37a6759 commit c59c574

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

.changeset/cursor-restore-fix.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@zag-js/number-input": patch
3+
---
4+
5+
Fix cursor jumping to start when value is changed externally via props while user is typing
6+

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
isValueAtMin,
1919
isValueWithinRange,
2020
} from "@zag-js/utils"
21-
import { restoreCursor } from "./cursor"
21+
import { recordCursor, restoreCursor } from "./cursor"
2222
import * as dom from "./number-input.dom"
2323
import type { HintValue, NumberInputSchema } from "./number-input.types"
2424
import { createFormatter, createParser, formatValue, getDefaultStep, parseValue } from "./number-input.utils"
@@ -432,7 +432,9 @@ export const machine = createMachine({
432432
syncInputElement({ context, event, computed, scope }) {
433433
const value = event.type.endsWith("CHANGE") ? context.get("value") : computed("formattedValue")
434434
const inputEl = dom.getInputEl(scope)
435-
const sel = event.selection
435+
// Record cursor position before sync if not provided in event
436+
// This handles external value changes while user is typing
437+
const sel = event.selection ?? recordCursor(inputEl, scope)
436438
raf(() => {
437439
setElementValue(inputEl, value)
438440
restoreCursor(inputEl, sel, scope)

0 commit comments

Comments
 (0)