Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to manually reference HTML element #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions lib/react-contenteditable.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,42 @@ var ContentEditable = function (_React$Component) {
_inherits(ContentEditable, _React$Component);

function ContentEditable() {
var _ref;

var _temp, _this, _ret;

_classCallCheck(this, ContentEditable);

var _this = _possibleConstructorReturn(this, (ContentEditable.__proto__ || Object.getPrototypeOf(ContentEditable)).call(this));
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

_this.emitChange = _this.emitChange.bind(_this);
return _this;
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ContentEditable.__proto__ || Object.getPrototypeOf(ContentEditable)).call.apply(_ref, [this].concat(args))), _this), _this.emitChange = function (evt) {
if (!_this.htmlEl) return;
var html = _this.htmlEl.innerHTML;
if (_this.props.onChange && html !== _this.lastHtml) {
evt.target = { value: html };
_this.props.onChange(evt);
}
_this.lastHtml = html;
}, _this.refEl = function (el) {
if (!_this.htmlEl) {
_this.htmlEl = el;
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}

_createClass(ContentEditable, [{
key: 'render',
value: function render() {
var _this2 = this;

var _props = this.props,
tagName = _props.tagName,
html = _props.html,
props = _objectWithoutProperties(_props, ['tagName', 'html']);

return _react2.default.createElement(tagName || 'div', _extends({}, props, {
ref: function ref(e) {
return _this2.htmlEl = e;
},
ref: this.refEl
}, typeof tagName === 'function' ? { refEl: this.refEl } : {}, {
onInput: this.emitChange,
onBlur: this.props.onBlur || this.emitChange,
contentEditable: !this.props.disabled,
Expand Down Expand Up @@ -77,17 +91,6 @@ var ContentEditable = function (_React$Component) {
this.htmlEl.innerHTML = this.props.html;
}
}
}, {
key: 'emitChange',
value: function emitChange(evt) {
if (!this.htmlEl) return;
var html = this.htmlEl.innerHTML;
if (this.props.onChange && html !== this.lastHtml) {
evt.target = { value: html };
this.props.onChange(evt);
}
this.lastHtml = html;
}
}]);

return ContentEditable;
Expand Down
19 changes: 11 additions & 8 deletions src/react-contenteditable.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import React from 'react';

export default class ContentEditable extends React.Component {
constructor() {
super();
this.emitChange = this.emitChange.bind(this);
}

render() {
var { tagName, html, ...props } = this.props;

return React.createElement(
tagName || 'div',
{
...props,
ref: (e) => this.htmlEl = e,
ref: this.refEl,
...(typeof tagName === 'function' ? { refEl: this.refEl } : {}),
onInput: this.emitChange,
onBlur: this.props.onBlur || this.emitChange,
contentEditable: !this.props.disabled,
dangerouslySetInnerHTML: {__html: html}
},
this.props.children);
this.props.children
);
}

shouldComponentUpdate(nextProps) {
Expand All @@ -44,7 +41,7 @@ export default class ContentEditable extends React.Component {
}
}

emitChange(evt) {
emitChange = (evt) => {
if (!this.htmlEl) return;
var html = this.htmlEl.innerHTML;
if (this.props.onChange && html !== this.lastHtml) {
Expand All @@ -53,4 +50,10 @@ export default class ContentEditable extends React.Component {
}
this.lastHtml = html;
}

refEl = (el) => {
if (!this.htmlEl) {
this.htmlEl = el;
}
};
}