UNPKG

chowa

Version:

UI component library based on React

67 lines (66 loc) 2.24 kB
/** * @license chowa v1.1.3 * * Copyright (c) Chowa Techonlogies Co.,Ltd.(http://www.chowa.cn). * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const React = require("react"); const PropTypes = require("prop-types"); const classnames_1 = require("classnames"); const utils_1 = require("../utils"); const icon_1 = require("../icon"); const image_1 = require("../image"); class Avatar extends React.PureComponent { constructor(props) { super(props); this.state = { imgLoadFailure: false }; this.onImageLoadError = this.onImageLoadError.bind(this); } onImageLoadError() { this.setState({ imgLoadFailure: true }); } render() { const { className, text, theme, iconType, src, shape, style } = this.props; const { imgLoadFailure } = this.state; let avatarNode; const componentClass = classnames_1.default({ [utils_1.preClass('avatar')]: true, [utils_1.preClass(`avatar-${theme}`)]: true, [utils_1.preClass(`avatar-${shape}`)]: true, [className]: utils_1.isExist(className) }); if (utils_1.isExist(src) && !imgLoadFailure) { avatarNode = (React.createElement(image_1.default, { onError: this.onImageLoadError, src: src })); } else if (utils_1.isExist(text)) { avatarNode = text.length > 3 ? text.substring(0, 3).toLowerCase() : text; } else { avatarNode = (React.createElement(icon_1.default, { type: iconType })); } return (React.createElement("span", { style: style, className: componentClass }, avatarNode)); } } Avatar.propTypes = { className: PropTypes.string, style: PropTypes.object, theme: PropTypes.oneOf(['dark', 'light', 'primary']), text: PropTypes.string, shape: PropTypes.oneOf(['circle', 'rect']), iconType: PropTypes.string, src: PropTypes.string }; Avatar.defaultProps = { theme: 'dark', shape: 'circle', iconType: 'avatar' }; exports.default = Avatar;