import * as React from 'react'; import Badge from '@uidu/badge'; import Lozenge from '@uidu/lozenge'; import Tooltip from '@uidu/tooltip'; import { ImageIcon } from '../../ImageIcon'; import { DetailViewModel, BadgeViewModel } from '../../ResolvedView'; import { Wrapper, WidgetWrapper, WidgetDetails, Title, Text } from './styled'; import { LozengeViewModel } from '../../../common'; export interface WidgetsProps { details?: DetailViewModel[]; } export default class Widgets extends React.Component { renderTitle(title: string): JSX.Element { return {title}:; } renderIcon(icon: string | React.ReactNode) { if (typeof icon === 'string') { return ; } else { return icon; } } renderBadge(badge: BadgeViewModel) { return ( ); } renderLozenge(lozenge: LozengeViewModel) { return ( {lozenge.text} ); } renderText(text: string) { return {text}; } renderWidgetDetails(attrs: React.ReactNode[], tooltip?: string) { if (tooltip) { return ( {attrs} ); } else { return {attrs}; } } renderWidget(key: any, detail: DetailViewModel) { const { title, text, icon, badge, lozenge, tooltip } = detail; const attrs: React.ReactNode[] = []; if (title) { attrs.push(this.renderTitle(title)); } if (icon) { attrs.push(this.renderIcon(icon)); } if (badge) { attrs.push(this.renderBadge(badge)); } if (lozenge) { attrs.push(this.renderLozenge(lozenge)); } if (text) { attrs.push(this.renderText(text)); } if (attrs.length === 0) { // tslint:disable-next-line console.warn( `Widgets: A widget doesn't contain any supported attributes: ${JSON.stringify( attrs, null, 2, )}`, ); return null; } return ( {this.renderWidgetDetails(attrs, tooltip)} ); } render() { const { details = [] } = this.props; if (details.length === 0) { return null; } return ( {details.map((detail, index) => this.renderWidget(index, detail))} ); } }