| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { Table, Thead, Tr, Th, Tbody, Td, ThProps } from '@patternfly/react-table'; |
| 3 |
import { |
| 4 |
Toolbar, |
| 5 |
ToolbarContent, |
| 6 |
ToolbarItem, |
| 7 |
Select, |
| 8 |
SelectGroup, |
| 9 |
SelectList, |
| 10 |
SelectOption, |
| 11 |
MenuToggle, |
| 12 |
MenuToggleElement |
| 13 |
} from '@patternfly/react-core'; |
| 14 |
import RhMicronsSortDownLargeToSmallIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-sort-down-large-to-small-icon'; |
| 15 |
|
| 16 |
interface Repository { |
| 17 |
name: string; |
| 18 |
branches: string; |
| 19 |
prs: string; |
| 20 |
workspaces: string; |
| 21 |
lastCommit: string; |
| 22 |
} |
| 23 |
|
| 24 |
export const TableSortableCustom: React.FunctionComponent = () => { |
| 25 |
// In real usage, this data would come from some external source like an API via props. |
| 26 |
const repositories: Repository[] = [ |
| 27 |
{ name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' }, |
| 28 |
{ name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' }, |
| 29 |
{ name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' } |
| 30 |
]; |
| 31 |
|
| 32 |
const columnNames = { |
| 33 |
name: 'Repositories', |
| 34 |
branches: 'Branches', |
| 35 |
prs: 'Pull requests', |
| 36 |
workspaces: 'Workspaces', |
| 37 |
lastCommit: 'Last commit' |
| 38 |
}; |
| 39 |
|
| 40 |
const [isSortDropdownOpen, setIsSortDropdownOpen] = useState(false); |
| 41 |
|
| 42 |
// Index of the currently sorted column |
| 43 |
// Note: if you intend to make columns reorderable, you may instead want to use a non-numeric key |
| 44 |
// as the identifier of the sorted column. See the "Compound expandable" example. |
| 45 |
const [activeSortIndex, setActiveSortIndex] = useState<number | null>(null); |
| 46 |
|
| 47 |
// Sort direction of the currently sorted column |
| 48 |
const [activeSortDirection, setActiveSortDirection] = useState<'asc' | 'desc' | null>(null); |
| 49 |
|
| 50 |
// Sort dropdown expansion |
| 51 |
// const [isSortDropdownOpen, setIsSortDropdownOpen] = useState(false); |
| 52 |
|
| 53 |
// Since OnSort specifies sorted columns by index, we need sortable values for our object by column index. |
| 54 |
// This example is trivial since our data objects just contain strings, but if the data was more complex |
| 55 |
// this would be a place to return simplified string or number versions of each column to sort by. |
| 56 |
const getSortableRowValues = (repo: Repository): (string | number)[] => { |
| 57 |
const { name, branches, prs, workspaces, lastCommit } = repo; |
| 58 |
return [name, branches, prs, workspaces, lastCommit]; |
| 59 |
}; |
| 60 |
|
| 61 |
// Note that we perform the sort as part of the component's render logic and not in onSort. |
| 62 |
// We shouldn't store the list of data in state because we don't want to have to sync that with props. |
| 63 |
let sortedRepositories = repositories; |
| 64 |
if (activeSortIndex !== null) { |
| 65 |
sortedRepositories = repositories.sort((a, b) => { |
| 66 |
const aValue = getSortableRowValues(a)[activeSortIndex]; |
| 67 |
const bValue = getSortableRowValues(b)[activeSortIndex]; |
| 68 |
if (typeof aValue === 'number') { |
| 69 |
// Numeric sort |
| 70 |
if (activeSortDirection === 'asc') { |
| 71 |
return (aValue as number) - (bValue as number); |
| 72 |
} |
| 73 |
return (bValue as number) - (aValue as number); |
| 74 |
} else { |
| 75 |
// String sort |
| 76 |
if (activeSortDirection === 'asc') { |
| 77 |
return (aValue as string).localeCompare(bValue as string); |
| 78 |
} |
| 79 |
return (bValue as string).localeCompare(aValue as string); |
| 80 |
} |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
const getSortParams = (columnIndex: number): ThProps['sort'] => ({ |
| 85 |
sortBy: { |
| 86 |
index: activeSortIndex, |
| 87 |
direction: activeSortDirection |
| 88 |
}, |
| 89 |
onSort: (_event, index, direction) => { |
| 90 |
setActiveSortIndex(index); |
| 91 |
setActiveSortDirection(direction as 'desc' | 'asc'); |
| 92 |
}, |
| 93 |
columnIndex |
| 94 |
}); |
| 95 |
|
| 96 |
return ( |
| 97 |
<Fragment> |
| 98 |
<Toolbar id="toolbar"> |
| 99 |
<ToolbarContent> |
| 100 |
<ToolbarItem> |
| 101 |
<Select |
| 102 |
isOpen={isSortDropdownOpen} |
| 103 |
selected={[activeSortDirection, activeSortIndex]} |
| 104 |
onOpenChange={(isOpen) => setIsSortDropdownOpen(isOpen)} |
| 105 |
onSelect={(event, value) => { |
| 106 |
if (value === 'asc' || value === 'desc') { |
| 107 |
setActiveSortDirection(value as 'desc' | 'asc'); |
| 108 |
} else { |
| 109 |
setActiveSortIndex(value as number); |
| 110 |
setActiveSortDirection(activeSortDirection !== null ? activeSortDirection : 'asc'); |
| 111 |
} |
| 112 |
}} |
| 113 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 114 |
<MenuToggle |
| 115 |
ref={toggleRef} |
| 116 |
onClick={() => setIsSortDropdownOpen(!isSortDropdownOpen)} |
| 117 |
isExpanded={isSortDropdownOpen} |
| 118 |
variant="plain" |
| 119 |
aria-label="Sort columns" |
| 120 |
icon={<RhMicronsSortDownLargeToSmallIcon />} |
| 121 |
/> |
| 122 |
)} |
| 123 |
> |
| 124 |
<SelectGroup label="Sort column"> |
| 125 |
<SelectList> |
| 126 |
{Object.values(columnNames).map((column, columnIndex) => ( |
| 127 |
<SelectOption key={column} value={columnIndex} isSelected={activeSortIndex === columnIndex}> |
| 128 |
{column} |
| 129 |
</SelectOption> |
| 130 |
))} |
| 131 |
</SelectList> |
| 132 |
</SelectGroup> |
| 133 |
<SelectGroup label="Sort direction"> |
| 134 |
<SelectList> |
| 135 |
<SelectOption isSelected={activeSortDirection === 'asc'} value="asc" key="ascending"> |
| 136 |
Ascending |
| 137 |
</SelectOption> |
| 138 |
<SelectOption isSelected={activeSortDirection === 'desc'} value="desc" key="descending"> |
| 139 |
Descending |
| 140 |
</SelectOption> |
| 141 |
</SelectList> |
| 142 |
</SelectGroup> |
| 143 |
</Select> |
| 144 |
</ToolbarItem> |
| 145 |
</ToolbarContent> |
| 146 |
</Toolbar> |
| 147 |
<Table aria-label="Sortable table custom toolbar"> |
| 148 |
<Thead> |
| 149 |
<Tr> |
| 150 |
<Th sort={getSortParams(0)}>{columnNames.name}</Th> |
| 151 |
<Th sort={getSortParams(1)}>{columnNames.branches}</Th> |
| 152 |
<Th sort={getSortParams(2)} info={{ tooltip: 'More information ' }}> |
| 153 |
{columnNames.prs} |
| 154 |
</Th> |
| 155 |
<Th sort={getSortParams(3)}>{columnNames.workspaces}</Th> |
| 156 |
<Th sort={getSortParams(4)}>{columnNames.lastCommit}</Th> |
| 157 |
</Tr> |
| 158 |
</Thead> |
| 159 |
<Tbody> |
| 160 |
{sortedRepositories.map((repo, rowIndex) => ( |
| 161 |
<Tr key={rowIndex}> |
| 162 |
<Td dataLabel={columnNames.name}>{repo.name}</Td> |
| 163 |
<Td dataLabel={columnNames.branches}>{repo.branches}</Td> |
| 164 |
<Td dataLabel={columnNames.prs}>{repo.prs}</Td> |
| 165 |
<Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td> |
| 166 |
<Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td> |
| 167 |
</Tr> |
| 168 |
))} |
| 169 |
</Tbody> |
| 170 |
</Table> |
| 171 |
</Fragment> |
| 172 |
); |
| 173 |
}; |