This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathGeneric.jsx
90 lines (80 loc) · 2.63 KB
/
Generic.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames/bind';
import styles from '../common/Avatar.module.scss';
import { setColor } from '../common/AvatarUtils';
const cx = classNames.bind(styles);
const GENERIC_VARIANTS = {
SINGLE_USER: 'single-user',
SHARED_USER: 'shared-user',
PROVIDER: 'provider',
};
const propTypes = {
/**
* Specifies the alternative text for the image.
*/
alt: PropTypes.string.isRequired,
/**
* Sets the background color. Defaults to `auto`. Accepted color variants are theme specific.
* One of: `'auto'`, `'neutral'`, `'one'`, `'two'`, `'three'`, `'four'`, `'five'`, `'six'`, `'seven'`, `'eight'`, `'nine'`, `'ten'`.
*/
color: PropTypes.oneOf(['auto', 'neutral', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine', 'ten']),
/**
* Value used for the hash function when color is set to `auto`. If not provided, hash function utilizes alt.
*/
hashValue: PropTypes.string,
/**
* Whether to hide avatar from the accessibility tree.
*/
isAriaHidden: PropTypes.bool,
/**
* Overrides the default size.
*/
size: PropTypes.string,
/**
* Sets the Generic Avatar type to One of the following variants `single-user`, `shared-user`, or `provider`.
*/
variant: PropTypes.oneOf([GENERIC_VARIANTS.SINGLE_USER, GENERIC_VARIANTS.SHARED_USER, GENERIC_VARIANTS.PROVIDER]),
};
const defaultProps = {
color: 'auto',
hashValue: undefined,
isAriaHidden: false,
size: undefined,
variant: GENERIC_VARIANTS.SINGLE_USER,
};
const Generic = ({
alt,
color,
hashValue,
isAriaHidden,
size,
variant,
...customProps
}) => {
const colorVariant = setColor(alt, color, hashValue);
const attributes = { ...customProps };
const customStyles = size ? ({ fontSize: size, ...attributes.style }) : attributes.style;
const GenericUserClassNames = cx([
'avatar',
`${colorVariant}`,
attributes.className,
]);
let genericIconClassNames = cx(['icon', 'user']);
if (variant === GENERIC_VARIANTS.SHARED_USER) {
genericIconClassNames = cx(['icon', GENERIC_VARIANTS.SHARED_USER]);
} else if (variant === GENERIC_VARIANTS.PROVIDER) {
genericIconClassNames = cx(['icon', GENERIC_VARIANTS.PROVIDER]);
}
/* eslint-disable react/forbid-dom-props */
return (
<div {...attributes} className={GenericUserClassNames} style={customStyles}>
<span className={genericIconClassNames} role="img" aria-label={alt} alt={alt} aria-hidden={isAriaHidden} />
</div>
);
/* eslint-enable react/forbid-dom-props */
};
Generic.propTypes = propTypes;
Generic.defaultProps = defaultProps;
export default Generic;