| 1 |
import { Fragment, useEffect, useState } from 'react'; |
| 2 |
|
| 3 |
import { |
| 4 |
Button, |
| 5 |
Content, |
| 6 |
DataList, |
| 7 |
DataListCheck, |
| 8 |
DataListCell, |
| 9 |
DataListItemCells, |
| 10 |
DataListControl, |
| 11 |
Label, |
| 12 |
Toolbar, |
| 13 |
ToolbarContent, |
| 14 |
ToolbarItem, |
| 15 |
MenuToggle, |
| 16 |
Modal, |
| 17 |
ModalHeader, |
| 18 |
ModalBody, |
| 19 |
ModalFooter, |
| 20 |
OverflowMenu, |
| 21 |
OverflowMenuGroup, |
| 22 |
OverflowMenuItem, |
| 23 |
PageSection, |
| 24 |
Pagination, |
| 25 |
PaginationVariant |
| 26 |
} from '@patternfly/react-core'; |
| 27 |
import { Table, TableText, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table'; |
| 28 |
import { DragDropSort } from '@patternfly/react-drag-drop'; |
| 29 |
import FilterIcon from '@patternfly/react-icons/dist/esm/icons/filter-icon'; |
| 30 |
import RhMicronsSortDownLargeToSmallIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-sort-down-large-to-small-icon'; |
| 31 |
import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper'; |
| 32 |
import { capitalize } from '@patternfly/react-table/src/components/Table/utils/utils'; |
| 33 |
import { rows, columns, SampleDataRow } from '@patternfly/react-table/dist/esm/demos/sampleData'; |
| 34 |
|
| 35 |
export const TableColumnManagement: React.FunctionComponent = () => { |
| 36 |
const defaultColumns = columns; |
| 37 |
const defaultRows = rows; |
| 38 |
|
| 39 |
const [filters, setFilters] = useState<string[]>([]); |
| 40 |
const [filteredColumns, setFilteredColumns] = useState<string[]>(defaultColumns); |
| 41 |
const [filteredRows, setFilteredRows] = useState<SampleDataRow[]>(defaultRows); |
| 42 |
const [managedColumns, setManagedColumns] = useState<string[]>(defaultColumns); |
| 43 |
const [managedRows, setManagedRows] = useState<SampleDataRow[]>(defaultRows); |
| 44 |
const [isModalOpen, setIsModalOpen] = useState(false); |
| 45 |
const [page, setPage] = useState(1); |
| 46 |
const [perPage, setPerPage] = useState(10); |
| 47 |
const [paginatedRows, setPaginatedRows] = useState<any[]>(rows); |
| 48 |
|
| 49 |
const [columnData, setColumnData] = useState(defaultColumns.map((col) => ({ name: col, checked: true }))); |
| 50 |
const [initialColData, setInitialColData] = useState(columnData); |
| 51 |
|
| 52 |
const matchSelectedColumnNameToAttr = (name: string): string => { |
| 53 |
switch (name) { |
| 54 |
case 'Servers': |
| 55 |
return 'name'; |
| 56 |
case 'Threads': |
| 57 |
return 'threads'; |
| 58 |
case 'Applications': |
| 59 |
return 'applications'; |
| 60 |
case 'Workspaces': |
| 61 |
return 'workspaces'; |
| 62 |
case 'Status': |
| 63 |
return 'status'; |
| 64 |
case 'Location': |
| 65 |
return 'location'; |
| 66 |
case 'Last Modified': |
| 67 |
return 'lastModified'; |
| 68 |
case 'URL': |
| 69 |
return 'url'; |
| 70 |
default: |
| 71 |
return ''; |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
// Pagination logic |
| 76 |
const handleSetPage = ( |
| 77 |
_evt: MouseEvent | React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<Element>, |
| 78 |
newPage: number |
| 79 |
) => { |
| 80 |
setPage(newPage); |
| 81 |
}; |
| 82 |
|
| 83 |
const handlePerPageSelect = ( |
| 84 |
_evt: MouseEvent | React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<Element>, |
| 85 |
newPerPage: number |
| 86 |
) => { |
| 87 |
setPerPage(newPerPage); |
| 88 |
}; |
| 89 |
|
| 90 |
const renderPagination = (variant: 'top' | 'bottom' | PaginationVariant, isCompact: boolean) => ( |
| 91 |
<Pagination |
| 92 |
isCompact={isCompact} |
| 93 |
itemCount={rows.length} |
| 94 |
page={page} |
| 95 |
perPage={perPage} |
| 96 |
onSetPage={handleSetPage} |
| 97 |
onPerPageSelect={handlePerPageSelect} |
| 98 |
variant={variant} |
| 99 |
titles={{ |
| 100 |
paginationAriaLabel: `${variant} pagination` |
| 101 |
}} |
| 102 |
/> |
| 103 |
); |
| 104 |
|
| 105 |
useEffect(() => { |
| 106 |
setPaginatedRows(managedRows.slice((page - 1) * perPage, page * perPage - 1)); |
| 107 |
}, [managedRows, page, perPage]); |
| 108 |
|
| 109 |
// Removes attribute from each node object in Data.jsx |
| 110 |
const removePropFromObject = (object: any, keys: string[]): any => |
| 111 |
keys.reduce((obj, prop) => { |
| 112 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 113 |
const { [prop]: _, ...keep } = obj; |
| 114 |
return keep; |
| 115 |
}, object); |
| 116 |
|
| 117 |
// Filters columns out of table that are not selected in the column management modal |
| 118 |
const filterData = (checked: boolean, name: string) => { |
| 119 |
const selectedColumn = matchSelectedColumnNameToAttr(name); |
| 120 |
|
| 121 |
const filteredRows: SampleDataRow[] = []; |
| 122 |
if (checked) { |
| 123 |
const updatedFilters = filters.filter((item) => item !== selectedColumn); |
| 124 |
|
| 125 |
// Only show the names of columns that were selected in the modal |
| 126 |
const filteredColumns = defaultColumns.filter( |
| 127 |
(column) => !updatedFilters.includes(matchSelectedColumnNameToAttr(column)) |
| 128 |
); |
| 129 |
|
| 130 |
// Remove the attributes (i.e. "columns") that were not selected |
| 131 |
defaultRows.forEach((item) => filteredRows.push(removePropFromObject(item, updatedFilters))); |
| 132 |
|
| 133 |
setFilters(updatedFilters); |
| 134 |
setFilteredColumns(filteredColumns); |
| 135 |
setFilteredRows(filteredRows); |
| 136 |
} else { |
| 137 |
const updatedFilters = filters; |
| 138 |
updatedFilters.push(selectedColumn); |
| 139 |
|
| 140 |
// Only show the names of columns that were selected in the modal |
| 141 |
const filteredColumns = managedColumns.filter( |
| 142 |
(column) => !filters.includes(matchSelectedColumnNameToAttr(column)) |
| 143 |
); |
| 144 |
|
| 145 |
// Remove the attributes (i.e. "columns") that were not selected |
| 146 |
managedRows.forEach((item) => filteredRows.push(removePropFromObject(item, updatedFilters))); |
| 147 |
|
| 148 |
setFilters(updatedFilters); |
| 149 |
setFilteredColumns(filteredColumns); |
| 150 |
setFilteredRows(filteredRows); |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
const unfilterAllData = () => { |
| 155 |
setFilters([]); |
| 156 |
setFilteredColumns(defaultColumns); |
| 157 |
setFilteredRows(defaultRows); |
| 158 |
}; |
| 159 |
|
| 160 |
const handleModalToggle = (_event: React.MouseEvent<Element, MouseEvent> | KeyboardEvent) => { |
| 161 |
setIsModalOpen(!isModalOpen); |
| 162 |
setInitialColData(columnData); |
| 163 |
}; |
| 164 |
|
| 165 |
const onSave = () => { |
| 166 |
const orderedFilteredColumns: string[] = []; |
| 167 |
columnData.map((col) => { |
| 168 |
if (filteredColumns.find((filteredCol) => filteredCol === col.name)) { |
| 169 |
orderedFilteredColumns.push(col.name); |
| 170 |
} |
| 171 |
}); |
| 172 |
|
| 173 |
const orderedFilteredRows = filteredRows.map((row) => { |
| 174 |
let orderedRow; |
| 175 |
columnData.map((col) => { |
| 176 |
if (filteredColumns.find((filteredCol) => filteredCol === col.name)) { |
| 177 |
const rowField = matchSelectedColumnNameToAttr(col.name); |
| 178 |
orderedRow = { ...orderedRow, [rowField]: row[rowField] }; |
| 179 |
} |
| 180 |
}); |
| 181 |
return orderedRow; |
| 182 |
}); |
| 183 |
|
| 184 |
setManagedColumns(orderedFilteredColumns); |
| 185 |
setManagedRows(orderedFilteredRows); |
| 186 |
setPaginatedRows(filteredRows); |
| 187 |
setIsModalOpen(!isModalOpen); |
| 188 |
}; |
| 189 |
|
| 190 |
const selectAllColumns = () => { |
| 191 |
unfilterAllData(); |
| 192 |
setColumnData(columnData.map((col) => ({ name: col.name, checked: true }))); |
| 193 |
}; |
| 194 |
|
| 195 |
const renderModal = () => ( |
| 196 |
<Modal |
| 197 |
isOpen={isModalOpen} |
| 198 |
variant="small" |
| 199 |
onClose={handleModalToggle} |
| 200 |
aria-labelledby="basic-modal-title" |
| 201 |
aria-describedby="modal-box-body-basic" |
| 202 |
> |
| 203 |
<ModalHeader |
| 204 |
title="Manage columns" |
| 205 |
labelId="basic-modal-title" |
| 206 |
description={ |
| 207 |
<Content> |
| 208 |
<p>Selected categories will be displayed in the table.</p> |
| 209 |
<Button isInline onClick={selectAllColumns} variant="secondary"> |
| 210 |
Select all |
| 211 |
</Button> |
| 212 |
</Content> |
| 213 |
} |
| 214 |
/> |
| 215 |
<ModalBody id="modal-box-body-basic"> |
| 216 |
<DragDropSort |
| 217 |
items={columnData.map((colData) => ({ |
| 218 |
id: colData.name, |
| 219 |
content: ( |
| 220 |
<> |
| 221 |
<DataListControl> |
| 222 |
<DataListCheck |
| 223 |
aria-labelledby={`table-column-management-item-${colData.name}`} |
| 224 |
isChecked={colData.checked} |
| 225 |
name={`check-${colData.name}`} |
| 226 |
id={`check-${colData.name}`} |
| 227 |
onChange={(event: React.FormEvent<HTMLInputElement>, _checked: boolean) => { |
| 228 |
let newColumnData: { name: string; checked: boolean }[] = []; |
| 229 |
|
| 230 |
columnData.map((colData) => { |
| 231 |
if (event.currentTarget.name === `check-${colData.name}`) { |
| 232 |
newColumnData = [...newColumnData, { name: colData.name, checked: !colData.checked }]; |
| 233 |
filterData(!colData.checked, colData.name); |
| 234 |
} else { |
| 235 |
newColumnData = [...newColumnData, colData]; |
| 236 |
} |
| 237 |
}); |
| 238 |
|
| 239 |
setColumnData(newColumnData); |
| 240 |
}} |
| 241 |
/> |
| 242 |
</DataListControl> |
| 243 |
<DataListItemCells |
| 244 |
dataListCells={[ |
| 245 |
<DataListCell |
| 246 |
id={`table-column-management-item-${colData.name}`} |
| 247 |
key={`table-column-management-item-${colData.name}`} |
| 248 |
> |
| 249 |
<label htmlFor={`check-${colData.name}`}>{colData.name}</label> |
| 250 |
</DataListCell> |
| 251 |
]} |
| 252 |
/> |
| 253 |
</> |
| 254 |
), |
| 255 |
data: colData |
| 256 |
}))} |
| 257 |
onDrop={(_, newItems) => { |
| 258 |
setColumnData(newItems.map((item) => (item as any).data)); |
| 259 |
}} |
| 260 |
variant="DataList" |
| 261 |
overlayProps={{ isCompact: true }} |
| 262 |
> |
| 263 |
<DataList aria-label="Table column management" id="table-column-management" isCompact /> |
| 264 |
</DragDropSort> |
| 265 |
</ModalBody> |
| 266 |
<ModalFooter> |
| 267 |
<Button key="save" variant="primary" onClick={onSave}> |
| 268 |
Save |
| 269 |
</Button> |
| 270 |
<Button |
| 271 |
key="cancel" |
| 272 |
variant="link" |
| 273 |
onClick={(event: React.MouseEvent<Element, MouseEvent> | KeyboardEvent) => { |
| 274 |
setColumnData(initialColData); |
| 275 |
handleModalToggle(event); |
| 276 |
}} |
| 277 |
> |
| 278 |
Cancel |
| 279 |
</Button> |
| 280 |
</ModalFooter> |
| 281 |
</Modal> |
| 282 |
); |
| 283 |
|
| 284 |
const renderLabel = (labelText: string): React.JSX.Element => { |
| 285 |
switch (labelText) { |
| 286 |
case 'Running': |
| 287 |
return <Label color="green">{labelText}</Label>; |
| 288 |
case 'Stopped': |
| 289 |
return <Label color="orange">{labelText}</Label>; |
| 290 |
case 'Needs maintenance': |
| 291 |
return <Label color="blue">{labelText}</Label>; |
| 292 |
case 'Down': |
| 293 |
return <Label color="red">{labelText}</Label>; |
| 294 |
default: |
| 295 |
return <></>; |
| 296 |
} |
| 297 |
}; |
| 298 |
|
| 299 |
const toolbarItems = ( |
| 300 |
<Fragment> |
| 301 |
<Toolbar id="page-layout-table-column-management-action-toolbar-top"> |
| 302 |
<span id="page-layout-table-column-management-action-toolbar-top-select-checkbox-label" hidden> |
| 303 |
Choose one |
| 304 |
</span> |
| 305 |
<ToolbarContent> |
| 306 |
<ToolbarItem> |
| 307 |
<OverflowMenu breakpoint="md"> |
| 308 |
<OverflowMenuItem> |
| 309 |
<MenuToggle icon={<FilterIcon />}>Name</MenuToggle> |
| 310 |
</OverflowMenuItem> |
| 311 |
<OverflowMenuItem> |
| 312 |
<MenuToggle |
| 313 |
variant="plain" |
| 314 |
aria-label="Sort columns" |
| 315 |
icon={<RhMicronsSortDownLargeToSmallIcon aria-hidden="true" />} |
| 316 |
/> |
| 317 |
</OverflowMenuItem> |
| 318 |
<OverflowMenuGroup groupType="button" isPersistent> |
| 319 |
<OverflowMenuItem> |
| 320 |
<Button variant="primary">Action</Button> |
| 321 |
</OverflowMenuItem> |
| 322 |
<OverflowMenuItem isPersistent> |
| 323 |
<Button variant="link" onClick={handleModalToggle}> |
| 324 |
Manage columns |
| 325 |
</Button> |
| 326 |
</OverflowMenuItem> |
| 327 |
</OverflowMenuGroup> |
| 328 |
</OverflowMenu> |
| 329 |
</ToolbarItem> |
| 330 |
<ToolbarItem variant="pagination">{renderPagination('top', false)}</ToolbarItem> |
| 331 |
</ToolbarContent> |
| 332 |
</Toolbar> |
| 333 |
</Fragment> |
| 334 |
); |
| 335 |
|
| 336 |
return ( |
| 337 |
<Fragment> |
| 338 |
<DashboardWrapper hasPageTemplateTitle> |
| 339 |
<PageSection isFilled aria-label="Draggable Column Management table data"> |
| 340 |
{toolbarItems} |
| 341 |
<Table variant="compact" aria-label="Column Management Table"> |
| 342 |
<Thead> |
| 343 |
<Tr> |
| 344 |
{managedColumns.map((column, columnIndex) => ( |
| 345 |
<Th key={columnIndex}>{column}</Th> |
| 346 |
))} |
| 347 |
</Tr> |
| 348 |
</Thead> |
| 349 |
<Tbody> |
| 350 |
{paginatedRows.map((row, rowIndex) => ( |
| 351 |
<Tr key={rowIndex}> |
| 352 |
<> |
| 353 |
{Object.entries(row).map(([key, value], idx) => |
| 354 |
// eslint-disable-next-line no-nested-ternary |
| 355 |
key === 'status' ? ( |
| 356 |
<Td width={15} dataLabel="Status" key={`${idx}-${value}`}> |
| 357 |
{renderLabel(value as string)} |
| 358 |
</Td> |
| 359 |
) : key === 'url' ? ( |
| 360 |
<Td width={10} dataLabel="URL" modifier="truncate" key={`${idx}-${value}`}> |
| 361 |
<TableText> |
| 362 |
<a href="#">{row.url}</a> |
| 363 |
</TableText> |
| 364 |
</Td> |
| 365 |
) : ( |
| 366 |
<Td |
| 367 |
width={key === 'name' ? 15 : 10} |
| 368 |
dataLabel={key === 'lastModified' ? 'Last modified' : capitalize(key)} |
| 369 |
key={`${idx}-${value}`} |
| 370 |
> |
| 371 |
{value as string} |
| 372 |
</Td> |
| 373 |
) |
| 374 |
)} |
| 375 |
</> |
| 376 |
</Tr> |
| 377 |
))} |
| 378 |
</Tbody> |
| 379 |
</Table> |
| 380 |
{renderPagination('bottom', false)} |
| 381 |
{renderModal()} |
| 382 |
</PageSection> |
| 383 |
</DashboardWrapper> |
| 384 |
</Fragment> |
| 385 |
); |
| 386 |
}; |