Skip to content

Commit

Permalink
feat(react): accept both array and object for content
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroschmitz committed May 12, 2021
1 parent cd92566 commit b9b2e23
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 85 deletions.
123 changes: 64 additions & 59 deletions example/content.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,66 @@
import { RichTextContent } from '@graphcms/rich-text-types';

export const content: RichTextContent = [
{
type: 'heading-two',
children: [{ text: 'Awesome new GraphCMS cap!' }],
},
{
type: 'paragraph',
children: [
{ text: 'Sweet black ' },
{ bold: true, text: 'cap' },
{ text: ' ' },
{ text: 'with', underline: true },
{ text: ' ' },
{ text: 'embroidered', italic: true },
{ text: ' ' },
{ bold: true, text: 'GraphCMS' },
{ text: ' logo.' },
],
},
{
type: 'bulleted-list',
children: [
{
type: 'list-item',
children: [
{
type: 'list-item-child',
children: [{ text: 'Embroided logo' }],
},
],
},
{
type: 'list-item',
children: [
{ type: 'list-item-child', children: [{ text: 'Fits well' }] },
],
},
{
type: 'list-item',
children: [
{
type: 'list-item-child',
children: [{ text: 'Comes in black' }],
},
],
},
{
type: 'list-item',
children: [
{
type: 'list-item-child',
children: [{ text: 'Reasonably priced' }],
},
],
},
],
},
{ type: 'paragraph', children: [{ text: '<Test />', code: true }] },
];
export const content: RichTextContent = {
children: [
{
type: 'heading-two',
children: [{ text: 'Awesome new GraphCMS cap!' }],
},
{
type: 'paragraph',
children: [
{ text: 'Sweet black ' },
{ bold: true, text: 'cap' },
{ text: ' ' },
{ text: 'with', underline: true },
{ text: ' ' },
{ text: 'embroidered', italic: true },
{ text: ' ' },
{ bold: true, text: 'GraphCMS' },
{ text: ' logo.' },
],
},
{
type: 'bulleted-list',
children: [
{
type: 'list-item',
children: [
{
type: 'list-item-child',
children: [{ text: 'Embroided logo' }],
},
],
},
{
type: 'list-item',
children: [
{
type: 'list-item-child',
children: [{ text: 'Fits well' }],
},
],
},
{
type: 'list-item',
children: [
{
type: 'list-item-child',
children: [{ text: 'Comes in black' }],
},
],
},
{
type: 'list-item',
children: [
{
type: 'list-item-child',
children: [{ text: 'Reasonably priced' }],
},
],
},
],
},
{ type: 'paragraph', children: [{ text: '<Test />', code: true }] },
],
};
28 changes: 13 additions & 15 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ To render the content on your application, you'll need to provide the array of e
```tsx
import { RichText } from '@graphcms/rich-text-react-renderer';

const content = [
{
type: 'paragraph',
children: [
{
bold: true,
text: 'Hello World!',
},
],
},
];
const content = {
children: [
{
type: 'paragraph',
children: [
{
bold: true,
text: 'Hello World!',
},
],
},
],
};

const App = () => {
return <RichText content={content} />;
Expand All @@ -53,10 +55,6 @@ By default, the elements won't have any styling, despite the `IFrame`, which we
```tsx
import { RichText } from '@graphcms/rich-text-react-renderer';

/**
* const content = [ ... ]
*/

const App = () => {
return (
<div>
Expand Down
14 changes: 12 additions & 2 deletions packages/react/src/RichText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,24 @@ export function RichText({ content, renderers: resolvers }: RichTextProps) {
};

if (__DEV__ && !content) {
console.error(`[@graphcms/rich-text-react-renderer]: Content is required.`);
console.error(`[@graphcms/rich-text-react-renderer]: content is required.`);

return <Fragment />;
}

if (__DEV__ && !Array.isArray(content) && !content.children) {
console.error(
`[@graphcms/rich-text-react-renderer]: children is required.`
);

return <Fragment />;
}

const elements = Array.isArray(content) ? content : content.children;

return (
<>
{content.map((node, index) => {
{elements.map((node, index) => {
return <RenderNode node={node} renderers={renderers} key={index} />;
})}
</>
Expand Down
44 changes: 36 additions & 8 deletions packages/react/test/RichText.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from './content';

describe('@graphcms/rich-text-react-renderer', () => {
it('renders content', async () => {
it('renders content', () => {
const { container } = render(<RichText content={content} />);

expect(container).toMatchInlineSnapshot(`
Expand All @@ -27,11 +27,39 @@ describe('@graphcms/rich-text-react-renderer', () => {
`);
});

it('renders content correctly if received a object with children', () => {
const contentObject: RichTextContent = {
children: [
{
type: 'paragraph',
children: [
{
bold: true,
text: 'Hello World!',
},
],
},
],
};

const { container } = render(<RichText content={contentObject} />);

expect(container).toMatchInlineSnapshot(`
<div>
<p>
<b>
Hello World!
</b>
</p>
</div>
`);
});

/**
* For now this is acceptable, but we should add a filter to remove all empty
* elements from the content, in the future.
*/
it('should not render empty text', async () => {
it('should not render empty text', () => {
const content: RichTextContent = [
{
type: 'paragraph',
Expand Down Expand Up @@ -108,7 +136,7 @@ describe('@graphcms/rich-text-react-renderer', () => {
expect(container).toMatchSnapshot();
});

it('renders link', async () => {
it('renders link', () => {
const linkContent: RichTextContent = [
{
type: 'link',
Expand Down Expand Up @@ -144,7 +172,7 @@ describe('@graphcms/rich-text-react-renderer', () => {
`);
});

it('renders iframe', async () => {
it('renders iframe', () => {
const iframeContent: RichTextContent = [
{
url: 'https://www.youtube.com/watch?v=Ylmd737tw5w',
Expand All @@ -162,7 +190,7 @@ describe('@graphcms/rich-text-react-renderer', () => {
expect(container).toMatchSnapshot();
});

it('renders class', async () => {
it('renders class', () => {
const { container } = render(<RichText content={iframeContent} />);

expect(container).toMatchInlineSnapshot(`
Expand All @@ -178,7 +206,7 @@ describe('@graphcms/rich-text-react-renderer', () => {
`);
});

it('renders class with custom renderer', async () => {
it('renders class with custom renderer', () => {
const { container } = render(
<RichText
content={iframeContent}
Expand All @@ -203,7 +231,7 @@ describe('@graphcms/rich-text-react-renderer', () => {
`);
});

it('renders image', async () => {
it('renders image', () => {
const { container } = render(<RichText content={imageContent} />);

expect(container).toMatchInlineSnapshot(`
Expand All @@ -220,7 +248,7 @@ describe('@graphcms/rich-text-react-renderer', () => {
`);
});

it('renders image with custom renderer', async () => {
it('renders image with custom renderer', () => {
const { container } = render(
<RichText
content={imageContent}
Expand Down
4 changes: 3 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ export type ElementNode =

export type Node = ElementNode | Text;

export type RichTextContent = Array<ElementNode>;
export type RichTextContent =
| Array<ElementNode>
| { children: Array<ElementNode> };

export type RichTextProps = {
content: RichTextContent;
Expand Down

0 comments on commit b9b2e23

Please sign in to comment.