| 1 |
import { useState } from 'react'; |
| 2 |
import { sortable, info, cellWidth, wrappable } from '@patternfly/react-table'; |
| 3 |
import { Table, TableHeader, TableBody, TableProps } from '@patternfly/react-table/deprecated'; |
| 4 |
|
| 5 |
interface Repository { |
| 6 |
name: string; |
| 7 |
branches: string; |
| 8 |
prs: string; |
| 9 |
workspaces: string; |
| 10 |
lastCommit: string; |
| 11 |
} |
| 12 |
|
| 13 |
export const LegacyTableSortable: 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: 'a', workspaces: 'four', lastCommit: 'five' }, |
| 17 |
{ name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, |
| 18 |
{ name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' } |
| 19 |
]; |
| 20 |
|
| 21 |
// Index of the currently sorted column |
| 22 |
// Note: if you intend to make columns reorderable, you may instead want to use a non-numeric key |
| 23 |
// as the identifier of the sorted column. See the "Compound expandable" example. |
| 24 |
const [activeSortIndex, setActiveSortIndex] = useState<number | null>(null); |
| 25 |
|
| 26 |
// Sort direction of the currently sorted column |
| 27 |
const [activeSortDirection, setActiveSortDirection] = useState<'asc' | 'desc' | null>(null); |
| 28 |
|
| 29 |
// Since OnSort specifies sorted columns by index, we need sortable values for our object by column index. |
| 30 |
// This example is trivial since our data objects just contain strings, but if the data was more complex |
| 31 |
// this would be a place to return simplified string or number versions of each column to sort by. |
| 32 |
const getSortableRowValues = (repo: Repository): (string | number)[] => { |
| 33 |
const { name, branches, prs, workspaces, lastCommit } = repo; |
| 34 |
return [name, branches, prs, workspaces, lastCommit]; |
| 35 |
}; |
| 36 |
|
| 37 |
// Note that we perform the sort as part of the component's render logic and not in onSort. |
| 38 |
// We shouldn't store the list of data in state because we don't want to have to sync that with props. |
| 39 |
let sortedRepositories = repositories; |
| 40 |
if (activeSortIndex !== null) { |
| 41 |
sortedRepositories = repositories.sort((a, b) => { |
| 42 |
const aValue = getSortableRowValues(a)[activeSortIndex]; |
| 43 |
const bValue = getSortableRowValues(b)[activeSortIndex]; |
| 44 |
if (typeof aValue === 'number') { |
| 45 |
// Numeric sort |
| 46 |
if (activeSortDirection === 'asc') { |
| 47 |
return (aValue as number) - (bValue as number); |
| 48 |
} |
| 49 |
return (bValue as number) - (aValue as number); |
| 50 |
} else { |
| 51 |
// String sort |
| 52 |
if (activeSortDirection === 'asc') { |
| 53 |
return (aValue as string).localeCompare(bValue as string); |
| 54 |
} |
| 55 |
return (bValue as string).localeCompare(aValue as string); |
| 56 |
} |
| 57 |
}); |
| 58 |
} |
| 59 |
|
| 60 |
const columns: TableProps['cells'] = [ |
| 61 |
{ title: 'Repositories', transforms: [sortable] }, |
| 62 |
{ |
| 63 |
title: 'Branches', |
| 64 |
transforms: [ |
| 65 |
info({ |
| 66 |
tooltip: 'More information about branches' |
| 67 |
}), |
| 68 |
sortable |
| 69 |
] |
| 70 |
}, |
| 71 |
{ title: 'Pull requests', transforms: [sortable] }, |
| 72 |
{ |
| 73 |
title: 'Workspaces - Also this long header will wrap since we have applied the wrappable transform', |
| 74 |
transforms: [sortable, wrappable, cellWidth(20)] |
| 75 |
}, |
| 76 |
{ |
| 77 |
title: 'Last commit', |
| 78 |
transforms: [ |
| 79 |
info({ |
| 80 |
tooltip: 'More information about commits' |
| 81 |
}) |
| 82 |
] |
| 83 |
} |
| 84 |
]; |
| 85 |
const rows: TableProps['rows'] = sortedRepositories.map((repo) => [ |
| 86 |
repo.name, |
| 87 |
repo.branches, |
| 88 |
repo.prs, |
| 89 |
repo.workspaces, |
| 90 |
repo.lastCommit |
| 91 |
]); |
| 92 |
|
| 93 |
return ( |
| 94 |
<Table |
| 95 |
aria-label="Sortable Table" |
| 96 |
sortBy={{ |
| 97 |
index: activeSortIndex, |
| 98 |
direction: activeSortDirection, |
| 99 |
defaultDirection: 'asc' // starting sort direction when first sorting a column. Defaults to 'asc' |
| 100 |
}} |
| 101 |
onSort={(_event, index, direction) => { |
| 102 |
setActiveSortIndex(index); |
| 103 |
setActiveSortDirection(direction); |
| 104 |
}} |
| 105 |
cells={columns} |
| 106 |
rows={rows} |
| 107 |
> |
| 108 |
<TableHeader /> |
| 109 |
<TableBody /> |
| 110 |
</Table> |
| 111 |
); |
| 112 |
}; |