如何检查空,编辑器中有空格? 当编辑器中有空格时,我不想提交表格。 我正在使用这个函数检测编辑器是否为空但它没有检测到编辑器是否有空格
contentState.hasText()
答案 0 :(得分:3)
您可以使用两种方法检查编辑器内容,您可以在当前内容状态上调用这两种方法。首先 - hasText和第二getPlainText
查看下面的演示。在这里,我们检查三种不同的编辑条件:
是编辑器为空(没有空格,没有字母,没有其他字符),
是编辑器,只包含空格(而不包含其他字符),
是编辑器包含一些文本(除空格之外的任何字符)
const {Editor, EditorState} = Draft;
class Container extends React.Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty()
};
}
_handleChange = (editorState) => {
this.setState({ editorState });
}
_checkEditorContent = () => {
const content = this.state.editorState.getCurrentContent();
const isEditorEmpty = !content.hasText();
const currentPlainText = content.getPlainText()
const lengthOfEditorContent = currentPlainText.length;
const lengthOfTrimmedContent = currentPlainText.trim().length;
const isContainOnlySpaces = !isEditorEmpty && !lengthOfTrimmedContent;
console.clear();
console.log('editor empty => ', isEditorEmpty)
console.log('editor containe only spaces => ', isContainOnlySpaces)
console.log('editor containe some text (not only spaces) => ', !!(!isEditorEmpty && lengthOfTrimmedContent))
}
render() {
return (
<div className="container-root">
<Editor
placeholder="Type away :)"
editorState={this.state.editorState}
onChange={this._handleChange}
/>
<button onClick={this._checkEditorContent}>
CHECK
</button>
</div>
);
}
}
ReactDOM.render(<Container />, document.getElementById('react-root'))
&#13;
body {
font-family: Helvetica, sans-serif;
}
.public-DraftEditor-content {
border: 1px solid black;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.7.0/Draft.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.10.0/Draft.js"></script>
<div id="react-root"></div>
&#13;
答案 1 :(得分:0)
您可以使用 String.trim() 删除所有跳转和空格,然后只需检查长度:
editorState.getCurrentContent().getPlainText().trim().length
或者如果您使用的是 EditorState:
df_train.index=pd.to_datetime(df_train.index,dayfirst=True)
future_dates=pd.date_range(df_train.index.max(), periods=12, freq='M')-pd.offsets.MonthBegin()