| 1 |
import { TreeViewList } from './TreeViewList'; |
| 2 |
import { TreeViewCheckProps, TreeViewListItem } from './TreeViewListItem'; |
| 3 |
import { TreeViewRoot } from './TreeViewRoot'; |
| 4 |
import { useHasAnimations } from '../../helpers'; |
| 5 |
|
| 6 |
/** Properties that make up a tree view data item. These properties should be passed in as an |
| 7 |
* object to one of the various tree view component properties which accept TreeViewDataItem as |
| 8 |
* a type. |
| 9 |
*/ |
| 10 |
|
| 11 |
export interface TreeViewDataItem { |
| 12 |
/** Action of a tree view item, which can be either a button or dropdown component. */ |
| 13 |
action?: React.ReactNode; |
| 14 |
/** Additional properties of the tree view item badge. */ |
| 15 |
badgeProps?: any; |
| 16 |
/** Additional properties of the tree view item checkbox. */ |
| 17 |
checkProps?: TreeViewCheckProps; |
| 18 |
/** Child nodes of a tree view item. */ |
| 19 |
children?: TreeViewDataItem[]; |
| 20 |
/** Optional prop for a custom badge. */ |
| 21 |
customBadgeContent?: React.ReactNode; |
| 22 |
/** Flag indicating if node is expanded by default. */ |
| 23 |
defaultExpanded?: boolean; |
| 24 |
/** Expanded icon of a tree view item. */ |
| 25 |
expandedIcon?: React.ReactNode; |
| 26 |
/** Flag indicating if a tree view item has a badge. */ |
| 27 |
hasBadge?: boolean; |
| 28 |
/** Flag indicating if a tree view item has a checkbox. */ |
| 29 |
hasCheckbox?: boolean; |
| 30 |
/** Default icon of a tree view item. */ |
| 31 |
icon?: React.ReactNode; |
| 32 |
/** ID of a tree view item. */ |
| 33 |
id?: string; |
| 34 |
/** Internal content of a tree view item. */ |
| 35 |
name: React.ReactNode; |
| 36 |
/** Title of a tree view item. Only used in compact presentations. */ |
| 37 |
title?: React.ReactNode; |
| 38 |
/** Flag indicating if the tree view item is disabled. */ |
| 39 |
isDisabled?: boolean; |
| 40 |
/** Flag indicating if the tree view item toggle is disabled. */ |
| 41 |
isToggleDisabled?: boolean; |
| 42 |
} |
| 43 |
|
| 44 |
/** The main tree view component. */ |
| 45 |
|
| 46 |
export interface TreeViewProps { |
| 47 |
/** Active items of tree view. */ |
| 48 |
activeItems?: TreeViewDataItem[]; |
| 49 |
/** Sets the expanded state on all tree nodes, overriding default behavior and current |
| 50 |
* internal state. |
| 51 |
*/ |
| 52 |
allExpanded?: boolean; |
| 53 |
/** A text string that sets the accessible name of the tree view list. Either this or the aria-labelledby property must |
| 54 |
* be passed in. |
| 55 |
*/ |
| 56 |
'aria-label'?: string; |
| 57 |
/** A space separated list of element id's that sets the accessible name of the tree view list. Either |
| 58 |
* this or the aria-label property must be passed in. |
| 59 |
*/ |
| 60 |
'aria-labelledby'?: string; |
| 61 |
/** Class to add if not passed a parentItem property. */ |
| 62 |
className?: string; |
| 63 |
/** Comparison function for determining active items. */ |
| 64 |
compareItems?: (item: TreeViewDataItem, itemToCheck: TreeViewDataItem) => boolean; |
| 65 |
/** Data of the tree view. */ |
| 66 |
data: TreeViewDataItem[]; |
| 67 |
/** Sets the default expanded behavior. */ |
| 68 |
defaultAllExpanded?: boolean; |
| 69 |
/** Icon for all expanded node items. */ |
| 70 |
expandedIcon?: React.ReactNode; |
| 71 |
/** Flag indicating if all nodes in the tree view should have badges. */ |
| 72 |
hasBadges?: boolean; |
| 73 |
/** Flag indicating if all nodes in the tree view should have checkboxes. */ |
| 74 |
hasCheckboxes?: boolean; |
| 75 |
/** Flag indicating if the tree view has guide lines. */ |
| 76 |
hasGuides?: boolean; |
| 77 |
/** Flag indicating if the tree view is nested. */ |
| 78 |
isNested?: boolean; |
| 79 |
/** Flag indicating that tree nodes should be independently selectable, even when having |
| 80 |
* children. |
| 81 |
*/ |
| 82 |
hasSelectableNodes?: boolean; |
| 83 |
/** Icon for all leaf or unexpanded node items. */ |
| 84 |
icon?: React.ReactNode; |
| 85 |
/** ID of the tree view. */ |
| 86 |
id?: string; |
| 87 |
/** Flag indicating whether multiple nodes can be selected in the tree view. This will also set the |
| 88 |
* aria-multiselectable attribute on the tree view list which is required to be true when multiple selection is intended. |
| 89 |
* Can only be applied to the root tree view list. |
| 90 |
*/ |
| 91 |
isMultiSelectable?: boolean; |
| 92 |
/** Callback for item checkbox selection. */ |
| 93 |
onCheck?: (event: React.ChangeEvent<HTMLInputElement>, item: TreeViewDataItem, parentItem: TreeViewDataItem) => void; |
| 94 |
/** Callback for item selection. */ |
| 95 |
onSelect?: (event: React.MouseEvent, item: TreeViewDataItem, parentItem: TreeViewDataItem) => void; |
| 96 |
/** Callback for expanding a node with children. */ |
| 97 |
onExpand?: (event: React.MouseEvent, item: TreeViewDataItem, parentItem: TreeViewDataItem) => void; |
| 98 |
/** Callback for collapsing a node with children. */ |
| 99 |
onCollapse?: (event: React.MouseEvent, item: TreeViewDataItem, parentItem: TreeViewDataItem) => void; |
| 100 |
/** Internal. Parent item of a tree view list item. */ |
| 101 |
parentItem?: TreeViewDataItem; |
| 102 |
/** Toolbar to display above the tree view. */ |
| 103 |
toolbar?: React.ReactNode; |
| 104 |
/** Flag indicating the tree view should utilize memoization to help render large data sets. |
| 105 |
* Setting this property requires that the activeItems property is passed an array containing |
| 106 |
* every node in the selected item's path. |
| 107 |
*/ |
| 108 |
useMemo?: boolean; |
| 109 |
/** Variant presentation styles for the tree view. */ |
| 110 |
variant?: 'default' | 'compact' | 'compactNoBackground'; |
| 111 |
/** Flag indicating whether a tree view has animations. This will always render |
| 112 |
* nested tree view items rather than dynamically rendering them. This prop will be removed in |
| 113 |
* the next breaking change release in favor of defaulting to always-rendered items. |
| 114 |
*/ |
| 115 |
hasAnimations?: boolean; |
| 116 |
} |
| 117 |
|
| 118 |
export const TreeView: React.FunctionComponent<TreeViewProps> = ({ |
| 119 |
data, |
| 120 |
isNested = false, |
| 121 |
hasCheckboxes = false, |
| 122 |
hasBadges = false, |
| 123 |
hasGuides = false, |
| 124 |
hasSelectableNodes = false, |
| 125 |
variant = 'default', |
| 126 |
defaultAllExpanded = false, |
| 127 |
allExpanded, |
| 128 |
icon, |
| 129 |
isMultiSelectable = false, |
| 130 |
expandedIcon, |
| 131 |
parentItem, |
| 132 |
onSelect, |
| 133 |
onCheck, |
| 134 |
onExpand, |
| 135 |
onCollapse, |
| 136 |
toolbar, |
| 137 |
activeItems, |
| 138 |
compareItems = (item, itemToCheck) => item.id === itemToCheck.id, |
| 139 |
className, |
| 140 |
useMemo, |
| 141 |
'aria-label': ariaLabel, |
| 142 |
'aria-labelledby': ariaLabelledby, |
| 143 |
hasAnimations: hasAnimationsProp, |
| 144 |
...props |
| 145 |
}: TreeViewProps) => { |
| 146 |
const hasAnimations = useHasAnimations(hasAnimationsProp); |
| 147 |
const treeViewList = ( |
| 148 |
<TreeViewList |
| 149 |
isNested={isNested} |
| 150 |
toolbar={toolbar} |
| 151 |
isMultiSelectable={isMultiSelectable} |
| 152 |
aria-label={ariaLabel} |
| 153 |
aria-labelledby={ariaLabelledby} |
| 154 |
{...props} |
| 155 |
> |
| 156 |
{data.map((item) => ( |
| 157 |
<TreeViewListItem |
| 158 |
key={item.id?.toString() || item.name?.toString()} |
| 159 |
name={item.name} |
| 160 |
hasAnimations={hasAnimations} |
| 161 |
title={item.title} |
| 162 |
id={item.id} |
| 163 |
isExpanded={allExpanded} |
| 164 |
isSelectable={hasSelectableNodes} |
| 165 |
isDisabled={item.isDisabled} |
| 166 |
isToggleDisabled={item.isToggleDisabled} |
| 167 |
defaultExpanded={item.defaultExpanded !== undefined ? item.defaultExpanded : defaultAllExpanded} |
| 168 |
onSelect={onSelect} |
| 169 |
onCheck={onCheck} |
| 170 |
onExpand={onExpand} |
| 171 |
onCollapse={onCollapse} |
| 172 |
hasCheckbox={item.hasCheckbox !== undefined ? item.hasCheckbox : hasCheckboxes} |
| 173 |
checkProps={item.checkProps} |
| 174 |
hasBadge={item.hasBadge !== undefined ? item.hasBadge : hasBadges} |
| 175 |
customBadgeContent={item.customBadgeContent} |
| 176 |
badgeProps={item.badgeProps} |
| 177 |
activeItems={activeItems} |
| 178 |
parentItem={parentItem} |
| 179 |
itemData={item} |
| 180 |
icon={item.icon !== undefined ? item.icon : icon} |
| 181 |
expandedIcon={item.expandedIcon !== undefined ? item.expandedIcon : expandedIcon} |
| 182 |
action={item.action} |
| 183 |
compareItems={compareItems} |
| 184 |
isCompact={variant === 'compact' || variant === 'compactNoBackground'} |
| 185 |
useMemo={useMemo} |
| 186 |
{...(item.children && { |
| 187 |
children: ( |
| 188 |
<TreeView |
| 189 |
data={item.children} |
| 190 |
isNested |
| 191 |
hasAnimations={hasAnimations} |
| 192 |
parentItem={item} |
| 193 |
hasCheckboxes={hasCheckboxes} |
| 194 |
hasBadges={hasBadges} |
| 195 |
hasGuides={hasGuides} |
| 196 |
hasSelectableNodes={hasSelectableNodes} |
| 197 |
variant={variant} |
| 198 |
allExpanded={allExpanded} |
| 199 |
defaultAllExpanded={defaultAllExpanded} |
| 200 |
onSelect={onSelect} |
| 201 |
onCheck={onCheck} |
| 202 |
onExpand={onExpand} |
| 203 |
onCollapse={onCollapse} |
| 204 |
activeItems={activeItems} |
| 205 |
icon={icon} |
| 206 |
expandedIcon={expandedIcon} |
| 207 |
/> |
| 208 |
) |
| 209 |
})} |
| 210 |
/> |
| 211 |
))} |
| 212 |
</TreeViewList> |
| 213 |
); |
| 214 |
return ( |
| 215 |
<> |
| 216 |
{parentItem ? ( |
| 217 |
treeViewList |
| 218 |
) : ( |
| 219 |
<TreeViewRoot |
| 220 |
hasSelectableNodes={hasSelectableNodes} |
| 221 |
hasCheckboxes={hasCheckboxes} |
| 222 |
hasGuides={hasGuides} |
| 223 |
variant={variant} |
| 224 |
className={className} |
| 225 |
{...props} |
| 226 |
> |
| 227 |
{treeViewList} |
| 228 |
</TreeViewRoot> |
| 229 |
)} |
| 230 |
</> |
| 231 |
); |
| 232 |
}; |
| 233 |
|
| 234 |
TreeView.displayName = 'TreeView'; |