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