| 1 |
import { forwardRef, useContext, useEffect, useState } from 'react'; |
| 2 |
import { useOUIAProps, OUIAProps } from '@patternfly/react-core/dist/esm/helpers'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Table/table'; |
| 4 |
import inlineStyles from '@patternfly/react-styles/css/components/InlineEdit/inline-edit'; |
| 5 |
import { css } from '@patternfly/react-styles'; |
| 6 |
import { TableContext } from './Table'; |
| 7 |
|
| 8 |
export interface TrProps extends Omit<React.HTMLProps<HTMLTableRowElement>, 'onResize'>, OUIAProps { |
| 9 |
/** Content rendered inside the <tr> row */ |
| 10 |
children?: React.ReactNode; |
| 11 |
/** Additional classes added to the <tr> row */ |
| 12 |
className?: string; |
| 13 |
/** @hide Forwarded ref */ |
| 14 |
innerRef?: React.Ref<any>; |
| 15 |
/** Flag indicating the Tr is hidden */ |
| 16 |
isHidden?: boolean; |
| 17 |
/** Flag indicating whether an "expandable" Tr is expanded or not. Only applicable to a Tr within a Tbody. |
| 18 |
* To prevent column widths from responding automatically when expandable rows are toggled, the width prop must also be passed into either the th or td component |
| 19 |
*/ |
| 20 |
isExpanded?: boolean; |
| 21 |
/** Flag indicating that the "control row" Tr has an expandable sibling Tr that is expanded or not. Only applicable to |
| 22 |
* a Tr within a Tbody, and should have the same value as an expandable Tr's isExpanded prop. |
| 23 |
*/ |
| 24 |
isContentExpanded?: boolean; |
| 25 |
/** Only applicable to Tr within the Tbody: Whether the row is editable */ |
| 26 |
isEditable?: boolean; |
| 27 |
/** Flag which adds hover styles for the clickable table row */ |
| 28 |
isClickable?: boolean; |
| 29 |
/** Flag indicating the row is selected - adds selected styling */ |
| 30 |
isRowSelected?: boolean; |
| 31 |
/** Flag indicating the row is striped */ |
| 32 |
isStriped?: boolean; |
| 33 |
/** Flag indicating the row will act as a border. This is typically used for a table with a nested and sticky header. */ |
| 34 |
isBorderRow?: boolean; |
| 35 |
/** Flag indicating the row is controlling the expansion of another row. */ |
| 36 |
isControlRow?: boolean; |
| 37 |
/** An event handler for the row */ |
| 38 |
onRowClick?: (event?: React.KeyboardEvent | React.MouseEvent) => void; |
| 39 |
/** Flag indicating that the row is selectable */ |
| 40 |
isSelectable?: boolean; |
| 41 |
/** Flag indicating the spacing offset of the first cell should be reset */ |
| 42 |
resetOffset?: boolean; |
| 43 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 44 |
ouiaId?: number | string; |
| 45 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 46 |
ouiaSafe?: boolean; |
| 47 |
} |
| 48 |
|
| 49 |
const TrBase: React.FunctionComponent<TrProps> = ({ |
| 50 |
children, |
| 51 |
className, |
| 52 |
isExpanded, |
| 53 |
isContentExpanded, |
| 54 |
isEditable, |
| 55 |
isHidden = false, |
| 56 |
isClickable = false, |
| 57 |
isRowSelected = false, |
| 58 |
isStriped = false, |
| 59 |
isBorderRow = false, |
| 60 |
isControlRow = false, |
| 61 |
innerRef, |
| 62 |
ouiaId, |
| 63 |
ouiaSafe = true, |
| 64 |
resetOffset = false, |
| 65 |
onRowClick, |
| 66 |
isSelectable, |
| 67 |
'aria-label': passedAriaLabel, |
| 68 |
...props |
| 69 |
}: TrProps) => { |
| 70 |
const ouiaProps = useOUIAProps('TableRow', ouiaId, ouiaSafe); |
| 71 |
const [computedAriaLabel, setComputedAriaLabel] = useState<string | undefined>(''); |
| 72 |
|
| 73 |
let onKeyDown = null; |
| 74 |
if (onRowClick) { |
| 75 |
onKeyDown = (e: React.KeyboardEvent) => { |
| 76 |
if (e.key === 'Enter' || e.key === ' ') { |
| 77 |
onRowClick(e); |
| 78 |
e.preventDefault(); |
| 79 |
} |
| 80 |
}; |
| 81 |
} |
| 82 |
|
| 83 |
const rowIsHidden = isHidden || (isExpanded !== undefined && !isExpanded); |
| 84 |
|
| 85 |
const { registerSelectableRow, hasAnimations } = useContext(TableContext); |
| 86 |
|
| 87 |
useEffect(() => { |
| 88 |
if (isSelectable && !rowIsHidden) { |
| 89 |
setComputedAriaLabel(`${isRowSelected ? 'Row selected' : ''}`); |
| 90 |
registerSelectableRow(); |
| 91 |
} else { |
| 92 |
setComputedAriaLabel(undefined); |
| 93 |
} |
| 94 |
}, [isRowSelected, isSelectable, registerSelectableRow, rowIsHidden]); |
| 95 |
|
| 96 |
const ariaLabel = passedAriaLabel || computedAriaLabel; |
| 97 |
|
| 98 |
return ( |
| 99 |
<> |
| 100 |
<tr |
| 101 |
className={css( |
| 102 |
styles.tableTr, |
| 103 |
className, |
| 104 |
isExpanded !== undefined && styles.tableExpandableRow, |
| 105 |
(isExpanded || isContentExpanded) && styles.modifiers.expanded, |
| 106 |
isEditable && inlineStyles.modifiers.inlineEditable, |
| 107 |
isClickable && styles.modifiers.clickable, |
| 108 |
isRowSelected && styles.modifiers.selected, |
| 109 |
isStriped && styles.modifiers.striped, |
| 110 |
isBorderRow && styles.modifiers.borderRow, |
| 111 |
isControlRow && styles.tableControlRow, |
| 112 |
resetOffset && styles.modifiers.firstCellOffsetReset |
| 113 |
)} |
| 114 |
hidden={rowIsHidden} |
| 115 |
{...(isClickable && { tabIndex: 0 })} |
| 116 |
aria-label={ariaLabel} |
| 117 |
ref={innerRef} |
| 118 |
{...(hasAnimations && rowIsHidden && { inert: '' })} |
| 119 |
{...(onRowClick && { onClick: onRowClick, onKeyDown })} |
| 120 |
{...ouiaProps} |
| 121 |
{...props} |
| 122 |
> |
| 123 |
{children} |
| 124 |
</tr> |
| 125 |
</> |
| 126 |
); |
| 127 |
}; |
| 128 |
|
| 129 |
export const Tr = forwardRef((props: TrProps, ref: React.Ref<HTMLTableRowElement>) => ( |
| 130 |
<TrBase {...props} innerRef={ref} /> |
| 131 |
)); |
| 132 |
Tr.displayName = 'Tr'; |