我想在我的 React Quill 上添加一个字符计数器。目前我有一个 950 的字符限制器。问题是用户必须知道字符数不能超过 950,因此我尝试getLength()
在渲染上添加字符计数器但给了我一个错误。这是我的代码:
attachQuillRefs = () => {
if (typeof this.reactQuillRef.getEditor !== "function") return;
this.quillRef = this.reactQuillRef.getEditor();
};
//Handles changes to description field
handleDetailChange(value) {
var limit = 950;
var quill = this.quillRef;
quill.on("text-change", function (delta, old, source) {
if (quill.getLength() > limit) {
quill.deleteText(limit, quill.getLength());
}
});
this.setState({
detail: value,
});
}
在渲染:
<ReactQuill
ref={(el) => {
this.reactQuillRef = el;
}}
onChange={this.handleDetailChange}
value={this.state.detail || ""}
>
<p>{this.reactQuillRef.getLength() + "/950"}</p>
</ReactQuill>