Skip to content

Commit

Permalink
fix(react): empty space not being rendered (#3)
Browse files Browse the repository at this point in the history
* fix(react): empty space not being rendered

* chore: add changeset

* test: adjust test for render empty spaces
  • Loading branch information
jpedroschmitz authored May 13, 2021
1 parent 91c22ba commit c064507
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 28 deletions.
7 changes: 7 additions & 0 deletions .changeset/chilled-bears-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphcms/rich-text-react-renderer': patch
---

🐛 Bug fixes

- fix(react): empty space not being rendered
4 changes: 1 addition & 3 deletions packages/react/src/RenderText.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment, ReactNode } from 'react';
import React, { ReactNode } from 'react';
import { NodeRendererType, Text } from '@graphcms/rich-text-types';

import { elementKeys } from './defaultElements';
Expand All @@ -12,8 +12,6 @@ export function RenderText({
}) {
const { text, bold, italic, underline, code } = textNode;

if (!text.trim()) return <Fragment />;

const Bold = renderers?.[elementKeys['bold'] as keyof NodeRendererType];
const Italic = renderers?.[elementKeys['italic'] as keyof NodeRendererType];
const Underline =
Expand Down
48 changes: 23 additions & 25 deletions packages/react/test/RichText.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,6 @@ describe('@graphcms/rich-text-react-renderer', () => {
`);
});

/**
* 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', () => {
const content: RichTextContent = [
{
type: 'paragraph',
children: [
{
text: ' ',
},
],
},
];

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

expect(container).toMatchInlineSnapshot(`
<div>
<p />
</div>
`);
});

it('renders content with custom elements', () => {
const { container } = render(
<RichText
Expand Down Expand Up @@ -306,4 +281,27 @@ describe('@graphcms/rich-text-react-renderer', () => {

expect(container).toHaveTextContent('<Test />');
});

it('should render empty text spaces', () => {
const content: RichTextContent = [
{
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.' },
],
},
];

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

expect(container).toMatchSnapshot();
});
});
24 changes: 24 additions & 0 deletions packages/react/test/__snapshots__/RichText.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,27 @@ exports[`@graphcms/rich-text-react-renderer renders video 1`] = `
</video>
</div>
`;

exports[`@graphcms/rich-text-react-renderer should render empty text spaces 1`] = `
<div>
<p>
Sweet black
<b>
cap
</b>
<u>
with
</u>
<i>
embroidered
</i>
<b>
GraphCMS
</b>
logo.
</p>
</div>
`;

0 comments on commit c064507

Please sign in to comment.