I would like to add a character counter on my React Quill.
At the moment I have a character limiter of 950.
The problem is that the user must be aware that the number of characters must not be more than 950, thus the character counter
I've tried adding getLength()
on the render but gives me an error.
This is my code:
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,
});
}
at Render:
<ReactQuill
ref={(el) => {
this.reactQuillRef = el;
}}
onChange={this.handleDetailChange}
value={this.state.detail || ""}
>
<p>{this.reactQuillRef.getLength() + "/950"}</p>
</ReactQuill>