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