-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathindex.js
80 lines (67 loc) · 2.26 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import debounce from 'lodash/debounce';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Quill from 'quill';
import { addField } from 'react-admin';
import FormHelperText from '@material-ui/core/FormHelperText';
import FormControl from '@material-ui/core/FormControl';
import { withStyles } from '@material-ui/core/styles';
import styles from './styles';
export class RichTextInput extends Component {
static propTypes = {
addLabel: PropTypes.bool.isRequired,
classes: PropTypes.object,
input: PropTypes.object,
label: PropTypes.string,
meta: PropTypes.object,
options: PropTypes.object,
source: PropTypes.string,
toolbar: PropTypes.oneOfType([PropTypes.array, PropTypes.bool]),
};
static defaultProps = {
addLabel: true,
options: {},
record: {},
toolbar: true,
};
componentDidMount() {
const {
input: { value },
toolbar,
} = this.props;
this.quill = new Quill(this.divRef, {
modules: { toolbar },
theme: 'snow',
});
this.quill.setContents(this.quill.clipboard.convert(value));
this.editor = this.divRef.querySelector('.ql-editor');
this.quill.on('text-change', debounce(this.onTextChange, 500));
}
componentWillUnmount() {
this.quill.off('text-change', this.onTextChange);
this.quill = null;
}
onTextChange = () => {
const value =
this.editor.innerHTML == '<p><br></p>' ? '' : this.editor.innerHTML;
this.props.input.onChange(value);
};
updateDivRef = ref => {
this.divRef = ref;
};
render() {
const { error, helperText = false } = this.props.meta;
return (
<FormControl error={error} className="ra-rich-text-input">
<div ref={this.updateDivRef} />
{error && <FormHelperText>{error}</FormHelperText>}
{helperText && <FormHelperText>{helperText}</FormHelperText>}
</FormControl>
);
}
}
const RichRextInputWithField = addField(withStyles(styles)(RichTextInput));
RichRextInputWithField.defaultProps = {
addLabel: true,
};
export default RichRextInputWithField;