Skip to content

Commit 917588a

Browse files
authored
Add badge to horizontal card (#514)
* Add badge to horizontal card * Fix deployment * Add tests
1 parent 290202e commit 917588a

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

src/components/CardHorizontal/CardHorizontal.stories.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ export const Playground = {
1313
description: "A description very interesting that presumably relates to the card.",
1414
disabled: false,
1515
isSelected: false,
16+
badgeText: null,
17+
badgeIcon: null,
1618
},
1719
};

src/components/CardHorizontal/CardHorizontal.test.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ describe("CardHorizontal Component", () => {
1313
title,
1414
icon: "warning",
1515
description: "",
16+
badgeText: "",
1617
});
1718

1819
expect(screen.getByText(title)).toBeDefined();
20+
expect(screen.queryByTestId("horizontal-card-badge")).toBeNull();
1921
});
2022

2123
it("should render the description when provided", () => {
@@ -28,5 +30,29 @@ describe("CardHorizontal Component", () => {
2830

2931
expect(screen.getByText(description)).toBeDefined();
3032
});
33+
34+
it("should render the badge when provided", () => {
35+
const description = "This is the card description";
36+
renderCard({
37+
icon: "warning",
38+
title: "title",
39+
description,
40+
badgeText: "Badge",
41+
});
42+
43+
expect(screen.getByText("title")).toBeDefined();
44+
expect(screen.getByTestId("horizontal-card-badge")).toBeDefined();
45+
});
46+
it("should not render the badge when badgeText is provided and not title", () => {
47+
const description = "This is the card description";
48+
renderCard({
49+
icon: "warning",
50+
title: "",
51+
description,
52+
badgeText: "Badge",
53+
});
54+
55+
expect(screen.queryByTestId("horizontal-card-badge")).toBeNull();
56+
});
3157
});
3258
});

src/components/CardHorizontal/CardHorizontal.tsx

+51-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { HTMLAttributes, ReactNode } from "react";
22
import { styled } from "styled-components";
3-
import { Icon, IconName } from "@/components";
3+
import {
4+
Badge,
5+
BadgeState,
6+
Container,
7+
HorizontalDirection,
8+
Icon,
9+
IconName,
10+
} from "@/components";
411

512
type CardColor = "default" | "muted";
613

@@ -13,13 +20,13 @@ export interface CardHorizontalProps
1320
isSelected?: boolean;
1421
children?: ReactNode;
1522
color?: CardColor;
23+
badgeText?: string;
24+
badgeState?: BadgeState;
25+
badgeIcon?: IconName;
26+
badgeIconDir?: HorizontalDirection;
1627
}
1728

1829
const Header = styled.div`
19-
display: flex;
20-
flex-direction: column;
21-
align-items: start;
22-
width: 100%;
2330
max-width: 100%;
2431
gap: inherit;
2532
`;
@@ -136,6 +143,10 @@ export const CardHorizontal = ({
136143
isSelected,
137144
children,
138145
color = "default",
146+
badgeText,
147+
badgeState,
148+
badgeIcon,
149+
badgeIconDir,
139150
...props
140151
}: CardHorizontalProps) => {
141152
return (
@@ -153,7 +164,41 @@ export const CardHorizontal = ({
153164
/>
154165
)}
155166
<ContentWrapper>
156-
{title && <Header>{title}</Header>}
167+
{title && (
168+
<Header
169+
as={Container}
170+
isResponsive={false}
171+
gap="xs"
172+
justifyContent="space-between"
173+
fillWidth
174+
>
175+
<Container
176+
orientation="horizontal"
177+
gap="xs"
178+
isResponsive={false}
179+
fillWidth={false}
180+
grow="1"
181+
>
182+
{title}
183+
</Container>
184+
{badgeText && (
185+
<Container
186+
isResponsive={false}
187+
justifyContent="end"
188+
fillWidth={false}
189+
data-testid="horizontal-card-badge"
190+
>
191+
<Badge
192+
text={badgeText}
193+
size="md"
194+
state={badgeState}
195+
icon={badgeIcon}
196+
iconDir={badgeIconDir}
197+
/>
198+
</Container>
199+
)}
200+
</Header>
201+
)}
157202

158203
{description && <Description>{description}</Description>}
159204
{children && <Description>{children}</Description>}

0 commit comments

Comments
 (0)