| 1 |
import { useEffect, useState } from 'react'; |
| 2 |
import { TreeView, TreeViewDataItem } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const TreeViewWithCheckboxes: React.FunctionComponent = () => { |
| 5 |
const [checkedItems, setCheckedItems] = useState<TreeViewDataItem[]>([]); |
| 6 |
|
| 7 |
useEffect(() => { |
| 8 |
// eslint-disable-next-line no-console |
| 9 |
console.log('Checked items: ', checkedItems); |
| 10 |
}, [checkedItems]); |
| 11 |
|
| 12 |
const options = [ |
| 13 |
{ |
| 14 |
name: 'Application launcher', |
| 15 |
id: 'example3-AppLaunch', |
| 16 |
checkProps: { 'aria-label': 'app-launcher-check', checked: false }, |
| 17 |
children: [ |
| 18 |
{ |
| 19 |
name: 'Application 1', |
| 20 |
id: 'example3-App1', |
| 21 |
checkProps: { checked: false }, |
| 22 |
children: [ |
| 23 |
{ |
| 24 |
name: 'Settings', |
| 25 |
id: 'example3-App1Settings', |
| 26 |
checkProps: { checked: false } |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: 'Current', |
| 30 |
id: 'example3-App1Current', |
| 31 |
checkProps: { checked: false } |
| 32 |
} |
| 33 |
] |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: 'Application 2', |
| 37 |
id: 'example3-App2', |
| 38 |
checkProps: { checked: false }, |
| 39 |
children: [ |
| 40 |
{ |
| 41 |
name: 'Settings', |
| 42 |
id: 'example3-App2Settings', |
| 43 |
checkProps: { checked: false } |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: 'Loader', |
| 47 |
id: 'example3-App2Loader', |
| 48 |
checkProps: { checked: false }, |
| 49 |
children: [ |
| 50 |
{ |
| 51 |
name: 'Loading App 1', |
| 52 |
id: 'example3-LoadApp1', |
| 53 |
checkProps: { checked: false } |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: 'Loading App 2', |
| 57 |
id: 'example3-LoadApp2', |
| 58 |
checkProps: { checked: false } |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: 'Loading App 3', |
| 62 |
id: 'example3-LoadApp3', |
| 63 |
checkProps: { checked: false } |
| 64 |
} |
| 65 |
] |
| 66 |
} |
| 67 |
] |
| 68 |
} |
| 69 |
], |
| 70 |
defaultExpanded: true |
| 71 |
}, |
| 72 |
{ |
| 73 |
name: 'Cost management', |
| 74 |
id: 'example3-Cost', |
| 75 |
checkProps: { 'aria-label': 'cost-check', checked: false }, |
| 76 |
children: [ |
| 77 |
{ |
| 78 |
name: 'Application 3', |
| 79 |
id: 'example3-App3', |
| 80 |
checkProps: { 'aria-label': 'app-3-check', checked: false }, |
| 81 |
children: [ |
| 82 |
{ |
| 83 |
name: 'Settings', |
| 84 |
id: 'example3-App3Settings', |
| 85 |
checkProps: { 'aria-label': 'app-3-settings-check', checked: false } |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: 'Current', |
| 89 |
id: 'example3-App3Current', |
| 90 |
checkProps: { 'aria-label': 'app-3-current-check', checked: false } |
| 91 |
} |
| 92 |
] |
| 93 |
} |
| 94 |
] |
| 95 |
}, |
| 96 |
{ |
| 97 |
name: 'Sources', |
| 98 |
id: 'example3-Sources', |
| 99 |
checkProps: { 'aria-label': 'sources-check', checked: false }, |
| 100 |
children: [ |
| 101 |
{ |
| 102 |
name: 'Application 4', |
| 103 |
id: 'example3-App4', |
| 104 |
checkProps: { 'aria-label': 'app-4-check', checked: false }, |
| 105 |
children: [ |
| 106 |
{ |
| 107 |
name: 'Settings', |
| 108 |
id: 'example3-App4Settings', |
| 109 |
checkProps: { 'aria-label': 'app-4-settings-check', checked: false } |
| 110 |
} |
| 111 |
] |
| 112 |
} |
| 113 |
] |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: 'Really really really long folder name that overflows the container it is in', |
| 117 |
id: 'example3-Long', |
| 118 |
checkProps: { 'aria-label': 'long-check', checked: false }, |
| 119 |
children: [ |
| 120 |
{ name: 'Application 5', id: 'example3-App5', checkProps: { 'aria-label': 'app-5-check', checked: false } } |
| 121 |
] |
| 122 |
} |
| 123 |
]; |
| 124 |
|
| 125 |
const onCheck = (event: React.ChangeEvent, treeViewItem: TreeViewDataItem) => { |
| 126 |
const checked = (event.target as HTMLInputElement).checked; |
| 127 |
|
| 128 |
const checkedItemTree = options |
| 129 |
.map((opt) => Object.assign({}, opt)) |
| 130 |
.filter((item) => filterItems(item, treeViewItem)); |
| 131 |
const flatCheckedItems = flattenTree(checkedItemTree); |
| 132 |
|
| 133 |
setCheckedItems((prevCheckedItems) => |
| 134 |
checked |
| 135 |
? prevCheckedItems.concat(flatCheckedItems.filter((item) => !checkedItems.some((i) => i.id === item.id))) |
| 136 |
: prevCheckedItems.filter((item) => !flatCheckedItems.some((i) => i.id === item.id)) |
| 137 |
); |
| 138 |
}; |
| 139 |
|
| 140 |
// Helper functions |
| 141 |
const isChecked = (dataItem: TreeViewDataItem) => checkedItems.some((item) => item.id === dataItem.id); |
| 142 |
const areAllDescendantsChecked = (dataItem: TreeViewDataItem) => |
| 143 |
dataItem.children ? dataItem.children.every((child) => areAllDescendantsChecked(child)) : isChecked(dataItem); |
| 144 |
const areSomeDescendantsChecked = (dataItem: TreeViewDataItem) => |
| 145 |
dataItem.children ? dataItem.children.some((child) => areSomeDescendantsChecked(child)) : isChecked(dataItem); |
| 146 |
|
| 147 |
const flattenTree = (tree: TreeViewDataItem[]) => { |
| 148 |
let result: TreeViewDataItem[] = []; |
| 149 |
tree.forEach((item) => { |
| 150 |
result.push(item); |
| 151 |
if (item.children) { |
| 152 |
result = result.concat(flattenTree(item.children)); |
| 153 |
} |
| 154 |
}); |
| 155 |
return result; |
| 156 |
}; |
| 157 |
|
| 158 |
const mapTree = (item: TreeViewDataItem) => { |
| 159 |
const hasCheck = areAllDescendantsChecked(item); |
| 160 |
// Reset checked properties to be updated |
| 161 |
if (item.checkProps) { |
| 162 |
item.checkProps.checked = false; |
| 163 |
|
| 164 |
if (hasCheck) { |
| 165 |
item.checkProps.checked = true; |
| 166 |
} else { |
| 167 |
const hasPartialCheck = areSomeDescendantsChecked(item); |
| 168 |
if (hasPartialCheck) { |
| 169 |
item.checkProps.checked = null; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if (item.children) { |
| 174 |
return { |
| 175 |
...item, |
| 176 |
children: item.children.map((child) => mapTree(child)) |
| 177 |
}; |
| 178 |
} |
| 179 |
} |
| 180 |
return item; |
| 181 |
}; |
| 182 |
|
| 183 |
const filterItems = (item: TreeViewDataItem, checkedItem: TreeViewDataItem) => { |
| 184 |
if (item.id === checkedItem.id) { |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
if (item.children) { |
| 189 |
return ( |
| 190 |
(item.children = item.children |
| 191 |
.map((opt) => Object.assign({}, opt)) |
| 192 |
.filter((child) => filterItems(child, checkedItem))).length > 0 |
| 193 |
); |
| 194 |
} |
| 195 |
}; |
| 196 |
const mapped = options.map((item) => mapTree(item)); |
| 197 |
return ( |
| 198 |
<TreeView |
| 199 |
hasAnimations |
| 200 |
aria-label="Tree View with checkboxes example" |
| 201 |
data={mapped} |
| 202 |
onCheck={onCheck} |
| 203 |
hasCheckboxes |
| 204 |
/> |
| 205 |
); |
| 206 |
}; |