Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Unable to set focus to end of textarea #224

Closed
brookslyrette opened this issue Mar 22, 2016 · 2 comments
Closed

Unable to set focus to end of textarea #224

brookslyrette opened this issue Mar 22, 2016 · 2 comments
Labels

Comments

@brookslyrette
Copy link
Contributor

I'm prepopulating draft-js with content but when I do focus is always at the start:

screen shot 2016-03-22 at 2 10 05 pm

I'm setting the value in my constructor:

constructor(props) {
        super(props);
        let editorState = EditorState.createEmpty();
        if (this.props.caption.content.trim() !== '') {
            const captionAsHtml = convertToHtml(this.props.caption);
            const processedHTML = DraftPasteProcessor.processHTML(captionAsHtml);
            const contentState = ContentState.createFromBlockArray(processedHTML);

            editorState = EditorState.push(editorState, contentState, 'inital-load');
            editorState = EditorState.forceSelection(editorState, contentState.getSelectionAfter());
        }

        this.state = {
            editorState: editorState
        };
    }

Selection after seems to be ignored. I've also tried forcing this in componentDidMount to no avail.

    componentDidMount() {
        this.refs.editor.focus();
        this.setState({
            editorState: EditorState.forceSelection(editorState, editorState.getCurrentContent().getSelectionAfter())
        });
    }

Am I misunderstanding the API or is this a missing feature?

@brookslyrette brookslyrette changed the title Setting focus to end? Unable to set focus to end of textarea Mar 22, 2016
@hellendag
Copy link

A couple notes:

Note that ContentState.createFromBlockArray will actually set the focus to the start of the content: https://github.com/facebook/draft-js/blob/master/src/model/immutable/ContentState.js#L119

Hopefully this resolves things -- if not, let me know. :)

@brookslyrette
Copy link
Contributor Author

Thanks @hellendag. Your suggestions worked perfectly.

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

No branches or pull requests

2 participants