Skip to content

Commit

Permalink
feat: [TextIntroduction] Support ReactNode content in the `descriptio…
Browse files Browse the repository at this point in the history
…n` (#339)

Close #338 

On SBL we have some descriptions that include lists (Errors page:
download and upload links). I'm seeing console errors about nesting
`ul`s within paragraphs.
 
This PR basically renders `description` within a `<p>` tag if it's a
string, otherwise renders it unchanged allowing us to pass more
customized content.

<img width="670" alt="Screenshot 2024-05-02 at 10 29 42 AM"
src="https://github.com/cfpb/design-system-react/assets/2592907/9dc19fbb-e482-4c29-9594-dad207b0c631">
  • Loading branch information
meissadia committed May 2, 2024
1 parent 1d727a3 commit 3ea74cd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/components/Headings/Heading.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classnames from 'classnames';
import type { JSXElement } from '~/src/types/jsxElement';
import type { ReactNode } from 'react';

export type HeadingType =
| '1'
Expand All @@ -21,7 +21,7 @@ export const Heading = ({
children,
className,
...properties
}: HeadingProperties): JSXElement => {
}: HeadingProperties): ReactNode => {
let DynamicHeading: keyof JSX.IntrinsicElements;
const classes = [className];

Expand Down
3 changes: 2 additions & 1 deletion src/components/Paragraph/Paragraph.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import classnames from 'classnames';
import type { ReactNode } from 'react';

interface ParagraphProperties extends React.HTMLProps<HTMLParagraphElement> {
isLead?: boolean;
Expand All @@ -14,7 +15,7 @@ export function Paragraph({
isLead,
className,
...properties
}: ParagraphProperties): JSX.Element {
}: ParagraphProperties): ReactNode {
const cnames = [className];

if (isLead) cnames.push('lead-paragraph');
Expand Down
11 changes: 9 additions & 2 deletions src/components/TextIntroduction/TextIntroduction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import List from '../List/List';
import ListItem from '../List/ListItem';
import { Paragraph } from '../Paragraph/Paragraph';

const renderDescription = (description: ReactNode | string): ReactNode => {
if (!description) return null;
if (typeof description === 'string')
return <Paragraph>{description}</Paragraph>;
return description;
};

interface TextIntroductionProperties extends React.HTMLProps<HTMLDivElement> {
// Page title
heading: string;
Expand Down Expand Up @@ -45,7 +52,7 @@ export const TextIntroduction = ({
>
<Heading type='1'>{heading}</Heading>
<Paragraph isLead>{subheading}</Paragraph>
{description ? <p>{description}</p> : null}
{renderDescription(description)}
{call2action}
</div>
);
Expand Down Expand Up @@ -84,7 +91,7 @@ export const TextIntroductionHeading = TextIntroduction.Heading;

TextIntroduction.Description = ({
children
}: TextIntroductionSubProperties): JSX.Element => <p>{children}</p>;
}: TextIntroductionSubProperties): ReactNode => renderDescription(children);

export const TextIntroductionDescription = TextIntroduction.Description;

Expand Down
1 change: 1 addition & 0 deletions src/stories/Introduction.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ In your `package.json`:
```

<Heading type='2'>Components - Verified vs Draft</Heading>

This library is still in it's early stages of developement, with lots of changes expected in the coming months. You will notice two subsections in the navigation sidebar: `Components (Verified)` and `Components (Draft)`.

`Verified` components have been thoroughly reviewed for consistency with their [Design System] counterparts and can be considered unlikely to change in the near future.
Expand Down

0 comments on commit 3ea74cd

Please sign in to comment.