| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { TableVariant, expandable, IRowCell } from '@patternfly/react-table'; |
| 3 |
import { Table, TableHeader, TableBody, TableProps } from '@patternfly/react-table/deprecated'; |
| 4 |
import { Checkbox } from '@patternfly/react-core'; |
| 5 |
|
| 6 |
interface Repository { |
| 7 |
name: string; |
| 8 |
branches: string; |
| 9 |
prs: string; |
| 10 |
workspaces: string; |
| 11 |
details?: { |
| 12 |
detail1?: string; |
| 13 |
detail2?: string; |
| 14 |
detail3?: string; |
| 15 |
detailFormat: 0 | 1 | 2 | 3; |
| 16 |
}; |
| 17 |
} |
| 18 |
|
| 19 |
export const LegacyTableStripedExpandable: React.FunctionComponent = () => { |
| 20 |
// In real usage, this data would come from some external source like an API via props. |
| 21 |
const repositories: Repository[] = [ |
| 22 |
{ name: 'one', branches: 'two', prs: 'a', workspaces: 'four' }, |
| 23 |
{ |
| 24 |
name: 'parent 1', |
| 25 |
branches: 'two', |
| 26 |
prs: 'k', |
| 27 |
workspaces: 'four', |
| 28 |
// This `details` structure is just for this example. You can drive expanded content from any kind of data. |
| 29 |
details: { detailFormat: 0, detail1: 'single cell' } |
| 30 |
}, |
| 31 |
{ |
| 32 |
name: 'parent 2', |
| 33 |
branches: 'two', |
| 34 |
prs: 'b', |
| 35 |
workspaces: 'four', |
| 36 |
details: { detailFormat: 1, detail1: 'single cell - fullWidth' } |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: 'parent 3', |
| 40 |
branches: '2', |
| 41 |
prs: 'b', |
| 42 |
workspaces: 'four', |
| 43 |
details: { detailFormat: 2, detail1: 'single cell - noPadding' } |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: 'parent 4', |
| 47 |
branches: '2', |
| 48 |
prs: 'b', |
| 49 |
workspaces: 'four', |
| 50 |
details: { detailFormat: 3, detail1: 'single cell - fullWidth & noPadding' } |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: 'parent 5', |
| 54 |
branches: '2', |
| 55 |
prs: 'b', |
| 56 |
workspaces: 'four', |
| 57 |
details: { |
| 58 |
detailFormat: 0, |
| 59 |
detail1: "spans 'Repositories and 'Branches'", |
| 60 |
detail2: "spans 'Pull requests' and 'Workspaces', and 'Last commit'" |
| 61 |
} |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: 'parent 6', |
| 65 |
branches: '2', |
| 66 |
prs: 'b', |
| 67 |
workspaces: 'four', |
| 68 |
details: { |
| 69 |
detailFormat: 1, |
| 70 |
detail1: "fullWidth, spans the collapsible column and 'Repositories'", |
| 71 |
detail2: "fullWidth, spans 'Branches' and 'Pull requests'", |
| 72 |
detail3: 'fullWidth, spans the empty column' |
| 73 |
} |
| 74 |
} |
| 75 |
]; |
| 76 |
|
| 77 |
// In this example, expanded rows are tracked by the repo names from each row. This could be any unique identifier. |
| 78 |
// This is to prevent state from being based on row order index in case we later add sorting. |
| 79 |
// Note that this behavior is very similar to selection state. |
| 80 |
const initialExpandedRepoNames = repositories.filter((repo) => !!repo.details).map((repo) => repo.name); // Default to all expanded |
| 81 |
const [expandedRepoNames, setExpandedRepoNames] = useState<string[]>(initialExpandedRepoNames); |
| 82 |
const setRepoExpanded = (repo: Repository, isExpanding = true) => |
| 83 |
setExpandedRepoNames((prevExpanded) => { |
| 84 |
const otherExpandedRepoNames = prevExpanded.filter((r) => r !== repo.name); |
| 85 |
return isExpanding ? [...otherExpandedRepoNames, repo.name] : otherExpandedRepoNames; |
| 86 |
}); |
| 87 |
const isRepoExpanded = (repo: Repository) => expandedRepoNames.includes(repo.name); |
| 88 |
|
| 89 |
const [isExampleCompact, setIsExampleCompact] = useState(true); |
| 90 |
|
| 91 |
// We want to be able to reference the original data object based on row index. But because an expanded |
| 92 |
// row takes up two row indexes, repositories[rowIndex] will not be accurate like it would in a normal table. |
| 93 |
// One solution to this is to create an array of data objects indexed by the displayed row index. |
| 94 |
const reposByRowIndex: (Repository | null)[] = []; |
| 95 |
|
| 96 |
const columns: TableProps['cells'] = [ |
| 97 |
{ |
| 98 |
title: 'Repositories', |
| 99 |
cellFormatters: [expandable] |
| 100 |
}, |
| 101 |
'Branches', |
| 102 |
{ title: 'Pull requests' }, |
| 103 |
{ title: '' /* deliberately empty */, dataLabel: 'Label for mobile view' } |
| 104 |
]; |
| 105 |
const rows: TableProps['rows'] = []; |
| 106 |
repositories.forEach((repo) => { |
| 107 |
rows.push({ |
| 108 |
...(repo.details ? { isOpen: isRepoExpanded(repo) } : {}), |
| 109 |
cells: [repo.name, repo.branches, repo.prs, repo.workspaces] |
| 110 |
}); |
| 111 |
reposByRowIndex.push(repo); |
| 112 |
if (repo.details) { |
| 113 |
// Some arbitrary examples of how you could customize the child row based on your needs |
| 114 |
const { detail1, detail2, detail3, detailFormat } = repo.details; |
| 115 |
const numColumns = 5; |
| 116 |
const childIsFullWidth = [1, 3].includes(detailFormat); |
| 117 |
const childHasNoPadding = [2, 3].includes(detailFormat); |
| 118 |
let detailColSpans = [1, 1, 1]; |
| 119 |
if (detail1 && !detail2 && !detail3) { |
| 120 |
detailColSpans = [childIsFullWidth ? numColumns : numColumns + 1]; // Account for toggle column |
| 121 |
} else if (detail1 && detail2 && !detail3) { |
| 122 |
detailColSpans = [2, childIsFullWidth ? 3 : 4]; |
| 123 |
} else if (detail1 && detail2 && detail3) { |
| 124 |
detailColSpans = [2, 2, childIsFullWidth ? 1 : 2]; |
| 125 |
} |
| 126 |
const cells: IRowCell[] = []; |
| 127 |
[detail1, detail2, detail3].forEach((detail, index) => { |
| 128 |
if (detail) { |
| 129 |
cells.push({ |
| 130 |
title: detail1, |
| 131 |
props: { |
| 132 |
colSpan: detailColSpans[index] |
| 133 |
} |
| 134 |
}); |
| 135 |
} |
| 136 |
}); |
| 137 |
rows.push({ |
| 138 |
parent: rows.length - 1, |
| 139 |
cells, |
| 140 |
fullWidth: childIsFullWidth, |
| 141 |
noPadding: childHasNoPadding |
| 142 |
}); |
| 143 |
reposByRowIndex.push(null); |
| 144 |
} |
| 145 |
}); |
| 146 |
|
| 147 |
return ( |
| 148 |
<Fragment> |
| 149 |
<Checkbox |
| 150 |
label="Compact" |
| 151 |
isChecked={isExampleCompact} |
| 152 |
onChange={(_event, checked) => setIsExampleCompact(checked)} |
| 153 |
aria-label="toggle striped compact variation" |
| 154 |
id="toggle-compact-striped" |
| 155 |
name="toggle-compact-striped" |
| 156 |
/> |
| 157 |
<Table |
| 158 |
aria-label="Expandable table" |
| 159 |
variant={isExampleCompact ? TableVariant.compact : undefined} |
| 160 |
onCollapse={(_event, rowIndex, isOpen) => { |
| 161 |
if (reposByRowIndex[rowIndex]) { |
| 162 |
setRepoExpanded(reposByRowIndex[rowIndex], isOpen); |
| 163 |
} |
| 164 |
}} |
| 165 |
rows={rows} |
| 166 |
cells={columns} |
| 167 |
isStriped |
| 168 |
isExpandable |
| 169 |
> |
| 170 |
<TableHeader /> |
| 171 |
<TableBody /> |
| 172 |
</Table> |
| 173 |
</Fragment> |
| 174 |
); |
| 175 |
}; |