|
| 1 | +/* eslint-disable complexity */ |
| 2 | +import * as React from 'react'; |
| 3 | + |
| 4 | +import { ButtonType } from '@/components/button'; |
| 5 | +import { ElementOrIcon } from '@/components/elementOrIcon'; |
| 6 | +import { Text, TextComponentType } from '@/components/text'; |
| 7 | + |
| 8 | +import { useContent } from '../hooks/useContent'; |
| 9 | +import { |
| 10 | + ListHeaderItemStylesStyled, |
| 11 | + ListItemExpandedStyled, |
| 12 | + ListItemHeaderPriorityStyled, |
| 13 | + ListRowContainerHeaderPriorityStyled, |
| 14 | + ListRowHeaderPriorityStyled, |
| 15 | + TableExpandedButton, |
| 16 | +} from '../table.styled'; |
| 17 | +import { IListComponent, ITableHeader, IValue } from '../types'; |
| 18 | + |
| 19 | +export const ListColumnHeaderPriority = (props: IListComponent): JSX.Element => { |
| 20 | + return ( |
| 21 | + <React.Fragment> |
| 22 | + {props.headers |
| 23 | + // By now you can't have a divider in a column header priority |
| 24 | + .filter(header => !header.config?.hasDivider) |
| 25 | + .map((header: ITableHeader, indexHeader: number) => { |
| 26 | + return ( |
| 27 | + <ListRowContainerHeaderPriorityStyled |
| 28 | + key={indexHeader} |
| 29 | + // By now you can't have a divider in a column header priority |
| 30 | + hasDivider={false} |
| 31 | + lineSeparatorLineStyles={props.lineSeparatorLineStyles} |
| 32 | + styles={props.styles} |
| 33 | + > |
| 34 | + {header.label && ( |
| 35 | + <ListHeaderItemStylesStyled |
| 36 | + customAlign={ |
| 37 | + header?.config?.alignHeader?.[props.device] || header?.config?.alignHeader |
| 38 | + } |
| 39 | + customBackgroundColor={header?.config?.backgroundColor} |
| 40 | + customWidth={header?.config?.width} |
| 41 | + flexWidth={header?.config?.flexWidth} |
| 42 | + hidden={props.hiddenHeaderOn?.[props.device]} |
| 43 | + lineSeparatorBottomOnHeader={props.lineSeparatorBottomOnHeader} |
| 44 | + lineSeparatorLineStyles={props.lineSeparatorLineStyles} |
| 45 | + lineSeparatorTopOnHeader={props.lineSeparatorTopOnHeader} |
| 46 | + styles={props.styles.header?.[props.headerVariant]} |
| 47 | + > |
| 48 | + <Text |
| 49 | + component={TextComponentType.SPAN} |
| 50 | + customTypography={props.styles.header?.[props.headerVariant]?.typography} |
| 51 | + dataTestId={`${props.dataTestId}ItemListRowHeader${header.label}`} |
| 52 | + > |
| 53 | + {header.label} |
| 54 | + </Text> |
| 55 | + </ListHeaderItemStylesStyled> |
| 56 | + )} |
| 57 | + <ListRowHeaderPriorityStyled styles={props.styles}> |
| 58 | + {/* If rowHeader, render it first */} |
| 59 | + {props.values.map((value, indexValue) => { |
| 60 | + return ( |
| 61 | + <ListColumnHeaderValue |
| 62 | + key={indexValue} |
| 63 | + {...props} |
| 64 | + hasExpandedIcon={indexValue === 0} |
| 65 | + header={header} |
| 66 | + indexHeader={indexHeader} |
| 67 | + value={value} |
| 68 | + /> |
| 69 | + ); |
| 70 | + })} |
| 71 | + </ListRowHeaderPriorityStyled> |
| 72 | + </ListRowContainerHeaderPriorityStyled> |
| 73 | + ); |
| 74 | + })} |
| 75 | + </React.Fragment> |
| 76 | + ); |
| 77 | +}; |
| 78 | + |
| 79 | +interface IListColumnHeaderValue extends IListComponent { |
| 80 | + hasExpandedIcon: boolean; |
| 81 | + header: ITableHeader; |
| 82 | + indexHeader: number; |
| 83 | + value: IValue; |
| 84 | +} |
| 85 | + |
| 86 | +const ListColumnHeaderValue = (props: IListColumnHeaderValue): JSX.Element => { |
| 87 | + const { |
| 88 | + rowHeader, |
| 89 | + getExpandedAria, |
| 90 | + getValue, |
| 91 | + getBackgroundColorCellValue, |
| 92 | + handleShowExpandedContent, |
| 93 | + hasExpandedContentRow, |
| 94 | + showExpandedContent, |
| 95 | + rowVariant, |
| 96 | + } = useContent({ ...props }); |
| 97 | + |
| 98 | + return ( |
| 99 | + <li> |
| 100 | + {rowHeader && ( |
| 101 | + <ListHeaderItemStylesStyled |
| 102 | + customAlign={ |
| 103 | + rowHeader?.config?.alignHeader?.[props.device] || rowHeader?.config?.alignHeader |
| 104 | + } |
| 105 | + customBackgroundColor={rowHeader?.config?.backgroundColor} |
| 106 | + customWidth={rowHeader?.config?.width} |
| 107 | + flexWidth={rowHeader?.config?.flexWidth} |
| 108 | + hidden={props.hiddenHeaderOn?.[props.device]} |
| 109 | + lineSeparatorBottomOnHeader={props.lineSeparatorBottomOnHeader} |
| 110 | + lineSeparatorLineStyles={props.lineSeparatorLineStyles} |
| 111 | + styles={props.styles.header?.[rowHeader.variant]} |
| 112 | + > |
| 113 | + <Text |
| 114 | + component={TextComponentType.SPAN} |
| 115 | + customTypography={props.styles.header?.[rowHeader.variant]?.typography} |
| 116 | + dataTestId={`${props.dataTestId}ItemListRowHeader${rowHeader.label}`} |
| 117 | + > |
| 118 | + {rowHeader.label} |
| 119 | + </Text> |
| 120 | + </ListHeaderItemStylesStyled> |
| 121 | + )} |
| 122 | + <ul> |
| 123 | + <ListItemHeaderPriorityStyled |
| 124 | + borderPosition={props.value.rowBorderPosition} |
| 125 | + customAlign={ |
| 126 | + props.header?.config?.alignValue?.[props.device] || |
| 127 | + props.header?.config?.alignValue || |
| 128 | + props.header?.config?.alignHeader?.[props.device] || |
| 129 | + props.header?.config?.alignHeader |
| 130 | + } |
| 131 | + customBackgroundColor={ |
| 132 | + getBackgroundColorCellValue(props.header) ?? props.value.backgroundColor |
| 133 | + } |
| 134 | + customWidth={props.header?.config?.width} |
| 135 | + flexWidth={props.header?.config?.flexWidth} |
| 136 | + hasSomeExpandedContent={props.hasSomeExpandedContent} |
| 137 | + lineSeparatorLineStyles={props.lineSeparatorLineStyles} |
| 138 | + styles={props.styles.bodyRows?.[rowVariant]} |
| 139 | + > |
| 140 | + {props.hasExpandedIcon && hasExpandedContentRow && ( |
| 141 | + <TableExpandedButton |
| 142 | + aria-label={getExpandedAria()} |
| 143 | + styles={props.styles.bodyRows?.[rowVariant]} |
| 144 | + type={ButtonType.BUTTON} |
| 145 | + onClick={() => { |
| 146 | + props.onExpandedContentOpen?.( |
| 147 | + !showExpandedContent, |
| 148 | + getValue(props.header), |
| 149 | + props.indexHeader |
| 150 | + ); |
| 151 | + handleShowExpandedContent(!showExpandedContent); |
| 152 | + }} |
| 153 | + > |
| 154 | + <ElementOrIcon |
| 155 | + customIconStyles={props.styles.bodyRows?.[rowVariant]?.accordionIcon} |
| 156 | + rotate={showExpandedContent ? '180deg' : '0deg'} |
| 157 | + {...props.accordionIcon} |
| 158 | + /> |
| 159 | + </TableExpandedButton> |
| 160 | + )} |
| 161 | + {getValue(props.header) as string | JSX.Element} |
| 162 | + {props.hasExpandedIcon && hasExpandedContentRow && ( |
| 163 | + <ListItemExpandedStyled |
| 164 | + showExpandedContent={showExpandedContent} |
| 165 | + styles={props.styles.bodyRows?.[rowVariant]} |
| 166 | + > |
| 167 | + {showExpandedContent && props.value.expandedContent |
| 168 | + ? props.value.expandedContent[props.device] |
| 169 | + : props.expandedContentHelpMessage} |
| 170 | + </ListItemExpandedStyled> |
| 171 | + )} |
| 172 | + </ListItemHeaderPriorityStyled> |
| 173 | + </ul> |
| 174 | + </li> |
| 175 | + ); |
| 176 | +}; |
0 commit comments