Skip to content

Commit 20ca2d6

Browse files
committed
feat: change heading and subheading to accept ReactNode
1 parent aaee7ce commit 20ca2d6

5 files changed

Lines changed: 55 additions & 7 deletions

File tree

src/@next/Card/Card.stories.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Story, Meta } from '@storybook/react';
33

44
import { BaseContainer } from '../../Layout/GlintsContainer/GlintsContainer';
55
import { Card, CardProps } from './Card';
6+
import { Typography } from '../Typography';
7+
import { Red } from '../utilities/colors';
68

79
(Card as React.FunctionComponent<CardProps>).displayName = 'Card';
810

@@ -140,3 +142,25 @@ export const SecondaryActionOnly = SecondaryActionOnlyTemplate.bind({});
140142
SecondaryActionOnly.args = {
141143
heading: 'Heading',
142144
};
145+
146+
const CustomHeadingSubHeadingTemplate: Story<CardProps> = args => {
147+
const heading = (
148+
<Typography variant="subtitle1" color={Red.B74}>
149+
Custom Heading
150+
</Typography>
151+
);
152+
153+
const subheading = (
154+
<Typography variant="subtitle2" color={Red.B65}>
155+
Custom Sub Heading
156+
</Typography>
157+
);
158+
159+
return (
160+
<Card {...args} heading={heading} subheading={subheading}>
161+
<Card.Section>This is a section</Card.Section>
162+
</Card>
163+
);
164+
};
165+
166+
export const CustomHeadingSubHeading = CustomHeadingSubHeadingTemplate.bind({});

src/@next/Card/Card.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ import {
1010
StyledCardHeaderSection,
1111
StyledCardHeaderSectionHalf,
1212
StyledCardHeaderWrapper,
13+
StyledCustomHeader,
1314
} from './CardStyle';
1415
import { Section } from './Section';
1516

17+
const PRIMITIVE_TYPES = ['string', 'number'];
18+
1619
export type CardProps = {
17-
heading?: string;
18-
subheading?: string;
20+
heading?: React.ReactNode;
21+
subheading?: React.ReactNode;
1922
children?: React.ReactNode;
2023
primaryAction?: ComponentAction;
2124
secondaryAction?: ComponentAction;
@@ -33,16 +36,20 @@ const CardComponent = React.forwardRef<HTMLDivElement, CardProps>(function Card(
3336
}: CardProps,
3437
ref
3538
) {
36-
const headingMarkup = (
39+
const headingMarkup = PRIMITIVE_TYPES.includes(typeof heading) ? (
3740
<Typography as="div" variant="body2">
3841
{heading}
3942
</Typography>
43+
) : (
44+
<StyledCustomHeader>{heading}</StyledCustomHeader>
4045
);
4146

42-
const subHeadingMarkup = (
47+
const subHeadingMarkup = PRIMITIVE_TYPES.includes(typeof subheading) ? (
4348
<Typography as="div" variant="subtitle2">
4449
{subheading}
4550
</Typography>
51+
) : (
52+
<StyledCustomHeader>{subheading}</StyledCustomHeader>
4653
);
4754

4855
const headerMarkup = () => {

src/@next/Card/CardStyle.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ export const StyledCardActionWrapper = styled.div`
6363
padding: 10px ${space16} ${space12};
6464
}
6565
`;
66+
67+
export const StyledCustomHeader = styled.div`
68+
& > * {
69+
margin: 0;
70+
}
71+
`;

test/e2e/card/card.spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ test('Card', async ({ page }) => {
1212

1313
test('Card - without subheading', async ({ page }) => {
1414
const cardPage = getPage(page);
15-
await cardPage.goto('args=subheading:');
15+
await cardPage.goto('args=subheading:!null');
1616
await expect(cardPage.container).toHaveScreenshot('card-no-subheading.png');
1717
});
1818

1919
test('Card - without heading', async ({ page }) => {
2020
const cardPage = getPage(page);
21-
await cardPage.goto('args=heading:');
21+
await cardPage.goto('args=heading:!null');
2222
await expect(cardPage.container).toHaveScreenshot('card-no-heading.png');
2323
});
2424

2525
test('Card - without header', async ({ page }) => {
2626
const cardPage = getPage(page);
27-
await cardPage.goto('args=heading:;subheading:');
27+
await cardPage.goto('args=heading:!null;subheading:!null');
2828
await expect(cardPage.container).toHaveScreenshot('card-no-header.png');
2929
});
3030

@@ -63,3 +63,14 @@ test('Card - secondary action only', async ({ page }) => {
6363
'card-secondary-action-only.png'
6464
);
6565
});
66+
67+
test('Card - Custom Heading and Sub Heading', async ({ page }) => {
68+
const cardPage = new StoryBookPage(
69+
page,
70+
'?path=/story/next-card--custom-heading-sub-heading'
71+
);
72+
await cardPage.goto();
73+
await expect(cardPage.container).toHaveScreenshot(
74+
'card-custom-heading-sub-heading.png'
75+
);
76+
});
10.2 KB
Loading

0 commit comments

Comments
 (0)