Skip to content

Commit

Permalink
Update RichText component to use forwarding & add theme prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie-n committed Jun 18, 2024
1 parent ec032b4 commit 6444526
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 27 deletions.
5 changes: 4 additions & 1 deletion dist/components/form/Form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ declare const Form: {
({ type, className, ...rest }: import("./components/FormCheck").FormCheckProps): React.JSX.Element;
Feedback: import("react-bootstrap/esm/helpers").BsPrefixRefForwardingComponent<"div", import("react-bootstrap/esm/Feedback").FeedbackProps>;
};
RichText: ({ modules, value, valueChange, ...rest }: import("./components/FormRichText").QuillEditorProps) => React.JSX.Element;
RichText: {
({ theme, modules, value, onChange, ...rest }: import("./components/FormRichText").QuillEditorProps): React.JSX.Element;
Feedback: import("react-bootstrap/esm/helpers").BsPrefixRefForwardingComponent<"div", import("react-bootstrap/esm/Feedback").FeedbackProps>;
};
DateTime: {
({ className, ...rest }: import("./components/FormDateTime").FormDateTimeProps): React.JSX.Element;
Feedback: import("react-bootstrap/esm/helpers").BsPrefixRefForwardingComponent<"div", import("react-bootstrap/esm/Feedback").FeedbackProps>;
Expand Down
2 changes: 1 addition & 1 deletion dist/components/form/Form.d.ts.map

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

8 changes: 6 additions & 2 deletions dist/components/form/components/FormRichText.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ export interface QuillEditorProps {
id?: string;
modules?: Record<string, unknown>;
value?: string;
valueChange?(value: string): any;
theme?: string;
onChange?(value: string): any;
}
declare const FormRichText: ({ modules, value, valueChange, ...rest }: QuillEditorProps) => React.JSX.Element;
declare const FormRichText: {
({ theme, modules, value, onChange, ...rest }: QuillEditorProps): React.JSX.Element;
Feedback: import("react-bootstrap/esm/helpers").BsPrefixRefForwardingComponent<"div", import("react-bootstrap/Feedback").FeedbackProps>;
};
export default FormRichText;
//# sourceMappingURL=FormRichText.d.ts.map
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.

22 changes: 18 additions & 4 deletions dist/components/form/components/FormRichText.js

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

2 changes: 1 addition & 1 deletion dist/components/form/components/FormRichText.js.map

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

8 changes: 4 additions & 4 deletions dist/index.es.js

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25532,24 +25532,25 @@ Quill.register({
}, true);

var FormRichText = function (_a) {
var modules = _a.modules, value = _a.value, valueChange = _a.valueChange, rest = __rest(_a, ["modules", "value", "valueChange"]);
var theme = _a.theme, modules = _a.modules, value = _a.value, onChange = _a.onChange, rest = __rest(_a, ["theme", "modules", "value", "onChange"]);
var editorRef = React.useRef(null);
var quillRef = React.useRef(null);
var quillOptions = __assign(__assign({}, modules), { theme: theme || 'snow' });
var setValue = function (quillRef) {
var delta = quillRef.clipboard.convert({ html: value });
quillRef.setContents(delta, 'silent');
};
var configureListeners = function (quill) {
quill.on('text-change', function () {
var _a;
if (valueChange) {
valueChange(((_a = quillRef.current) === null || _a === void 0 ? void 0 : _a.getSemanticHTML()) || '');
if (onChange) {
onChange(((_a = quillRef.current) === null || _a === void 0 ? void 0 : _a.getSemanticHTML()) || '');
}
});
};
React.useEffect(function () {
if (editorRef.current) {
var quill = new Quill(editorRef.current, modules);
var quill = new Quill(editorRef.current, quillOptions);
quillRef.current = quill; // Store the Quill instance in a ref
if (value) {
setValue(quill);
Expand All @@ -25559,6 +25560,7 @@ var FormRichText = function (_a) {
}, []);
return React.createElement("div", { ref: editorRef, style: rest.style, id: rest.id });
};
FormRichText.Feedback = Feedback$1;

var FormDateTime = function (_a) {
var className = _a.className, rest = __rest(_a, ["className"]);
Expand Down
8 changes: 4 additions & 4 deletions dist/index.min.js

Large diffs are not rendered by default.

21 changes: 16 additions & 5 deletions src/components/form/components/FormRichText.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
import React, { useRef, CSSProperties, useEffect } from 'react';
import Quill from 'quill';
import Feedback from 'react-bootstrap/Feedback';

export interface QuillEditorProps {
className?: string;
style?: CSSProperties;
id?: string;
modules?: Record<string, unknown>;
value?: string;
theme?: string;

valueChange?(value: string): any;
onChange?(value: string): any;
}

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

const quillOptions = {
...modules,
theme: theme || 'snow',
};

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

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

useEffect(() => {
if (editorRef.current) {
const quill = new Quill(editorRef.current, modules);
const quill = new Quill(editorRef.current, quillOptions);
quillRef.current = quill; // Store the Quill instance in a ref

if (value) {
Expand All @@ -47,4 +55,7 @@ const FormRichText = ({

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

FormRichText.Feedback = Feedback;

export default FormRichText;

0 comments on commit 6444526

Please sign in to comment.