import React from 'react'; import { Tooltip } from '@wordpress/components'; import './BreakdownList.css'; import BarList from './BarList'; export interface Item { title: string, value: number, [ k: string ]: any, } type ItemProps = Item & { total: number, showPercent: boolean, onSelect?(): void, }; const formatPercentage = ( value: number ) => ( value < 1 ? '<1' : Math.round( value ) ) + '%'; // eslint-disable-next-line @typescript-eslint/no-redeclare function Item( props: ItemProps ) { const percentage = props.value / props.total * 100; return (
  • { props.onSelect ? ( ) : ( { props.title } ) } { props.value } { props.showPercent && ( <> { ' ' } ({ formatPercentage( percentage ) }) ) }
  • ); } export interface Props { header?: { title: string, value: string | number, }, maxItems?: number, items: Item[], showPercent?: boolean, total?: number, onSelectItem?( item: Item ): void, } export default function BreakdownList( props: Props ) { const newItems = props.items.map( item => ( { _orig: item, name: item.title, value: item.value || 0, } ) ); return ( <> { props.header && (

    { props.header.title }

    ) } props.onSelectItem!( v._orig ) : undefined } /> ); }