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

Cannot declaratively specificy select position for controlled input elements #5951

Closed
dminkovsky opened this issue Jan 31, 2016 · 3 comments
Closed

Comments

@dminkovsky
Copy link

I am working on a component that has a controlled input element. The component has an onChange handler that manipulates user input before setting the value back on the input's value prop. As a result of this manipulation, the caret position as restored by React may be different than where the user was typing.

To control this situation, I keep track of the caret position in the component's state:

onKeyUp({ target }) {                    // also in `onChange`, `onMouseUp`, etc
    this.saveSelectState(target);
}

and then set it back to the desired position after the component updates:

componentDidUpdate() {
    const { selectionStart, selectionEnd } = this.state;
    const { input } = this.refs;

    try {        
        if (input === document.activeElement) {
            input.setSelectionRange(selectionStart, selectionEnd);
        }
    } catch(e) {}
}

The API I desire, however, would be more like:

<input
    type='text'
    selectionStart={ 5 }
    selectionEnd={ 5 }
    ... and so on
/>

It was pretty simple to implement this abstraction for myself, but it seems like something that's actually missing from React. I think this goes along well with the idea that React is an API for declaratively controlling UI components' state.

@dminkovsky
Copy link
Author

Draft.js might somehow be related to this.

@zpao
Copy link
Member

zpao commented Feb 25, 2016

I think we'll opt to keep core clean of this and just provide the building blocks. We don't support every possible property that a node might have, focusing on the markup attributes (for markup generation). Another example of this is scrollTop, which could be described declaratively but is not supported.

@zpao zpao closed this as completed Feb 25, 2016
@dminkovsky
Copy link
Author

Cool, thank you.

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

No branches or pull requests

2 participants