|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { css } from '@patternfly/react-styles'; |
| 3 | +import styles from '@patternfly/react-styles/css/components/TreeView/tree-view'; |
| 4 | +import AngleRightIcon from '@patternfly/react-icons/dist/js/icons/angle-right-icon'; |
| 5 | +import AngleDownIcon from '@patternfly/react-icons/dist/js/icons/angle-down-icon'; |
| 6 | +import { TreeViewDataItem } from './TreeView'; |
| 7 | +import { Badge } from '../Badge'; |
| 8 | + |
| 9 | +export interface TreeViewListItemProps { |
| 10 | + /** Internal content of a tree view item */ |
| 11 | + name: React.ReactNode; |
| 12 | + /** ID of a tree view item */ |
| 13 | + id?: string; |
| 14 | + /** Flag indicating if node is expanded by default */ |
| 15 | + defaultExpanded?: boolean; |
| 16 | + /** Child nodes of a tree view item */ |
| 17 | + children?: React.ReactNode; |
| 18 | + /** Callback for item selection */ |
| 19 | + onSelect?: (event: React.MouseEvent, item: TreeViewDataItem, parent: TreeViewDataItem) => void; |
| 20 | + /** Callback for item checkbox selection */ |
| 21 | + onCheck?: (event: React.ChangeEvent, item: TreeViewDataItem, parent: TreeViewDataItem) => void; |
| 22 | + /** Flag indicating if a tree view item has a checkbox */ |
| 23 | + hasCheck?: boolean; |
| 24 | + /** Additional properties of the tree view item checkbox */ |
| 25 | + checkProps?: any; |
| 26 | + /** Flag indicating if a tree view item has a badge */ |
| 27 | + hasBadge?: boolean; |
| 28 | + /** Additional properties of the tree view item badge */ |
| 29 | + badgeProps?: any; |
| 30 | + /** Active items of tree view */ |
| 31 | + activeItems?: TreeViewDataItem[]; |
| 32 | + /** Data structure of tree view item */ |
| 33 | + itemData?: TreeViewDataItem; |
| 34 | + /** Parent item of tree view item */ |
| 35 | + parentItem?: TreeViewDataItem; |
| 36 | + /** Default icon of a tree view item */ |
| 37 | + icon?: React.ReactNode; |
| 38 | + /** Expanded icon of a tree view item */ |
| 39 | + expandedIcon?: React.ReactNode; |
| 40 | + /** Action of a tree view item, nested inside a button */ |
| 41 | + action?: React.ReactNode; |
| 42 | + /** Additional properties of the tree view item action button */ |
| 43 | + actionProps?: any; |
| 44 | + /** Callback for item comparison function */ |
| 45 | + compareItems?: (item: TreeViewDataItem, itemToCheck: TreeViewDataItem) => boolean; |
| 46 | +} |
| 47 | + |
| 48 | +export const TreeViewListItem: React.FunctionComponent<TreeViewListItemProps> = ({ |
| 49 | + name, |
| 50 | + id, |
| 51 | + defaultExpanded = false, |
| 52 | + children = null, |
| 53 | + onSelect, |
| 54 | + onCheck, |
| 55 | + hasCheck = false, |
| 56 | + checkProps = { |
| 57 | + checked: false |
| 58 | + }, |
| 59 | + hasBadge = false, |
| 60 | + badgeProps = { isRead: true }, |
| 61 | + activeItems = [], |
| 62 | + itemData, |
| 63 | + parentItem, |
| 64 | + icon, |
| 65 | + expandedIcon, |
| 66 | + action, |
| 67 | + actionProps = { |
| 68 | + onClick: (evt: React.MouseEvent) => { |
| 69 | + evt.stopPropagation(); |
| 70 | + evt.preventDefault(); |
| 71 | + onSelect && onSelect(evt, itemData, parentItem); |
| 72 | + } |
| 73 | + }, |
| 74 | + compareItems |
| 75 | +}: TreeViewListItemProps) => { |
| 76 | + const [isExpanded, setIsExpanded] = useState(defaultExpanded); |
| 77 | + return ( |
| 78 | + <li |
| 79 | + id={id} |
| 80 | + className={css( |
| 81 | + styles.treeViewListItem, |
| 82 | + !!children && styles.modifiers.expandable, |
| 83 | + isExpanded && styles.modifiers.expanded |
| 84 | + )} |
| 85 | + {...(isExpanded && { 'aria-expanded': 'true' })} |
| 86 | + role="treeitem" |
| 87 | + tabIndex={0} |
| 88 | + > |
| 89 | + <div className={css(styles.treeViewContent)}> |
| 90 | + <button |
| 91 | + className={css( |
| 92 | + styles.treeViewNode, |
| 93 | + activeItems && |
| 94 | + activeItems.length > 0 && |
| 95 | + activeItems.some(item => compareItems && item && compareItems(item, itemData)) |
| 96 | + ? children |
| 97 | + ? styles.modifiers.active |
| 98 | + : styles.modifiers.current |
| 99 | + : '' |
| 100 | + )} |
| 101 | + onClick={(evt: React.MouseEvent) => { |
| 102 | + if (children) { |
| 103 | + setIsExpanded(!isExpanded); |
| 104 | + } |
| 105 | + onSelect && onSelect(evt, itemData, parentItem); |
| 106 | + }} |
| 107 | + > |
| 108 | + {children && ( |
| 109 | + <span className={css(styles.treeViewNodeToggleIcon)}> |
| 110 | + {!isExpanded && <AngleRightIcon aria-hidden="true" />} |
| 111 | + {isExpanded && <AngleDownIcon aria-hidden="true" />} |
| 112 | + </span> |
| 113 | + )} |
| 114 | + {hasCheck && ( |
| 115 | + <span className={css(styles.treeViewNodeCheck)}> |
| 116 | + <input |
| 117 | + type="checkbox" |
| 118 | + onChange={(evt: React.ChangeEvent) => onCheck && onCheck(evt, itemData, parentItem)} |
| 119 | + onClick={(evt: React.MouseEvent) => evt.stopPropagation()} |
| 120 | + {...checkProps} |
| 121 | + /> |
| 122 | + </span> |
| 123 | + )} |
| 124 | + {icon && ( |
| 125 | + <span className={css(styles.treeViewNodeIcon)}> |
| 126 | + {!isExpanded && icon} |
| 127 | + {isExpanded && (expandedIcon || icon)} |
| 128 | + </span> |
| 129 | + )} |
| 130 | + <span className={css(styles.treeViewNodeText)}>{name}</span> |
| 131 | + {hasBadge && children && ( |
| 132 | + <span className={css(styles.treeViewNodeCount)}> |
| 133 | + <Badge {...badgeProps}>{(children as React.ReactElement).props.data.length}</Badge> |
| 134 | + </span> |
| 135 | + )} |
| 136 | + </button> |
| 137 | + {action && ( |
| 138 | + <button className={css(styles.treeViewAction)} {...actionProps}> |
| 139 | + {action} |
| 140 | + </button> |
| 141 | + )} |
| 142 | + </div> |
| 143 | + {isExpanded && children} |
| 144 | + </li> |
| 145 | + ); |
| 146 | +}; |
| 147 | +TreeViewListItem.displayName = 'TreeViewListItem'; |
0 commit comments