Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DataViewTable, DataViewTh, DataViewTrTree } from '@patternfly/react-dat
import { useDataViewSelection } from '@patternfly/react-data-view/dist/dynamic/Hooks';
import { FolderIcon, FolderOpenIcon, LeafIcon } from '@patternfly/react-icons';


interface Repository {
name: string;
branches: string | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ To define rows and columns for your table, use these props:

It is also possible to disable row selection using the `isSelectDisabled` function, which can be passed to the wrapping `DataView` component through the `selection` prop.

If you want to have all expandable nodes open on initial load pass the `expandAll` prop to the DataViewTable component

### Table example
```js file="./DataViewTableExample.tsx"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ describe('DataViewTableTree component', () => {
expect(container).toMatchSnapshot();
});

test('should render tree table with all expandable nodes expanded', () => {
const { container } = render(
<DataView selection={mockSelection}>
<DataViewTable isTreeTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} expandAll rows={rows} leafIcon={<LeafIcon/>} expandedIcon={<FolderOpenIcon aria-hidden />} collapsedIcon={<FolderIcon aria-hidden />} />
</DataView>
);
expect(container).toMatchSnapshot();
});

test('should render tree table with a loading state', () => {
const { container } = render(
<DataView activeState="loading">
Expand Down
32 changes: 31 additions & 1 deletion packages/module/src/DataViewTableTree/DataViewTableTree.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useState, useMemo, ReactNode } from 'react';
import { FC, useState, useMemo, useEffect, ReactNode } from 'react';
import {
Table,
TableProps,
Expand Down Expand Up @@ -67,6 +67,8 @@ export interface DataViewTableTreeProps extends Omit<TableProps, 'onSelect' | 'r
expandedIcon?: React.ReactNode;
/** Optional icon for the collapsed parent rows */
collapsedIcon?: React.ReactNode;
/** Expand all expandable nodes on initial load */
expandAll?: boolean;
/** Custom OUIA ID */
ouiaId?: string;
}
Expand All @@ -79,6 +81,7 @@ export const DataViewTableTree: FC<DataViewTableTreeProps> = ({
leafIcon = null,
expandedIcon = null,
collapsedIcon = null,
expandAll = false,
ouiaId = 'DataViewTableTree',
...props
}: DataViewTableTreeProps) => {
Expand All @@ -87,6 +90,33 @@ export const DataViewTableTree: FC<DataViewTableTreeProps> = ({
const [ expandedNodeIds, setExpandedNodeIds ] = useState<string[]>([]);
const [ expandedDetailsNodeNames, setExpandedDetailsNodeIds ] = useState<string[]>([]);

// Helper function to collect all node IDs that have children (are expandable)
const getExpandableNodeIds = (nodes: DataViewTrTree[]): string[] => {
const expandableIds: string[] = [];

const traverse = (nodeList: DataViewTrTree[]) => {
nodeList.forEach(node => {
if (node.children && node.children.length > 0) {
expandableIds.push(node.id);
traverse(node.children);
}
});
};

traverse(nodes);
return expandableIds;
};

// Effect to handle expandAll behavior
useEffect(() => {
if (expandAll) {
const expandableIds = getExpandableNodeIds(rows);
setExpandedNodeIds(expandableIds);
} else {
setExpandedNodeIds([]);
}
}, [ expandAll, rows ]);

const activeHeadState = useMemo(() => activeState ? headStates?.[activeState] : undefined, [ activeState, headStates ]);
const activeBodyState = useMemo(() => activeState ? bodyStates?.[activeState] : undefined, [ activeState, bodyStates ]);

Expand Down
Loading
Loading