-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(keyboard): maintain cursor position on controlled React input (#665)
* test: maintain cursor position on controlled React input * fix: maintain cursor position on controlled React input * fix: remove obsolete call of setSelectionRange
- Loading branch information
1 parent
180a3d3
commit 03f38b9
Showing
2 changed files
with
33 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React, { useState } from 'react' | ||
import { render, screen } from '@testing-library/react' | ||
import userEvent from 'index' | ||
|
||
test('maintain cursor position on controlled input', () => { | ||
function Input({initialValue}: {initialValue: string}) { | ||
const [val, setVal] = useState(initialValue) | ||
|
||
return <input value={val} onChange={e => setVal(e.target.value)}/> | ||
} | ||
|
||
render(<Input initialValue="acd"/>) | ||
|
||
;screen.getByRole('textbox').focus() | ||
;(screen.getByRole('textbox') as HTMLInputElement).setSelectionRange(1,1) | ||
userEvent.keyboard('b') | ||
|
||
expect(screen.getByRole('textbox')).toHaveValue('abcd') | ||
expect(screen.getByRole('textbox')).toHaveProperty('selectionStart', 2) | ||
expect(screen.getByRole('textbox')).toHaveProperty('selectionEnd', 2) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters