我创建了一个textarea
带有使用 Tiny MCE 选项的 React 组件,我使用该组件实现了该react-tinymce
组件。
import React, { Component } from "react";
import TinyMCE from "react-tinymce";
class Textarea extends Component {
constructor(props) {
super(props);
this.state = {
data: {
body: ""
}
};
}
// ..a few methods not relevant to this question..
handleFieldChange(e) {
const data = { ...this.state.data };
data[e.target.name] = e.target.value;
this.setState({ data });
}
render() {
return (
<div>
{this.props.showLabel ? (
<label htmlFor={this.props.id}>{this.props.label}</label>
) : null}
{!this.props.mce ? (
<textarea
className={`form-control ${this.state.error ? "error" : ""}`}
id={this.props.id}
type={this.props.type}
name={this.props.name}
value={this.props.value}
placeholder={this.props.placeholder}
onChange={this.handleFieldChange}
/>
) : (
<TinyMCE
name={this.props.name}
content={this.props.value}
config={{
plugins: "autolink link image lists print preview code",
toolbar:
"undo redo | bold italic | alignleft aligncenter alignright | code",
height: 300,
branding: false,
statusbar: false,
menubar: false
}}
onChange={this.handleFieldChange}
/>
)}
</div>
);
}
}
export default Textarea;
我基本上会使用这样的组件:
<Textarea
name="body"
label="Body"
showLabel={true}
placeholder="body"
value={data.body}
mce={true}
/>
所以基本上如果mce
道具设置为true
你得到 TinyMCE 组件。
但是,虽然常规textarea
版本会绑定您输入的任何内容 state.data.body
,但 TinyMCE 版本不会。
// after typing into TinyMCE and submitting
console.log(this.state.data.body); // empty string
请注意,此Textarea
组件用作带有 onSubmit 方法的表单组件的一部分。