Skip to content
This repository was archived by the owner on Sep 29, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions playground/examples/AvatarExample.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const AvatarExample = () => (
<h1>Avatar</h1>
<div>
<h4>X-Small</h4>
<Avatar initials="JW" size="x-small" src="" />
<Avatar initials="EJF" name="Jon Wahlström" size="x-small" src="" />

<Avatar name="Jon Wahlström" size="x-small" src="" />
<Avatar name="Jon Wahlström" size="x-small" src="http://via.placeholder.com/400x400" />

Expand Down
32 changes: 17 additions & 15 deletions src/components/Avatar/Avatar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as styles from './Avatar.css';

const propTypes = {
className: PropTypes.string,
initials: PropTypes.string,
name: PropTypes.string,
size: PropTypes.oneOf([
'x-small',
Expand All @@ -19,6 +20,7 @@ const propTypes = {

const defaultProps = {
className: undefined,
initials: undefined,
name: undefined,
size: 'medium',
src: undefined,
Expand All @@ -29,7 +31,6 @@ class Avatar extends React.Component {
constructor(props) {
super(props);
this.handleError = this.handleError.bind(this);
this.getInitials = this.getInitials.bind(this);
this.state = { error: false };
}

Expand All @@ -38,22 +39,13 @@ class Avatar extends React.Component {
this.setState({ error: false });
}

getInitials() {
const { name } = this.props;
const parts = name.split(' ');
let result = '';
for (let i = 0; i < parts.length; i++) {
result += parts[i].substr(0, 1).toUpperCase();
}

return result.slice(0, 2);
}

handleError() {
this.setState({ error: true });
}

render() {
const { error } = this.state;
let { initials } = this.props;
const {
className,
name,
Expand All @@ -62,24 +54,34 @@ class Avatar extends React.Component {
...other
} = this.props;


const classes = classNames(
className,
styles.avatar,
styles[`avatar-${size}`],
);

if (this.state.error) {
if (name) {
const parts = name.split(' ');
let result = '';
for (let i = 0; i < parts.length; i++) {
result += parts[i].substr(0, 1).toUpperCase();
}

initials = result.slice(0, 2);
}

if (error) {
return (
<div className={classes}>
<span>{this.getInitials()}</span>
<span>{initials}</span>
</div>
);
}

return (
<Tag
{...other}
alt={name}
className={classes}
onError={this.handleError}
/>
Expand Down