-
Notifications
You must be signed in to change notification settings - Fork 1k
/
index.js
60 lines (52 loc) · 1.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import React, { Component } from 'react';
import Editor, { createEditorStateWithText } from 'draft-js-plugins-editor';
import createCounterPlugin from 'draft-js-counter-plugin';
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
`;
export default class SimpleCounterEditor extends Component {
state = {
editorState: createEditorStateWithText(text),
};
onChange = (editorState) => {
this.setState({ editorState });
};
focus = () => {
this.editor.focus();
};
customCountFunction(str) {
const wordArray = str.match(/\S+/g); // matches words according to whitespace
return wordArray ? wordArray.length : 0;
}
render() {
return (
<div>
<div className={editorStyles.editor} onClick={this.focus}>
<Editor
editorState={this.state.editorState}
onChange={this.onChange}
plugins={plugins}
ref={(element) => { this.editor = element; }}
/>
</div>
<div><CharCounter limit={200} /> characters</div>
<div><WordCounter limit={30} /> words</div>
<div><LineCounter limit={10} /> lines</div>
<div>
<CustomCounter limit={40} countFunction={this.customCountFunction} />
<span> words (custom function)</span>
</div>
<br />
<br />
</div>
);
}
}