Skip to content

Commit

Permalink
Update avatar-name.tsx (#7406)
Browse files Browse the repository at this point in the history
* Update avatar-name.tsx

When users name more than 2 words, initials do not get displayed as expected. For example, Anil Pak is displayed as AP, but also Anil Pak Chakra is displayed as AP instead of AC.

* Comments resolved.

* docs: add changelog

---------

Co-authored-by: Segun Adebayo <joseshegs@gmail.com>
  • Loading branch information
anLpk and segunadebayo authored Apr 24, 2023
1 parent 7c62a36 commit 417293f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/shy-pumas-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chakra-ui/avatar": patch
---

Fix issue where initials was not correctly computed when name includes a middle name like "Sage Naruto Manich"
3 changes: 2 additions & 1 deletion packages/components/avatar/src/avatar-name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { useAvatarStyles } from "./avatar-context"
import { AvatarOptions } from "./avatar-types"

export function initials(name: string) {
const [firstName, lastName] = name.split(" ")
const [firstName, ...otherNames] = name.split(" ");
const [lastName] = otherNames.reverse();
return firstName && lastName
? `${firstName.charAt(0)}${lastName.charAt(0)}`
: firstName.charAt(0)
Expand Down

1 comment on commit 417293f

@vercel
Copy link

@vercel vercel bot commented on 417293f Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.