Skip to content

Commit 47c199b

Browse files
authored
fix(PinInput): value now updates correctly when caret at the start of the input (#2059)
1 parent 3f216d7 commit 47c199b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

packages/core/src/PinInput/PinInput.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ describe('given default PinInput', () => {
3030
expect(inputs[4].element.placeholder).toBe('*')
3131
})
3232

33+
describe('caret handling', () => {
34+
it('should handle caret at the start of the input', async () => {
35+
await userEvent.keyboard('a')
36+
inputs[0].element.focus()
37+
inputs[0].element.setSelectionRange(0, 0)
38+
await userEvent.keyboard('b')
39+
expect(inputs.map(i => i.element.value)).toStrictEqual(['b', '', '', '', ''])
40+
expect(inputs[1].element).toBe(document.activeElement)
41+
})
42+
43+
it('should handle caret at the end of the input (default focus)', async () => {
44+
await userEvent.keyboard('a')
45+
inputs[0].element.focus()
46+
await userEvent.keyboard('b')
47+
expect(inputs.map(i => i.element.value)).toStrictEqual(['b', '', '', '', ''])
48+
expect(inputs[1].element).toBe(document.activeElement)
49+
})
50+
})
51+
3352
describe('after user input', () => {
3453
beforeEach(async () => {
3554
await userEvent.keyboard('test')

packages/core/src/PinInput/PinInputInput.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function handleInput(event: InputEvent) {
4242
return
4343
}
4444
45-
target.value = target.value.slice(-1)
45+
target.value = event.data ?? ''
4646
updateModelValueAt(props.index, target.value)
4747
4848
const nextEl = inputElements.value[props.index + 1]

0 commit comments

Comments
 (0)