Skip to content

Commit 1c441f9

Browse files
committed
Allow onChange to be string or void
1 parent f6d2ed3 commit 1c441f9

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

dist/components/form/components/FormRichText.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface QuillEditorProps {
55
style?: CSSProperties;
66
id?: string;
77
modules?: Record<string, unknown>;
8-
onChange?: (value: string) => string;
8+
onChange?: (value: string) => string | void;
99
value?: string;
1010
}
1111
declare const _default: React.ForwardRefExoticComponent<QuillEditorProps & React.RefAttributes<Quill>>;

dist/components/form/components/FormRichText.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

src/components/form/components/FormRichText.tsx

+29-28
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,40 @@ import React, {
33
useRef,
44
useImperativeHandle,
55
forwardRef,
6-
ForwardedRef, CSSProperties,
6+
ForwardedRef,
7+
CSSProperties,
78
} from 'react';
89
import Quill from 'quill';
910

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;
1818
}
1919

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+
) => {
2124
const editorRef = useRef<HTMLDivElement | null>(null);
2225
const quillRef = useRef<Quill | null>(null);
2326

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+
2440
useEffect(() => {
2541
if (editorRef.current) {
2642
const quill = new Quill(editorRef.current, modules);
@@ -38,26 +54,11 @@ const FormRichText: React.ForwardRefRenderFunction<Quill, QuillEditorProps> = ({
3854
configureListeners(quill);
3955
}
4056
}
41-
}, []);
57+
});
4258

4359
useImperativeHandle(ref, () => quillRef.current as Quill);
4460

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+
};
6163

6264
export default forwardRef(FormRichText);
63-

0 commit comments

Comments
 (0)