Skip to content

Commit 6b6396f

Browse files
authored
Merge pull request #414 from GetStream/avatar-initials
feat: Add initialsType input to avatar
2 parents 18d71f8 + c6fb0bf commit 6b6396f

File tree

6 files changed

+42
-3
lines changed

6 files changed

+42
-3
lines changed

projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
let-channel="channel"
88
let-user="user"
99
let-location="location"
10+
let-initialsType="initialsType"
1011
>
1112
<stream-avatar
1213
[name]="name"
@@ -16,6 +17,7 @@
1617
[channel]="channel"
1718
[user]="user"
1819
[location]="location"
20+
[initialsType]="initialsType"
1921
></stream-avatar>
2022
</ng-template>
2123
<ng-container

projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export class AvatarPlaceholderComponent {
4545
* The type of the avatar: channel if channel avatar is displayed, user if user avatar is displayed
4646
*/
4747
@Input() type: AvatarType | undefined;
48+
/**
49+
* If channel/user image isn't provided the initials of the name of the channel/user is shown instead, you can choose how the initals should be computed
50+
*/
51+
@Input() initialsType:
52+
| 'first-letter-of-first-word'
53+
| 'first-letter-of-each-word' = 'first-letter-of-first-word';
4854
constructor(public customTemplatesService: CustomTemplatesService) {}
4955

5056
getAvatarContext(): AvatarContext {
@@ -56,6 +62,7 @@ export class AvatarPlaceholderComponent {
5662
type: this.type,
5763
user: this.user,
5864
channel: this.channel,
65+
initialsType: this.initialsType,
5966
};
6067
}
6168
}

projects/stream-chat-angular/src/lib/avatar/avatar.component.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
title="{{ name }}"
66
[style]="{
77
flexBasis: 'calc(var(--str-chat__spacing-px, 1px) * ' + size + ')',
8-
fontSize: 'calc(var(--str-chat__spacing-px, 1px) * ' + size / 2 + ')',
8+
fontSize:
9+
initialsType === 'first-letter-of-first-word'
10+
? 'calc(var(--str-chat__spacing-px, 1px) * ' + size / 2 + ')'
11+
: 'calc(var(--str-chat__spacing-px, 1px) * ' + size / 3 + ')',
912
height: 'calc(var(--str-chat__spacing-px, 1px) * ' + size + ')',
1013
lineHeight: 'calc(var(--str-chat__spacing-px, 1px) * ' + size + ')',
1114
width: 'calc(var(--str-chat__spacing-px, 1px) * ' + size + ')'
@@ -29,7 +32,11 @@
2932
}"
3033
/>
3134
<ng-template #fallback>
32-
<div data-testid="fallback-img" class="str-chat__avatar-fallback">
35+
<div
36+
data-testid="fallback-img"
37+
style="overflow: hidden; white-space: nowrap; text-overflow: ellipsis"
38+
class="str-chat__avatar-fallback"
39+
>
3340
{{ initials }}
3441
</div>
3542
</ng-template>

projects/stream-chat-angular/src/lib/avatar/avatar.component.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,13 @@ describe('AvatarComponent', () => {
370370

371371
expect(queryOnlineIndicator()).not.toBeNull();
372372
});
373+
374+
it('should display initials correctly, #initialsType is "first-letter-of-each-word"', () => {
375+
component.type = 'user';
376+
component.initialsType = 'first-letter-of-each-word';
377+
component.name = 'John Doe';
378+
fixture.detectChanges();
379+
380+
expect(component.initials).toBe('JD');
381+
});
373382
});

projects/stream-chat-angular/src/lib/avatar/avatar.component.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ export class AvatarComponent implements OnChanges {
5656
* If a channel avatar is displayed, and if the channel has exactly two members a green dot is displayed if the other member is online. Set this flag to `false` to turn off this behavior.
5757
*/
5858
@Input() showOnlineIndicator = true;
59+
/**
60+
* If channel/user image isn't provided the initials of the name of the channel/user is shown instead, you can choose how the initals should be computed
61+
*/
62+
@Input() initialsType:
63+
| 'first-letter-of-first-word'
64+
| 'first-letter-of-each-word' = 'first-letter-of-first-word';
5965
isLoaded = false;
6066
isError = false;
6167
isOnline = false;
@@ -118,7 +124,14 @@ export class AvatarComponent implements OnChanges {
118124
}
119125
}
120126

121-
return result.charAt(0) || '';
127+
const words = result.split(' ');
128+
let initials: string;
129+
if (this.initialsType === 'first-letter-of-each-word') {
130+
initials = words.map((w) => w.charAt(0) || '').join('');
131+
} else {
132+
initials = words[0].charAt(0) || '';
133+
}
134+
return initials;
122135
}
123136

124137
get fallbackChannelImage() {

projects/stream-chat-angular/src/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export type AvatarContext = {
190190
location: AvatarLocation | undefined;
191191
channel?: Channel<DefaultStreamChatGenerics>;
192192
user?: User<DefaultStreamChatGenerics>;
193+
initialsType?: 'first-letter-of-first-word' | 'first-letter-of-each-word';
193194
};
194195

195196
export type AttachmentPreviewListContext = {

0 commit comments

Comments
 (0)