| 1 |
import { useState } from 'react'; |
| 2 |
import { |
| 3 |
TreeView, |
| 4 |
Button, |
| 5 |
Dropdown, |
| 6 |
DropdownList, |
| 7 |
DropdownItem, |
| 8 |
MenuToggle, |
| 9 |
TreeViewDataItem |
| 10 |
} from '@patternfly/react-core'; |
| 11 |
import RhUiClipboardFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-clipboard-fill-icon'; |
| 12 |
import RhUiStorageFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-storage-fill-icon'; |
| 13 |
import RhUiEllipsisVerticalFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-ellipsis-vertical-fill-icon'; |
| 14 |
|
| 15 |
export const TreeViewWithActionItems: React.FunctionComponent = () => { |
| 16 |
const [activeItems, setActiveItems] = useState<TreeViewDataItem[]>(); |
| 17 |
const [isOpen, setIsOpen] = useState<boolean>(false); |
| 18 |
|
| 19 |
const onSelect = (_event: React.MouseEvent, treeViewItem: TreeViewDataItem) => { |
| 20 |
// Ignore folders for selection |
| 21 |
if (treeViewItem && !treeViewItem.children) { |
| 22 |
setActiveItems([treeViewItem]); |
| 23 |
} |
| 24 |
}; |
| 25 |
|
| 26 |
const onToggle = () => { |
| 27 |
setIsOpen((prevIsOpen) => !prevIsOpen); |
| 28 |
}; |
| 29 |
|
| 30 |
const onAppLaunchSelect = () => { |
| 31 |
setIsOpen((prevIsOpen) => !prevIsOpen); |
| 32 |
}; |
| 33 |
|
| 34 |
const options = [ |
| 35 |
{ |
| 36 |
name: 'Application launcher', |
| 37 |
id: 'example7-AppLaunch', |
| 38 |
action: ( |
| 39 |
<Dropdown |
| 40 |
onSelect={onAppLaunchSelect} |
| 41 |
isOpen={isOpen} |
| 42 |
onOpenChange={(isOpen) => setIsOpen(isOpen)} |
| 43 |
toggle={(toggleRef) => ( |
| 44 |
<MenuToggle |
| 45 |
ref={toggleRef} |
| 46 |
isExpanded={isOpen} |
| 47 |
onClick={onToggle} |
| 48 |
variant="plain" |
| 49 |
aria-label="Tree view with actions example kebab toggle" |
| 50 |
icon={<RhUiEllipsisVerticalFillIcon />} |
| 51 |
/> |
| 52 |
)} |
| 53 |
> |
| 54 |
<DropdownList> |
| 55 |
<DropdownItem>Action</DropdownItem> |
| 56 |
<DropdownItem |
| 57 |
to="#default-link2" |
| 58 |
// Prevent the default onClick functionality for example purposes |
| 59 |
onClick={(ev) => ev.preventDefault()} |
| 60 |
> |
| 61 |
Link |
| 62 |
</DropdownItem> |
| 63 |
<DropdownItem isDisabled>Disabled Action</DropdownItem> |
| 64 |
<DropdownItem isDisabled to="#default-link4"> |
| 65 |
Disabled Link |
| 66 |
</DropdownItem> |
| 67 |
</DropdownList> |
| 68 |
</Dropdown> |
| 69 |
), |
| 70 |
children: [ |
| 71 |
{ |
| 72 |
name: 'Application 1', |
| 73 |
id: 'example7-App1', |
| 74 |
action: <Button variant="plain" aria-label="Launch app 1" icon={<RhUiClipboardFillIcon />} />, |
| 75 |
actionProps: { |
| 76 |
'aria-label': 'Launch app 1' |
| 77 |
}, |
| 78 |
children: [ |
| 79 |
{ name: 'Settings', id: 'example7-App1Settings' }, |
| 80 |
{ name: 'Current', id: 'example7-App1Current' } |
| 81 |
] |
| 82 |
}, |
| 83 |
{ |
| 84 |
name: 'Application 2', |
| 85 |
id: 'example7-App2', |
| 86 |
action: <Button variant="plain" aria-label="Launch app 2" icon={<RhUiStorageFillIcon />} />, |
| 87 |
children: [ |
| 88 |
{ name: 'Settings', id: 'example7-App2Settings' }, |
| 89 |
{ |
| 90 |
name: 'Loader', |
| 91 |
id: 'example7-App2Loader', |
| 92 |
children: [ |
| 93 |
{ name: 'Loading App 1', id: 'example7-LoadApp1' }, |
| 94 |
{ name: 'Loading App 2', id: 'example7-LoadApp2' }, |
| 95 |
{ name: 'Loading App 3', id: 'example7-LoadApp3' } |
| 96 |
] |
| 97 |
} |
| 98 |
] |
| 99 |
} |
| 100 |
], |
| 101 |
defaultExpanded: true |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: 'Cost management', |
| 105 |
id: 'example7-Cost', |
| 106 |
children: [ |
| 107 |
{ |
| 108 |
name: 'Application 3', |
| 109 |
id: 'example7-App3', |
| 110 |
children: [ |
| 111 |
{ name: 'Settings', id: 'example7-App3Settings' }, |
| 112 |
{ name: 'Current', id: 'example7-App3Current' } |
| 113 |
] |
| 114 |
} |
| 115 |
] |
| 116 |
}, |
| 117 |
{ |
| 118 |
name: 'Sources', |
| 119 |
id: 'example7-Sources', |
| 120 |
children: [ |
| 121 |
{ name: 'Application 4', id: 'example7-App4', children: [{ name: 'Settings', id: 'example7-App4Settings' }] } |
| 122 |
] |
| 123 |
}, |
| 124 |
{ |
| 125 |
name: 'Really really really long folder name that overflows the container it is in', |
| 126 |
id: 'example7-Long', |
| 127 |
children: [{ name: 'Application 5', id: 'example7-App5' }] |
| 128 |
} |
| 129 |
]; |
| 130 |
return ( |
| 131 |
<TreeView |
| 132 |
hasAnimations |
| 133 |
aria-label="Tree View with actions example" |
| 134 |
data={options} |
| 135 |
activeItems={activeItems} |
| 136 |
onSelect={onSelect} |
| 137 |
/> |
| 138 |
); |
| 139 |
}; |