| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { |
| 3 |
Button, |
| 4 |
Toolbar, |
| 5 |
ToolbarContent, |
| 6 |
ToolbarGroup, |
| 7 |
ToolbarItem, |
| 8 |
Pagination, |
| 9 |
PageSection, |
| 10 |
MenuToggle, |
| 11 |
MenuToggleElement, |
| 12 |
Label, |
| 13 |
Select, |
| 14 |
SelectOption, |
| 15 |
PaginationVariant |
| 16 |
} from '@patternfly/react-core'; |
| 17 |
import { Table, TableText, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table'; |
| 18 |
import FilterIcon from '@patternfly/react-icons/dist/esm/icons/filter-icon'; |
| 19 |
import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper'; |
| 20 |
import { rows, columns } from '@patternfly/react-table/dist/esm/demos/sampleData'; |
| 21 |
|
| 22 |
export const TableStaticBottomPagination: React.FunctionComponent = () => { |
| 23 |
const [isSelectOpen, setIsSelectOpen] = useState(false); |
| 24 |
const [page, setPage] = useState(1); |
| 25 |
const [perPage, setPerPage] = useState(10); |
| 26 |
const [paginatedRows, setPaginatedRows] = useState(rows.slice(0, 10)); |
| 27 |
const handleSetPage = ( |
| 28 |
_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, |
| 29 |
newPage: number, |
| 30 |
_perPage: number, |
| 31 |
startIdx: number, |
| 32 |
endIdx: number |
| 33 |
) => { |
| 34 |
setPaginatedRows(rows.slice(startIdx, endIdx)); |
| 35 |
setPage(newPage); |
| 36 |
}; |
| 37 |
const handlePerPageSelect = ( |
| 38 |
_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, |
| 39 |
newPerPage: number, |
| 40 |
newPage: number, |
| 41 |
startIdx: number, |
| 42 |
endIdx: number |
| 43 |
) => { |
| 44 |
setPaginatedRows(rows.slice(startIdx, endIdx)); |
| 45 |
setPage(newPage); |
| 46 |
setPerPage(newPerPage); |
| 47 |
}; |
| 48 |
|
| 49 |
const renderPagination = (variant: PaginationVariant | 'bottom' | 'top', isCompact: boolean) => ( |
| 50 |
<Pagination |
| 51 |
isCompact={isCompact} |
| 52 |
itemCount={rows.length} |
| 53 |
page={page} |
| 54 |
perPage={perPage} |
| 55 |
onSetPage={handleSetPage} |
| 56 |
onPerPageSelect={handlePerPageSelect} |
| 57 |
variant={variant} |
| 58 |
titles={{ |
| 59 |
paginationAriaLabel: `${variant} pagination` |
| 60 |
}} |
| 61 |
isStatic |
| 62 |
/> |
| 63 |
); |
| 64 |
|
| 65 |
const renderLabel = (labelText: string) => { |
| 66 |
switch (labelText) { |
| 67 |
case 'Running': |
| 68 |
return ( |
| 69 |
<span> |
| 70 |
<Label color="green">{labelText}</Label> |
| 71 |
</span> |
| 72 |
); |
| 73 |
case 'Stopped': |
| 74 |
return ( |
| 75 |
<span> |
| 76 |
<Label color="orange">{labelText}</Label> |
| 77 |
</span> |
| 78 |
); |
| 79 |
case 'Needs Maintenance': |
| 80 |
return ( |
| 81 |
<span> |
| 82 |
<Label color="blue">{labelText}</Label> |
| 83 |
</span> |
| 84 |
); |
| 85 |
case 'Down': |
| 86 |
return ( |
| 87 |
<span> |
| 88 |
<Label color="red">{labelText}</Label> |
| 89 |
</span> |
| 90 |
); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
const tableToolbar = ( |
| 95 |
<Toolbar usePageInsets id="pagination-toolbar"> |
| 96 |
<ToolbarContent> |
| 97 |
<ToolbarItem> |
| 98 |
<Select |
| 99 |
id="select-example" |
| 100 |
aria-label="Select Input" |
| 101 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 102 |
<MenuToggle |
| 103 |
icon={<FilterIcon />} |
| 104 |
ref={toggleRef} |
| 105 |
onClick={() => setIsSelectOpen(!isSelectOpen)} |
| 106 |
isExpanded={isSelectOpen} |
| 107 |
> |
| 108 |
Status |
| 109 |
</MenuToggle> |
| 110 |
)} |
| 111 |
isOpen={isSelectOpen} |
| 112 |
onOpenChange={(isOpen: boolean) => setIsSelectOpen(isOpen)} |
| 113 |
onSelect={() => setIsSelectOpen(!isSelectOpen)} |
| 114 |
> |
| 115 |
{[ |
| 116 |
<SelectOption key={0} value="Debug"> |
| 117 |
Debug |
| 118 |
</SelectOption>, |
| 119 |
<SelectOption key={1} value="Info"> |
| 120 |
Info |
| 121 |
</SelectOption>, |
| 122 |
<SelectOption key={2} value="Warn"> |
| 123 |
Warn |
| 124 |
</SelectOption>, |
| 125 |
<SelectOption key={3} value="Error"> |
| 126 |
Error |
| 127 |
</SelectOption> |
| 128 |
]} |
| 129 |
</Select> |
| 130 |
</ToolbarItem> |
| 131 |
<ToolbarGroup> |
| 132 |
<ToolbarItem> |
| 133 |
<Button variant="primary">Action</Button> |
| 134 |
</ToolbarItem> |
| 135 |
</ToolbarGroup> |
| 136 |
<ToolbarItem variant="pagination">{renderPagination('top', true)}</ToolbarItem> |
| 137 |
</ToolbarContent> |
| 138 |
</Toolbar> |
| 139 |
); |
| 140 |
|
| 141 |
return ( |
| 142 |
<Fragment> |
| 143 |
<DashboardWrapper hasPageTemplateTitle> |
| 144 |
<PageSection isFilled aria-label="Paginated table data"> |
| 145 |
{tableToolbar} |
| 146 |
<Table variant="compact" aria-label="Paginated Table"> |
| 147 |
<Thead> |
| 148 |
<Tr> |
| 149 |
{columns.map((column, columnIndex) => ( |
| 150 |
<Th key={columnIndex}>{column}</Th> |
| 151 |
))} |
| 152 |
</Tr> |
| 153 |
</Thead> |
| 154 |
<Tbody> |
| 155 |
{paginatedRows.map((row, rowIndex) => ( |
| 156 |
<Tr key={rowIndex}> |
| 157 |
<Td dataLabel={columns[0]}>{row.name}</Td> |
| 158 |
<Td dataLabel={columns[1]}>{row.threads}</Td> |
| 159 |
<Td dataLabel={columns[2]}>{row.applications}</Td> |
| 160 |
<Td dataLabel={columns[3]}>{row.workspaces}</Td> |
| 161 |
<Td dataLabel={columns[4]}>{renderLabel(row.status)}</Td> |
| 162 |
<Td dataLabel={columns[5]}>{row.location}</Td> |
| 163 |
<Td dataLabel={columns[6]}>{row.lastModified}</Td> |
| 164 |
<Td dataLabel={columns[7]} modifier="truncate"> |
| 165 |
<TableText> |
| 166 |
<a href="#">{row.url}</a> |
| 167 |
</TableText> |
| 168 |
</Td> |
| 169 |
</Tr> |
| 170 |
))} |
| 171 |
</Tbody> |
| 172 |
</Table> |
| 173 |
{renderPagination('bottom', false)} |
| 174 |
</PageSection> |
| 175 |
</DashboardWrapper> |
| 176 |
</Fragment> |
| 177 |
); |
| 178 |
}; |