| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { TreeView, Button, TreeViewDataItem } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const TreeViewSingleSelectable: React.FunctionComponent = () => { |
| 5 |
const [activeItems, setActiveItems] = useState<TreeViewDataItem[]>(); |
| 6 |
const [allExpanded, setAllExpanded] = useState<boolean>(); |
| 7 |
|
| 8 |
const onSelect = (_event: React.MouseEvent, treeViewItem: TreeViewDataItem) => { |
| 9 |
// Ignore folders for selection |
| 10 |
if (treeViewItem && !treeViewItem.children) { |
| 11 |
setActiveItems([treeViewItem]); |
| 12 |
} |
| 13 |
}; |
| 14 |
|
| 15 |
const onToggle = (_event: React.MouseEvent) => { |
| 16 |
setAllExpanded((prevAllExpanded) => !prevAllExpanded); |
| 17 |
}; |
| 18 |
|
| 19 |
const options = [ |
| 20 |
{ |
| 21 |
name: 'Application launcher', |
| 22 |
id: 'example1-AppLaunch', |
| 23 |
children: [ |
| 24 |
{ |
| 25 |
name: 'Application 1', |
| 26 |
id: 'example1-App1', |
| 27 |
children: [ |
| 28 |
{ name: 'Settings', id: 'example1-App1Settings' }, |
| 29 |
{ name: 'Current', id: 'example1-App1Current' } |
| 30 |
] |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: 'Application 2', |
| 34 |
id: 'example1-App2', |
| 35 |
children: [ |
| 36 |
{ name: 'Settings', id: 'example1-App2Settings' }, |
| 37 |
{ |
| 38 |
name: 'Loader', |
| 39 |
id: 'example1-App2Loader', |
| 40 |
children: [ |
| 41 |
{ name: 'Loading App 1', id: 'example1-LoadApp1' }, |
| 42 |
{ name: 'Loading App 2', id: 'example1-LoadApp2' }, |
| 43 |
{ name: 'Loading App 3', id: 'example1-LoadApp3' } |
| 44 |
] |
| 45 |
} |
| 46 |
] |
| 47 |
} |
| 48 |
], |
| 49 |
defaultExpanded: true |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: 'Cost management', |
| 53 |
id: 'example1-Cost', |
| 54 |
children: [ |
| 55 |
{ |
| 56 |
name: 'Application 3', |
| 57 |
id: 'example1-App3', |
| 58 |
children: [ |
| 59 |
{ name: 'Settings', id: 'example1-App3Settings' }, |
| 60 |
{ name: 'Current', id: 'example1-App3Current' } |
| 61 |
] |
| 62 |
} |
| 63 |
] |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: 'Sources', |
| 67 |
id: 'example1-Sources', |
| 68 |
children: [ |
| 69 |
{ name: 'Application 4', id: 'example1-App4', children: [{ name: 'Settings', id: 'example1-App4Settings' }] } |
| 70 |
] |
| 71 |
}, |
| 72 |
{ |
| 73 |
name: 'Really really really long folder name that overflows the container it is in', |
| 74 |
id: 'example1-Long', |
| 75 |
children: [{ name: 'Application 5', id: 'example1-App5' }] |
| 76 |
} |
| 77 |
]; |
| 78 |
return ( |
| 79 |
<Fragment> |
| 80 |
<Button variant="link" onClick={onToggle}> |
| 81 |
{allExpanded && 'Collapse all'} |
| 82 |
{!allExpanded && 'Expand all'} |
| 83 |
</Button> |
| 84 |
<TreeView |
| 85 |
hasAnimations |
| 86 |
aria-label="Tree View single selectable example" |
| 87 |
data={options} |
| 88 |
activeItems={activeItems} |
| 89 |
onSelect={onSelect} |
| 90 |
allExpanded={allExpanded} |
| 91 |
/> |
| 92 |
</Fragment> |
| 93 |
); |
| 94 |
}; |