Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | 55x 55x 55x 55x 55x 54x 54x 54x 53x 1x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 2x 2x 2x 2x | import React from 'react'; import { defaultProps } from './props/defaultProps'; import { propTypes } from './props/propTypes'; import { getInitial } from '../utils/getInitial'; import AvatarSize from '../Provider/AvatarSize'; import style from './Avatar.module.css'; /* 1. need to change name into firstName lastName 2. pattern support via context as well as props 3. prop high priority to arrange firstName lastName 4. team pass name as lastName and pass pattern via prop */ /* eslint css-modules/no-unused-class: [2, { markAsUsed: ['circle', 'square', 'small','medium','xmedium',"xsmall", "large", "xlarge", "primary", "secondary", "info","default"] }] */ export default class Avatar extends React.Component { constructor(props) { super(props); this.state = { isLoaded: false }; this.putInvalidImageURLJSON = this.putInvalidImageURLJSON.bind(this); this.putValidImageURLJSON = this.putValidImageURLJSON.bind(this); this.getInitialByFullName = this.getInitialByFullName.bind(this); } componentDidUpdate(prevProps, prevState) { let { src } = this.props; let { isLoaded } = this.state; if (prevProps.src !== src) { this.setState({ isLoaded: !isLoaded }); } } putInvalidImageURLJSON() { const { src, alternateSrc } = this.props; let { isLoaded } = this.state; let _validImgArr = [...Avatar.validImageURLs]; let _invalidImgArr = [...Avatar.invalidImageURLs]; // if (alternateSrc) { // _validImgArr.push(alternateSrc); // } const validIndex = _validImgArr.indexOf(src); if (validIndex !== -1) { _validImgArr.splice(validIndex, 1); } _invalidImgArr.push(src); Avatar.invalidImageURLs = _invalidImgArr; Avatar.validImageURLs = _validImgArr; this.setState({ isLoaded: !isLoaded }); } putValidImageURLJSON() { const { src } = this.props; let { isLoaded } = this.state; const validImgArr = Avatar.validImageURLs; const invalidImgArr = Avatar.invalidImageURLs; if (src && validImgArr.indexOf(src) === -1 && invalidImgArr.indexOf(src) === -1) { let _validImgArr = [...validImgArr]; _validImgArr.push(src); Avatar.validImageURLs = _validImgArr; this.setState({ isLoaded: !isLoaded }); } } /* this will cause error if user name already have some space need to move firstName lastName user preference pattern*/ getInitialByFullName(fullName = '') { fullName = (fullName || '').trim(); let nameList = fullName.split(' '); if (nameList.length === 1) { return getInitial('', nameList[0]); } return getInitial(nameList[0], nameList[1]); } // componentDidMount() { // const { src } = this.props; // const validImgArr = Avatar.validImageURLs; // if (validImgArr.indexOf(src) === -1) { // let _validImgArr = [...validImgArr]; // _validImgArr.push(src); // } // } render() { let { src, name, size, palette, onClick, title, shape, needTitle, dataId, initial, needBorder, borderOnActive, borderOnHover, textPalette, customClass, alternateSrc, isAnimate, dataSelectorId, customProps, needInnerBorder, needDefaultBorder } = this.props; let { AvatarProps = {} } = customProps; let textStyle = textPalette ? style[textPalette] : palette === 'secondary' ? style.white : style.black; let shapeStyle = shape === 'circle' ? 'circle' : `square_${size}`; initial = initial || this.getInitialByFullName(name); let isInvalidImageList = Avatar.invalidImageURLs.indexOf(src) !== -1; //let isValidImageList = Avatar.validImageURLs.indexOf(src) !== -1; let showAvatar = src && !isInvalidImageList; let showInitial = !showAvatar || (showAvatar && isInvalidImageList); const showAlternateAvatar = alternateSrc ? showInitial : false; let border = borderOnHover || onClick; let showDefaultBorder = src && !isInvalidImageList && needDefaultBorder; let borderStyle = (showDefaultBorder || !src || !showAvatar || showInitial) && needBorder ? `${style.border} ${borderOnActive ? style.borderOnActive : border ? style.borderOnHover : ''} ` : ''; borderStyle = showAlternateAvatar ? '' : borderStyle; return ( <div className={`${style.avatar} ${style[size]} ${AvatarSize(size)} ${style[shapeStyle]} ${ style[palette] } ${textStyle} ${borderStyle} ${customClass ? customClass : ''}`} data-title={needTitle ? (title ? title : name) : null} data-id={dataId} data-test-id={dataId} onClick={onClick} data-selector-id={dataSelectorId} {...AvatarProps} > {showInitial && !showAlternateAvatar && ( <span className={`${style.initial}`} data-id={`${dataId}_AvatarInitial`} data-test-id={`${dataId}_AvatarInitial`} data-selector-id={`${dataSelectorId}_AvatarInitial`} > {initial} </span> )} {(showAvatar || showAlternateAvatar) && ( <> <img className={`${style.image} ${isAnimate ? style.animate : ''} ${needInnerBorder ? style.innerBorder : ''}`} data-id={`${dataId}_AvatarImg`} data-test-id={`${dataId}_AvatarImg`} data-selector-id={`${dataSelectorId}_AvatarImg`} name={name} onError={this.putInvalidImageURLJSON} onLoad={this.putValidImageURLJSON} src={showAlternateAvatar ? alternateSrc : src} alt={name} /> {showDefaultBorder ? null : <span className={`${style.shadow} ${textStyle} `}></span>} </> )} </div> ); } } Avatar.defaultProps = defaultProps; Avatar.propTypes = propTypes; Avatar.invalidImageURLs = []; Avatar.validImageURLs = []; // if (__DOCS__) { // Avatar.docs = { // componentGroup: 'Avatar Group', // folderName: 'Style Guide' // }; // } |