This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
91 lines (86 loc) · 2.37 KB
/
index.js
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
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Plugins from 'plugins';
import { PluginType } from 'constants/common';
const propTypes = {
/** The Avatar visual name, should be provide via an AssetPlugin with prefix "avatar" */
name: PropTypes.string,
/** Avatar size variants */
size: PropTypes.oneOf([
'extraSmall',
'small',
'medium',
'large',
'extraLarge',
'extraLargePlus',
'huge',
]),
/** Providing a `src` will render an `<img>` element */
src: PropTypes.string,
/** Providing a `text` will render an `<div>` element */
text: PropTypes.string,
/** Providing a alt if `src` exits */
alt: PropTypes.string,
/** Set the width of the avatar */
width: PropTypes.number,
/** Set the height of the avatar */
height: PropTypes.number,
/**
* You can use a custom element type for this component.
* @default div
* */
as: PropTypes.elementType,
};
const defaultProps = {
size: 'medium',
alt: 'Avatar',
};
const Avatar = React.forwardRef(({ className, size, name, src, alt, height, width, text, as: Component = 'div', ...props }, ref) => {
let nameOri = name;
let srcOri = src;
let textOri = text;
if (srcOri) {
nameOri = false;
textOri = false;
} else if (textOri) {
nameOri = false;
} else if (nameOri) {
srcOri = Plugins
.getPlugins(PluginType.ASSET)
.traverseCall('getAsset', 'avatar', nameOri)
.find(asset => !!asset);
}
const heightStyle = {
width: width && width,
height: height && height,
};
const mergeProps = {
ref,
style: { ...props.style, ...heightStyle },
...props,
};
return (
<Component
{...mergeProps}
ref={ref}
className={classNames(
'Avatar u-positionRelative u-block u-paddingNone u-overflowHidden',
size && `Avatar--${size}`,
text && 'u-roundedCircle',
className && className
)}
>
{srcOri && (
<img src={srcOri} alt={alt} className="u-maxWidthFull u-roundedCircle u-positionAbsolute u-borderNone u-positionFull u-widthFull u-heightFull" />
)}
{textOri && (
<div className="u-lineHeightReset u-positionAbsolute u-positionCenter">{textOri}</div>
)}
</Component>
);
});
Avatar.displayName = 'Avatar';
Avatar.defaultProps = defaultProps;
Avatar.propTypes = propTypes;
export default Avatar;