forked from terrestris/react-geo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserChip.tsx
158 lines (141 loc) · 3 KB
/
UserChip.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as React from 'react';
import {
Avatar,
Dropdown
} from 'antd';
import { AvatarProps } from 'antd/lib/avatar';
import './UserChip.less';
import { CSS_PREFIX } from '../constants';
// non default props
export interface BaseProps {
/**
* An optional CSS class which should be added.
*/
className?: string;
/**
* The image src.
*/
imageSrc?: string;
/**
* The react element representing the user menu
*/
userMenu?: React.ReactNode;
/**
* The user name.
*/
userName?: React.ReactNode;
/**
* The style object
*/
style?: any;
}
export type UserChipProps = BaseProps & AvatarProps;
/**
* Class representing the user chip containing an image of the user and his/her
* name
*
* @class The UserChip
* @extends React.Component
*/
class UserChip extends React.Component<UserChipProps> {
/**
* The className added to this component.
* @private
*/
_className: string = `${CSS_PREFIX}userchip`;
/**
* Create a UserChip.
* @constructs UserChip
*/
constructor(props: UserChipProps) {
super(props);
}
/**
* Determine initials for a given user name. The username will be splitted by
* a whitespace and the first character of each part (capital letter) is added
* to the initials.
* e.g. 'John Doe' leads to 'JD'
*
* @return Initials if the user name.
*
* @method getInitials
*/
getInitials(): string {
const {
userName
} = this.props;
if (!(userName instanceof String || typeof userName === 'string')) {
return '??';
}
const splittedName = (userName as string).split(' ');
const initals = [];
splittedName.forEach((part) => {
initals.push(part[0].toUpperCase());
});
return initals.join('');
}
/**
* getUserMenu - Description
*
* @return Description
*/
getUserMenu() {
const {
className,
imageSrc,
userMenu,
userName,
style,
...passThroughProps
} = this.props;
const finalClassName = className
? `${className} ${this._className}`
: this._className;
return (
<div
className={finalClassName}
style={style}
>
<Avatar
src={imageSrc}
size="large"
className="userimage"
{...passThroughProps}
>
{
imageSrc ? '' : this.getInitials()
}
</Avatar>
<span
className="username"
>
{userName}
</span>
</div>
);
}
/**
* The render function
*/
render() {
const {
userMenu
} = this.props;
if (userMenu && React.isValidElement(userMenu)) {
return (
<Dropdown
overlay={userMenu}
trigger={['click']}
getPopupContainer={() => {
return document.getElementsByClassName(this._className)[0] as HTMLElement;
}}
>
{this.getUserMenu()}
</Dropdown>
);
} else {
return this.getUserMenu();
}
}
}
export default UserChip;