Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autofocus Text puts cursor at end instead of beginning #14125

Open
Matthew-Goldberg opened this issue Nov 6, 2018 · 10 comments
Open

Autofocus Text puts cursor at end instead of beginning #14125

Matthew-Goldberg opened this issue Nov 6, 2018 · 10 comments

Comments

@Matthew-Goldberg
Copy link

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
In React 16, a text input with a value and autofocus will set the cursor at the end of the input.
<input autoFocus={true} type="text" defaultValue="4444" />

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

See the React 16 repro here: https://codepen.io/matthewg0/pen/XymjYo

What is the expected behavior?
In React 15, and in plain HTML, the behavior is that the cursor is placed at the start of the input.
React 15: https://codepen.io/matthewg0/pen/NEGRzv
Plain HTML: https://codepen.io/matthewg0/pen/wQKoap

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Reproduces as of React 16. Worked as expected in React 15.

@gaearon
Copy link
Collaborator

gaearon commented Nov 7, 2018

Want to look into a fix?

@Matthew-Goldberg
Copy link
Author

Sure thing, thanks for confirming. I'll look into it.

@mmarkelov
Copy link
Contributor

@Matthew-Goldberg I just tried to figure out what is wrong with it, but I don't have any ideas with it. So if you stack on it - you could try this - works in all modern browsers

@heyimalex
Copy link

Just to narrow it down, v16.2.0 works and v16.3.0 doesn't, so it's somewhere in there.

@chasestarr
Copy link

bisected to this commit

@Matthew-Goldberg
Copy link
Author

Thank you @chasestarr that makes sense. It also happens in plain HTML the cursor will be at the end if you set the value using script input.value = 'something' instead of attribute <input value="something"

@Matthew-Goldberg
Copy link
Author

@mmarkelov I think this will be hard to write a test, it seems jsdom is showing that the selectionStart is 0 even though in the browser the same code shows selectionStart is the end/length of the text value.

@Matthew-Goldberg
Copy link
Author

OK the jsdom issue is now fixed: jsdom/jsdom#2799
When that is released we can upgrade the jsdom version to pick up the fix, and re-implement the test which should now be useful.

@bl00mber
Copy link
Contributor

bl00mber commented Mar 25, 2020

(#14171)
Strictly speaking it looks like a feature rather than a bug because it is reasonable to place cursor at the end of input to use backspace immediately

@xrutayisire
Copy link

(#14171)
Strictly speaking it looks like a feature rather than a bug because it is reasonable to place cursor at the end of input to use backspace immediately

Hum, for me it depends on the use case. But the thing is in React 16 the behavior changed without any info about it, so for me it's just a regression until it's proven that it's the desired effect for most use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants