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