import _ from 'lodash'; import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { PropsItemRow } from '../../interface'; import { numberToFixed } from '../../../utils/format'; import { ItemRowContainer, ItemRow, ExpandedContainer, ItemDisplayWithGraph, ItemDisplayWithCustomModal, ItemContainer, ItemGraphContainer, ModalContainer, } from './styledComponents'; import ActionButton from '../ActionButton/ActionButton'; import ItemDisplay from '../ItemDisplay/ItemDisplay'; import GraphButton from '../GraphButton/GraphButton'; import CustomModalButtonDisplay from '../CustomModalButton/CustomModalButtonDisplay'; import ChildItemRowDisplay from '../ChildItemRowDisplay/ChildItemRowDisplay'; const ItemRowDisplay = (props: PropsItemRow): JSX.Element => { const { theme, headers, items, parentIndex, openRow, openGraph, openChildGraph, customModalIcon, openParentModal, openChildModal, } = props; const isExpandable = _.get(items, `parentItem.items.isExpandable`); return ( <> (isExpandable ? openRow() : null)} > {headers.map((header, index) => { let value: string | number = '-'; const isAction = header.propertyKey === 'action'; if (items.parentItem.items[header.propertyKey]) { value = header.isNumber ? numberToFixed( items.parentItem.items[header.propertyKey] ? items.parentItem.items[header.propertyKey] : '-', 2, '-', header.unit ) : items.parentItem.items[header.propertyKey]; } return ( {!isAction && !header.graphConfiguration && !header.customModalRender && ( )} {!isAction && !header.graphConfiguration && header.customModalRender && ( )} {!isAction && header.graphConfiguration && !header.customModalRender && ( )} {isAction && ( )} ); })} openRow()} open={items.parentItem.items.opened} > {items.parentItem.items.opened && items.childItems.map((childItem, index) => ( openChildGraph(parentIndex, index, 'graphOpened') } customModalIcon={customModalIcon} openChildModal={() => openChildModal(parentIndex, index, 'modalOpened') } /> ))} ); }; ItemRowDisplay.propTypes = { headers: PropTypes.arrayOf( PropTypes.shape({ name: PropTypes.string.isRequired, propertyKey: PropTypes.string.isRequired, isNumber: PropTypes.bool, unit: PropTypes.string, tooltipDisplay: PropTypes.string, large: PropTypes.bool, bold: PropTypes.bool, textTransform: PropTypes.string, textDecorationChild: PropTypes.string, hrefPrefix: PropTypes.string, hrefSuffix: PropTypes.string, render: PropTypes.func, graphConfiguration: PropTypes.shape({ title: PropTypes.string, subtitle: PropTypes.string, }), }) ).isRequired, items: PropTypes.shape({ parentItem: PropTypes.shape({ items: PropTypes.objectOf( PropTypes.oneOfType([ PropTypes.number, PropTypes.string, PropTypes.bool, PropTypes.func, ]) ), chart: PropTypes.arrayOf( PropTypes.shape({ x: PropTypes.string, y: PropTypes.number, }) ), }), childItems: PropTypes.arrayOf( PropTypes.shape({ items: PropTypes.objectOf( PropTypes.oneOfType([ PropTypes.number, PropTypes.string, PropTypes.bool, PropTypes.func, ]) ), chart: PropTypes.arrayOf( PropTypes.shape({ x: PropTypes.string.isRequired, y: PropTypes.number.isRequired, }) ), }) ), }).isRequired, parentIndex: PropTypes.number.isRequired, openRow: PropTypes.func.isRequired, openGraph: PropTypes.func.isRequired, openChildGraph: PropTypes.func.isRequired, }; ItemRowDisplay.defaultProps = {}; export default ItemRowDisplay;