import React, { useState, useRef, ReactElement } from 'react'; import Editor, { createEditorStateWithText } from '@draft-js-plugins/editor'; import createCounterPlugin from '@draft-js-plugins/counter'; import editorStyles from './editorStyles.css'; const counterPlugin = createCounterPlugin(); const { CharCounter, WordCounter, LineCounter, CustomCounter } = counterPlugin; const plugins = [counterPlugin]; const text = `This editor has counters below! Try typing here and watch the numbers go up. 🙌 Note that the color changes when you pass one of the following limits: - 200 characters - 30 words - 10 lines `; const SimpleCounterEditor = (): ReactElement => { const [editorState, setEditorState] = useState( createEditorStateWithText(text) ); const editor = useRef(); const onChange = (value): void => { setEditorState(value); }; const focus = (): void => { editor.current.focus(); }; const customCountFunction = (str: string): number => { const wordArray = str.match(/\S+/g); // matches words according to whitespace return wordArray ? wordArray.length : 0; }; return (
{ editor.current = element; }} />
characters
words
lines
words (custom function)


); }; export default SimpleCounterEditor;