Created
September 14, 2015 09:37
-
-
Save ezmiller/698d1212eb8144b609be to your computer and use it in GitHub Desktop.
A React component for a content editable div
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ContentEditable = React.createClass({ | |
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; | |
}, | |
render: function() { | |
return <div | |
className={this.props.className} | |
id="contenteditable" | |
onKeyDown={this.handleKeyDown} | |
onChange={this.handleChange} | |
onInput={this.emitChange} | |
onBlur={this.emitChange} | |
contentEditable | |
dangerouslySetInnerHTML={{__html: this.props.html}}></div>; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment