UNPKG

6.77 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _propTypes = require('prop-types');
14
15var _propTypes2 = _interopRequireDefault(_propTypes);
16
17var _style = require('./style.scss');
18
19var _style2 = _interopRequireDefault(_style);
20
21var _bind = require('classnames/bind');
22
23var _bind2 = _interopRequireDefault(_bind);
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29function _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
31function _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
33var 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 // If image src was passed in, it is not yet loaded
43 // Else, if letters were passed in, set loaded to true
44 loaded: !_this.props.src
45 };
46 _this.state = {
47 loaded: !!(!_this.props.fadeIn || !_this.props.src)
48
49 /* If the source changes, and fadeIn
50 * is set, fade in the new avatar */
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 // If no letters passed in, but a src exists
73 if (!_this.props.letters && _this.props.src) return 'transparent';
74
75 // If no letters passed in, return a default color
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 /* If fadeIn is set to false,
150 * or if it's a letter-based avatar,
151 * set loaded to true, so it won't fade in */
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
171Avatar.propTypes = {
172 /**
173 * Optional source of the image to load.
174 */
175 src: _propTypes2.default.string,
176 /**
177 * Optional letters to display in lieu of an image.
178 */
179 letters: _propTypes2.default.string,
180 /**
181 * Optional background for the letters.
182 */
183 letterBackgroundColor: _propTypes2.default.string,
184 /**
185 * Optional alt text for the image
186 */
187 alt: _propTypes2.default.string,
188 /**
189 * Optional size to constrain the image (only supports square images)
190 */
191 size: _propTypes2.default.string,
192 /**
193 * Optional CSS class to pass to the badge.
194 */
195 optClass: _propTypes2.default.string,
196 /**
197 * Option to turn the opacity fade off (defaults to true)
198 */
199 fadeIn: _propTypes2.default.bool
200};
201Avatar.defaultProps = {
202 fadeIn: true };
203exports.default = Avatar;
\No newline at end of file