import { Fragment, ReactNode, useEffect, useState } from 'react'; import { Content, Label, PageSection } from '@patternfly/react-core'; import { Table, Thead, Tbody, Tr, Th, Td, ExpandableRowContent } from '@patternfly/react-table'; import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper'; const expandableColumns = ['Servers', 'Threads', 'Applications', 'Workspaces', 'Status']; interface Server { name: string; threads: number; applications: number; workspaces: number; status: { title: ReactNode }; details: ReactNode; } const serverData: Server[] = [ { name: 'US-Node 1', threads: 18, applications: 42, workspaces: 7, status: { title: }, details: (

LocationBoston

Last Modified2 hours ago

URLhttp://www.redhat.com/en/office-locations/US-node1

) }, { name: 'US-Node 2', threads: 9, applications: 24, workspaces: 17, status: { title: }, details: (

LocationAtlanta

Last Modified5 hours ago

URLhttp://www.redhat.com/en/office-locations/US-node2

) }, { name: 'US-Node 3', threads: 8, applications: 47, workspaces: 3, status: { title: }, details: (

LocationSan Francisco

Last Modified20 minutes ago

URLhttp://www.redhat.com/en/office-locations/US-node3

) }, { name: 'US-Node 4', threads: 6, applications: 4, workspaces: 15, status: { title: }, details: (

LocationRaleigh

Last Modified10 minutes ago

URLhttp://www.redhat.com/en/office-locations/US-node4

) }, { name: 'US-Node 5', threads: 9, applications: 24, workspaces: 17, status: { title: }, details: (

LocationAtlanta

Last Modified15 minutes ago

URLhttp://www.redhat.com/en/office-locations/US-node5

) } ]; const initialExpandedServerNames = ['US-Node 2']; // Default to expanded export const TableExpandCollapseAll: React.FunctionComponent = () => { const [areAllExpanded, setAreAllExpanded] = useState(false); const [collapseAllAriaLabel, setCollapseAllAriaLabel] = useState('Expand all'); const [expandedServerNames, setExpandedServerNames] = useState(initialExpandedServerNames); useEffect(() => { const allExpanded = expandedServerNames.length === serverData.length; setAreAllExpanded(allExpanded); setCollapseAllAriaLabel(allExpanded ? 'Collapse all' : 'Expand all'); }, [expandedServerNames]); const setServerExpanded = (server: Server, isExpanding: boolean) => { const otherExpandedServerNames = expandedServerNames.filter((r) => r !== server.name); setExpandedServerNames(isExpanding ? [...otherExpandedServerNames, server.name] : otherExpandedServerNames); }; const isServerExpanded = (server: Server) => expandedServerNames.includes(server.name); // We want to be able to reference the original data object based on row index. But because an expanded // row takes up two row indexes, servers[rowIndex] will not be accurate like it would in a normal table. // One solution to this is to create an array of data objects indexed by the displayed row index. const onCollapseAll = (_event: any, _rowIndex: number, isOpen: boolean) => { setExpandedServerNames(isOpen ? [...serverData.map((server) => server.name)] : []); }; return ( ))} {serverData.map((server, serverIndex) => ( ))}
{expandableColumns.map((column) => ( {column}
setServerExpanded(server, !isServerExpanded(server)) } : undefined } > {server.details} {server?.name} {server?.threads} {server?.applications} {server?.workspaces} {server?.status?.title}
{server?.details}
); };