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

Fix propTypes and React.CreateClass deprecation #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
82 changes: 42 additions & 40 deletions lib/components/TinyMCE.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { findDOMNode } from 'react-dom';
import isEqual from 'lodash/lang/isEqual';
import clone from 'lodash/lang/clone';
Expand Down Expand Up @@ -29,31 +30,32 @@ const HANDLER_NAMES = EVENTS.map((event) => {
return 'on' + ucFirst(event);
});

const TinyMCE = React.createClass({
displayName: 'TinyMCE',

propTypes: {
config: React.PropTypes.object,
content: React.PropTypes.string,
id: React.PropTypes.string,
className: React.PropTypes.string
},
class TinyMCE extends Component {
static defaultProps = {
config: {},
content: ''
};

static propTypes = {
config: PropTypes.object,
content: PropTypes.string,
id: PropTypes.string,
className: PropTypes.string
}

getDefaultProps() {
return {
config: {},
content: ''
};
},
constructor(props) {
super(props);
this.displayName = 'TinyMCE';
}

componentWillMount() {
this.id = this.id || this.props.id || uuid();
},
}

componentDidMount() {
const config = clone(this.props.config);
this._init(config);
},
}

componentWillReceiveProps(nextProps) {
if (!isEqual(this.props.config, nextProps.config)) {
Expand All @@ -62,34 +64,18 @@ const TinyMCE = React.createClass({
if (!isEqual(this.props.id, nextProps.id)) {
this.id = nextProps.id;
}
},
}

shouldComponentUpdate(nextProps) {
return (
!isEqual(this.props.content, nextProps.content) ||
!isEqual(this.props.config, nextProps.config)
);
},
}

componentWillUnmount() {
this._remove();
},

render() {
return this.props.config.inline ? (
<div
id={this.id}
className={this.props.className}
dangerouslySetInnerHTML={{__html: this.props.content}}
/>
) : (
<textarea
id={this.id}
className={this.props.className}
defaultValue={this.props.content}
/>
);
},
}

_init(config, content) {
if (this._isInit) {
Expand Down Expand Up @@ -129,17 +115,33 @@ const TinyMCE = React.createClass({
findDOMNode(this).style.hidden = '';

this._isInit = true;
},
}

_remove() {
tinymce.EditorManager.execCommand('mceRemoveEditor', true, this.id);
this._isInit = false;
}
});

render() {
return this.props.config.inline ? (
<div
id={this.id}
className={this.props.className}
dangerouslySetInnerHTML={{__html: this.props.content}}
/>
) : (
<textarea
id={this.id}
className={this.props.className}
defaultValue={this.props.content}
/>
);
}
}

// add handler propTypes
HANDLER_NAMES.forEach((name) => {
TinyMCE.propTypes[name] = React.PropTypes.func;
TinyMCE.propTypes[name] = PropTypes.func;
});

module.exports = TinyMCE;