@@ -3,24 +3,40 @@ import React, {
3
3
useRef ,
4
4
useImperativeHandle ,
5
5
forwardRef ,
6
- ForwardedRef , CSSProperties ,
6
+ ForwardedRef ,
7
+ CSSProperties ,
7
8
} from 'react' ;
8
9
import Quill from 'quill' ;
9
10
10
-
11
- export interface QuillEditorProps {
12
- className ?: string ,
13
- style ?: CSSProperties ,
14
- id ?: string ,
15
- modules ?: Record < string , unknown > ,
16
- onChange ?: ( value : string ) => string ;
17
- value ?: string ,
11
+ export interface QuillEditorProps {
12
+ className ?: string ;
13
+ style ?: CSSProperties ;
14
+ id ?: string ;
15
+ modules ?: Record < string , unknown > ;
16
+ onChange ?: ( value : string ) => string | void ;
17
+ value ?: string ;
18
18
}
19
19
20
- const FormRichText : React . ForwardRefRenderFunction < Quill , QuillEditorProps > = ( { modules, value, onChange, ...rest } :QuillEditorProps , ref : ForwardedRef < Quill > ) => {
20
+ const FormRichText : React . ForwardRefRenderFunction < Quill , QuillEditorProps > = (
21
+ { modules, value, onChange, ...rest } : QuillEditorProps ,
22
+ ref : ForwardedRef < Quill >
23
+ ) => {
21
24
const editorRef = useRef < HTMLDivElement | null > ( null ) ;
22
25
const quillRef = useRef < Quill | null > ( null ) ;
23
26
27
+ const setValue = ( quillRef : Quill ) => {
28
+ const delta = quillRef . clipboard . convert ( { html : value } ) ;
29
+ quillRef . setContents ( delta , 'silent' ) ;
30
+ } ;
31
+
32
+ const configureListeners = ( quill : Quill ) => {
33
+ quill . on ( 'text-change' , ( ) => {
34
+ if ( onChange ) {
35
+ onChange ( quillRef . current ?. getSemanticHTML ( ) || '' ) ;
36
+ }
37
+ } ) ;
38
+ } ;
39
+
24
40
useEffect ( ( ) => {
25
41
if ( editorRef . current ) {
26
42
const quill = new Quill ( editorRef . current , modules ) ;
@@ -38,26 +54,11 @@ const FormRichText: React.ForwardRefRenderFunction<Quill, QuillEditorProps> = ({
38
54
configureListeners ( quill ) ;
39
55
}
40
56
}
41
- } , [ ] ) ;
57
+ } ) ;
42
58
43
59
useImperativeHandle ( ref , ( ) => quillRef . current as Quill ) ;
44
60
45
- const setValue = ( quillRef : Quill ) => {
46
- const delta = quillRef . clipboard . convert ( { html : value } )
47
- quillRef . setContents ( delta , 'silent' )
48
- }
49
-
50
- const configureListeners = ( quill : Quill ) => {
51
- quill . on ( 'text-change' , ( e ) => {
52
- if ( onChange ) {
53
- onChange ( quillRef . current ?. getSemanticHTML ( ) || '' ) ;
54
- }
55
- } ) ;
56
- }
57
-
58
- return < div ref = { editorRef } style = { rest . style } id = { rest . id } />
59
- }
60
-
61
+ return < div ref = { editorRef } style = { rest . style } id = { rest . id } /> ;
62
+ } ;
61
63
62
64
export default forwardRef ( FormRichText ) ;
63
-
0 commit comments