I'm using ReactJS to render text values into contenteditable
DOM nodes. Eg:
var data = [{
value: 'Hello '
},{
value: 'World!'
}];
var component = React.createClass({
render: function () {
var pieces = this.props.data.map(function (piece) {
return (
<span contentEditable="false">
<span contentEditable="true">{piece.value}</span>;
</span>
);
});
return <div contentEditable="true">{pieces}</div>;
}
});
React.renderComponent(<component data={data} />, someContainer);
Resulting in DOM that looks like this (for many reasons):
<div contenteditable="true">
<span contentEditable="false">
<span contentEditable="true">Hello </span>;
</span>
<span contentEditable="false">
<span contentEditable="true">World!</span>;
</span>
</div>
When a user interacts with these editable spans by, for example, backspacing from the second one into the first one, I can update the data by removing the last character from the first model, but I need to somehow tell React to position the cursor at the end of the first span[contenteditable="true"]
My Question:
In my .render()
function I have a this.state.caretPosition
property telling me the position the cursor should be (this only exists on ONE component). render()
is supposed to just return a (virtual) DOM Node. How do I tell ReactJS to position the caret inside the returned DOM Node at that position?
Example: jsbin