1
1
import { defineElement } from '@umbraco-ui/uui-base/lib/registration' ;
2
2
import { css , html , LitElement } from 'lit' ;
3
- import { property , state } from 'lit/decorators.js' ;
3
+ import { property } from 'lit/decorators.js' ;
4
4
5
5
/**
6
6
* Avatar for displaying users
@@ -43,19 +43,20 @@ export class UUIAvatarElement extends LitElement {
43
43
* @default ''
44
44
*/
45
45
@property ( { type : String , reflect : true } )
46
- get name ( ) {
47
- return this . _name ;
48
- }
49
- set name ( newVal ) {
50
- const oldValue = this . _name ;
51
- this . _name = newVal ;
52
- this . initials = this . createInitials ( this . _name ) ;
53
- this . requestUpdate ( 'title' , oldValue ) ;
54
- }
55
- private _name = '' ;
46
+ name = '' ;
47
+
48
+ /**
49
+ * Use this to override the initials generated from the name.
50
+ * @type { string }
51
+ * @attr
52
+ * @default undefined
53
+ */
54
+ @ property ( { type : String } )
55
+ initials ?: string ;
56
56
57
- @state ( )
58
- private initials = '' ;
57
+ private get _initials ( ) {
58
+ return this . initials ?. substring ( 0 , 3 ) || this . createInitials ( this . name ) ;
59
+ }
59
60
60
61
connectedCallback ( ) {
61
62
super . connectedCallback ( ) ;
@@ -77,10 +78,10 @@ export class UUIAvatarElement extends LitElement {
77
78
return initials ;
78
79
}
79
80
80
- initials = words [ 0 ] . substring ( 0 , 1 ) ;
81
+ initials = words [ 0 ] . charAt ( 0 ) ;
81
82
82
83
if ( words . length > 1 ) {
83
- initials += words [ words . length - 1 ] . substring ( 0 , 1 ) ;
84
+ initials += words [ words . length - 1 ] . charAt ( 0 ) ;
84
85
}
85
86
86
87
return initials . toUpperCase ( ) ;
@@ -90,14 +91,14 @@ export class UUIAvatarElement extends LitElement {
90
91
return html ` <img
91
92
src= "${ this . imgSrc } "
92
93
srcset = "${ this . imgSrcset } "
93
- alt = "${ this . initials } "
94
+ alt = "${ this . _initials } "
94
95
title = "${ this . name } " / > ` ;
95
96
}
96
97
97
98
render ( ) {
98
99
return html `
99
100
${ this . imgSrc ? this . renderImage ( ) : '' }
100
- ${ ! this . imgSrc ? this . initials : '' }
101
+ ${ ! this . imgSrc ? this . _initials : '' }
101
102
<slot> </ slot>
102
103
` ;
103
104
}
0 commit comments