| 1 |
import { useState } from 'react'; |
| 2 |
import { TreeView, TreeViewDataItem } from '@patternfly/react-core'; |
| 3 |
import GitlabIcon from '@patternfly/react-icons/dist/esm/icons/gitlab-icon'; |
| 4 |
import GithubIcon from '@patternfly/react-icons/dist/esm/icons/github-icon'; |
| 5 |
import GoogleIcon from '@patternfly/react-icons/dist/esm/icons/google-icon'; |
| 6 |
|
| 7 |
export const TreeViewWithIconPerItem: React.FunctionComponent = () => { |
| 8 |
const [activeItems, setActiveItems] = useState<TreeViewDataItem[]>(); |
| 9 |
|
| 10 |
const onSelect = (_event: React.MouseEvent, treeViewItem: TreeViewDataItem) => { |
| 11 |
// Ignore folders for selection |
| 12 |
if (treeViewItem && !treeViewItem.children) { |
| 13 |
setActiveItems([treeViewItem]); |
| 14 |
} |
| 15 |
}; |
| 16 |
const options: TreeViewDataItem[] = [ |
| 17 |
{ |
| 18 |
name: 'Github accounts', |
| 19 |
id: 'iconPerItem-Github', |
| 20 |
icon: <GithubIcon />, |
| 21 |
children: [ |
| 22 |
{ |
| 23 |
name: 'Account 1', |
| 24 |
id: 'iconPerItem-Acc1' |
| 25 |
}, |
| 26 |
{ |
| 27 |
name: 'Account 2', |
| 28 |
id: 'iconPerItem-Acc2' |
| 29 |
} |
| 30 |
], |
| 31 |
defaultExpanded: true |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: 'Gitlab accounts', |
| 35 |
id: 'iconPerItem-Gitlab', |
| 36 |
icon: <GitlabIcon />, |
| 37 |
children: [ |
| 38 |
{ |
| 39 |
name: 'Account 3', |
| 40 |
id: 'iconPerItem-Acc3' |
| 41 |
} |
| 42 |
] |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: 'Google accounts', |
| 46 |
id: 'iconPerItem-Google', |
| 47 |
icon: <GoogleIcon />, |
| 48 |
children: [ |
| 49 |
{ |
| 50 |
name: 'Account 4', |
| 51 |
id: 'iconPerItem-Acc4' |
| 52 |
} |
| 53 |
] |
| 54 |
} |
| 55 |
]; |
| 56 |
return ( |
| 57 |
<TreeView |
| 58 |
hasAnimations |
| 59 |
aria-label="Tree View with unique icon per item example" |
| 60 |
data={options} |
| 61 |
activeItems={activeItems} |
| 62 |
onSelect={onSelect} |
| 63 |
/> |
| 64 |
); |
| 65 |
}; |