| 1 |
import { useState } from 'react'; |
| 2 |
import { Nav, NavExpandable, NavItem, NavList } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const NavExpandableExample: React.FunctionComponent = () => { |
| 5 |
const [activeGroup, setActiveGroup] = useState('nav-expandable-group-1'); |
| 6 |
const [activeItem, setActiveItem] = useState('nav-expandable-group-1_item-1'); |
| 7 |
|
| 8 |
const onSelect = ( |
| 9 |
_event: React.FormEvent<HTMLInputElement>, |
| 10 |
result: { itemId: number | string; groupId: number | string } |
| 11 |
) => { |
| 12 |
setActiveGroup(result.groupId as string); |
| 13 |
setActiveItem(result.itemId as string); |
| 14 |
}; |
| 15 |
|
| 16 |
const onToggle = ( |
| 17 |
_event: React.MouseEvent<HTMLButtonElement>, |
| 18 |
result: { groupId: number | string; isExpanded: boolean } |
| 19 |
) => { |
| 20 |
// eslint-disable-next-line no-console |
| 21 |
console.log(`Group ${result.groupId} expanded? ${result.isExpanded}`); |
| 22 |
}; |
| 23 |
|
| 24 |
const onItemClick = ( |
| 25 |
_event: React.FormEvent<HTMLInputElement>, |
| 26 |
itemId: number | string, |
| 27 |
_groupId: number | string |
| 28 |
) => { |
| 29 |
// eslint-disable-next-line no-console |
| 30 |
console.log(`Custom click handler on ${itemId}`); |
| 31 |
}; |
| 32 |
|
| 33 |
return ( |
| 34 |
<Nav onSelect={onSelect} onToggle={onToggle} aria-label="Expandable global"> |
| 35 |
<NavList> |
| 36 |
<NavExpandable |
| 37 |
title="Expandable Group 1" |
| 38 |
groupId="nav-expandable-group-1" |
| 39 |
isActive={activeGroup === 'nav-expandable-group-1'} |
| 40 |
isExpanded |
| 41 |
> |
| 42 |
<NavItem |
| 43 |
preventDefault |
| 44 |
id="expandable-1" |
| 45 |
to="#expandable-1" |
| 46 |
groupId="nav-expandable-group-1" |
| 47 |
itemId="nav-expandable-group-1_item-1" |
| 48 |
isActive={activeItem === 'nav-expandable-group-1_item-1'} |
| 49 |
> |
| 50 |
Subnav 1 Link 1 |
| 51 |
</NavItem> |
| 52 |
<NavItem |
| 53 |
preventDefault |
| 54 |
id="expandable-2" |
| 55 |
to="#expandable-2" |
| 56 |
groupId="nav-expandable-group-1" |
| 57 |
itemId="nav-expandable-group-1_item-2" |
| 58 |
isActive={activeItem === 'nav-expandable-group-1_item-2'} |
| 59 |
> |
| 60 |
Subnav 1 Link 2 |
| 61 |
</NavItem> |
| 62 |
<NavItem |
| 63 |
preventDefault |
| 64 |
id="expandable-3" |
| 65 |
to="#expandable-3" |
| 66 |
groupId="nav-expandable-group-1" |
| 67 |
itemId="nav-expandable-group-1_item-3" |
| 68 |
isActive={activeItem === 'nav-expandable-group-1_item-3'} |
| 69 |
> |
| 70 |
Subnav 1 Link 3 |
| 71 |
</NavItem> |
| 72 |
</NavExpandable> |
| 73 |
<NavExpandable |
| 74 |
title="Expandable Group 2" |
| 75 |
groupId="nav-expandable-group-2" |
| 76 |
isActive={activeGroup === 'nav-expandable-group-2'} |
| 77 |
isExpanded |
| 78 |
> |
| 79 |
<NavItem |
| 80 |
preventDefault |
| 81 |
onClick={onItemClick} |
| 82 |
id="expandable-custom-click" |
| 83 |
to="#expandable-custom-click" |
| 84 |
groupId="nav-expandable-group-2" |
| 85 |
itemId="nav-expandable-group-2_custom-click" |
| 86 |
isActive={activeItem === 'nav-expandable-group-2_custom-click'} |
| 87 |
> |
| 88 |
Custom onClick Link |
| 89 |
</NavItem> |
| 90 |
<NavItem |
| 91 |
preventDefault |
| 92 |
id="expandable-4" |
| 93 |
to="#expandable-4" |
| 94 |
groupId="nav-expandable-group-2" |
| 95 |
itemId="nav-expandable-group-2_item-1" |
| 96 |
isActive={activeItem === 'nav-expandable-group-2_item-1'} |
| 97 |
> |
| 98 |
Subnav 2 Link 1 |
| 99 |
</NavItem> |
| 100 |
<NavItem |
| 101 |
preventDefault |
| 102 |
id="expandable-5" |
| 103 |
to="#expandable-5" |
| 104 |
groupId="nav-expandable-group-2" |
| 105 |
itemId="nav-expandable-group-2_item-2" |
| 106 |
isActive={activeItem === 'nav-expandable-group-2_item-2'} |
| 107 |
> |
| 108 |
Subnav 2 Link 2 |
| 109 |
</NavItem> |
| 110 |
<NavItem |
| 111 |
preventDefault |
| 112 |
id="expandable-6" |
| 113 |
to="#expandable-6" |
| 114 |
groupId="nav-expandable-group-2" |
| 115 |
itemId="nav-expandable-group-2_item-3" |
| 116 |
isActive={activeItem === 'nav-expandable-group-2_item-3'} |
| 117 |
> |
| 118 |
Subnav 2 Link 3 |
| 119 |
</NavItem> |
| 120 |
</NavExpandable> |
| 121 |
</NavList> |
| 122 |
</Nav> |
| 123 |
); |
| 124 |
}; |