Skip to content

Commit 52104c2

Browse files
feat(topbar): add optional initials for user avatar (#481)
1 parent 9ddcf54 commit 52104c2

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

components/topbar/src/profile-menu.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const ProfileMenu = ({
3131
language,
3232
name,
3333
email,
34+
initials,
3435
onSignOut,
3536
signOutLabel,
3637
tag,
@@ -50,6 +51,7 @@ export const ProfileMenu = ({
5051
<Avatar
5152
color="mink"
5253
name={name}
54+
initials={initials}
5355
size={24}
5456
badge={
5557
hasNotification
@@ -79,7 +81,12 @@ export const ProfileMenu = ({
7981
</MenuTrigger>
8082
<MenuPopover>
8183
<MenuList>
82-
<UserInformation name={name} email={email} tag={tag} />
84+
<UserInformation
85+
name={name}
86+
email={email}
87+
initials={initials}
88+
tag={tag}
89+
/>
8390
{(customContent === undefined || showCustomContentTopDivider) && (
8491
<MenuDivider />
8592
)}

components/topbar/src/profile-menu.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export type UserInformationProps = {
2727
readonly email: string;
2828
// Optional third tagline (for extra information)
2929
readonly tag?: string;
30+
// Optional custom initials for avatar
31+
readonly initials?: string;
3032
};
3133

3234
export type ProfileMenuProps = UserInformationProps & {

components/topbar/src/profile-user-information.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ import React from "react";
33
import { UserInformationProps } from "./profile-menu.types";
44
import { useUserInfoStyles } from "./profile-user-information.styles";
55

6-
export const UserInformation = ({ name, email, tag }: UserInformationProps) => {
6+
export const UserInformation = ({
7+
name,
8+
email,
9+
initials,
10+
tag,
11+
}: UserInformationProps) => {
712
const styles = useUserInfoStyles();
813

914
return (
1015
<div className={styles.root}>
1116
<Persona
17+
data-testid="profile-menu-persona"
18+
avatar={{ initials: initials }}
1219
name={name}
1320
secondaryText={email}
1421
size="large"

components/topbar/src/top-bar.spec.tsx

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { render } from "@testing-library/react";
2-
import React from "react";
2+
import React, { act } from "react";
33
import { TopBar } from "./top-bar";
44

55
describe("Topbar", () => {
@@ -65,4 +65,54 @@ describe("Topbar", () => {
6565
const appMenuButton = await findByTestId("application-drawer-trigger");
6666
expect(appMenuButton).toBeTruthy();
6767
});
68+
69+
it("should render standard initials", async () => {
70+
const { findByTestId } = render(
71+
<TopBar
72+
profileMenu={{
73+
name: "Super User",
74+
75+
language: { onChange: () => {}, value: "en" },
76+
theme: { onChange: () => {}, value: "dark" },
77+
onSignOut: () => {},
78+
}}
79+
/>
80+
);
81+
82+
const profileMenuButton = await findByTestId("profile-menu-button");
83+
expect(profileMenuButton).toHaveTextContent("SU");
84+
85+
act(() => {
86+
profileMenuButton.click();
87+
});
88+
const profileMenuPersona = await findByTestId("profile-menu-persona");
89+
expect(profileMenuPersona).toHaveTextContent(
90+
91+
);
92+
});
93+
94+
it("should render custom initials", async () => {
95+
const { findByTestId } = render(
96+
<TopBar
97+
profileMenu={{
98+
name: "Super User",
99+
100+
initials: "XY",
101+
language: { onChange: () => {}, value: "en" },
102+
theme: { onChange: () => {}, value: "dark" },
103+
onSignOut: () => {},
104+
}}
105+
/>
106+
);
107+
108+
const profileMenuButton = await findByTestId("profile-menu-button");
109+
expect(profileMenuButton).toHaveTextContent("XY");
110+
act(() => {
111+
profileMenuButton.click();
112+
});
113+
const profileMenuPersona = await findByTestId("profile-menu-persona");
114+
expect(profileMenuPersona).toHaveTextContent(
115+
116+
);
117+
});
68118
});

0 commit comments

Comments
 (0)