diff --git a/packages/react-table/src/demos/Table.md b/packages/react-table/src/demos/Table.md index 341692f5dd5..3e648405ad8 100644 --- a/packages/react-table/src/demos/Table.md +++ b/packages/react-table/src/demos/Table.md @@ -22,6 +22,8 @@ import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-i import BarsIcon from '@patternfly/react-icons/dist/esm/icons/bars-icon'; import AttentionBellIcon from '@patternfly/react-icons/dist/esm/icons/attention-bell-icon'; +import DashboardWrapper from '@patternfly/react-core/src/demos/examples/DashboardWrapper'; + ### Bulk select ```js @@ -268,6 +270,11 @@ class BulkSelectTableDemo extends React.Component { ```js file="table-demos/ExpandCollapseAll.jsx" ``` +### Compound expansion + +```js isFullscreen file="table-demos/CompoundExpansion.jsx" +``` + ### Column management ```js diff --git a/packages/react-table/src/demos/table-demos/CompoundExpansion.jsx b/packages/react-table/src/demos/table-demos/CompoundExpansion.jsx new file mode 100644 index 00000000000..c8f80a2dba9 --- /dev/null +++ b/packages/react-table/src/demos/table-demos/CompoundExpansion.jsx @@ -0,0 +1,260 @@ +import React from 'react'; +import { + ActionsColumn, + TableComposable, + Thead, + Tr, + Th, + Tbody, + Td, + ExpandableRowContent +} from '@patternfly/react-table'; +import { + Button, + Card, + Flex, + FlexItem, + Toolbar, + ToolbarContent, + ToolbarGroup, + ToolbarItem, + Pagination, + Select, + SelectVariant, + SelectOption, + PageSection +} from '@patternfly/react-core'; +import CodeBranchIcon from '@patternfly/react-icons/dist/esm/icons/code-branch-icon'; +import CodeIcon from '@patternfly/react-icons/dist/esm/icons/code-icon'; +import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon'; +import DashboardWrapper from '@patternfly/react-core/src/demos/examples/DashboardWrapper'; +import FilterIcon from '@patternfly/react-icons/dist/esm/icons/filter-icon'; + +export const CompoundExpandable = () => { + // In real usage, this data would come from some external source like an API via props. + const [isSelectOpen, setIsSelectOpen] = React.useState(false); + + const NestedItemsTable = () => { + // In real usage, this data would come from some external source like an API via props. + const items = [ + { description: 'Item 1', date: 'May 9, 2018', status: 'Active' }, + { description: 'Item 2', date: 'May 9, 2018', status: 'Warning' }, + { description: 'Item 3', date: 'May 9, 2018', status: 'Active' }, + { description: 'Item 4', date: 'May 9, 2018', status: 'Active' }, + { description: 'Item 5', date: 'May 9, 2018', status: 'Active' } + ]; + + const columnNames = { + description: 'Description', + date: 'Date', + status: 'Status' + }; + + return ( + + + + {columnNames.description} + {columnNames.date} + {columnNames.status} + + + + + {items.map(item => ( + + {item.description} + {item.date} + {item.status} + + + + + ))} + + + ); + }; + + const renderPagination = (variant, isCompact) => ( + + ); + + const tableToolbar = ( + + + + + Status + > + } + isOpen={isSelectOpen} + onToggle={() => setIsSelectOpen(!isSelectOpen)} + onSelect={() => setIsSelectOpen(!isSelectOpen)} + > + {[ + , + , + , + + ]} + + + + + Action + + + {renderPagination('top', true)} + + + ); + + const defaultActions = () => [ + { + title: 'Settings', + // eslint-disable-next-line no-console + onClick: () => console.log(`clicked on Settings`) + }, + { + title: 'Help', + // eslint-disable-next-line no-console + onClick: () => console.log(`clicked on Help`) + } + ]; + + const repositories = [ + { + name: 'siemur/test-space', + branches: 10, + prs: 4, + workspaces: 4, + lastCommit: '20 minutes' + }, + { name: 'siemur/test-space-2', branches: 3, prs: 4, workspaces: 4, lastCommit: '20 minutes' } + ]; + + const columnNames = { + name: 'Repositories', + branches: 'Branches', + prs: 'Pull requests', + workspaces: 'Workspaces', + lastCommit: 'Last commit' + }; + + // In this example, expanded cells are tracked by the repo and property names from each row. This could be any pair of unique identifiers. + // This is to prevent state from being based on row and column order index in case we later add sorting and rearranging columns. + // Note that this behavior is very similar to selection state. + const [expandedCells, setExpandedCells] = React.useState({ + 'siemur/test-space': 'branches' // Default to the first cell of the first row being expanded + }); + const setCellExpanded = (repo, columnKey, isExpanding = true) => { + const newExpandedCells = { ...expandedCells }; + if (isExpanding) { + newExpandedCells[repo.name] = columnKey; + } else { + delete newExpandedCells[repo.name]; + } + setExpandedCells(newExpandedCells); + }; + const compoundExpandParams = (repo, columnKey) => ({ + isExpanded: expandedCells[repo.name] === columnKey, + onToggle: () => setCellExpanded(repo, columnKey, expandedCells[repo.name] !== columnKey) + }); + + return ( + + + + {tableToolbar} + + + + {columnNames.name} + {columnNames.branches} + {columnNames.prs} + {columnNames.workspaces} + {columnNames.lastCommit} + + + + {repositories.map(repo => { + const expandedCellKey = expandedCells[repo.name]; + const isRowExpanded = !!expandedCellKey; + return ( + + + + {repo.name} + + + + + + + {repo.branches} + + + + + + + + {repo.prs} + {' '} + + + + + + + {repo.workspaces} + + + {repo.lastCommit} + + Open in GitHub + + + + + + {isRowExpanded ? ( + + + {expandedCellKey === 'branches' && repo.name === 'siemur/test-space' ? ( + + ) : ( + + + Expanded content for {repo.name}: {expandedCellKey} goes here! + + + )} + + + ) : null} + + ); + })} + + {renderPagination('bottom', false)} + + + + ); +};