我有一个表格,其中包括电子邮件发件人,主题和文本,其中使用丰富的文本编辑器的草稿。我可以验证其他字段,但是如何验证draftjs编辑器呢?当我点击编辑器输入时,如果它是空的,它应该会显示错误,否则会像附件截图中那样成功。
以下是我的代码
function FieldGroup({ validationState, ...props }) {
console.log(props);
return (
<FormGroup validationState={validationState}>
<ControlLabel>{props.label}</ControlLabel>
<FormControl {...props} />
</FormGroup>
);
}
class EmailTemplate extends React.Component {
state = {
emailFrom: ''
};
handleChange = event =>
this.setState({ [event.target.name]: event.target.value });
render() {
const { templateName, emailSubject, emailFrom } = this.state;
return (
<form>
<FieldGroup
id="formControlsText"
name="emailFrom"
type="email"
label="Email From"
placeholder="Enter Email From"
onChange={this.handleChange}
validationState={emailFrom ? 'success' : 'error'}
required
/>
<AdminEditor />
<Button type="submit">
Submit
</Button>
</form>
);
}
}
export default EmailTemplate;
render() {
const { editorContent, contentState, editorState } = this.state;
return (
<div>
<Editor
editorState={this.state.editorState}
initialContentState={rawContentState}
wrapperClassName="home-wrapper"
editorClassName="home-editor"
onEditorStateChange={this.onEditorStateChange}
toolbar={{
history: { inDropdown: true },
inline: { inDropdown: false },
list: { inDropdown: true },
link: { showOpenOptionOnHover: true },
textAlign: { inDropdown: true },
image: { uploadCallback: this.imageUploadCallBack }
}}
onContentStateChange={this.onEditorChange}
placeholder="write text here..."
spellCheck
/>
</div>
);
}

发布于 2020-01-08 03:15:27
可以使用draft.js的onBlur和onFocus鼠标事件。
当用户离开编辑器并单击其他地方时,
onBlur处理程序将检查编辑器的内容。如果编辑器内容为空,则在state.onFocus句柄中将editorValidated设置为false将始终将editorValidated设置为true。因此,当用户再次开始键入时不会出现错误。在component中,您可以使用editorValidated通过Editor类添加自定义样式。
function FieldGroup({ validationState, ...props }) {
console.log(props);
return (
<FormGroup validationState={validationState}>
<ControlLabel>{props.label}</ControlLabel>
<FormControl {...props} />
</FormGroup>
);
}
class EmailTemplate extends React.Component {
state = {
emailFrom: '',
editorValidated:true,
};
handleChange = event =>
this.setState({ [event.target.name]: event.target.value });
render() {
const { templateName, emailSubject, emailFrom } = this.state;
return (
<form>
<FieldGroup
id="formControlsText"
name="emailFrom"
type="email"
label="Email From"
placeholder="Enter Email From"
onChange={this.handleChange}
validationState={emailFrom ? 'success' : 'error'}
required
/>
<AdminEditor />
<Button type="submit">
Submit
</Button>
</form>
);
}
}
export default EmailTemplate;
render() {
const { editorContent, contentState, editorState } = this.state;
return (
<div>
<Editor
editorState={this.state.editorState}
initialContentState={rawContentState}
wrapperClassName="home-wrapper"
editorClassName=`home-editor ${(this.state.editorValidated)?'validated','not-validated'}`
onEditorStateChange={this.onEditorStateChange}
toolbar={{
history: { inDropdown: true },
inline: { inDropdown: false },
list: { inDropdown: true },
link: { showOpenOptionOnHover: true },
textAlign: { inDropdown: true },
image: { uploadCallback: this.imageUploadCallBack }
}}
onFocus={()=>{this.setState({editorValidated:true})}}
onBlur={()=>{this.setState({editorValidated:(this.state.editorState.getCurrentContent().getPlainText()!='')})}}
onContentStateChange={this.onEditorChange}
placeholder="write text here..."
spellCheck
/>
</div>
);
}发布于 2020-12-15 15:26:08
您可以使用editorState.getCurrentContent().hasText()检查用户是否在编辑器中输入了任何文本,并相应地应用样式。
https://stackoverflow.com/questions/44151375
复制相似问题