123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- // @flow
-
- import React from 'react';
- import { Image, Text, View } from 'react-native';
-
- import { Icon } from '../../../icons';
- import { type StyleType } from '../../../styles';
- import AbstractStatelessAvatar, { type Props as AbstractProps } from '../AbstractStatelessAvatar';
-
- import styles from './styles';
-
- const DEFAULT_AVATAR = require('../../../../../../images/avatar.png');
-
- type Props = AbstractProps & {
-
- /**
- * One of the expected status strings (e.g. 'available') to render a badge on the avatar, if necessary.
- */
- status?: ?string,
-
- /**
- * External style passed to the component.
- */
- style?: StyleType
- };
-
- /**
- * Implements a stateless avatar component that renders an avatar purely from what gets passed through
- * props.
- */
- export default class StatelessAvatar extends AbstractStatelessAvatar<Props> {
- /**
- * Implements {@code Component#render}.
- *
- * @inheritdoc
- */
- render() {
- const { initials, size, style, url } = this.props;
-
- let avatar;
-
- if (this._isIcon(url)) {
- avatar = this._renderIconAvatar(url);
- } else if (url) {
- avatar = this._renderURLAvatar();
- } else if (initials) {
- avatar = this._renderInitialsAvatar();
- } else {
- avatar = this._renderDefaultAvatar();
- }
-
- return (
- <View>
- <View
- style = { [
- styles.avatarContainer(size),
- style
- ] }>
- { avatar }
- </View>
- { this._renderAvatarStatus() }
- </View>
- );
- }
-
- _isIcon: (?string | ?Object) => boolean
-
- /**
- * Renders a badge representing the avatar status.
- *
- * @returns {React$Elementaa}
- */
- _renderAvatarStatus() {
- const { size, status } = this.props;
-
- if (!status) {
- return null;
- }
-
- return (
- <View style = { styles.badgeContainer }>
- <View style = { styles.badge(size, status) } />
- </View>
- );
- }
-
- /**
- * Renders the default avatar.
- *
- * @returns {React$Element<*>}
- */
- _renderDefaultAvatar() {
- const { size } = this.props;
-
- return (
- <Image
- source = { DEFAULT_AVATAR }
- style = { [
- styles.avatarContent(size),
- styles.staticAvatar
- ] } />
- );
- }
-
- /**
- * Renders the icon avatar.
- *
- * @param {Object} icon - The icon component to render.
- * @returns {React$Element<*>}
- */
- _renderIconAvatar(icon) {
- const { color, size } = this.props;
-
- return (
- <View
- style = { [
- styles.initialsContainer,
- {
- backgroundColor: color
- }
- ] }>
- <Icon
- src = { icon }
- style = { styles.initialsText(size) } />
- </View>
- );
- }
-
- /**
- * Renders the initials-based avatar.
- *
- * @returns {React$Element<*>}
- */
- _renderInitialsAvatar() {
- const { color, initials, size } = this.props;
-
- return (
- <View
- style = { [
- styles.initialsContainer,
- {
- backgroundColor: color
- }
- ] }>
- <Text style = { styles.initialsText(size) }> { initials } </Text>
- </View>
- );
- }
-
- /**
- * Renders the url-based avatar.
- *
- * @returns {React$Element<*>}
- */
- _renderURLAvatar() {
- const { onAvatarLoadError, size, url } = this.props;
-
- return (
- <Image
- defaultSource = { DEFAULT_AVATAR }
- onError = { onAvatarLoadError }
- resizeMode = 'cover'
- source = {{ uri: url }}
- style = { styles.avatarContent(size) } />
- );
- }
- }
|