import { Fragment, useState } from 'react'; import { Table, Thead, Tr, Th, Tbody, Td, ThProps } from '@patternfly/react-table'; import { Toolbar, ToolbarContent, ToolbarItem, Select, SelectGroup, SelectList, SelectOption, MenuToggle, MenuToggleElement } from '@patternfly/react-core'; import RhMicronsSortDownLargeToSmallIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-sort-down-large-to-small-icon'; interface Repository { name: string; branches: string; prs: string; workspaces: string; lastCommit: string; } export const TableSortableCustom: 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', branches: 'Branches', prs: 'Pull requests', workspaces: 'Workspaces', lastCommit: 'Last commit' }; const [isSortDropdownOpen, setIsSortDropdownOpen] = useState(false); // 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); // Sort dropdown expansion // const [isSortDropdownOpen, setIsSortDropdownOpen] = useState(false); // 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 }, onSort: (_event, index, direction) => { setActiveSortIndex(index); setActiveSortDirection(direction as 'desc' | 'asc'); }, columnIndex }); 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}
); };