Skip to content

Commit

Permalink
Update RichTextEditor to be a forwardRef and expose setContent throug…
Browse files Browse the repository at this point in the history
…h useImperativeHandle (#1173)
  • Loading branch information
eric2523 authored Mar 27, 2024
1 parent d213c7c commit 97d7563
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
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;
}

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;

0 comments on commit 97d7563

Please sign in to comment.