| 1 |
import { useState } from 'react'; |
| 2 |
import { info, textCenter, ICell } from '@patternfly/react-table'; |
| 3 |
import { Table, TableHeader, TableBody, TableProps } from '@patternfly/react-table/deprecated'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Table/table'; |
| 5 |
|
| 6 |
interface Repository { |
| 7 |
name: string; |
| 8 |
branches: string | null; |
| 9 |
prs: string | null; |
| 10 |
workspaces: string; |
| 11 |
lastCommit: string; |
| 12 |
} |
| 13 |
|
| 14 |
export const LegacyTableClickable: React.FunctionComponent = () => { |
| 15 |
// In real usage, this data would come from some external source like an API via props. |
| 16 |
const repositories: Repository[] = [ |
| 17 |
{ |
| 18 |
name: 'Repository one', |
| 19 |
branches: 'Branch one', |
| 20 |
prs: 'PR one', |
| 21 |
workspaces: 'Workspace one', |
| 22 |
lastCommit: 'Commit one' |
| 23 |
}, |
| 24 |
{ name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' }, |
| 25 |
{ |
| 26 |
name: 'one - 3', |
| 27 |
branches: 'two - 3', |
| 28 |
prs: 'three - 3', |
| 29 |
workspaces: 'four - 3', |
| 30 |
lastCommit: 'five - 3 (not centered)' |
| 31 |
} |
| 32 |
]; |
| 33 |
|
| 34 |
// In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier. |
| 35 |
// This is to prevent state from being based on row order index in case we later add sorting. |
| 36 |
const [selectedRepoName, setSelectedRepoName] = useState(''); |
| 37 |
const columns: TableProps['cells'] = [ |
| 38 |
{ |
| 39 |
title: 'Repositories', |
| 40 |
transforms: [ |
| 41 |
info({ |
| 42 |
tooltip: 'More information about repositories', |
| 43 |
className: 'repositories-info-tip', |
| 44 |
tooltipProps: { |
| 45 |
isContentLeftAligned: true |
| 46 |
} |
| 47 |
}) |
| 48 |
] |
| 49 |
}, |
| 50 |
'Branches', |
| 51 |
{ |
| 52 |
title: 'Pull requests', |
| 53 |
transforms: [ |
| 54 |
info({ |
| 55 |
popover: ( |
| 56 |
<div> |
| 57 |
More <strong>information</strong> on pull requests |
| 58 |
</div> |
| 59 |
), |
| 60 |
ariaLabel: 'More information on pull requests', |
| 61 |
popoverProps: { |
| 62 |
headerContent: 'Pull requests', |
| 63 |
footerContent: <a href="#">Click here for even more info</a> |
| 64 |
} |
| 65 |
}) |
| 66 |
] |
| 67 |
}, |
| 68 |
'Workspaces', |
| 69 |
{ |
| 70 |
title: 'Last commit', |
| 71 |
transforms: [textCenter], |
| 72 |
cellTransforms: [textCenter] |
| 73 |
} |
| 74 |
]; |
| 75 |
const rows: TableProps['rows'] = repositories.map((repo) => { |
| 76 |
let cells: (string | ICell)[] = [repo.name, repo.branches || '', repo.prs || '', repo.workspaces, repo.lastCommit]; |
| 77 |
// These rows have arbitrary differences for this example, but these could be based on some other conditions |
| 78 |
if (repo.name === 'one - 2') { |
| 79 |
cells = [ |
| 80 |
{ |
| 81 |
title: <div>{repo.name}</div>, |
| 82 |
props: { title: 'hover title', colSpan: 3 } |
| 83 |
}, |
| 84 |
repo.workspaces, |
| 85 |
repo.lastCommit |
| 86 |
]; |
| 87 |
} else if (repo.name === 'five - 3 (not centrered)') { |
| 88 |
cells = [ |
| 89 |
repo.name, |
| 90 |
repo.branches || '', |
| 91 |
repo.prs || '', |
| 92 |
repo.workspaces, |
| 93 |
{ |
| 94 |
title: repo.lastCommit, |
| 95 |
props: { textCenter: false } |
| 96 |
} |
| 97 |
]; |
| 98 |
} |
| 99 |
return { |
| 100 |
cells, |
| 101 |
isClickable: true, |
| 102 |
isRowSelected: selectedRepoName === repo.name |
| 103 |
}; |
| 104 |
}); |
| 105 |
|
| 106 |
return ( |
| 107 |
<Table caption="Row click handler table" cells={columns} rows={rows}> |
| 108 |
<TableHeader className={styles.modifiers.nowrap} /> |
| 109 |
<TableBody |
| 110 |
onRowClick={(_event, row, rowProps) => { |
| 111 |
if (rowProps) { |
| 112 |
const repo = repositories[rowProps.rowIndex]; |
| 113 |
setSelectedRepoName(repo.name); |
| 114 |
} |
| 115 |
}} |
| 116 |
/> |
| 117 |
</Table> |
| 118 |
); |
| 119 |
}; |