Skip to content

Commit

Permalink
Allowing cards to accept custom icons (#430)
Browse files Browse the repository at this point in the history
* Allowing cards to accept custom icons

* consistent naming
  • Loading branch information
gjones authored Jun 3, 2024
1 parent 446c3fb commit ccc7714
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ const App = () => {
<CardSecondary
title="Card title"
icon="building"
iconUrl="https://upload.wikimedia.org/wikipedia/sco/3/3c/Cardiff_City_crest.svg"
description="This is a card description"
badgeText="experiment"
infoText="Read More"
Expand All @@ -308,6 +309,7 @@ const App = () => {
<Container alignItems="start">
<CardPrimary
title="Card title"
size="sm"
icon="building"
description="This is a card description"
infoText="Read More"
Expand All @@ -316,6 +318,7 @@ const App = () => {
<CardPrimary
title="Card title"
icon="building"
iconUrl="https://upload.wikimedia.org/wikipedia/sco/3/3c/Cardiff_City_crest.svg"
description="This is a card description"
infoText="Read More"
infoUrl="#"
Expand Down
14 changes: 14 additions & 0 deletions src/components/CardPrimary/CardPrimary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,19 @@ describe("CardPrimary Component", () => {

expect(queryAllByTestId("card-top-badge").length).toEqual(0);
});

it("should render an image when iconUrl is provided", () => {
const iconUrl = "https://example.com/icon.png";
renderCard({
iconUrl,
title: "Card with custom icon",
description: "",
infoUrl: "",
infoText: "",
});

const imgElement = screen.getByAltText("card icon");
expect(imgElement).toHaveAttribute("src", iconUrl);
});
});
});
19 changes: 15 additions & 4 deletions src/components/CardPrimary/CardPrimary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface CardPrimaryProps
WithTopBadgeProps {
title?: string;
icon?: IconName;
iconUrl?: string;
hasShadow?: boolean;
disabled?: boolean;
description?: ReactNode;
Expand Down Expand Up @@ -109,7 +110,8 @@ const Header = styled.div<{
: theme.click.global.color.text.default};
}
svg {
svg,
img {
height: ${({ $size = "md", theme }) => theme.click.card.primary.size.icon[$size].all};
width: ${({ $size = "md", theme }) => theme.click.card.primary.size.icon[$size].all};
}
Expand All @@ -136,6 +138,7 @@ const Card = ({
alignContent,
title,
icon,
iconUrl,
hasShadow = false,
description,
infoUrl,
Expand Down Expand Up @@ -173,11 +176,19 @@ const Card = ({
$disabled={disabled}
$alignContent={alignContent}
>
{icon && (
<Icon
name={icon}
{iconUrl ? (
<img
src={iconUrl}
alt="card icon"
aria-hidden
/>
) : (
icon && (
<Icon
name={icon}
aria-hidden
/>
)
)}
{title && <Title type="h3">{title}</Title>}
</Header>
Expand Down
14 changes: 14 additions & 0 deletions src/components/CardSecondary/CardSecondary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ describe("CardSecondary Component", () => {

expect(screen.getAllByText(badgeText).length).toEqual(1);
});

it("should render an image when iconUrl is provided", () => {
const iconUrl = "https://example.com/icon.png";
renderCard({
iconUrl,
title: "Card with custom icon",
description: "",
infoUrl: "",
infoText: "",
});

const imgElement = screen.getByAltText("card icon");
expect(imgElement).toHaveAttribute("src", iconUrl);
});
});
29 changes: 23 additions & 6 deletions src/components/CardSecondary/CardSecondary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export type BadgeState =

export interface CardSecondaryProps extends HTMLAttributes<HTMLDivElement> {
title: string;
icon: IconName;
icon?: IconName;
iconUrl?: string;
badgeState?: BadgeState;
hasShadow?: boolean;
disabled?: boolean;
Expand Down Expand Up @@ -51,6 +52,11 @@ const Content = styled.div`
flex: 1;
`;

const CustomIcon = styled.img`
height: ${({ theme }) => theme.click.image.lg.size.height};
width: ${({ theme }) => theme.click.image.lg.size.width};
`;

const InfoLink = styled.a`
display: flex;
align-items: center;
Expand Down Expand Up @@ -112,6 +118,7 @@ const Wrapper = styled.div<{
export const CardSecondary = ({
title,
icon,
iconUrl,
badgeState,
badgeText = "",
hasShadow = false,
Expand All @@ -130,11 +137,21 @@ export const CardSecondary = ({
>
<Header>
<HeaderLeft $disabled={disabled}>
<Icon
name={icon}
size="lg"
area-hidden=""
/>
{iconUrl ? (
<CustomIcon
src={iconUrl}
alt="card icon"
aria-hidden
/>
) : (
icon && (
<Icon
name={icon}
aria-hidden
size="lg"
/>
)
)}
<Title type="h3">{title}</Title>
</HeaderLeft>
{badgeText && (
Expand Down

0 comments on commit ccc7714

Please sign in to comment.