|
| 1 | +import { FunctionComponent } from 'react'; |
| 2 | +import { DataViewTable, DataViewTr, DataViewTh } from '@patternfly/react-data-view/dist/dynamic/DataViewTable'; |
| 3 | +import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView'; |
| 4 | +import { useDataViewSelection } from '@patternfly/react-data-view/dist/dynamic/Hooks'; |
| 5 | +import { ExclamationCircleIcon } from '@patternfly/react-icons'; |
| 6 | +import { Button } from '@patternfly/react-core'; |
| 7 | +import { ActionsColumn } from '@patternfly/react-table'; |
| 8 | + |
| 9 | +interface Repository { |
| 10 | + id: number; |
| 11 | + name: string; |
| 12 | + branches: string | null; |
| 13 | + prs: string | null; |
| 14 | + workspaces: string; |
| 15 | + lastCommit: string; |
| 16 | + contributors: string; |
| 17 | + stars: string; |
| 18 | + forks: string; |
| 19 | +} |
| 20 | + |
| 21 | +const repositories: Repository[] = [ |
| 22 | + { id: 1, name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one', contributors: '25 contributors', stars: '1.2k stars', forks: '340 forks' }, |
| 23 | + { id: 2, name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two', contributors: '45 contributors', stars: '3.5k stars', forks: '890 forks' }, |
| 24 | + { id: 3, name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three', contributors: '200 contributors', stars: '15k stars', forks: '2.1k forks' }, |
| 25 | + { id: 4, name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four', contributors: '80 contributors', stars: '5.7k stars', forks: '1.2k forks' }, |
| 26 | + { id: 5, name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five', contributors: '60 contributors', stars: '4.3k stars', forks: '780 forks' }, |
| 27 | + { id: 6, name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six', contributors: '300 contributors', stars: '22k stars', forks: '4.5k forks' }, |
| 28 | + { id: 7, name: 'Repository seven', branches: 'Branch seven', prs: 'Pull request seven', workspaces: 'Workspace seven', lastCommit: 'Timestamp seven', contributors: '12 contributors', stars: '567 stars', forks: '120 forks' }, |
| 29 | + { id: 8, name: 'Repository eight', branches: 'Branch eight', prs: 'Pull request eight', workspaces: 'Workspace eight', lastCommit: 'Timestamp eight', contributors: '98 contributors', stars: '7.8k stars', forks: '1.5k forks' }, |
| 30 | + { id: 9, name: 'Repository nine', branches: 'Branch nine', prs: 'Pull request nine', workspaces: 'Workspace nine', lastCommit: 'Timestamp nine', contributors: '33 contributors', stars: '2.1k stars', forks: '456 forks' }, |
| 31 | + { id: 10, name: 'Repository ten', branches: 'Branch ten', prs: 'Pull request ten', workspaces: 'Workspace ten', lastCommit: 'Timestamp ten', contributors: '150 contributors', stars: '11k stars', forks: '2.8k forks' }, |
| 32 | + { id: 11, name: 'Repository eleven', branches: 'Branch eleven', prs: 'Pull request eleven', workspaces: 'Workspace eleven', lastCommit: 'Timestamp eleven', contributors: '67 contributors', stars: '5.2k stars', forks: '980 forks' }, |
| 33 | + { id: 12, name: 'Repository twelve', branches: 'Branch twelve', prs: 'Pull request twelve', workspaces: 'Workspace twelve', lastCommit: 'Timestamp twelve', contributors: '41 contributors', stars: '3.1k stars', forks: '670 forks' } |
| 34 | +]; |
| 35 | + |
| 36 | +const rowActions = [ |
| 37 | + { |
| 38 | + title: 'Some action', |
| 39 | + onClick: () => console.log('clicked on Some action') // eslint-disable-line no-console |
| 40 | + }, |
| 41 | + { |
| 42 | + title: <div>Another action</div>, |
| 43 | + onClick: () => console.log('clicked on Another action') // eslint-disable-line no-console |
| 44 | + }, |
| 45 | + { |
| 46 | + isSeparator: true |
| 47 | + }, |
| 48 | + { |
| 49 | + title: 'Third action', |
| 50 | + onClick: () => console.log('clicked on Third action') // eslint-disable-line no-console |
| 51 | + } |
| 52 | +]; |
| 53 | + |
| 54 | +const rows: DataViewTr[] = repositories.map(({ id, name, branches, prs, workspaces, lastCommit, contributors, stars, forks }) => [ |
| 55 | + { id, cell: <Button href='#' variant='link' isInline>{name}</Button>, props: { isStickyColumn: true, hasRightBorder: true, modifier: "nowrap" } }, |
| 56 | + { cell: branches, props: { modifier: "nowrap" } }, |
| 57 | + { cell: prs, props: { modifier: "nowrap" } }, |
| 58 | + { cell: workspaces, props: { modifier: "nowrap" } }, |
| 59 | + { cell: lastCommit, props: { modifier: "nowrap" } }, |
| 60 | + { cell: contributors, props: { modifier: "nowrap" } }, |
| 61 | + { cell: stars, props: { modifier: "nowrap" } }, |
| 62 | + { cell: forks, props: { modifier: "nowrap" } }, |
| 63 | + { cell: <ActionsColumn items={rowActions}/>, props: { isActionCell: true } }, |
| 64 | +]); |
| 65 | + |
| 66 | +const columns: DataViewTh[] = [ |
| 67 | + { cell: 'Repositories', props: { isStickyColumn: true, modifier: 'fitContent', hasRightBorder: true } }, |
| 68 | + { cell: <>Branches<ExclamationCircleIcon className='pf-v6-u-ml-sm' color="var(--pf-t--global--color--status--danger--default)"/></>, props: { width: 20 } }, |
| 69 | + { cell: 'Pull requests', props: { width: 20 } }, |
| 70 | + { cell: 'Workspaces', props: { info: { tooltip: 'More information' }, width: 20 } }, |
| 71 | + { cell: 'Last commit', props: { sort: { sortBy: {}, columnIndex: 4 }, width: 20 } }, |
| 72 | + { cell: 'Contributors', props: { width: 20 } }, |
| 73 | + { cell: 'Stars', props: { width: 20 } }, |
| 74 | + { cell: 'Forks', props: { width: 20 } }, |
| 75 | + null, // Actions column header |
| 76 | +]; |
| 77 | + |
| 78 | +const ouiaId = 'TableStickySelectionExample'; |
| 79 | + |
| 80 | +export const StickySelectionExample: FunctionComponent = () => { |
| 81 | + const selection = useDataViewSelection({ matchOption: (a, b) => a[0].id === b[0].id }); |
| 82 | + |
| 83 | + return ( |
| 84 | + <DataView selection={selection}> |
| 85 | + <div style={{ height: '400px', width: '800px', overflow: 'auto' }}> |
| 86 | + <DataViewTable |
| 87 | + aria-label='Sticky selection repositories table' |
| 88 | + ouiaId={ouiaId} |
| 89 | + columns={columns} |
| 90 | + rows={rows} |
| 91 | + isSticky |
| 92 | + /> |
| 93 | + </div> |
| 94 | + </DataView> |
| 95 | + ); |
| 96 | +}; |
0 commit comments