1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
3 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
4 | import _inherits from 'babel-runtime/helpers/inherits';
|
5 |
|
6 | var _class, _temp2;
|
7 |
|
8 | import React, { Component } from 'react';
|
9 | import PropTypes from 'prop-types';
|
10 | import classNames from 'classnames';
|
11 | import ConfigProvider from '../config-provider';
|
12 | import Icon from '../icon';
|
13 | import { obj } from '../util';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var Avatar = (_temp2 = _class = function (_Component) {
|
19 | _inherits(Avatar, _Component);
|
20 |
|
21 | function Avatar() {
|
22 | var _temp, _this, _ret;
|
23 |
|
24 | _classCallCheck(this, Avatar);
|
25 |
|
26 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
27 | args[_key] = arguments[_key];
|
28 | }
|
29 |
|
30 | return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.state = {
|
31 | isImgExist: true
|
32 | }, _this.handleImgLoadError = function () {
|
33 | var onError = _this.props.onError;
|
34 |
|
35 | var errorFlag = onError ? onError() : undefined;
|
36 | if (errorFlag !== false) {
|
37 | _this.setState({ isImgExist: false });
|
38 | }
|
39 | }, _this.getIconSize = function (avatarSize) {
|
40 | return typeof avatarSize === 'number' ? avatarSize / 2 : avatarSize;
|
41 | }, _temp), _possibleConstructorReturn(_this, _ret);
|
42 | }
|
43 |
|
44 | Avatar.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
|
45 | if (prevProps.src !== this.props.src) {
|
46 |
|
47 | this.setState({ isImgExist: true });
|
48 | }
|
49 | };
|
50 |
|
51 | Avatar.prototype.render = function render() {
|
52 | var _classNames;
|
53 |
|
54 | var _props = this.props,
|
55 | prefix = _props.prefix,
|
56 | className = _props.className,
|
57 | style = _props.style,
|
58 | size = _props.size,
|
59 | icon = _props.icon,
|
60 | alt = _props.alt,
|
61 | srcSet = _props.srcSet,
|
62 | shape = _props.shape,
|
63 | src = _props.src;
|
64 | var isImgExist = this.state.isImgExist;
|
65 | var children = this.props.children;
|
66 |
|
67 |
|
68 | var others = obj.pickOthers(Avatar.propTypes, this.props);
|
69 |
|
70 | var cls = classNames((_classNames = {}, _classNames[prefix + 'avatar'] = true, _classNames[prefix + 'avatar-' + shape] = !!shape, _classNames[prefix + 'avatar-image'] = src && isImgExist, _classNames[prefix + 'avatar-' + size] = typeof size === 'string', _classNames), className);
|
71 |
|
72 | var sizeStyle = typeof size === 'number' ? {
|
73 | width: size,
|
74 | height: size,
|
75 | lineHeight: size + 'px',
|
76 | fontSize: icon ? size / 2 : 18
|
77 | } : {};
|
78 |
|
79 | var iconSize = this.getIconSize(size);
|
80 | if (src) {
|
81 | if (isImgExist) {
|
82 | children = React.createElement('img', { src: src, srcSet: srcSet, onError: this.handleImgLoadError, alt: alt });
|
83 | } else {
|
84 | children = React.createElement(Icon, { type: 'picture', size: iconSize });
|
85 | }
|
86 | } else if (typeof icon === 'string') {
|
87 | children = React.createElement(Icon, { type: icon, size: iconSize });
|
88 | } else if (icon) {
|
89 | var newIconSize = 'size' in icon.props ? icon.props.size : iconSize;
|
90 | children = React.cloneElement(icon, { size: newIconSize });
|
91 | }
|
92 |
|
93 | return React.createElement(
|
94 | 'span',
|
95 | _extends({ className: cls, style: _extends({}, sizeStyle, style) }, others),
|
96 | children
|
97 | );
|
98 | };
|
99 |
|
100 | return Avatar;
|
101 | }(Component), _class.propTypes = {
|
102 | prefix: PropTypes.string,
|
103 | children: PropTypes.any,
|
104 | className: PropTypes.string,
|
105 | |
106 |
|
107 |
|
108 |
|
109 | size: PropTypes.oneOfType([PropTypes.oneOf(['small', 'medium', 'large']), PropTypes.number]),
|
110 | |
111 |
|
112 |
|
113 |
|
114 | shape: PropTypes.oneOf(['circle', 'square']),
|
115 | |
116 |
|
117 |
|
118 | icon: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
|
119 | |
120 |
|
121 |
|
122 | src: PropTypes.string,
|
123 | |
124 |
|
125 |
|
126 | onError: PropTypes.func,
|
127 | |
128 |
|
129 |
|
130 | alt: PropTypes.string,
|
131 | |
132 |
|
133 |
|
134 | srcSet: PropTypes.string
|
135 | }, _class.defaultProps = {
|
136 | prefix: 'next-',
|
137 | size: 'medium',
|
138 | shape: 'circle'
|
139 | }, _temp2);
|
140 | Avatar.displayName = 'Avatar';
|
141 |
|
142 |
|
143 | export default ConfigProvider.config(Avatar); |
\ | No newline at end of file |