1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
8 |
|
9 | var _react = require('react');
|
10 |
|
11 | var _react2 = _interopRequireDefault(_react);
|
12 |
|
13 | var _propTypes = require('prop-types');
|
14 |
|
15 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
16 |
|
17 | var _style = require('./style.scss');
|
18 |
|
19 | var _style2 = _interopRequireDefault(_style);
|
20 |
|
21 | var _bind = require('classnames/bind');
|
22 |
|
23 | var _bind2 = _interopRequireDefault(_bind);
|
24 |
|
25 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
26 |
|
27 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
28 |
|
29 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
30 |
|
31 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
32 |
|
33 | var Avatar = function (_React$Component) {
|
34 | _inherits(Avatar, _React$Component);
|
35 |
|
36 | function Avatar(props) {
|
37 | _classCallCheck(this, Avatar);
|
38 |
|
39 | var _this = _possibleConstructorReturn(this, (Avatar.__proto__ || Object.getPrototypeOf(Avatar)).call(this, props));
|
40 |
|
41 | _this.state = {
|
42 |
|
43 |
|
44 | loaded: !_this.props.src
|
45 | };
|
46 | _this.state = {
|
47 | loaded: !!(!_this.props.fadeIn || !_this.props.src)
|
48 |
|
49 | |
50 |
|
51 | };
|
52 |
|
53 | _this.UNSAFE_componentWillReceiveProps = function (nextProps) {
|
54 | if (nextProps.src !== _this.props.src && _this.props.fadeIn) {
|
55 | _this.setState({ loaded: false });
|
56 | }
|
57 | };
|
58 |
|
59 | _this.shouldComponentUpdate = function (nextProps, nextState) {
|
60 | return nextProps.letters !== _this.props.letters || nextProps.src !== _this.props.src || nextProps.size !== _this.props.size || nextProps.fadeIn !== _this.props.fadeIn || nextState.loaded !== _this.state.loaded;
|
61 | };
|
62 |
|
63 | _this.getWrapperStyle = function () {
|
64 | return {
|
65 | backgroundColor: _this.props.letterBackgroundColor || _this.getBackgroundColor(),
|
66 | width: _this.props.size + 'px',
|
67 | height: _this.props.size + 'px'
|
68 | };
|
69 | };
|
70 |
|
71 | _this.getBackgroundColor = function () {
|
72 |
|
73 | if (!_this.props.letters && _this.props.src) return 'transparent';
|
74 |
|
75 |
|
76 | if (!_this.props.letters) return '#F93943';
|
77 |
|
78 | switch (_this.props.letters.charAt(0).toLowerCase()) {
|
79 | case 'a':
|
80 | case 'k':
|
81 | case 'u':
|
82 | return '#F93943';
|
83 | case 'b':
|
84 | case 'l':
|
85 | case 'v':
|
86 | return '#796DE8';
|
87 | case 'c':
|
88 | case 'm':
|
89 | case 'w':
|
90 | return '#6E3FAF';
|
91 | case 'd':
|
92 | case 'n':
|
93 | case 'x':
|
94 | return '#28D397';
|
95 | case 'e':
|
96 | case 'o':
|
97 | case 'y':
|
98 | return '#ED7C5A';
|
99 | case 'f':
|
100 | case 'p':
|
101 | case 'z':
|
102 | return '#F93983';
|
103 | case 'g':
|
104 | case 'q':
|
105 | return '#F9B339';
|
106 | case 'h':
|
107 | case 'r':
|
108 | return '#6BE2F9';
|
109 | case 'i':
|
110 | case 's':
|
111 | return '#AAE667';
|
112 | case 'j':
|
113 | case 't':
|
114 | return '#ED7BE9';
|
115 | default:
|
116 | return '#F93943';
|
117 | }
|
118 | };
|
119 |
|
120 | _this.getTextStyle = function () {
|
121 | return {
|
122 | fontSize: +_this.props.size * 0.6 + 'px'
|
123 | };
|
124 | };
|
125 |
|
126 | _this.handleLoad = function () {
|
127 | _this.setState({ loaded: true });
|
128 | };
|
129 |
|
130 | _this.loadAvatar = function () {
|
131 | if (_this.props.src) {
|
132 | return _react2.default.createElement('img', { src: _this.props.src, onLoad: _this.handleLoad, alt: _this.props.alt, height: _this.props.size });
|
133 | } else if (_this.props.letters) {
|
134 | return _react2.default.createElement(
|
135 | 'div',
|
136 | { style: _this.getWrapperStyle() },
|
137 | _react2.default.createElement(
|
138 | 'span',
|
139 | { style: _this.getTextStyle() },
|
140 | _this.props.letters.length <= 2 ? _this.props.letters : _this.props.letters.substr(0, 2)
|
141 | )
|
142 | );
|
143 | }
|
144 | };
|
145 |
|
146 | return _this;
|
147 | }
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 |
|
154 | _createClass(Avatar, [{
|
155 | key: 'render',
|
156 | value: function render() {
|
157 | var cx = _bind2.default.bind(_style2.default);
|
158 | var avatarClasses = cx(_style2.default['avatar-wrapper'], this.state.loaded ? 'loaded' : null, this.props.optClass, this.props.className);
|
159 |
|
160 | return _react2.default.createElement(
|
161 | 'div',
|
162 | { className: avatarClasses, style: this.props.size ? this.getWrapperStyle() : null },
|
163 | this.loadAvatar()
|
164 | );
|
165 | }
|
166 | }]);
|
167 |
|
168 | return Avatar;
|
169 | }(_react2.default.Component);
|
170 |
|
171 | Avatar.propTypes = {
|
172 | |
173 |
|
174 |
|
175 | src: _propTypes2.default.string,
|
176 | |
177 |
|
178 |
|
179 | letters: _propTypes2.default.string,
|
180 | |
181 |
|
182 |
|
183 | letterBackgroundColor: _propTypes2.default.string,
|
184 | |
185 |
|
186 |
|
187 | alt: _propTypes2.default.string,
|
188 | |
189 |
|
190 |
|
191 | size: _propTypes2.default.string,
|
192 | |
193 |
|
194 |
|
195 | optClass: _propTypes2.default.string,
|
196 | |
197 |
|
198 |
|
199 | fadeIn: _propTypes2.default.bool
|
200 | };
|
201 | Avatar.defaultProps = {
|
202 | fadeIn: true };
|
203 | exports.default = Avatar; |
\ | No newline at end of file |