Skip to content

Commit 0e75a47

Browse files
authored
Add Badge Ellipsis content boolean toggle (#531)
* Add Badge Ellipsis content boolean toggle * Update test
1 parent b28a918 commit 0e75a47

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/components/Badge/Badge.test.tsx

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@ import { Badge } from "./Badge";
22
import { renderCUI } from "@/utils/test-utils";
33

44
describe("Badge", () => {
5-
test("given a text, should render it", () => {
5+
test("given a text, should render ellipsed badge", () => {
66
const text = "text to render";
77
const rendered = renderCUI(<Badge text={text} />, "light");
88

99
expect(rendered.getByText(text).textContent).toEqual(text);
10+
expect(rendered.queryByTestId("ellipsed-badge-content")).not.toBeNull();
11+
expect(rendered.queryByTestId("ellipsed-icon-wrapper-text")).not.toBeNull();
12+
expect(rendered.queryByTestId("normal-badge-content")).toBeNull();
13+
expect(rendered.queryByTestId("normal-icon-wrapper-text")).toBeNull();
14+
});
15+
test("given a text, should render normal badge when ellipsisContent is false", () => {
16+
const text = "text to render";
17+
const rendered = renderCUI(
18+
<Badge
19+
text={text}
20+
ellipsisContent={false}
21+
/>,
22+
"light"
23+
);
24+
25+
expect(rendered.getByText(text).textContent).toEqual(text);
26+
expect(rendered.queryByTestId("ellipsed-badge-content")).toBeNull();
27+
expect(rendered.queryByTestId("ellipsed-icon-wrapper-text")).toBeNull();
28+
expect(rendered.queryByTestId("normal-badge-content")).not.toBeNull();
29+
expect(rendered.queryByTestId("normal-icon-wrapper-text")).not.toBeNull();
1030
});
1131
});

src/components/Badge/Badge.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface CommonBadgeProps extends HTMLAttributes<HTMLDivElement> {
2424
type?: BadgeType;
2525
icon?: ImageName;
2626
iconDir?: HorizontalDirection;
27+
ellipsisContent?: boolean;
2728
}
2829

2930
export interface DismissibleBadge extends CommonBadgeProps {
@@ -95,6 +96,7 @@ export const Badge = ({
9596
type,
9697
dismissible,
9798
onClose,
99+
ellipsisContent = true,
98100
...props
99101
}: BadgeProps) => (
100102
<Wrapper
@@ -103,13 +105,14 @@ export const Badge = ({
103105
$type={type}
104106
{...props}
105107
>
106-
<Content>
108+
<Content data-testid={`${ellipsisContent ? "ellipsed" : "normal"}-badge-content`}>
107109
<BadgeContent
108110
as={IconWrapper}
109111
icon={icon}
110112
iconDir={iconDir}
111113
size={size}
112114
$state={state}
115+
ellipsisContent={ellipsisContent}
113116
>
114117
{text}
115118
</BadgeContent>

src/components/IconWrapper/IconWrapper.tsx

+18-9
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,27 @@ const LabelContainer = styled.div<{ $hasIcon: boolean; $iconDir: HorizontalDirec
2121
gap: ${({ theme }) => theme.click.sidebar.navigation.item.default.space.gap};
2222
`;
2323

24+
interface IconWrapperProps {
25+
icon?: IconName;
26+
iconDir?: HorizontalDirection;
27+
size?: IconSize;
28+
width?: number | string;
29+
height?: number | string;
30+
children: ReactNode;
31+
ellipsisContent?: boolean;
32+
}
33+
2434
const IconWrapper = ({
2535
icon,
2636
iconDir = "start",
2737
size = "sm",
2838
width,
2939
height,
3040
children,
41+
ellipsisContent = true,
3142
...props
32-
}: {
33-
icon?: IconName;
34-
iconDir?: HorizontalDirection;
35-
children: ReactNode;
36-
size?: IconSize;
37-
width?: number | string;
38-
height?: number | string;
39-
}) => {
43+
}: IconWrapperProps) => {
44+
const TextWrapper = ellipsisContent ? EllipsisContent : "div";
4045
return (
4146
<LabelContainer
4247
$hasIcon={typeof icon === "string"}
@@ -51,7 +56,11 @@ const IconWrapper = ({
5156
height={height}
5257
/>
5358
)}
54-
<EllipsisContent>{children}</EllipsisContent>
59+
<TextWrapper
60+
data-testid={`${ellipsisContent ? "ellipsed" : "normal"}-icon-wrapper-text`}
61+
>
62+
{children}
63+
</TextWrapper>
5564
{icon && iconDir === "end" && (
5665
<Icon
5766
name={icon}

0 commit comments

Comments
 (0)