我正在尝试在 redux-form 中使用 react-draft-wysiwyg 编辑器,一切正常,但唯一的问题是 initialValues 似乎不适用于编辑器字段。
这是我的 Form.js :
<form onSubmit={this.props.handleSubmit(this.onSubmit)} autoComplete="off">
<Field name="title" className="form-input" type="text" placeholder="Title" component={this.titleComponent} />
<Field name="description" className="desc" type="text" placeholder="What is your article about ?" component={this.titleComponent} />
<Field name="editor" type="text" component={EditorFieldComponent} />
<button className="form-button" type="submit" value="Submit">{this.props.button}</button>
</form>
这是 EditorFieldComponent.js:
const EditorFieldComponent = (props) => {
const { meta: {touched, error}, input } = props;
return(
<EditorComponent
onChange={input.onChange}
value={input.value}
touched={touched}
error={error}
input = {{...input}}
/>
);
}
这是 EditorComponent.js:
class EditorComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty()
};
this.props.onChange(
draftToHtml(convertToRaw(this.state.editorState.getCurrentContent()))
);
}
onEditorStateChange = (editorState) => {
const { onChange, value } = this.props;
const newValue = draftToHtml(convertToRaw(editorState.getCurrentContent()));
if (value !== newValue) {
onChange(newValue);
}
this.setState({
editorState
});
};
render(){
return(
<React.Fragment>
<Editor
editorState={this.state.editorState}
toolbarClassName="toolbarClassName"
wrapperClassName="wrapperClassName"
editorClassName="editorClassName"
editorStyle={{ height: "500px", border: "solid 0.1px rgba(0, 0, 0, 0.1)", padding: "10px"}}
onEditorStateChange={this.onEditorStateChange}
toolbar={{
options: ['inline', 'blockType', 'fontSize', 'fontFamily', 'list', 'textAlign', 'colorPicker', 'link', 'embedded'/*, 'emoji'*/, 'image', 'remove', 'history'],
inline: { inDropdown: true },
list: { inDropdown: true },
textAlign: { inDropdown: true },
link: { inDropdown: true },
history: { inDropdown: true }
}}
/>
{this.renderError()}
</React.Fragment>
);}
这是我尝试使用initialValues的地方:
return(
<div>
<Form
initialValues={{title: "some title", description: "some description", editor: "some text"}}
/>
</div>
);
另外我应该说 initialValues 适用于 'description' 和 'title' 字段,只有 'editor' 字段有这个问题。