import * as React from 'react'; import { Component, ComponentClass, ReactNode } from 'react'; import getPassThrough, { PassTroughFunction } from '../../utils/getPassThrough'; export interface WrapperNodeProps { className?: string; cover?: boolean; image?: string; } export interface ImgNodeProps { alt?: string; cover?: boolean; onError(): void; src: string; } export interface AvatarFactoryArgs { ImgNode: ComponentClass; WrapperNode: ComponentClass; passthrough: PassTroughFunction; } export interface AvatarProps { alt?: string; children?: ReactNode; className?: string; cover?: boolean; image?: string; title?: string; } export interface AvatarState { errored: boolean; } export default function avatarFactory({ ImgNode, WrapperNode, passthrough, }: AvatarFactoryArgs): ComponentClass { const passProps = getPassThrough(passthrough); return class Avatar extends Component { state = { errored: false, }; componentWillUpdate(nextProps) { if (this.props.image !== nextProps.image) { this.setState({ errored: false }); } } handleError = () => { this.setState({ errored: true }); }; render() { const { alt, children, className, cover, image, title, ...other, } = this.props; const { errored } = this.state; return ( {image && ( )} {(!image || errored) && title && getFirstLetter(title)} {children} ); } }; } function getFirstLetter(title) { return title ? title[0].toUpperCase() : ''; }