Skip to content

Commit

Permalink
fix(treeview): fix initial expanded items should expand all parents (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliirumiantsev-cengage authored Sep 26, 2024
1 parent e16206f commit 24680b2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-magma-dom': patch
---

fix(TreeView): fix initial expanded items should expand all parents
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ export const Complex = args => {
Complex.args = {
selectable: TreeViewSelectable.multi,
ariaLabel: 'Textbook tree',
initialExpandedItems: ['pt1', 'pt1ch1'],
initialExpandedItems: ['pt1', 'pt1ch1', 'pt2ch5.1'],
preselectedItems: [
{ itemId: 'pt1ch1', checkedStatus: IndeterminateCheckboxStatus.checked },
{ itemId: 'pt1', checkedStatus: IndeterminateCheckboxStatus.indeterminate },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ describe('TreeView', () => {
expect(getByTestId('item3')).toHaveAttribute('aria-expanded', 'false');
});

it('when child item is part of the array, that item is expanded', () => {
it('when child item is part of the array, that item is expanded including parents', () => {
const { getByTestId } = render(
getTreeItemsMultiLevel({
initialExpandedItems: ['item2', 'item-child2.1'],
initialExpandedItems: ['item-child2.1'],
})
);

Expand Down
10 changes: 7 additions & 3 deletions packages/react-magma-dom/src/components/TreeView/useTreeView.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { useDescendants } from '../../hooks/useDescendants';
import { TreeItemSelectedInterface, TreeViewItemInterface } from './TreeViewContext';
import { getInitialItems, selectMulti, selectSingle } from './utils';
import { getInitialExpandedIds, getInitialItems, selectMulti, selectSingle } from './utils';
import { TreeViewSelectable } from './types';
import { IndeterminateCheckboxStatus } from '../IndeterminateCheckbox';

Expand Down Expand Up @@ -86,7 +86,7 @@ export function useTreeView(props: UseTreeViewProps) {
selectable = TreeViewSelectable.single,
onSelectedItemChange,
onExpandedChange,
initialExpandedItems,
initialExpandedItems: rawInitialExpandedItems,
preselectedItems,
checkChildren = selectable !== TreeViewSelectable.single,
checkParents = selectable !== TreeViewSelectable.single,
Expand All @@ -107,7 +107,11 @@ export function useTreeView(props: UseTreeViewProps) {

const selectedItems = React.useMemo(() => {
return items.filter((item) => item.checkedStatus === IndeterminateCheckboxStatus.checked)
}, [items]);
}, [items]);

const initialExpandedItems = React.useMemo(() => {
return getInitialExpandedIds({ items, initialExpandedItems: rawInitialExpandedItems })
}, [items, rawInitialExpandedItems]);

const itemToFocus = React.useMemo(() => {
const [firstItem] = items;
Expand Down
22 changes: 22 additions & 0 deletions packages/react-magma-dom/src/components/TreeView/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,25 @@ export const selectMulti = ({items, itemId, checkedStatus, checkChildren, checkP
const itemsWithProcessedChildrenSelection = checkChildren ? processChildrenSelection({ items: itemsWithProcessedItemSelection, itemId, checkedStatus }) : itemsWithProcessedItemSelection
return checkParents ? processParentsSelection({ items: itemsWithProcessedChildrenSelection, itemId, checkedStatus }) : itemsWithProcessedChildrenSelection;
}

const getParentIds = ({ items, itemId, prevParentIds = []}: { items: TreeViewItemInterface[]; itemId: TreeViewItemInterface['itemId']; prevParentIds?: TreeViewItemInterface['itemId'][] }) => {
const item = items.find(item => item.itemId === itemId);

if (!item) {
return prevParentIds;
}

const { parentId } = item;

return parentId ? getParentIds({ itemId: parentId, items, prevParentIds: [...prevParentIds, parentId]}) : prevParentIds;
}

export const getInitialExpandedIds = ({ items, initialExpandedItems }: { items: TreeViewItemInterface[]; } & Pick<UseTreeViewProps, 'initialExpandedItems'>) => {
if (!initialExpandedItems) {
return initialExpandedItems;
}

return initialExpandedItems.reduce((result, itemId) => {
return [...result, itemId, ...getParentIds({ itemId, items })];
}, []);
}

2 comments on commit 24680b2

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.