Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RichTextEditor to be a forwardRef and expose setContent through useImperativeHandle #1173

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/RichTextEditor/RichTextEditor.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as ComponentStories from './RichTextEditor.stories';

<Canvas of={ComponentStories.Default} />

### When to use
### When to use
To allow users to edit text where formatting is important, such as messages to be sent via email.

### When to not use
Expand Down Expand Up @@ -45,3 +45,7 @@ RichTextEditor also has an error state to display that the input is invalid. The
but by setting `hasErrors` via your provided `onChange` callback you can check the HTML produced by the RichTextEditor for any requirements that you have.

<Canvas of={ComponentStories.Error} />

RichTextEditor can optionally take in a forwardedRef to allow content to be programmatically manipulated.

<Canvas of={ComponentStories.SetContent} />
22 changes: 21 additions & 1 deletion src/RichTextEditor/RichTextEditor.stories.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import React, { useRef } from 'react';

import Button from 'src/Button';
import { RichTextEditor, RichTextEditorActions } from 'src/RichTextEditor';

import mdx from './RichTextEditor.mdx';
Expand Down Expand Up @@ -60,3 +61,22 @@ export const Error = () => (
onChange={() => null}
/>
);

export const SetContent = () => {
const ref = useRef(null);

const handleClick = () => {
ref.current.setContent('Oh hey');
};

return (
<>
<Button onClick={handleClick}>Set content to "Oh hey"</Button>
<RichTextEditor
id="text-editor"
ref={ref}
onChange={() => null}
/>
</>
);
};
48 changes: 31 additions & 17 deletions src/RichTextEditor/RichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Extension, Node as TipTapNode, Mark } from '@tiptap/core';

import './RichTextEditor.scss';

import React from 'react';
import React, { forwardRef, type ForwardedRef, useImperativeHandle } from 'react';

import classNames from 'classnames';

Expand Down Expand Up @@ -94,21 +94,28 @@ export type RichTextEditorProps = {
onChange: (arg0: string) => void;
}

function RichTextEditor({
allowedAttributes,
allowedTags,
ariaAttributes,
availableActions = RichTextEditorDefaultActionsArray,
characterLimit,
className,
hasErrors,
id,
initialValue,
isOneLine,
onChange,
placeholder,
customExtensions = [],
}: RichTextEditorProps) {
export type RichTextEditorRef = {
setContent: (content: string) => void;
}
Comment on lines +97 to +99
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call only exposing this particular function of the editor


const RichTextEditor = forwardRef((
{
allowedAttributes,
allowedTags,
ariaAttributes,
availableActions = RichTextEditorDefaultActionsArray,
characterLimit,
className,
hasErrors,
id,
initialValue,
isOneLine,
onChange,
placeholder,
customExtensions = [],
}: RichTextEditorProps,
ref: ForwardedRef<RichTextEditorRef> = null,
) => {
const oneLineExtension = isOneLine ? [OneLineLimit] : [];

const requiredExtensions = [
Expand Down Expand Up @@ -183,6 +190,13 @@ function RichTextEditor({
},
});

useImperativeHandle(ref, () => ({
setContent: (content: string) => {
editor?.commands.setContent(content);
onChange(content);
},
}));

return (
editor ? (
<div
Expand Down Expand Up @@ -221,7 +235,7 @@ function RichTextEditor({
</>
)
);
}
});

// eslint-disable-next-line import/no-default-export
export default RichTextEditor;
Loading