| 1 |
import { Table, TableHeader, TableBody, TableProps } from '@patternfly/react-table/deprecated'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
|
| 4 |
interface Repository { |
| 5 |
name: string; |
| 6 |
branches: string | null; |
| 7 |
prs: string | null; |
| 8 |
workspaces: string; |
| 9 |
lastCommit: string; |
| 10 |
} |
| 11 |
|
| 12 |
export const LegacyTableStripedCustomTr: React.FunctionComponent = () => { |
| 13 |
// In real usage, this data would come from some external source like an API via props. |
| 14 |
const repositories: Repository[] = [ |
| 15 |
{ name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' }, |
| 16 |
{ name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' }, |
| 17 |
{ name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' } |
| 18 |
]; |
| 19 |
|
| 20 |
const columns: TableProps['cells'] = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit']; |
| 21 |
const rows: TableProps['rows'] = repositories.map((repo) => [ |
| 22 |
repo.name, |
| 23 |
repo.branches || '', |
| 24 |
repo.prs || '', |
| 25 |
repo.workspaces, |
| 26 |
repo.lastCommit |
| 27 |
]); |
| 28 |
|
| 29 |
const customRowWrapper: TableProps['rowWrapper'] = ({ trRef, className, rowProps, row: _row }) => { |
| 30 |
const isOddRow = rowProps ? !!((rowProps.rowIndex + 1) % 2) : true; |
| 31 |
return <tr ref={trRef as React.Ref<HTMLTableRowElement>} className={css(className, isOddRow && 'pf-m-striped')} />; |
| 32 |
}; |
| 33 |
|
| 34 |
return ( |
| 35 |
<Table |
| 36 |
caption="Table with custom row wrapper that stripes odd rows" |
| 37 |
cells={columns} |
| 38 |
rows={rows} |
| 39 |
rowWrapper={customRowWrapper} |
| 40 |
> |
| 41 |
<TableHeader /> |
| 42 |
<TableBody /> |
| 43 |
</Table> |
| 44 |
); |
| 45 |
}; |