| 1 |
import { Children, Component, cloneElement, isValidElement } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/DataList/data-list'; |
| 4 |
import menuStyles from '@patternfly/react-styles/css/components/Menu/menu'; |
| 5 |
import { DataListContext } from './DataList'; |
| 6 |
import { KeyTypes } from '../../helpers/constants'; |
| 7 |
|
| 8 |
export interface DataListItemProps extends Omit<React.HTMLProps<HTMLLIElement>, 'children' | 'ref'> { |
| 9 |
/** Flag to show if the expanded content of the DataList item is visible */ |
| 10 |
isExpanded?: boolean; |
| 11 |
/** Content rendered inside the DataList item */ |
| 12 |
children: React.ReactNode; |
| 13 |
/** Additional classes added to the DataList item should be either <DataListItemRow> or <DataListContent> */ |
| 14 |
className?: string; |
| 15 |
/** Adds an accessible name to the selectable input if one is rendered */ |
| 16 |
'aria-labelledby': string; |
| 17 |
/** Unique id for the DataList item */ |
| 18 |
id?: string; |
| 19 |
/** Aria label to apply to the selectable input if one is rendered */ |
| 20 |
selectableInputAriaLabel?: string; |
| 21 |
} |
| 22 |
|
| 23 |
export interface DataListItemChildProps { |
| 24 |
/** Id for the row */ |
| 25 |
rowid: string; |
| 26 |
} |
| 27 |
|
| 28 |
class DataListItem extends Component<DataListItemProps> { |
| 29 |
static displayName = 'DataListItem'; |
| 30 |
static defaultProps: DataListItemProps = { |
| 31 |
isExpanded: false, |
| 32 |
className: '', |
| 33 |
id: '', |
| 34 |
children: null, |
| 35 |
'aria-labelledby': '' |
| 36 |
}; |
| 37 |
render() { |
| 38 |
const { |
| 39 |
children, |
| 40 |
isExpanded, |
| 41 |
className, |
| 42 |
id, |
| 43 |
'aria-labelledby': ariaLabelledBy, |
| 44 |
selectableInputAriaLabel, |
| 45 |
...props |
| 46 |
} = this.props; |
| 47 |
return ( |
| 48 |
<DataListContext.Consumer> |
| 49 |
{({ isSelectable, selectedDataListItemId, updateSelectedDataListItem, onSelectableRowChange }) => { |
| 50 |
const selectDataListItem = (event: React.MouseEvent) => { |
| 51 |
let target: any = event.target; |
| 52 |
while (event.currentTarget !== target) { |
| 53 |
if ( |
| 54 |
('onclick' in target && target.onclick) || |
| 55 |
target.parentNode.classList.contains(styles.dataListItemAction) || |
| 56 |
target.parentNode.classList.contains(styles.dataListItemControl) |
| 57 |
) { |
| 58 |
// check other event handlers are not present. |
| 59 |
return; |
| 60 |
} else { |
| 61 |
target = target.parentNode; |
| 62 |
} |
| 63 |
} |
| 64 |
updateSelectedDataListItem(event, id); |
| 65 |
}; |
| 66 |
|
| 67 |
const onKeyDown = (event: React.KeyboardEvent) => { |
| 68 |
if ([KeyTypes.Enter, KeyTypes.Space].includes(event.key)) { |
| 69 |
const target: any = event.target as HTMLElement; |
| 70 |
|
| 71 |
if ( |
| 72 |
target.closest(`.${menuStyles.menuItem}`) || |
| 73 |
target.parentNode.classList.contains(styles.dataListItemAction) || |
| 74 |
target.parentNode.classList.contains(styles.dataListItemControl) |
| 75 |
) { |
| 76 |
// check other event handlers are not present. |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
event.preventDefault(); |
| 81 |
updateSelectedDataListItem(event, id); |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
const isSelected = selectedDataListItemId === id; |
| 86 |
|
| 87 |
const selectableInputAriaProps = selectableInputAriaLabel |
| 88 |
? { 'aria-label': selectableInputAriaLabel } |
| 89 |
: { 'aria-labelledby': ariaLabelledBy }; |
| 90 |
|
| 91 |
return ( |
| 92 |
<li |
| 93 |
id={id} |
| 94 |
className={css( |
| 95 |
styles.dataListItem, |
| 96 |
isExpanded && styles.modifiers.expanded, |
| 97 |
isSelectable && styles.modifiers.clickable, |
| 98 |
selectedDataListItemId && isSelected && styles.modifiers.selected, |
| 99 |
className |
| 100 |
)} |
| 101 |
{...(isSelectable && { tabIndex: 0, onClick: selectDataListItem, onKeyDown })} |
| 102 |
{...props} |
| 103 |
> |
| 104 |
{onSelectableRowChange && ( |
| 105 |
<input |
| 106 |
className="pf-v6-screen-reader" |
| 107 |
type="radio" |
| 108 |
checked={isSelected} |
| 109 |
onChange={(event) => onSelectableRowChange(event, id)} |
| 110 |
tabIndex={-1} |
| 111 |
{...selectableInputAriaProps} |
| 112 |
/> |
| 113 |
)} |
| 114 |
{Children.map( |
| 115 |
children, |
| 116 |
(child) => |
| 117 |
isValidElement(child) && |
| 118 |
cloneElement(child as React.ReactElement<any>, { |
| 119 |
rowid: ariaLabelledBy |
| 120 |
}) |
| 121 |
)} |
| 122 |
</li> |
| 123 |
); |
| 124 |
}} |
| 125 |
</DataListContext.Consumer> |
| 126 |
); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
export { DataListItem }; |