| 1 |
import { Fragment, useEffect, useState } from 'react'; |
| 2 |
import { |
| 3 |
Button, |
| 4 |
Content, |
| 5 |
Dropdown, |
| 6 |
DropdownList, |
| 7 |
Flex, |
| 8 |
FlexItem, |
| 9 |
MenuToggle, |
| 10 |
MenuToggleElement, |
| 11 |
PageSection, |
| 12 |
Pagination, |
| 13 |
SelectOption, |
| 14 |
SelectList, |
| 15 |
SelectGroup, |
| 16 |
Toolbar, |
| 17 |
ToolbarContent, |
| 18 |
ToolbarGroup, |
| 19 |
ToolbarItem, |
| 20 |
OverflowMenuDropdownItem, |
| 21 |
PaginationVariant, |
| 22 |
Label, |
| 23 |
Select, |
| 24 |
OverflowMenu, |
| 25 |
OverflowMenuContent, |
| 26 |
OverflowMenuControl, |
| 27 |
OverflowMenuGroup, |
| 28 |
OverflowMenuItem, |
| 29 |
PageSectionVariants |
| 30 |
} from '@patternfly/react-core'; |
| 31 |
import { Table, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table'; |
| 32 |
import CloneIcon from '@patternfly/react-icons/dist/esm/icons/clone-icon'; |
| 33 |
import EditIcon from '@patternfly/react-icons/dist/esm/icons/edit-icon'; |
| 34 |
import SyncIcon from '@patternfly/react-icons/dist/esm/icons/sync-icon'; |
| 35 |
import RhUiCodeIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-code-icon'; |
| 36 |
import RhMicronsSortDownLargeToSmallIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-sort-down-large-to-small-icon'; |
| 37 |
import RhUiBranchFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-branch-fill-icon'; |
| 38 |
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon'; |
| 39 |
import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper'; |
| 40 |
import RhUiEllipsisVerticalFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-ellipsis-vertical-fill-icon'; |
| 41 |
import { rows, columns, SampleDataRow } from '@patternfly/react-table/dist/esm/demos/sampleData'; |
| 42 |
|
| 43 |
type Direction = 'asc' | 'desc' | undefined; |
| 44 |
|
| 45 |
export const TableSortableResponsive: React.FunctionComponent = () => { |
| 46 |
const [isKebabDropdownOpen, setIsKebabDropdownOpen] = useState(false); |
| 47 |
|
| 48 |
const sortRows = (rows: SampleDataRow[], sortIndex: number, sortDirection: Direction) => |
| 49 |
[...rows].sort((a, b) => { |
| 50 |
let returnValue = 0; |
| 51 |
if (typeof Object.values(a)[sortIndex] === 'number') { |
| 52 |
// numeric sort |
| 53 |
returnValue = Object.values(a)[sortIndex] - Object.values(b)[sortIndex]; |
| 54 |
} else { |
| 55 |
// string sort using natural sort |
| 56 |
returnValue = Object.values(a)[sortIndex].localeCompare(Object.values(b)[sortIndex], undefined, { |
| 57 |
numeric: true |
| 58 |
}); |
| 59 |
} |
| 60 |
if (sortDirection === 'desc') { |
| 61 |
return returnValue * -1; |
| 62 |
} |
| 63 |
return returnValue; |
| 64 |
}); |
| 65 |
|
| 66 |
const [sortedData, setSortedData] = useState([...sortRows(rows, 0, 'asc')]); |
| 67 |
const [sortedRows, setSortedRows] = useState([...sortedData]); |
| 68 |
const [page, setPage] = useState(1); |
| 69 |
const [perPage, setPerPage] = useState(10); |
| 70 |
|
| 71 |
// index of the currently active column |
| 72 |
const [activeSortIndex, setActiveSortIndex] = useState(0); |
| 73 |
// sort direction of the currently active column |
| 74 |
const [activeSortDirection, setActiveSortDirection] = useState<Direction>('asc'); |
| 75 |
// sort dropdown expansion |
| 76 |
const [isSortDropdownOpen, setIsSortDropdownOpen] = useState(false); |
| 77 |
|
| 78 |
const onSort = (_event: any, index: number, direction: Direction) => { |
| 79 |
setActiveSortIndex(index); |
| 80 |
setActiveSortDirection(direction); |
| 81 |
|
| 82 |
setSortedData(sortRows(rows, index, direction)); |
| 83 |
}; |
| 84 |
|
| 85 |
const kebabDropdownItems = [<OverflowMenuDropdownItem key="kebab-1">Some action</OverflowMenuDropdownItem>]; |
| 86 |
|
| 87 |
useEffect(() => { |
| 88 |
setSortedRows(sortedData.slice((page - 1) * perPage, page * perPage)); |
| 89 |
}, [sortedData, page, perPage]); |
| 90 |
|
| 91 |
const handleSetPage = (_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, newPage: number) => { |
| 92 |
setPage(newPage); |
| 93 |
}; |
| 94 |
|
| 95 |
const handlePerPageSelect = (_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, newPerPage: number) => { |
| 96 |
setPerPage(newPerPage); |
| 97 |
}; |
| 98 |
|
| 99 |
const renderPagination = (variant: 'top' | 'bottom' | PaginationVariant, isCompact: boolean) => ( |
| 100 |
<Pagination |
| 101 |
isCompact={isCompact} |
| 102 |
itemCount={rows.length} |
| 103 |
page={page} |
| 104 |
perPage={perPage} |
| 105 |
onSetPage={handleSetPage} |
| 106 |
onPerPageSelect={handlePerPageSelect} |
| 107 |
perPageOptions={[ |
| 108 |
{ title: '10', value: 10 }, |
| 109 |
{ title: '20', value: 20 }, |
| 110 |
{ title: '50', value: 50 }, |
| 111 |
{ title: '100', value: 100 } |
| 112 |
]} |
| 113 |
variant={variant} |
| 114 |
titles={{ |
| 115 |
paginationAriaLabel: `${variant} pagination` |
| 116 |
}} |
| 117 |
/> |
| 118 |
); |
| 119 |
|
| 120 |
const renderLabel = (labelText: string) => { |
| 121 |
switch (labelText) { |
| 122 |
case 'Running': |
| 123 |
return <Label color="green">{labelText}</Label>; |
| 124 |
case 'Stopped': |
| 125 |
return <Label color="orange">{labelText}</Label>; |
| 126 |
case 'Needs Maintenance': |
| 127 |
return <Label color="blue">{labelText}</Label>; |
| 128 |
case 'Down': |
| 129 |
return <Label color="red">{labelText}</Label>; |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
const tableToolbar = ( |
| 134 |
<Toolbar id="sortable-toolbar"> |
| 135 |
<ToolbarContent> |
| 136 |
<ToolbarItem visibility={{ md: 'hidden' }}> |
| 137 |
<Select |
| 138 |
isOpen={isSortDropdownOpen} |
| 139 |
selected={[activeSortDirection, activeSortIndex]} |
| 140 |
onOpenChange={(isOpen: boolean) => setIsSortDropdownOpen(isOpen)} |
| 141 |
onSelect={(event: React.MouseEvent<Element, MouseEvent>, value: string | number) => { |
| 142 |
if (value === 'asc' || value === 'desc') { |
| 143 |
onSort(event, activeSortIndex, value); |
| 144 |
} else { |
| 145 |
onSort(event, value as number, activeSortDirection ?? 'asc'); |
| 146 |
} |
| 147 |
}} |
| 148 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 149 |
<MenuToggle |
| 150 |
ref={toggleRef} |
| 151 |
onClick={() => setIsSortDropdownOpen(!isSortDropdownOpen)} |
| 152 |
isExpanded={isSortDropdownOpen} |
| 153 |
variant="plain" |
| 154 |
icon={<RhMicronsSortDownLargeToSmallIcon />} |
| 155 |
/> |
| 156 |
)} |
| 157 |
> |
| 158 |
<SelectGroup label="Sort column"> |
| 159 |
<SelectList> |
| 160 |
{columns.map((column, columnIndex) => ( |
| 161 |
<SelectOption key={column} value={columnIndex} isSelected={activeSortIndex === columnIndex}> |
| 162 |
{column} |
| 163 |
</SelectOption> |
| 164 |
))} |
| 165 |
</SelectList> |
| 166 |
</SelectGroup> |
| 167 |
<SelectGroup label="Sort direction"> |
| 168 |
<SelectList> |
| 169 |
<SelectOption isSelected={activeSortDirection === 'asc'} value="asc" key="ascending"> |
| 170 |
Ascending |
| 171 |
</SelectOption> |
| 172 |
<SelectOption isSelected={activeSortDirection === 'desc'} value="desc" key="descending"> |
| 173 |
Descending |
| 174 |
</SelectOption> |
| 175 |
</SelectList> |
| 176 |
</SelectGroup> |
| 177 |
</Select> |
| 178 |
</ToolbarItem> |
| 179 |
<ToolbarItem> |
| 180 |
<OverflowMenu breakpoint="lg"> |
| 181 |
<OverflowMenuContent isPersistent> |
| 182 |
<OverflowMenuGroup isPersistent groupType="button"> |
| 183 |
<OverflowMenuItem> |
| 184 |
<Button variant="primary">Create instance</Button> |
| 185 |
</OverflowMenuItem> |
| 186 |
<OverflowMenuItem> |
| 187 |
<Button variant="secondary">Action</Button> |
| 188 |
</OverflowMenuItem> |
| 189 |
</OverflowMenuGroup> |
| 190 |
</OverflowMenuContent> |
| 191 |
<OverflowMenuControl hasAdditionalOptions> |
| 192 |
<Dropdown |
| 193 |
onSelect={() => setIsKebabDropdownOpen(!isKebabDropdownOpen)} |
| 194 |
onOpenChange={(isKebabDropdownOpen: boolean) => setIsKebabDropdownOpen(isKebabDropdownOpen)} |
| 195 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 196 |
<MenuToggle |
| 197 |
ref={toggleRef} |
| 198 |
aria-label="overflow menu" |
| 199 |
variant="plain" |
| 200 |
onClick={() => setIsKebabDropdownOpen(!isKebabDropdownOpen)} |
| 201 |
isExpanded={false} |
| 202 |
icon={<RhUiEllipsisVerticalFillIcon />} |
| 203 |
/> |
| 204 |
)} |
| 205 |
isOpen={isKebabDropdownOpen} |
| 206 |
> |
| 207 |
<DropdownList>{kebabDropdownItems}</DropdownList> |
| 208 |
</Dropdown> |
| 209 |
</OverflowMenuControl> |
| 210 |
</OverflowMenu> |
| 211 |
</ToolbarItem> |
| 212 |
<ToolbarGroup variant="action-group-plain"> |
| 213 |
<ToolbarItem> |
| 214 |
<Button aria-label="Edit" variant="plain" icon={<EditIcon />} /> |
| 215 |
</ToolbarItem> |
| 216 |
<ToolbarItem> |
| 217 |
<Button aria-label="Clone" variant="plain" icon={<CloneIcon />} /> |
| 218 |
</ToolbarItem> |
| 219 |
<ToolbarItem> |
| 220 |
<Button aria-label="Sync" variant="plain" icon={<SyncIcon />} /> |
| 221 |
</ToolbarItem> |
| 222 |
</ToolbarGroup> |
| 223 |
<ToolbarItem variant="pagination">{renderPagination('top', true)}</ToolbarItem> |
| 224 |
</ToolbarContent> |
| 225 |
</Toolbar> |
| 226 |
); |
| 227 |
|
| 228 |
return ( |
| 229 |
<Fragment> |
| 230 |
<DashboardWrapper> |
| 231 |
<PageSection isWidthLimited variant={PageSectionVariants.light} aria-labelledby="table-title"> |
| 232 |
<Content> |
| 233 |
<h1 id="table-title">Table demos</h1> |
| 234 |
<p> |
| 235 |
Below is an example of a responsive sortable table. When the screen size shrinks the table into a compact |
| 236 |
form, the toolbar will display a dropdown menu containing sorting options. |
| 237 |
</p> |
| 238 |
</Content> |
| 239 |
</PageSection> |
| 240 |
<PageSection |
| 241 |
padding={{ |
| 242 |
default: 'noPadding', |
| 243 |
xl: 'padding' |
| 244 |
}} |
| 245 |
aria-label="Sortable table data" |
| 246 |
> |
| 247 |
{tableToolbar} |
| 248 |
<Table aria-label="Sortable Table"> |
| 249 |
<Thead> |
| 250 |
<Tr> |
| 251 |
{columns.map((column, columnIndex) => { |
| 252 |
const sortParams = { |
| 253 |
sort: { |
| 254 |
sortBy: { |
| 255 |
index: activeSortIndex, |
| 256 |
direction: activeSortDirection |
| 257 |
}, |
| 258 |
onSort, |
| 259 |
columnIndex |
| 260 |
} |
| 261 |
}; |
| 262 |
return ( |
| 263 |
<Th modifier={columnIndex !== 6 ? 'wrap' : undefined} key={columnIndex} {...sortParams}> |
| 264 |
{column} |
| 265 |
</Th> |
| 266 |
); |
| 267 |
})} |
| 268 |
</Tr> |
| 269 |
</Thead> |
| 270 |
<Tbody> |
| 271 |
{sortedRows.map((row, rowIndex) => ( |
| 272 |
<Tr key={rowIndex}> |
| 273 |
<> |
| 274 |
<Td dataLabel={columns[0]} width={15}> |
| 275 |
<div>{row.name}</div> |
| 276 |
</Td> |
| 277 |
<Td dataLabel={columns[1]} width={10}> |
| 278 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 279 |
<FlexItem> |
| 280 |
<RhUiBranchFillIcon key="icon" /> |
| 281 |
</FlexItem> |
| 282 |
<FlexItem>{row.threads}</FlexItem> |
| 283 |
</Flex> |
| 284 |
</Td> |
| 285 |
<Td dataLabel={columns[2]} width={10}> |
| 286 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 287 |
<FlexItem> |
| 288 |
<RhUiCodeIcon key="icon" /> |
| 289 |
</FlexItem> |
| 290 |
<FlexItem>{row.applications}</FlexItem> |
| 291 |
</Flex> |
| 292 |
</Td> |
| 293 |
<Td dataLabel={columns[3]} width={10}> |
| 294 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 295 |
<FlexItem> |
| 296 |
<CubeIcon key="icon" /> |
| 297 |
</FlexItem> |
| 298 |
<FlexItem>{row.workspaces}</FlexItem> |
| 299 |
</Flex> |
| 300 |
</Td> |
| 301 |
<Td dataLabel={columns[4]} width={15}> |
| 302 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 303 |
<FlexItem>{renderLabel(row.status)}</FlexItem> |
| 304 |
</Flex> |
| 305 |
</Td> |
| 306 |
<Td dataLabel={columns[5]} width={10}> |
| 307 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 308 |
<FlexItem>{row.location}</FlexItem> |
| 309 |
</Flex> |
| 310 |
</Td> |
| 311 |
<Td dataLabel={columns[6]} width={10}> |
| 312 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 313 |
<FlexItem>{row.lastModified[0]} days ago</FlexItem> |
| 314 |
</Flex> |
| 315 |
</Td> |
| 316 |
<Td dataLabel={columns[7]} modifier="truncate"> |
| 317 |
<a href="#">{row.url}</a> |
| 318 |
</Td> |
| 319 |
</> |
| 320 |
</Tr> |
| 321 |
))} |
| 322 |
</Tbody> |
| 323 |
</Table> |
| 324 |
{renderPagination('bottom', false)} |
| 325 |
</PageSection> |
| 326 |
</DashboardWrapper> |
| 327 |
</Fragment> |
| 328 |
); |
| 329 |
}; |