import { useState } from 'react'; import { Table, Thead, Tr, Th, Tbody, Td, ThProps } from '@patternfly/react-table'; interface Repository { name: string; branches: string; prs: string; workspaces: string; lastCommit: string; } export const TableSortable: React.FunctionComponent = () => { // In real usage, this data would come from some external source like an API via props. const repositories: Repository[] = [ { name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' }, { name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, { name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' } ]; const columnNames = { name: 'Repositories table header that goes on for a long time.', branches: 'Branches table header that goes on for a long time.', prs: 'Pull requests table header that goes on for a long time.', workspaces: 'Workspaces table header that goes on for a long time.', lastCommit: 'Last commit table header that goes on for a long time.' }; // Index of the currently sorted column // Note: if you intend to make columns reorderable, you may instead want to use a non-numeric key // as the identifier of the sorted column. See the "Compound expandable" example. const [activeSortIndex, setActiveSortIndex] = useState(null); // Sort direction of the currently sorted column const [activeSortDirection, setActiveSortDirection] = useState<'asc' | 'desc' | null>(null); // Since OnSort specifies sorted columns by index, we need sortable values for our object by column index. // This example is trivial since our data objects just contain strings, but if the data was more complex // this would be a place to return simplified string or number versions of each column to sort by. const getSortableRowValues = (repo: Repository): (string | number)[] => { const { name, branches, prs, workspaces, lastCommit } = repo; return [name, branches, prs, workspaces, lastCommit]; }; // Note that we perform the sort as part of the component's render logic and not in onSort. // We shouldn't store the list of data in state because we don't want to have to sync that with props. let sortedRepositories = repositories; if (activeSortIndex !== null) { sortedRepositories = repositories.sort((a, b) => { const aValue = getSortableRowValues(a)[activeSortIndex]; const bValue = getSortableRowValues(b)[activeSortIndex]; if (typeof aValue === 'number') { // Numeric sort if (activeSortDirection === 'asc') { return (aValue as number) - (bValue as number); } return (bValue as number) - (aValue as number); } else { // String sort if (activeSortDirection === 'asc') { return (aValue as string).localeCompare(bValue as string); } return (bValue as string).localeCompare(aValue as string); } }); } const getSortParams = (columnIndex: number): ThProps['sort'] => ({ sortBy: { index: activeSortIndex, direction: activeSortDirection, defaultDirection: 'asc' // starting sort direction when first sorting a column. Defaults to 'asc' }, onSort: (_event, index, direction) => { setActiveSortIndex(index); setActiveSortDirection(direction); }, columnIndex }); // In this example, we wrap all but the 1st column and make the 1st and 3rd columns sortable just to demonstrate. return ( {sortedRepositories.map((repo, rowIndex) => ( ))}
{columnNames.name} {columnNames.branches} {columnNames.prs} {columnNames.workspaces} {columnNames.lastCommit}
{repo.name} {repo.branches} {repo.prs} {repo.workspaces} {repo.lastCommit}
); };