| 1 |
import { memo, useState, useEffect, Children, isValidElement, cloneElement } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/TreeView/tree-view'; |
| 4 |
import RhMicronsCaretRightIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-right-icon'; |
| 5 |
import { TreeViewDataItem } from './TreeView'; |
| 6 |
import { Badge } from '../Badge'; |
| 7 |
import { useSSRSafeId } from '../../helpers'; |
| 8 |
import { useHasAnimations } from '../../helpers'; |
| 9 |
|
| 10 |
export interface TreeViewCheckProps extends Omit<Partial<React.InputHTMLAttributes<HTMLInputElement>>, 'checked'> { |
| 11 |
checked?: boolean | null; |
| 12 |
} |
| 13 |
|
| 14 |
export interface TreeViewListItemProps { |
| 15 |
/** Action of a tree view item, which can be either a button or dropdown component. */ |
| 16 |
action?: React.ReactNode; |
| 17 |
/** Active items of tree view. */ |
| 18 |
activeItems?: TreeViewDataItem[]; |
| 19 |
/** Additional properties of the tree view item badge. */ |
| 20 |
badgeProps?: any; |
| 21 |
/** Additional properties of the tree view item checkbox. */ |
| 22 |
checkProps?: TreeViewCheckProps; |
| 23 |
/** Child nodes of a tree view item. */ |
| 24 |
children?: React.ReactNode; |
| 25 |
/** Callback for item comparison function. */ |
| 26 |
compareItems?: (item: TreeViewDataItem, itemToCheck: TreeViewDataItem) => boolean; |
| 27 |
/** Optional prop for a custom badge. */ |
| 28 |
customBadgeContent?: React.ReactNode; |
| 29 |
/** Flag indicating if node is expanded by default. */ |
| 30 |
defaultExpanded?: boolean; |
| 31 |
/** Expanded icon of a tree view item. */ |
| 32 |
expandedIcon?: React.ReactNode; |
| 33 |
/** Flag indicating if a tree view item has a badge. */ |
| 34 |
hasBadge?: boolean; |
| 35 |
/** Flag indicating if a tree view item has a checkbox. */ |
| 36 |
hasCheckbox?: boolean; |
| 37 |
/** Default icon of a tree view item. */ |
| 38 |
icon?: React.ReactNode; |
| 39 |
/** ID of a tree view item. */ |
| 40 |
id?: string; |
| 41 |
/** Flag indicating if the tree view is using a compact variation. */ |
| 42 |
isCompact?: boolean; |
| 43 |
/** Flag indicating if the node is expanded, overrides internal state. */ |
| 44 |
isExpanded?: boolean; |
| 45 |
/** Flag indicating that tree nodes should be independently selectable, even when having |
| 46 |
* children. |
| 47 |
*/ |
| 48 |
isSelectable?: boolean; |
| 49 |
/** Flag indicating if the tree view item is disabled. */ |
| 50 |
isDisabled?: boolean; |
| 51 |
/** Flag indicating if the tree view item toggle is disabled. */ |
| 52 |
isToggleDisabled?: boolean; |
| 53 |
/** Data structure of tree view item. */ |
| 54 |
itemData?: TreeViewDataItem; |
| 55 |
/** Internal content of a tree view item. */ |
| 56 |
name: React.ReactNode; |
| 57 |
/** Callback for item checkbox selection. */ |
| 58 |
onCheck?: (event: React.ChangeEvent<HTMLInputElement>, item: TreeViewDataItem, parentItem: TreeViewDataItem) => void; |
| 59 |
/** Callback for item selection. Note: calling event.preventDefault() will prevent the node |
| 60 |
* from toggling. |
| 61 |
*/ |
| 62 |
onSelect?: (event: React.MouseEvent, item: TreeViewDataItem, parentItem: TreeViewDataItem) => void; |
| 63 |
/** Callback for expanding a node with children. */ |
| 64 |
onExpand?: (event: React.MouseEvent, item: TreeViewDataItem, parentItem: TreeViewDataItem) => void; |
| 65 |
/** Callback for collapsing a node with children. */ |
| 66 |
onCollapse?: (event: React.MouseEvent, item: TreeViewDataItem, parentItem: TreeViewDataItem) => void; |
| 67 |
/** Parent item of tree view item. */ |
| 68 |
parentItem?: TreeViewDataItem; |
| 69 |
/** Title of a tree view item. */ |
| 70 |
title: React.ReactNode; |
| 71 |
/** Flag indicating the tree view should utilize memoization to help render large data sets. |
| 72 |
* Setting this property requires that the activeItems property is passed an array containing |
| 73 |
* every node in the selected item's path. |
| 74 |
*/ |
| 75 |
useMemo?: boolean; |
| 76 |
/** Flag indicating whether a tree view has animations. This will always render |
| 77 |
* nested tree view items rather than dynamically rendering them. This prop will be removed in |
| 78 |
* the next breaking change release in favor of defaulting to always-rendered items. |
| 79 |
*/ |
| 80 |
hasAnimations?: boolean; |
| 81 |
} |
| 82 |
|
| 83 |
const TreeViewListItemBase: React.FunctionComponent<TreeViewListItemProps> = ({ |
| 84 |
name, |
| 85 |
title, |
| 86 |
id, |
| 87 |
isExpanded, |
| 88 |
isDisabled = false, |
| 89 |
isToggleDisabled = false, |
| 90 |
defaultExpanded = false, |
| 91 |
children = null, |
| 92 |
onSelect, |
| 93 |
onExpand, |
| 94 |
onCollapse, |
| 95 |
onCheck, |
| 96 |
hasCheckbox = false, |
| 97 |
checkProps = { |
| 98 |
checked: false |
| 99 |
}, |
| 100 |
hasBadge = false, |
| 101 |
customBadgeContent, |
| 102 |
badgeProps = { isRead: true }, |
| 103 |
isSelectable = false, |
| 104 |
isCompact, |
| 105 |
activeItems = [], |
| 106 |
itemData, |
| 107 |
parentItem, |
| 108 |
icon, |
| 109 |
expandedIcon, |
| 110 |
action, |
| 111 |
compareItems, |
| 112 |
hasAnimations: hasAnimationsProp, |
| 113 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 114 |
useMemo |
| 115 |
}: TreeViewListItemProps) => { |
| 116 |
const randomId = useSSRSafeId(isSelectable ? 'selectable-id' : 'checkbox-id'); |
| 117 |
const hasAnimations = useHasAnimations(hasAnimationsProp); |
| 118 |
const [internalIsExpanded, setIsExpanded] = useState(defaultExpanded); |
| 119 |
useEffect(() => { |
| 120 |
if (isExpanded !== undefined && isExpanded !== null) { |
| 121 |
setIsExpanded(isExpanded); |
| 122 |
} else if (defaultExpanded !== undefined && defaultExpanded !== null) { |
| 123 |
setIsExpanded(internalIsExpanded || defaultExpanded); |
| 124 |
} |
| 125 |
}, [isExpanded, defaultExpanded]); |
| 126 |
|
| 127 |
let Component: 'label' | 'div' | 'button' = 'button'; |
| 128 |
if (hasCheckbox) { |
| 129 |
Component = 'label'; |
| 130 |
} else if (isSelectable) { |
| 131 |
Component = 'div'; |
| 132 |
} |
| 133 |
|
| 134 |
const ToggleComponent = hasCheckbox || isSelectable ? 'button' : 'span'; |
| 135 |
const hasDisabledToggleClass = isToggleDisabled || (Component === 'button' && isDisabled); |
| 136 |
|
| 137 |
const renderToggle = (randomId: string) => ( |
| 138 |
<ToggleComponent |
| 139 |
className={css(styles.treeViewNodeToggle, hasDisabledToggleClass && styles.modifiers.disabled)} |
| 140 |
onClick={(evt: React.MouseEvent) => { |
| 141 |
if (!isToggleDisabled && (isSelectable || hasCheckbox)) { |
| 142 |
if (internalIsExpanded) { |
| 143 |
onCollapse && onCollapse(evt, itemData, parentItem); |
| 144 |
} else { |
| 145 |
onExpand && onExpand(evt, itemData, parentItem); |
| 146 |
} |
| 147 |
setIsExpanded(!internalIsExpanded); |
| 148 |
} |
| 149 |
if (!isToggleDisabled && isSelectable) { |
| 150 |
evt.stopPropagation(); |
| 151 |
} |
| 152 |
}} |
| 153 |
{...((hasCheckbox || isSelectable) && { 'aria-labelledby': `label-${randomId}` })} |
| 154 |
{...(ToggleComponent === 'button' && { disabled: isToggleDisabled, type: 'button' })} |
| 155 |
tabIndex={-1} |
| 156 |
> |
| 157 |
<span className={css(styles.treeViewNodeToggleIcon)}> |
| 158 |
<RhMicronsCaretRightIcon /> |
| 159 |
</span> |
| 160 |
</ToggleComponent> |
| 161 |
); |
| 162 |
|
| 163 |
const isCheckboxChecked = checkProps.checked === null ? false : checkProps.checked; |
| 164 |
const renderCheck = (randomId: string) => ( |
| 165 |
<span className={css(styles.treeViewNodeCheck)}> |
| 166 |
<input |
| 167 |
type="checkbox" |
| 168 |
onChange={(evt) => onCheck && onCheck(evt, itemData, parentItem)} |
| 169 |
onClick={(evt) => evt.stopPropagation()} |
| 170 |
ref={(elem) => { |
| 171 |
elem && (elem.indeterminate = checkProps.checked === null); |
| 172 |
}} |
| 173 |
disabled={isDisabled || checkProps.disabled} |
| 174 |
{...checkProps} |
| 175 |
checked={isCheckboxChecked} |
| 176 |
id={randomId} |
| 177 |
tabIndex={-1} |
| 178 |
/> |
| 179 |
</span> |
| 180 |
); |
| 181 |
const iconRendered = ( |
| 182 |
<span className={css(styles.treeViewNodeIcon)}> |
| 183 |
{!internalIsExpanded && icon} |
| 184 |
{internalIsExpanded && (expandedIcon || icon)} |
| 185 |
</span> |
| 186 |
); |
| 187 |
const renderNodeContent = () => { |
| 188 |
const content = ( |
| 189 |
<> |
| 190 |
{isCompact && title && <span className={css(styles.treeViewNodeTitle)}>{title}</span>} |
| 191 |
{isSelectable ? ( |
| 192 |
<button tabIndex={-1} className={css(styles.treeViewNodeText)} type="button" disabled={isDisabled}> |
| 193 |
{name} |
| 194 |
</button> |
| 195 |
) : ( |
| 196 |
<span className={css(styles.treeViewNodeText)}>{name}</span> |
| 197 |
)} |
| 198 |
</> |
| 199 |
); |
| 200 |
return isCompact ? <span className={css(styles.treeViewNodeContent)}>{content}</span> : content; |
| 201 |
}; |
| 202 |
const badgeRendered = ( |
| 203 |
<> |
| 204 |
{hasBadge && children && ( |
| 205 |
<span className={css(styles.treeViewNodeCount)}> |
| 206 |
<Badge {...badgeProps}>{customBadgeContent ?? (children as React.ReactElement<any>).props.data.length}</Badge> |
| 207 |
</span> |
| 208 |
)} |
| 209 |
{hasBadge && !children && customBadgeContent !== undefined && ( |
| 210 |
<span className={css(styles.treeViewNodeCount)}> |
| 211 |
<Badge {...badgeProps}>{customBadgeContent}</Badge> |
| 212 |
</span> |
| 213 |
)} |
| 214 |
</> |
| 215 |
); |
| 216 |
|
| 217 |
const isSelected = |
| 218 |
(!children || isSelectable) && |
| 219 |
activeItems && |
| 220 |
activeItems.length > 0 && |
| 221 |
activeItems.some((item) => compareItems && item && compareItems(item, itemData)); |
| 222 |
|
| 223 |
const clonedChildren = Children.map( |
| 224 |
children, |
| 225 |
(child) => |
| 226 |
isValidElement(child) && |
| 227 |
cloneElement(child as React.ReactElement<any>, { |
| 228 |
inert: internalIsExpanded ? undefined : '' |
| 229 |
}) |
| 230 |
); |
| 231 |
|
| 232 |
const isFullyDisabled = |
| 233 |
(Component === 'button' && isDisabled) || (Component !== 'button' && isDisabled && isToggleDisabled); |
| 234 |
|
| 235 |
return ( |
| 236 |
<li |
| 237 |
id={id} |
| 238 |
className={css(styles.treeViewListItem, internalIsExpanded && styles.modifiers.expanded)} |
| 239 |
aria-expanded={internalIsExpanded} |
| 240 |
role="treeitem" |
| 241 |
tabIndex={-1} |
| 242 |
{...(hasCheckbox && { 'aria-checked': isCheckboxChecked })} |
| 243 |
{...(!hasCheckbox && { 'aria-selected': isSelected })} |
| 244 |
{...(isFullyDisabled && { 'aria-disabled': true })} |
| 245 |
> |
| 246 |
<div className={css(styles.treeViewContent)}> |
| 247 |
<Component |
| 248 |
className={css( |
| 249 |
styles.treeViewNode, |
| 250 |
isSelected && styles.modifiers.current, |
| 251 |
isDisabled && styles.modifiers.disabled |
| 252 |
)} |
| 253 |
onClick={(evt: React.MouseEvent) => { |
| 254 |
if (!hasCheckbox) { |
| 255 |
!isDisabled && onSelect && onSelect(evt, itemData, parentItem); |
| 256 |
if (!isDisabled && !isSelectable && children && evt.isDefaultPrevented() !== true) { |
| 257 |
if (internalIsExpanded) { |
| 258 |
onCollapse && onCollapse(evt, itemData, parentItem); |
| 259 |
} else { |
| 260 |
onExpand && onExpand(evt, itemData, parentItem); |
| 261 |
} |
| 262 |
setIsExpanded(!internalIsExpanded); |
| 263 |
} |
| 264 |
} |
| 265 |
}} |
| 266 |
{...(hasCheckbox && { htmlFor: randomId })} |
| 267 |
{...((hasCheckbox || (isSelectable && children)) && { id: `label-${randomId}` })} |
| 268 |
{...(Component === 'button' && { type: 'button', disabled: isDisabled })} |
| 269 |
> |
| 270 |
<span className={css(styles.treeViewNodeContainer)}> |
| 271 |
{children && renderToggle(randomId)} |
| 272 |
{hasCheckbox && renderCheck(randomId)} |
| 273 |
{icon && iconRendered} |
| 274 |
{renderNodeContent()} |
| 275 |
{badgeRendered} |
| 276 |
</span> |
| 277 |
</Component> |
| 278 |
{action && <div className={css(styles.treeViewAction)}>{action}</div>} |
| 279 |
</div> |
| 280 |
{(internalIsExpanded || hasAnimations) && clonedChildren} |
| 281 |
</li> |
| 282 |
); |
| 283 |
}; |
| 284 |
|
| 285 |
export const TreeViewListItem = memo(TreeViewListItemBase, (prevProps, nextProps) => { |
| 286 |
if (!nextProps.useMemo) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
const prevIncludes = |
| 291 |
prevProps.activeItems && |
| 292 |
prevProps.activeItems.length > 0 && |
| 293 |
prevProps.activeItems.some( |
| 294 |
(item) => prevProps.compareItems && item && prevProps.compareItems(item, prevProps.itemData) |
| 295 |
); |
| 296 |
const nextIncludes = |
| 297 |
nextProps.activeItems && |
| 298 |
nextProps.activeItems.length > 0 && |
| 299 |
nextProps.activeItems.some( |
| 300 |
(item) => nextProps.compareItems && item && nextProps.compareItems(item, nextProps.itemData) |
| 301 |
); |
| 302 |
|
| 303 |
if (prevIncludes || nextIncludes) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
if ( |
| 308 |
prevProps.name !== nextProps.name || |
| 309 |
prevProps.title !== nextProps.title || |
| 310 |
prevProps.id !== nextProps.id || |
| 311 |
prevProps.isExpanded !== nextProps.isExpanded || |
| 312 |
prevProps.defaultExpanded !== nextProps.defaultExpanded || |
| 313 |
prevProps.isDisabled !== nextProps.isDisabled || |
| 314 |
prevProps.isToggleDisabled !== nextProps.isToggleDisabled || |
| 315 |
prevProps.onSelect !== nextProps.onSelect || |
| 316 |
prevProps.onCheck !== nextProps.onCheck || |
| 317 |
prevProps.onExpand !== nextProps.onExpand || |
| 318 |
prevProps.onCollapse !== nextProps.onCollapse || |
| 319 |
prevProps.hasCheckbox !== nextProps.hasCheckbox || |
| 320 |
prevProps.checkProps !== nextProps.checkProps || |
| 321 |
prevProps.hasBadge !== nextProps.hasBadge || |
| 322 |
prevProps.customBadgeContent !== nextProps.customBadgeContent || |
| 323 |
prevProps.badgeProps !== nextProps.badgeProps || |
| 324 |
prevProps.isCompact !== nextProps.isCompact || |
| 325 |
prevProps.icon !== nextProps.icon || |
| 326 |
prevProps.expandedIcon !== nextProps.expandedIcon || |
| 327 |
prevProps.action !== nextProps.action || |
| 328 |
prevProps.parentItem !== nextProps.parentItem || |
| 329 |
prevProps.itemData !== nextProps.itemData |
| 330 |
) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
return true; |
| 335 |
}); |
| 336 |
|
| 337 |
TreeViewListItem.displayName = 'TreeViewListItem'; |