-
Notifications
You must be signed in to change notification settings - Fork 26
/
Counter.js
70 lines (60 loc) · 1.78 KB
/
Counter.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
61
62
63
64
65
66
67
68
69
70
import React, { Component } from 'react'
import Editor, { createEditorStateWithText } from 'draft-js-plugins-editor'
import createCounterPlugin from 'draft-js-counter-plugin'
import 'draft-js-counter-plugin/lib/plugin.css'
const counterPlugin = createCounterPlugin()
/*
import styles from './Counter.css'
const theme = {
counter: styles.counter,
counterOverLimit: styles.counterOverLimit
}
const counterPlugin = createCounterPlugin({ theme })
*/
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 CustomCounterEditor extends Component {
state = {
editorState: createEditorStateWithText(text)
}
onChange = (editorState) => {
this.setState({ editorState })
}
focus = () => {
this.editor.focus()
}
customCountFunction(str) {
const wordArray = str.match(/\S+/g)
return wordArray ? wordArray.length : 0
}
render() {
return (
<div>
<div className='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>
)
}
}