| 1 |
import { useState } from 'react'; |
| 2 |
import { ActionsColumn, Table, Thead, Tr, Th, Tbody, Td, ExpandableRowContent } from '@patternfly/react-table'; |
| 3 |
import { |
| 4 |
Button, |
| 5 |
Flex, |
| 6 |
FlexItem, |
| 7 |
MenuToggle, |
| 8 |
MenuToggleElement, |
| 9 |
Toolbar, |
| 10 |
ToolbarContent, |
| 11 |
ToolbarGroup, |
| 12 |
ToolbarItem, |
| 13 |
Pagination, |
| 14 |
PageSection, |
| 15 |
Select, |
| 16 |
SelectOption, |
| 17 |
PaginationVariant |
| 18 |
} from '@patternfly/react-core'; |
| 19 |
import RhUiCodeIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-code-icon'; |
| 20 |
import RhUiBranchFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-branch-fill-icon'; |
| 21 |
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon'; |
| 22 |
import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper'; |
| 23 |
import FilterIcon from '@patternfly/react-icons/dist/esm/icons/filter-icon'; |
| 24 |
|
| 25 |
export const TableCompoundExpansion: React.FunctionComponent = () => { |
| 26 |
// In real usage, this data would come from some external source like an API via props. |
| 27 |
const [isSelectOpen, setIsSelectOpen] = useState(false); |
| 28 |
|
| 29 |
const NestedItemsTable = () => { |
| 30 |
// In real usage, this data would come from some external source like an API via props. |
| 31 |
const items = [ |
| 32 |
{ description: 'Item 1', date: 'May 9, 2018', status: 'Active' }, |
| 33 |
{ description: 'Item 2', date: 'May 9, 2018', status: 'Warning' }, |
| 34 |
{ description: 'Item 3', date: 'May 9, 2018', status: 'Active' }, |
| 35 |
{ description: 'Item 4', date: 'May 9, 2018', status: 'Active' }, |
| 36 |
{ description: 'Item 5', date: 'May 9, 2018', status: 'Active' } |
| 37 |
]; |
| 38 |
|
| 39 |
const columnNames = { |
| 40 |
description: 'Description', |
| 41 |
date: 'Date', |
| 42 |
status: 'Status' |
| 43 |
}; |
| 44 |
|
| 45 |
return ( |
| 46 |
<Table borders={false} aria-label="Nested table" variant="compact"> |
| 47 |
<Thead> |
| 48 |
<Tr> |
| 49 |
<Th>{columnNames.description}</Th> |
| 50 |
<Th>{columnNames.date}</Th> |
| 51 |
<Th>{columnNames.status}</Th> |
| 52 |
<Th screenReaderText="Actions" /> |
| 53 |
</Tr> |
| 54 |
</Thead> |
| 55 |
<Tbody> |
| 56 |
{items.map((item) => ( |
| 57 |
<Tr key={item.description}> |
| 58 |
<Td dataLabel={columnNames.description}>{item.description}</Td> |
| 59 |
<Td dataLabel={columnNames.date}>{item.date}</Td> |
| 60 |
<Td dataLabel={columnNames.status}>{item.status}</Td> |
| 61 |
<Td isActionCell> |
| 62 |
<ActionsColumn items={defaultActions()} /> |
| 63 |
</Td> |
| 64 |
</Tr> |
| 65 |
))} |
| 66 |
</Tbody> |
| 67 |
</Table> |
| 68 |
); |
| 69 |
}; |
| 70 |
|
| 71 |
const renderPagination = (variant: 'top' | 'bottom' | PaginationVariant, isCompact: boolean) => ( |
| 72 |
<Pagination |
| 73 |
isCompact={isCompact} |
| 74 |
itemCount={36} |
| 75 |
page={1} |
| 76 |
perPage={10} |
| 77 |
variant={variant} |
| 78 |
titles={{ |
| 79 |
paginationAriaLabel: `${variant} pagination` |
| 80 |
}} |
| 81 |
/> |
| 82 |
); |
| 83 |
|
| 84 |
const tableToolbar = ( |
| 85 |
<Toolbar id="compact-toolbar"> |
| 86 |
<ToolbarContent> |
| 87 |
<ToolbarItem> |
| 88 |
<Select |
| 89 |
id="select-example" |
| 90 |
aria-label="Select Input" |
| 91 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 92 |
<MenuToggle |
| 93 |
ref={toggleRef} |
| 94 |
onClick={() => setIsSelectOpen(!isSelectOpen)} |
| 95 |
isExpanded={isSelectOpen} |
| 96 |
icon={<FilterIcon />} |
| 97 |
> |
| 98 |
Status |
| 99 |
</MenuToggle> |
| 100 |
)} |
| 101 |
isOpen={isSelectOpen} |
| 102 |
onOpenChange={(isOpen: boolean) => setIsSelectOpen(isOpen)} |
| 103 |
onSelect={() => setIsSelectOpen(!isSelectOpen)} |
| 104 |
> |
| 105 |
{[ |
| 106 |
<SelectOption key={0} value="Debug"> |
| 107 |
Debug |
| 108 |
</SelectOption>, |
| 109 |
<SelectOption key={1} value="Info"> |
| 110 |
Info |
| 111 |
</SelectOption>, |
| 112 |
<SelectOption key={2} value="Warn"> |
| 113 |
Warn |
| 114 |
</SelectOption>, |
| 115 |
<SelectOption key={3} value="Error"> |
| 116 |
Error |
| 117 |
</SelectOption> |
| 118 |
]} |
| 119 |
</Select> |
| 120 |
</ToolbarItem> |
| 121 |
<ToolbarGroup> |
| 122 |
<ToolbarItem> |
| 123 |
<Button variant="primary">Action</Button> |
| 124 |
</ToolbarItem> |
| 125 |
</ToolbarGroup> |
| 126 |
<ToolbarItem variant="pagination">{renderPagination('top', true)}</ToolbarItem> |
| 127 |
</ToolbarContent> |
| 128 |
</Toolbar> |
| 129 |
); |
| 130 |
|
| 131 |
const defaultActions = () => [ |
| 132 |
{ |
| 133 |
title: 'Settings', |
| 134 |
// eslint-disable-next-line no-console |
| 135 |
onClick: () => console.log(`clicked on Settings`) |
| 136 |
}, |
| 137 |
{ |
| 138 |
title: 'Help', |
| 139 |
// eslint-disable-next-line no-console |
| 140 |
onClick: () => console.log(`clicked on Help`) |
| 141 |
} |
| 142 |
]; |
| 143 |
|
| 144 |
interface Repo { |
| 145 |
name: string; |
| 146 |
branches: number; |
| 147 |
prs: number; |
| 148 |
workspaces: number; |
| 149 |
lastCommit: string; |
| 150 |
} |
| 151 |
|
| 152 |
interface IDictionary<T> { |
| 153 |
[Key: string]: T; |
| 154 |
} |
| 155 |
|
| 156 |
const repositories: Repo[] = [ |
| 157 |
{ |
| 158 |
name: 'siemur/test-space', |
| 159 |
branches: 10, |
| 160 |
prs: 4, |
| 161 |
workspaces: 4, |
| 162 |
lastCommit: '20 minutes' |
| 163 |
}, |
| 164 |
{ name: 'siemur/test-space-2', branches: 3, prs: 4, workspaces: 4, lastCommit: '20 minutes' } |
| 165 |
]; |
| 166 |
|
| 167 |
const columnNames: IDictionary<string> = { |
| 168 |
name: 'Repositories', |
| 169 |
branches: 'Branches', |
| 170 |
prs: 'Pull requests', |
| 171 |
workspaces: 'Workspaces', |
| 172 |
lastCommit: 'Last commit' |
| 173 |
}; |
| 174 |
|
| 175 |
// In this example, expanded cells are tracked by the repo and property names from each row. This could be any pair of unique identifiers. |
| 176 |
// This is to prevent state from being based on row and column order index in case we later add sorting and rearranging columns. |
| 177 |
// Note that this behavior is very similar to selection state. |
| 178 |
const [expandedCells, setExpandedCells] = useState<IDictionary<string>>({ |
| 179 |
'siemur/test-space': 'branches' // Default to the first cell of the first row being expanded |
| 180 |
}); |
| 181 |
const setCellExpanded = (repo: Repo, columnKey: string, isExpanding = true) => { |
| 182 |
const newExpandedCells: IDictionary<string> = { ...expandedCells }; |
| 183 |
if (isExpanding) { |
| 184 |
newExpandedCells[repo.name] = columnKey; |
| 185 |
} else { |
| 186 |
delete newExpandedCells[repo.name]; |
| 187 |
} |
| 188 |
setExpandedCells(newExpandedCells); |
| 189 |
}; |
| 190 |
const compoundExpandParams = (repo: Repo, columnKey: string, rowIndex: number, columnIndex: number) => ({ |
| 191 |
isExpanded: expandedCells[repo.name] === columnKey, |
| 192 |
onToggle: () => setCellExpanded(repo, columnKey, expandedCells[repo.name] !== columnKey), |
| 193 |
expandId: 'compound-expandable-demo', |
| 194 |
rowIndex, |
| 195 |
columnIndex |
| 196 |
}); |
| 197 |
|
| 198 |
return ( |
| 199 |
<DashboardWrapper hasPageTemplateTitle> |
| 200 |
<PageSection padding={{ default: 'noPadding', xl: 'padding' }} aria-label="Compound expandable table data "> |
| 201 |
{tableToolbar} |
| 202 |
<Table isExpandable hasAnimations aria-label="Compound expandable table"> |
| 203 |
<Thead> |
| 204 |
<Tr> |
| 205 |
<Th>{columnNames.name}</Th> |
| 206 |
<Th>{columnNames.branches}</Th> |
| 207 |
<Th>{columnNames.prs}</Th> |
| 208 |
<Th>{columnNames.workspaces}</Th> |
| 209 |
<Th>{columnNames.lastCommit}</Th> |
| 210 |
<Th screenReaderText="URL" /> |
| 211 |
<Th screenReaderText="Actions" /> |
| 212 |
</Tr> |
| 213 |
</Thead> |
| 214 |
{repositories.map((repo, rowIndex) => { |
| 215 |
const expandedCellKey = expandedCells[repo.name]; |
| 216 |
const isRowExpanded = !!expandedCellKey; |
| 217 |
return ( |
| 218 |
<Tbody key={repo.name} isExpanded={isRowExpanded}> |
| 219 |
<Tr isContentExpanded={isRowExpanded} isControlRow> |
| 220 |
<Td dataLabel={columnNames.name} component="th"> |
| 221 |
<a href="#">{repo.name}</a> |
| 222 |
</Td> |
| 223 |
<Td |
| 224 |
dataLabel={columnNames.branches} |
| 225 |
compoundExpand={compoundExpandParams(repo, 'branches', rowIndex, 1)} |
| 226 |
> |
| 227 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 228 |
<FlexItem> |
| 229 |
<RhUiBranchFillIcon key="icon" /> |
| 230 |
</FlexItem> |
| 231 |
<FlexItem>{repo.branches}</FlexItem> |
| 232 |
</Flex> |
| 233 |
</Td> |
| 234 |
<Td dataLabel={columnNames.prs} compoundExpand={compoundExpandParams(repo, 'prs', rowIndex, 2)}> |
| 235 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 236 |
<FlexItem> |
| 237 |
<RhUiCodeIcon key="icon" /> |
| 238 |
</FlexItem> |
| 239 |
<FlexItem>{repo.prs}</FlexItem> |
| 240 |
</Flex>{' '} |
| 241 |
</Td> |
| 242 |
<Td |
| 243 |
dataLabel={columnNames.workspaces} |
| 244 |
compoundExpand={compoundExpandParams(repo, 'workspaces', rowIndex, 3)} |
| 245 |
> |
| 246 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 247 |
<FlexItem> |
| 248 |
<CubeIcon key="icon" /> |
| 249 |
</FlexItem> |
| 250 |
<FlexItem>{repo.workspaces}</FlexItem> |
| 251 |
</Flex> |
| 252 |
</Td> |
| 253 |
<Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td> |
| 254 |
<Td> |
| 255 |
<a href="#">Open in GitHub</a> |
| 256 |
</Td> |
| 257 |
<Td isActionCell> |
| 258 |
<ActionsColumn items={defaultActions()} /> |
| 259 |
</Td> |
| 260 |
</Tr> |
| 261 |
|
| 262 |
<Tr isExpanded={isRowExpanded && columnNames[expandedCellKey] === columnNames.branches}> |
| 263 |
<Td dataLabel={columnNames[expandedCellKey]} noPadding colSpan={7}> |
| 264 |
<ExpandableRowContent hasNoBackground> |
| 265 |
<NestedItemsTable /> |
| 266 |
</ExpandableRowContent> |
| 267 |
</Td> |
| 268 |
</Tr> |
| 269 |
<Tr isExpanded={isRowExpanded && columnNames[expandedCellKey] === columnNames.prs}> |
| 270 |
<Td dataLabel={columnNames[expandedCellKey]} colSpan={7}> |
| 271 |
<ExpandableRowContent> |
| 272 |
<div>Expanded content for {repo.name}: prs goes here!</div> |
| 273 |
</ExpandableRowContent> |
| 274 |
</Td> |
| 275 |
</Tr> |
| 276 |
<Tr isExpanded={isRowExpanded && columnNames[expandedCellKey] === columnNames.workspaces}> |
| 277 |
<Td dataLabel={columnNames[expandedCellKey]} colSpan={7}> |
| 278 |
<ExpandableRowContent> |
| 279 |
<div>Expanded content for {repo.name}: workspaces goes here!</div> |
| 280 |
</ExpandableRowContent> |
| 281 |
</Td> |
| 282 |
</Tr> |
| 283 |
</Tbody> |
| 284 |
); |
| 285 |
})} |
| 286 |
</Table> |
| 287 |
{renderPagination('bottom', false)} |
| 288 |
</PageSection> |
| 289 |
</DashboardWrapper> |
| 290 |
); |
| 291 |
}; |