2022-01-09 16:22:04 +05:30
|
|
|
import React from 'react';
|
2021-07-28 18:45:52 +05:30
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
import './Avatar.scss';
|
|
|
|
|
|
|
|
|
|
import Text from '../text/Text';
|
|
|
|
|
import RawIcon from '../system-icons/RawIcon';
|
|
|
|
|
|
2022-01-16 10:41:37 +05:30
|
|
|
import ImageBrokenSVG from '../../../../public/res/svg/image-broken.svg';
|
2022-02-15 12:48:25 +01:00
|
|
|
import { avatarInitials } from '../../../util/common';
|
2022-01-16 10:41:37 +05:30
|
|
|
|
2024-07-08 16:57:10 +05:30
|
|
|
const Avatar = React.forwardRef(({ text, bgColor, iconSrc, iconColor, imageSrc, size }, ref) => {
|
2021-07-28 18:45:52 +05:30
|
|
|
let textSize = 's1';
|
|
|
|
|
if (size === 'large') textSize = 'h1';
|
|
|
|
|
if (size === 'small') textSize = 'b1';
|
|
|
|
|
if (size === 'extra-small') textSize = 'b3';
|
|
|
|
|
|
|
|
|
|
return (
|
2022-03-08 16:29:01 +05:30
|
|
|
<div ref={ref} className={`avatar-container avatar-container__${size} noselect`}>
|
2024-07-08 16:57:10 +05:30
|
|
|
{imageSrc !== null ? (
|
|
|
|
|
<img
|
|
|
|
|
draggable="false"
|
|
|
|
|
src={imageSrc}
|
|
|
|
|
onLoad={(e) => {
|
|
|
|
|
e.target.style.backgroundColor = 'transparent';
|
|
|
|
|
}}
|
|
|
|
|
onError={(e) => {
|
|
|
|
|
e.target.src = ImageBrokenSVG;
|
|
|
|
|
}}
|
|
|
|
|
alt=""
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<span
|
|
|
|
|
style={{ backgroundColor: iconSrc === null ? bgColor : 'transparent' }}
|
|
|
|
|
className={`avatar__border${iconSrc !== null ? '--active' : ''}`}
|
|
|
|
|
>
|
|
|
|
|
{iconSrc !== null ? (
|
|
|
|
|
<RawIcon size={size} src={iconSrc} color={iconColor} />
|
|
|
|
|
) : (
|
|
|
|
|
text !== null && (
|
|
|
|
|
<Text variant={textSize} primary>
|
|
|
|
|
{avatarInitials(text)}
|
|
|
|
|
</Text>
|
|
|
|
|
)
|
|
|
|
|
)}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
2021-07-28 18:45:52 +05:30
|
|
|
</div>
|
|
|
|
|
);
|
2022-03-08 16:29:01 +05:30
|
|
|
});
|
2021-07-28 18:45:52 +05:30
|
|
|
|
|
|
|
|
Avatar.defaultProps = {
|
|
|
|
|
text: null,
|
|
|
|
|
bgColor: 'transparent',
|
|
|
|
|
iconSrc: null,
|
2021-12-17 11:32:21 +05:30
|
|
|
iconColor: null,
|
2021-07-28 18:45:52 +05:30
|
|
|
imageSrc: null,
|
|
|
|
|
size: 'normal',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Avatar.propTypes = {
|
|
|
|
|
text: PropTypes.string,
|
|
|
|
|
bgColor: PropTypes.string,
|
|
|
|
|
iconSrc: PropTypes.string,
|
2021-12-17 11:32:21 +05:30
|
|
|
iconColor: PropTypes.string,
|
2021-07-28 18:45:52 +05:30
|
|
|
imageSrc: PropTypes.string,
|
|
|
|
|
size: PropTypes.oneOf(['large', 'normal', 'small', 'extra-small']),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Avatar;
|