| 1 |
import { useState } from 'react'; |
| 2 |
import { |
| 3 |
Dropdown, |
| 4 |
DropdownList, |
| 5 |
DropdownItem, |
| 6 |
MenuToggle, |
| 7 |
MenuToggleCheckbox, |
| 8 |
MenuToggleElement, |
| 9 |
PageSection, |
| 10 |
Pagination, |
| 11 |
Toolbar, |
| 12 |
ToolbarContent, |
| 13 |
ToolbarGroup, |
| 14 |
ToolbarItem, |
| 15 |
PaginationVariant |
| 16 |
} from '@patternfly/react-core'; |
| 17 |
import { Table, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table'; |
| 18 |
import { rows, columns, SampleDataRow } from '@patternfly/react-table/dist/esm/demos/sampleData'; |
| 19 |
import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper'; |
| 20 |
|
| 21 |
export const TableBulkSelect: React.FunctionComponent = () => { |
| 22 |
const [isBulkSelectDropdownOpen, setIsBulkSelectDropdownOpen] = useState(false); |
| 23 |
const [bulkSelection, setBulkSelection] = useState(''); |
| 24 |
const [page, setPage] = useState(1); |
| 25 |
const [perPage, setPerPage] = useState(10); |
| 26 |
const [paginatedRows, setPaginatedRows] = useState(rows.slice(0, 10)); |
| 27 |
const [selectedRows, setSelectedRows] = useState<string[]>([]); |
| 28 |
|
| 29 |
const handleSetPage = ( |
| 30 |
_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, |
| 31 |
newPage: number, |
| 32 |
_perPage: number, |
| 33 |
startIdx: number, |
| 34 |
endIdx: number |
| 35 |
) => { |
| 36 |
setPaginatedRows(rows?.slice(startIdx, endIdx)); |
| 37 |
setPage(newPage); |
| 38 |
}; |
| 39 |
|
| 40 |
const handlePerPageSelect = ( |
| 41 |
_evt: React.MouseEvent | React.KeyboardEvent | MouseEvent, |
| 42 |
newPerPage: number, |
| 43 |
newPage: number, |
| 44 |
startIdx: number, |
| 45 |
endIdx: number |
| 46 |
) => { |
| 47 |
setPaginatedRows(rows.slice(startIdx, endIdx)); |
| 48 |
setPage(newPage); |
| 49 |
setPerPage(newPerPage); |
| 50 |
}; |
| 51 |
|
| 52 |
const setRowSelected = (row: SampleDataRow, isSelecting: boolean) => |
| 53 |
setSelectedRows((prevSelected) => { |
| 54 |
const otherSelectedRows = prevSelected.filter((r) => r !== row.name); |
| 55 |
return isSelecting ? [...otherSelectedRows, row.name] : otherSelectedRows; |
| 56 |
}); |
| 57 |
|
| 58 |
const selectAllRows = (isSelecting: boolean) => setSelectedRows(isSelecting ? rows.map((r) => r.name) : []); |
| 59 |
|
| 60 |
const selectPageRows = (isSelecting: boolean) => setSelectedRows(isSelecting ? paginatedRows.map((r) => r.name) : []); |
| 61 |
|
| 62 |
const isRowSelected = (row: any) => selectedRows.includes(row.name); |
| 63 |
|
| 64 |
const buildPagination = (variant: 'bottom' | 'top' | PaginationVariant, isCompact: boolean) => ( |
| 65 |
<Pagination |
| 66 |
isCompact={isCompact} |
| 67 |
itemCount={rows.length} |
| 68 |
page={page} |
| 69 |
perPage={perPage} |
| 70 |
onSetPage={handleSetPage} |
| 71 |
onPerPageSelect={handlePerPageSelect} |
| 72 |
variant={variant} |
| 73 |
titles={{ |
| 74 |
paginationAriaLabel: `${variant} pagination` |
| 75 |
}} |
| 76 |
/> |
| 77 |
); |
| 78 |
|
| 79 |
const buildBulkSelectDropdown = () => { |
| 80 |
const numSelected = selectedRows.length; |
| 81 |
const allSelected = numSelected === rows.length; |
| 82 |
const anySelected = numSelected > 0; |
| 83 |
const someChecked = anySelected ? null : false; |
| 84 |
const isChecked = allSelected ? true : someChecked; |
| 85 |
|
| 86 |
const items = ( |
| 87 |
<> |
| 88 |
<DropdownItem value="none">Select none (0 items)</DropdownItem> |
| 89 |
<DropdownItem value="page">Select page ({perPage} items)</DropdownItem> |
| 90 |
<DropdownItem value="all">Select all ({rows.length} items)</DropdownItem> |
| 91 |
</> |
| 92 |
); |
| 93 |
|
| 94 |
return ( |
| 95 |
<Dropdown |
| 96 |
role="menu" |
| 97 |
onSelect={(_event: React.MouseEvent<Element, MouseEvent>, value: string) => { |
| 98 |
if (value === 'all') { |
| 99 |
selectAllRows(bulkSelection !== 'all'); |
| 100 |
} else if (value === 'page') { |
| 101 |
selectPageRows(bulkSelection !== 'page'); |
| 102 |
} else { |
| 103 |
setSelectedRows([]); |
| 104 |
} |
| 105 |
setBulkSelection(value as string); |
| 106 |
}} |
| 107 |
isOpen={isBulkSelectDropdownOpen} |
| 108 |
onOpenChange={(isOpen: boolean) => setIsBulkSelectDropdownOpen(isOpen)} |
| 109 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 110 |
<MenuToggle |
| 111 |
ref={toggleRef} |
| 112 |
isExpanded={isBulkSelectDropdownOpen} |
| 113 |
onClick={() => setIsBulkSelectDropdownOpen(!isBulkSelectDropdownOpen)} |
| 114 |
aria-label="Select cards" |
| 115 |
splitButtonItems={[ |
| 116 |
<MenuToggleCheckbox |
| 117 |
id="split-dropdown-checkbox" |
| 118 |
key="split-dropdown-checkbox" |
| 119 |
aria-label={anySelected ? 'Deselect all cards' : 'Select all cards'} |
| 120 |
isChecked={isChecked} |
| 121 |
onClick={() => { |
| 122 |
anySelected ? setSelectedRows([]) : selectAllRows(bulkSelection !== 'all'); |
| 123 |
}} |
| 124 |
> |
| 125 |
{numSelected !== 0 && `${numSelected} selected`} |
| 126 |
</MenuToggleCheckbox> |
| 127 |
]} |
| 128 |
></MenuToggle> |
| 129 |
)} |
| 130 |
> |
| 131 |
<DropdownList>{items}</DropdownList> |
| 132 |
</Dropdown> |
| 133 |
); |
| 134 |
}; |
| 135 |
|
| 136 |
const toolbar = ( |
| 137 |
<Toolbar> |
| 138 |
<ToolbarContent> |
| 139 |
<ToolbarGroup> |
| 140 |
<ToolbarItem variant="bulk-select">{buildBulkSelectDropdown()}</ToolbarItem> |
| 141 |
</ToolbarGroup> |
| 142 |
<ToolbarItem variant="pagination">{buildPagination('top', false)}</ToolbarItem> |
| 143 |
</ToolbarContent> |
| 144 |
</Toolbar> |
| 145 |
); |
| 146 |
|
| 147 |
return ( |
| 148 |
<DashboardWrapper hasPageTemplateTitle> |
| 149 |
<PageSection isWidthLimited aria-label="Bulk select table data"> |
| 150 |
{toolbar} |
| 151 |
<Table aria-label="Selectable table"> |
| 152 |
<Thead> |
| 153 |
<Tr> |
| 154 |
<Th screenReaderText="Row select" /> |
| 155 |
<Th key={0}>{columns[0]}</Th> |
| 156 |
<Th key={1}>{columns[1]}</Th> |
| 157 |
<Th key={2}>{columns[2]}</Th> |
| 158 |
<Th key={3}>{columns[3]}</Th> |
| 159 |
</Tr> |
| 160 |
</Thead> |
| 161 |
<Tbody> |
| 162 |
{paginatedRows.map((row, rowIndex) => ( |
| 163 |
<Tr key={row.name}> |
| 164 |
<Td |
| 165 |
select={{ |
| 166 |
rowIndex, |
| 167 |
onSelect: (_event: React.FormEvent<HTMLInputElement>, isSelecting: boolean) => |
| 168 |
setRowSelected(row, isSelecting), |
| 169 |
isSelected: isRowSelected(row) |
| 170 |
}} |
| 171 |
/> |
| 172 |
<Td dataLabel={columns[0]}>{row.name}</Td> |
| 173 |
<Td dataLabel={columns[1]}>{row.threads}</Td> |
| 174 |
<Td dataLabel={columns[2]}>{row.applications}</Td> |
| 175 |
<Td dataLabel={columns[3]}>{row.workspaces}</Td> |
| 176 |
</Tr> |
| 177 |
))} |
| 178 |
</Tbody> |
| 179 |
</Table> |
| 180 |
{buildPagination('bottom', true)} |
| 181 |
</PageSection> |
| 182 |
</DashboardWrapper> |
| 183 |
); |
| 184 |
}; |