Draft-js: Unable to set focus to end of textarea

Created on 22 Mar 2016  路  2Comments  路  Source: facebook/draft-js

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?

question

Most helpful comment

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. :)

All 2 comments

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. :)

Thanks @hellendag. Your suggestions worked perfectly.

Was this page helpful?
0 / 5 - 0 ratings