forked from OrderMyGear/react-contenteditable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (31 loc) · 892 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React from 'react';
export default class ContentEditable extends React.Component {
constructor() {
super();
this.emitChange = this.emitChange.bind(this);
}
render() {
return <div
{...this.props}
onInput={this.emitChange}
onBlur={this.emitChange}
contentEditable="true"
dangerouslySetInnerHTML={{__html: this.props.html}}></div>;
}
shouldComponentUpdate(nextProps) {
return nextProps.html !== React.findDOMNode(this).innerHTML;
}
componentDidUpdate() {
if ( this.props.html !== React.findDOMNode(this).innerHTML ) {
React.findDOMNode(this).innerHTML = this.props.html;
}
}
emitChange(evt) {
var html = React.findDOMNode(this).innerHTML;
if (this.props.onChange && html !== this.lastHtml) {
evt.target = { value: html };
this.props.onChange(evt);
}
this.lastHtml = html;
}
}