Input field caret position always resets to the end of the input
See original GitHub issueThe bug is testable with the Scrollable container example on http://react-autosuggest.js.org/
To reproduce:
- Focus on the input field
- Type
ac
- Place the caret between
a
andc
- Type
b
Observed behavior:
The caret jumps to the end.
Expected behavior:
The caret should stay between b
and c
.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:7 (1 by maintainers)
Top Results From Across the Web
The 'input' element's cursor position is reset when ...
The problem is that you're replacing the element's entire value when the keyup event is fired. In doing so, the cursor will always...
Read more >Solving Caret Jumping in React Inputs
When you inject programmatically a different value in a DOM input, the input makes no assumption about caret position and moves it to...
Read more >Cursor jumps to end of input when inputValue is a ...
Problem description: Visit this codesandbox; type text; move cursor to the left; type a letter; the letter will be added at the correct...
Read more >React: handling an input caret position | by Alberto De Natale
The new caret position will be recomputed after we modify the text causing our changes to be overridden. Solution: setState callbacks. When we ......
Read more >Possible fix of cursor jumping at the end of the input field
A possible fix is to save and restore the position of the cursor (aka caret) while updating the input field with something like...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I happened to encounter this behavior recently. If your are using
react-autosuggest
withredux-form
, here is a workaround for you.Thanks to advices from https://github.com/facebook/react/issues/955
Demo
https://codesandbox.io/s/0o7wpk6ywv
Code
Wrapping
react-autosuggest
in component:And combining with
redux-form
:This should be enough for handling change of
value
with keyboard events orredux-form
’s action creator (e.g.change
,initialize
,reset
…).Remember it’s still not working with normalization since normalization is a totally new transformation for the
value
.Note
UNSAFE_componentWillReceiveProps
is intentionally used here. For some unknown reasons it doesn’t work withstatic getDerivedStateFromProps(props, state)
.I have a very similar caret behaviour To reproduce:
Focus on the input field Type ac Place the caret between a and c delete a Observed behavior:
The caret jumps to the end.