Skip to content

Commit

Permalink
Allow onChange to be string or void
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie-n committed Jun 18, 2024
1 parent f6d2ed3 commit 1c441f9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dist/components/form/components/FormRichText.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface QuillEditorProps {
style?: CSSProperties;
id?: string;
modules?: Record<string, unknown>;
onChange?: (value: string) => string;
onChange?: (value: string) => string | void;
value?: string;
}
declare const _default: React.ForwardRefExoticComponent<QuillEditorProps & React.RefAttributes<Quill>>;
Expand Down
2 changes: 1 addition & 1 deletion dist/components/form/components/FormRichText.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 29 additions & 28 deletions src/components/form/components/FormRichText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,40 @@ import React, {
useRef,
useImperativeHandle,
forwardRef,
ForwardedRef, CSSProperties,
ForwardedRef,
CSSProperties,
} from 'react';
import Quill from 'quill';


export interface QuillEditorProps{
className?: string,
style?: CSSProperties,
id?: string,
modules?: Record<string, unknown>,
onChange?: (value : string) => string;
value?: string,
export interface QuillEditorProps {
className?: string;
style?: CSSProperties;
id?: string;
modules?: Record<string, unknown>;
onChange?: (value: string) => string | void;
value?: string;
}

const FormRichText: React.ForwardRefRenderFunction<Quill, QuillEditorProps> = ({modules, value, onChange, ...rest } :QuillEditorProps, ref: ForwardedRef<Quill>) => {
const FormRichText: React.ForwardRefRenderFunction<Quill, QuillEditorProps> = (
{ modules, value, onChange, ...rest }: QuillEditorProps,
ref: ForwardedRef<Quill>
) => {
const editorRef = useRef<HTMLDivElement | null>(null);
const quillRef = useRef<Quill | null>(null);

const setValue = (quillRef: Quill) => {
const delta = quillRef.clipboard.convert({ html: value });
quillRef.setContents(delta, 'silent');
};

const configureListeners = (quill: Quill) => {
quill.on('text-change', () => {
if (onChange) {
onChange(quillRef.current?.getSemanticHTML() || '');
}
});
};

useEffect(() => {
if (editorRef.current) {
const quill = new Quill(editorRef.current, modules);
Expand All @@ -38,26 +54,11 @@ const FormRichText: React.ForwardRefRenderFunction<Quill, QuillEditorProps> = ({
configureListeners(quill);
}
}
}, []);
});

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

const setValue = (quillRef: Quill) => {
const delta = quillRef.clipboard.convert({html: value})
quillRef.setContents(delta, 'silent')
}

const configureListeners = (quill: Quill) => {
quill.on('text-change', (e) => {
if (onChange) {
onChange(quillRef.current?.getSemanticHTML() || '');
}
});
}

return <div ref={editorRef} style={rest.style} id={rest.id}/>
}

return <div ref={editorRef} style={rest.style} id={rest.id} />;
};

export default forwardRef(FormRichText);

0 comments on commit 1c441f9

Please sign in to comment.