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