| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { |
| 3 |
Button, |
| 4 |
ButtonVariant, |
| 5 |
Select, |
| 6 |
SelectList, |
| 7 |
SelectOption, |
| 8 |
Pagination, |
| 9 |
Dropdown, |
| 10 |
DropdownItem, |
| 11 |
DropdownList, |
| 12 |
Divider, |
| 13 |
MenuToggle, |
| 14 |
MenuToggleCheckbox, |
| 15 |
MenuToggleElement, |
| 16 |
OverflowMenu, |
| 17 |
OverflowMenuContent, |
| 18 |
OverflowMenuControl, |
| 19 |
OverflowMenuGroup, |
| 20 |
OverflowMenuItem, |
| 21 |
Toolbar, |
| 22 |
ToolbarContent, |
| 23 |
ToolbarToggleGroup, |
| 24 |
ToolbarItem |
| 25 |
} from '@patternfly/react-core'; |
| 26 |
import FilterIcon from '@patternfly/react-icons/dist/esm/icons/filter-icon'; |
| 27 |
import RhUiEllipsisVerticalFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-ellipsis-vertical-fill-icon'; |
| 28 |
|
| 29 |
export const ToolbarStacked: React.FunctionComponent = () => { |
| 30 |
// toggle group - three option menus with labels, two icon buttons, Kebab menu - right aligned |
| 31 |
// pagination - right aligned |
| 32 |
const resourceOptions = ['All resources', 'Deployment', 'Pod']; |
| 33 |
const statusOptions = ['New', 'Pending', 'Running', 'Cancelled']; |
| 34 |
|
| 35 |
const [kebabIsOpen, setKebabIsOpen] = useState(false); |
| 36 |
const [resourceIsExpanded, setResourceIsExpanded] = useState(false); |
| 37 |
const [resourceSelected, setResourceSelected] = useState(''); |
| 38 |
const [statusIsExpanded, setStatusIsExpanded] = useState(false); |
| 39 |
const [statusSelected, setStatusSelected] = useState(''); |
| 40 |
const [isSplitButtonDropdownOpen, setIsSplitButtonDropdownOpen] = useState(false); |
| 41 |
const [page, setPage] = useState(1); |
| 42 |
const [perPage, setPerPage] = useState(20); |
| 43 |
|
| 44 |
const onKebabToggle = () => { |
| 45 |
setKebabIsOpen(!kebabIsOpen); |
| 46 |
}; |
| 47 |
|
| 48 |
const onResourceToggle = () => { |
| 49 |
setResourceIsExpanded(!resourceIsExpanded); |
| 50 |
}; |
| 51 |
|
| 52 |
const onResourceSelect = (_event: React.ChangeEvent | React.MouseEvent, selection: string) => { |
| 53 |
setResourceSelected(selection); |
| 54 |
setResourceIsExpanded(false); |
| 55 |
}; |
| 56 |
|
| 57 |
const onResourceSelectDropdown = () => { |
| 58 |
setResourceIsExpanded(false); |
| 59 |
}; |
| 60 |
|
| 61 |
const onStatusToggle = () => { |
| 62 |
setStatusIsExpanded(!statusIsExpanded); |
| 63 |
}; |
| 64 |
|
| 65 |
const onStatusSelect = (_event: React.ChangeEvent | React.MouseEvent, selection: string) => { |
| 66 |
setStatusSelected(selection); |
| 67 |
setStatusIsExpanded(false); |
| 68 |
}; |
| 69 |
|
| 70 |
const onSetPage = (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent, pageNumber: number) => { |
| 71 |
setPage(pageNumber); |
| 72 |
}; |
| 73 |
|
| 74 |
const onPerPageSelect = ( |
| 75 |
_event: React.MouseEvent | React.KeyboardEvent | MouseEvent, |
| 76 |
perPage: number, |
| 77 |
page: number |
| 78 |
) => { |
| 79 |
setPage(page); |
| 80 |
setPerPage(perPage); |
| 81 |
}; |
| 82 |
|
| 83 |
const onSplitButtonToggle = () => { |
| 84 |
setIsSplitButtonDropdownOpen(!isSplitButtonDropdownOpen); |
| 85 |
}; |
| 86 |
|
| 87 |
const onSplitButtonSelect = () => { |
| 88 |
setIsSplitButtonDropdownOpen(!isSplitButtonDropdownOpen); |
| 89 |
}; |
| 90 |
|
| 91 |
const dropdownItems = ( |
| 92 |
<> |
| 93 |
<DropdownItem>Action</DropdownItem> |
| 94 |
<DropdownItem |
| 95 |
to="#default-link2" |
| 96 |
// Prevent the default onClick functionality for example purposes |
| 97 |
onClick={(ev: any) => ev.preventDefault()} |
| 98 |
> |
| 99 |
Link |
| 100 |
</DropdownItem> |
| 101 |
<DropdownItem isDisabled>Disabled Action</DropdownItem> |
| 102 |
<DropdownItem isDisabled to="#default-link4"> |
| 103 |
Disabled Link |
| 104 |
</DropdownItem> |
| 105 |
</> |
| 106 |
); |
| 107 |
const splitButtonDropdownItems = ( |
| 108 |
<> |
| 109 |
<DropdownItem value={0} key="action"> |
| 110 |
Action |
| 111 |
</DropdownItem> |
| 112 |
<DropdownItem |
| 113 |
value={1} |
| 114 |
key="link" |
| 115 |
to="#default-link2" |
| 116 |
// Prevent the default onClick functionality for example purposes |
| 117 |
onClick={(ev: any) => ev.preventDefault()} |
| 118 |
> |
| 119 |
Link |
| 120 |
</DropdownItem> |
| 121 |
<DropdownItem value={2} isDisabled key="disabled action"> |
| 122 |
Disabled Action |
| 123 |
</DropdownItem> |
| 124 |
<DropdownItem value={3} isDisabled key="disabled link" to="#default-link4"> |
| 125 |
Disabled Link |
| 126 |
</DropdownItem> |
| 127 |
</> |
| 128 |
); |
| 129 |
|
| 130 |
const toggleGroupItems = ( |
| 131 |
<Fragment> |
| 132 |
<ToolbarItem variant="label" id="stacked-example-resource-select"> |
| 133 |
Resource |
| 134 |
</ToolbarItem> |
| 135 |
<ToolbarItem> |
| 136 |
<Select |
| 137 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 138 |
<MenuToggle |
| 139 |
ref={toggleRef} |
| 140 |
onClick={() => onResourceToggle()} |
| 141 |
isExpanded={resourceIsExpanded} |
| 142 |
style={ |
| 143 |
{ |
| 144 |
width: '150px' |
| 145 |
} as React.CSSProperties |
| 146 |
} |
| 147 |
> |
| 148 |
{resourceSelected || 'Resource'} |
| 149 |
</MenuToggle> |
| 150 |
)} |
| 151 |
onSelect={onResourceSelect} |
| 152 |
selected={resourceSelected} |
| 153 |
onOpenChange={(isOpen) => setResourceIsExpanded(isOpen)} |
| 154 |
isOpen={resourceIsExpanded} |
| 155 |
aria-labelledby="stacked-example-resource-select" |
| 156 |
> |
| 157 |
<SelectList> |
| 158 |
{resourceOptions.map((option, index) => ( |
| 159 |
<SelectOption key={index} value={option}> |
| 160 |
{option} |
| 161 |
</SelectOption> |
| 162 |
))} |
| 163 |
</SelectList> |
| 164 |
</Select> |
| 165 |
</ToolbarItem> |
| 166 |
<ToolbarItem variant="label" id="stacked-example-status-select"> |
| 167 |
Status |
| 168 |
</ToolbarItem> |
| 169 |
<ToolbarItem> |
| 170 |
<Select |
| 171 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 172 |
<MenuToggle |
| 173 |
ref={toggleRef} |
| 174 |
onClick={() => onStatusToggle()} |
| 175 |
isExpanded={statusIsExpanded} |
| 176 |
style={ |
| 177 |
{ |
| 178 |
width: '150px' |
| 179 |
} as React.CSSProperties |
| 180 |
} |
| 181 |
> |
| 182 |
{statusSelected || 'Status'} |
| 183 |
</MenuToggle> |
| 184 |
)} |
| 185 |
onSelect={onStatusSelect} |
| 186 |
onOpenChange={(isOpen) => setStatusIsExpanded(isOpen)} |
| 187 |
selected={statusSelected} |
| 188 |
isOpen={statusIsExpanded} |
| 189 |
> |
| 190 |
<SelectList> |
| 191 |
{statusOptions.map((option, index) => ( |
| 192 |
<SelectOption key={index} value={option}> |
| 193 |
{option} |
| 194 |
</SelectOption> |
| 195 |
))} |
| 196 |
</SelectList> |
| 197 |
</Select> |
| 198 |
</ToolbarItem> |
| 199 |
</Fragment> |
| 200 |
); |
| 201 |
|
| 202 |
const firstRowItems = ( |
| 203 |
<Fragment> |
| 204 |
<Toolbar> |
| 205 |
<ToolbarContent> |
| 206 |
<ToolbarToggleGroup variant="label-group" toggleIcon={<FilterIcon />} breakpoint="lg"> |
| 207 |
{toggleGroupItems} |
| 208 |
</ToolbarToggleGroup> |
| 209 |
<ToolbarItem> |
| 210 |
<OverflowMenu breakpoint="xl"> |
| 211 |
<OverflowMenuContent> |
| 212 |
<OverflowMenuGroup groupType="button"> |
| 213 |
<OverflowMenuItem> |
| 214 |
<Button variant={ButtonVariant.primary}>Primary</Button> |
| 215 |
</OverflowMenuItem> |
| 216 |
<OverflowMenuItem> |
| 217 |
<Button variant={ButtonVariant.secondary}>Secondary</Button> |
| 218 |
</OverflowMenuItem> |
| 219 |
</OverflowMenuGroup> |
| 220 |
</OverflowMenuContent> |
| 221 |
<OverflowMenuControl hasAdditionalOptions> |
| 222 |
<Dropdown |
| 223 |
onSelect={onResourceSelectDropdown} |
| 224 |
onOpenChange={(isOpen: boolean) => setKebabIsOpen(isOpen)} |
| 225 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 226 |
<MenuToggle |
| 227 |
ref={toggleRef} |
| 228 |
aria-label="Kebab overflow menu" |
| 229 |
variant="plain" |
| 230 |
onClick={onKebabToggle} |
| 231 |
isExpanded={kebabIsOpen} |
| 232 |
icon={<RhUiEllipsisVerticalFillIcon />} |
| 233 |
/> |
| 234 |
)} |
| 235 |
isOpen={kebabIsOpen} |
| 236 |
> |
| 237 |
<DropdownList>{dropdownItems}</DropdownList> |
| 238 |
</Dropdown> |
| 239 |
</OverflowMenuControl> |
| 240 |
</OverflowMenu> |
| 241 |
</ToolbarItem> |
| 242 |
</ToolbarContent> |
| 243 |
</Toolbar> |
| 244 |
</Fragment> |
| 245 |
); |
| 246 |
|
| 247 |
const secondRowItems = ( |
| 248 |
<Fragment> |
| 249 |
<Toolbar> |
| 250 |
<ToolbarContent> |
| 251 |
<ToolbarItem> |
| 252 |
<Dropdown |
| 253 |
onSelect={onSplitButtonSelect} |
| 254 |
isOpen={isSplitButtonDropdownOpen} |
| 255 |
onOpenChange={(isOpen: boolean) => setIsSplitButtonDropdownOpen(isOpen)} |
| 256 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 257 |
<MenuToggle |
| 258 |
ref={toggleRef} |
| 259 |
isExpanded={isSplitButtonDropdownOpen} |
| 260 |
onClick={onSplitButtonToggle} |
| 261 |
aria-label="Toolbar stacked example split toggle" |
| 262 |
splitButtonItems={[ |
| 263 |
<MenuToggleCheckbox |
| 264 |
key="toolbar-stacked-split-button-checkbox-1" |
| 265 |
id="toolbar-stacked-split-button-checkbox-1" |
| 266 |
aria-label="Select all" |
| 267 |
/> |
| 268 |
]} |
| 269 |
/> |
| 270 |
)} |
| 271 |
> |
| 272 |
<DropdownList>{splitButtonDropdownItems}</DropdownList> |
| 273 |
</Dropdown> |
| 274 |
</ToolbarItem> |
| 275 |
<ToolbarItem variant="pagination" align={{ default: 'alignEnd' }}> |
| 276 |
<Pagination |
| 277 |
itemCount={37} |
| 278 |
perPage={perPage} |
| 279 |
page={page} |
| 280 |
onSetPage={onSetPage} |
| 281 |
widgetId="pagination-options-menu-top" |
| 282 |
onPerPageSelect={onPerPageSelect} |
| 283 |
/> |
| 284 |
</ToolbarItem> |
| 285 |
</ToolbarContent> |
| 286 |
</Toolbar> |
| 287 |
</Fragment> |
| 288 |
); |
| 289 |
|
| 290 |
return ( |
| 291 |
<Fragment> |
| 292 |
{firstRowItems} |
| 293 |
<Divider /> |
| 294 |
{secondRowItems} |
| 295 |
</Fragment> |
| 296 |
); |
| 297 |
}; |