|
| 1 | +/* eslint-disable no-nested-ternary */ |
| 2 | +import React, { useMemo } from 'react'; |
| 3 | +import { useDataViewSort } from '@patternfly/react-data-view/dist/dynamic/Hooks'; |
| 4 | +import { DataViewTable, DataViewTr, DataViewTh } from '@patternfly/react-data-view/dist/dynamic/DataViewTable'; |
| 5 | +import { ThProps } from '@patternfly/react-table'; |
| 6 | +import { BrowserRouter, useSearchParams } from 'react-router-dom'; |
| 7 | + |
| 8 | +interface Repository { |
| 9 | + name: string; |
| 10 | + branches: string; |
| 11 | + prs: string; |
| 12 | + workspaces: string; |
| 13 | + lastCommit: string; |
| 14 | +}; |
| 15 | + |
| 16 | +const COLUMNS = [ |
| 17 | + { label: 'Repository', key: 'name', index: 0 }, |
| 18 | + { label: 'Branch', key: 'branches', index: 1 }, |
| 19 | + { label: 'Pull request', key: 'prs', index: 2 }, |
| 20 | + { label: 'Workspace', key: 'workspaces', index: 3 }, |
| 21 | + { label: 'Last commit', key: 'lastCommit', index: 4 } |
| 22 | +]; |
| 23 | + |
| 24 | +const repositories: Repository[] = [ |
| 25 | + { name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' }, |
| 26 | + { name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' }, |
| 27 | + { name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' }, |
| 28 | + { name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' }, |
| 29 | + { name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' }, |
| 30 | + { name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' } |
| 31 | +]; |
| 32 | + |
| 33 | +const sortData = (data: Repository[], sortBy: string | undefined, direction: 'asc' | 'desc' | undefined) => |
| 34 | + sortBy && direction |
| 35 | + ? [ ...data ].sort((a, b) => |
| 36 | + direction === 'asc' |
| 37 | + ? a[sortBy] < b[sortBy] ? -1 : a[sortBy] > b[sortBy] ? 1 : 0 |
| 38 | + : a[sortBy] > b[sortBy] ? -1 : a[sortBy] < b[sortBy] ? 1 : 0 |
| 39 | + ) |
| 40 | + : data; |
| 41 | + |
| 42 | +const ouiaId = 'TableExample'; |
| 43 | + |
| 44 | +export const MyTable: React.FunctionComponent = () => { |
| 45 | + const [ searchParams, setSearchParams ] = useSearchParams(); |
| 46 | + const { sortBy, direction, onSort } = useDataViewSort({ searchParams, setSearchParams }); |
| 47 | + const sortByIndex = useMemo(() => COLUMNS.findIndex(item => item.key === sortBy), [ sortBy ]); |
| 48 | + |
| 49 | + const getSortParams = (columnIndex: number): ThProps['sort'] => ({ |
| 50 | + sortBy: { |
| 51 | + index: sortByIndex, |
| 52 | + direction, |
| 53 | + defaultDirection: 'asc' |
| 54 | + }, |
| 55 | + onSort: (_event, index, direction) => onSort(_event, COLUMNS[index].key, direction), |
| 56 | + columnIndex |
| 57 | + }); |
| 58 | + |
| 59 | + const columns: DataViewTh[] = COLUMNS.map((column, index) => ({ |
| 60 | + cell: column.label, |
| 61 | + props: { sort: getSortParams(index) } |
| 62 | + })); |
| 63 | + |
| 64 | + const rows: DataViewTr[] = useMemo(() => sortData(repositories, sortBy, direction).map(({ name, branches, prs, workspaces, lastCommit }) => [ |
| 65 | + name, |
| 66 | + branches, |
| 67 | + prs, |
| 68 | + workspaces, |
| 69 | + lastCommit, |
| 70 | + ]), [ sortBy, direction ]); |
| 71 | + |
| 72 | + return ( |
| 73 | + <DataViewTable |
| 74 | + aria-label="Repositories table" |
| 75 | + ouiaId={ouiaId} |
| 76 | + columns={columns} |
| 77 | + rows={rows} |
| 78 | + /> |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +export const BasicExample: React.FunctionComponent = () => ( |
| 83 | + <BrowserRouter> |
| 84 | + <MyTable/> |
| 85 | + </BrowserRouter> |
| 86 | +) |
| 87 | + |
0 commit comments