question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Input field caret position always resets to the end of the input

See original GitHub issue

The bug is testable with the Scrollable container example on http://react-autosuggest.js.org/

To reproduce:

  1. Focus on the input field
  2. Type ac
  3. Place the caret between a and c
  4. Type b

Observed behavior:

The caret jumps to the end.

Expected behavior:

The caret should stay between b and c.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
zushenyancommented, Oct 25, 2018

I happened to encounter this behavior recently. If your are using react-autosuggest with redux-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:

class MyInput extends React.PureComponent {
  state = { value: "" };

  UNSAFE_componentWillReceiveProps = props => {
    this.setState({ value: props.input.value });
  };

  onChange = (e, { newValue }) => {
    e.persist();
    this.setState({ value: newValue }, () => {
      this.props.input.onChange(e, newValue);
    });
  };

  render() {
    const inputProps = {
      ...this.props.input,
      value: this.state.value,
      onChange: this.onChange
    };
    return (
      <Autosuggest
        suggestions={[]}
        onSuggestionsFetchRequested={() => {}}
        onSuggestionsClearRequested={() => {}}
        inputProps={inputProps}
      />
    );
  }
}

And combining with redux-form:

const SimpleForm = props => {
  const { handleSubmit } = props;
  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label>First Name</label>
        <div>
          <Field name="firstName" component={MyInput} />
        </div>
      </div>
    </form>
  );
};

export default reduxForm({
  form: "simple" // a unique identifier for this form
})(SimpleForm);

This should be enough for handling change of value with keyboard events or redux-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 with static getDerivedStateFromProps(props, state).

0reactions
ghostcommented, Jul 16, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found