here is a great way to make div contentEditable with html and onChange details. I used this with the emojione shortnameToImage function.
here is the working code
styles can be added to contentEditable details if you want them to come from state
the only thing that needs to be addressed is the carriage position after adding emoji.
class Application extends React.Component { state = { value: "Start Writing Here" }; render() { return ( <ContentEditable html={emojione.shortnameToImage(this.state.value)} onChange={this._onChange} /> ); } _onChange = e => { this.setState({ value: e.target.value }); }; } var ContentEditable = React.createClass({ render: function() { return ( <div contentEditable className="message-input" onInput={this.emitChange} onBlur={this.emitChange} dangerouslySetInnerHTML={{ __html: this.props.html }} /> ); }, shouldComponentUpdate: function(nextProps) { return nextProps.html !== this.getDOMNode().innerHTML; }, componentDidUpdate: function() { if (this.props.html !== this.getDOMNode().innerHTML) { this.getDOMNode().innerHTML = this.props.html; } }, emitChange: function() { var html = this.getDOMNode().innerHTML; if (this.props.onChange && html !== this.lastHtml) { this.props.onChange({ target: { value: html } }); } this.lastHtml = html; } }); React.render(<Application />, document.getElementById("app"));
html, body { height: 100% } .message-input { width: 100%; height: 100%; font-size: 30px; } .emojione { height: 30px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.min.js"></script> <div id="app"></div>
source share