Skip to content

Commit 0d95d7f

Browse files
IsDyh01wingkwong
andauthored
fix(avatar): add the safeInitials function to get the initials name (#5677)
* fix(Avatar): fix getInitials * chore: update test * chore(user): revise example --------- Co-authored-by: WK Wong <[email protected]>
1 parent 7b7190e commit 0d95d7f

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

.changeset/twelve-pianos-serve.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@heroui/shared-utils": patch
3+
"@heroui/avatar": patch
4+
---
5+
6+
fix getInitials (#5671)

packages/components/avatar/__tests__/avatar.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe("Avatar", () => {
3131
it("should render initials", () => {
3232
const {container} = render(<Avatar name="Junior" />);
3333

34-
expect(container.querySelector("span")).toHaveTextContent("Jun");
34+
expect(container.querySelector("span")).toHaveTextContent("J");
3535
});
3636

3737
it('should work with custom "getInitials" function', () => {
@@ -96,7 +96,7 @@ describe("Avatar - fallback + loading strategy", () => {
9696
test("should render a name avatar if no src", () => {
9797
const {container} = render(<Avatar name="Junior" />);
9898

99-
expect(container.querySelector("span")).toHaveTextContent("Jun");
99+
expect(container.querySelector("span")).toHaveTextContent("J");
100100
});
101101

102102
test("should render a default avatar if no name or src", () => {
@@ -118,7 +118,7 @@ describe("Avatar - fallback + loading strategy", () => {
118118
jest.runAllTimers();
119119
});
120120

121-
expect(container.querySelector("span")).toHaveTextContent("Jun");
121+
expect(container.querySelector("span")).toHaveTextContent("JG");
122122
});
123123

124124
test("should render default avatar fallback when image fails to load with no name and showFallback is true", async () => {
@@ -149,7 +149,7 @@ describe("Avatar - fallback + loading strategy", () => {
149149
jest.runAllTimers();
150150
});
151151

152-
expect(container.querySelector("span")).not.toHaveTextContent("Jun");
152+
expect(container.querySelector("span")).not.toHaveTextContent("JG");
153153
expect(container.querySelector("svg")).not.toBeInTheDocument();
154154
});
155155

@@ -166,7 +166,7 @@ describe("Avatar - fallback + loading strategy", () => {
166166
jest.runAllTimers();
167167
});
168168

169-
expect(container.querySelector("span")).not.toHaveTextContent("Jun");
169+
expect(container.querySelector("span")).not.toHaveTextContent("JG");
170170
expect(container.querySelector("svg")).not.toBeInTheDocument();
171171
});
172172
});

packages/components/avatar/src/use-avatar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {ReactRef} from "@heroui/react-utils";
55
import {avatar} from "@heroui/theme";
66
import {useProviderContext} from "@heroui/system";
77
import {useDOMRef, filterDOMProps} from "@heroui/react-utils";
8-
import {clsx, safeText, dataAttr, mergeProps} from "@heroui/shared-utils";
8+
import {clsx, dataAttr, mergeProps, safeInitials} from "@heroui/shared-utils";
99
import {useFocusRing} from "@react-aria/focus";
1010
import {useMemo, useCallback} from "react";
1111
import {useImage} from "@heroui/use-image";
@@ -117,7 +117,7 @@ export function useAvatar(originalProps: UseAvatarProps = {}) {
117117
isBordered = groupContext?.isBordered ?? false,
118118
isDisabled = groupContext?.isDisabled ?? false,
119119
isFocusable = false,
120-
getInitials = safeText,
120+
getInitials = safeInitials,
121121
ignoreFallback = false,
122122
showFallback: showFallbackProp = false,
123123
ImgComponent = "img",

packages/components/user/__tests__/user.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ describe("User", () => {
9494
<User
9595
avatarProps={{
9696
icon: <AvatarIcon />,
97-
name: "WK",
97+
name: "Marcus Wong",
9898
}}
9999
name="test"
100100
/>,
101101
);
102102

103-
expect(getByRole("img")).toHaveTextContent("WK");
103+
expect(getByRole("img")).toHaveTextContent("MW");
104104
});
105105
});

packages/utilities/shared-utils/src/common/text.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ export const safeText = (text: string): string => {
44
return text?.slice(0, 3);
55
};
66

7+
export const safeInitials = (text: string): string => {
8+
const initials =
9+
text
10+
?.trim()
11+
.split(/[\s\-_.]+/)
12+
.filter(Boolean)
13+
.map((word) => word.charAt(0).toUpperCase())
14+
.join("") || "";
15+
16+
return safeText(initials);
17+
};
18+
719
export const safeAriaLabel = (...texts: any[]): string => {
820
let ariaLabel = " ";
921

0 commit comments

Comments
 (0)