Skip to content

Commit

Permalink
feat(TreeView): Fixing onSelectedItemChange (#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
silvalaura committed Sep 18, 2024
1 parent dcfdaa7 commit 8dda990
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/treeview-5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-magma-dom": minor
---

feat(TreeView): TreeView & TreeItem updates
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const Complex = args => {
label={<>Chapter 1: I love tiramisu jelly beans soufflé</>}
itemId="pt1ch1"
testId="pt1ch1"
isDisabled
// isDisabled
>
<TreeItem
icon={<ArticleIcon aria-hidden={true} />}
Expand Down
16 changes: 13 additions & 3 deletions packages/react-magma-dom/src/components/TreeView/TreeView.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,11 @@ describe('TreeView', () => {
describe('initialExpandedItems and initialSelectedItems', () => {
it('when initialExpandedItems and initialSelectedItems are empty, no TreeItem is expanded or selected', () => {
const { getByTestId } = render(
getTreeItemsOneLevel({ selectable: TreeViewSelectable.multi, initialExpandedItems: [], initialSelectedItems: []})
getTreeItemsOneLevel({
selectable: TreeViewSelectable.multi,
initialExpandedItems: [],
initialSelectedItems: [],
})
);

expect(getByTestId('item1')).toHaveAttribute('aria-expanded', 'false');
Expand Down Expand Up @@ -1128,11 +1132,17 @@ describe('TreeView', () => {
expect(getByTestId('item0')).toHaveAttribute('aria-checked', 'false');
expect(getByTestId('item1')).toHaveAttribute('aria-checked', 'false');
expect(getByTestId('item2')).toHaveAttribute('aria-checked', 'false');
expect(getByTestId('item-child2.1')).toHaveAttribute('aria-checked', 'true');
expect(getByTestId('item-child2.1')).toHaveAttribute(
'aria-checked',
'true'
);
expect(getByTestId('item3')).toHaveAttribute('aria-checked', 'true');

userEvent.click(getByTestId('item3-expand'));
expect(getByTestId('item-child3')).toHaveAttribute('aria-checked', 'true');
expect(getByTestId('item-child3')).toHaveAttribute(
'aria-checked',
'true'
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
getChildrenItemIdsInTree,
getAllParentIds,
getChildrenItemIdsFlat,
getCheckedStatus,
} from './utils';

export interface UseTreeItemProps extends React.HTMLAttributes<HTMLLIElement> {
Expand Down Expand Up @@ -522,7 +523,6 @@ export function useTreeItem(props: UseTreeItemProps, forwardedRef) {
) {
setStatusUpdatedBy(StatusUpdatedByOptions.children);
setCheckedStatus(statusFromChildren);

if (
statusFromChildren === IndeterminateCheckboxStatus.checked ||
statusFromChildren === IndeterminateCheckboxStatus.indeterminate
Expand Down Expand Up @@ -580,6 +580,9 @@ export function useTreeItem(props: UseTreeItemProps, forwardedRef) {
}
} else {
setSelectedItems(updateItemStatus);
if (!expanded) {
updateSelectedItemsChanged();
}
}
}
}, [childrenCheckedStatus]);
Expand All @@ -601,7 +604,7 @@ export function useTreeItem(props: UseTreeItemProps, forwardedRef) {
Array(numberOfTreeItemChildren).fill(status)
);
} else {
const childrenIds = getChildrenItemIds(treeItemChildren, 'something');
const childrenIds = getChildrenItemIds(treeItemChildren);

const newChildrenCheckedStatus = getChildrenCheckedStatus(
childrenIds,
Expand Down Expand Up @@ -660,7 +663,6 @@ export function useTreeItem(props: UseTreeItemProps, forwardedRef) {
childrenIds,
{ itemId, checkedStatus }
);

setSelectedItems(newSelectedItems);
updateSelectedItemsChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function useTreeView(props: UseTreeViewProps) {
setInitialSelectedItemsNeedUpdate(true);
}
if (initialExpandedItems) {
setInitialExpandedItemsNeedUpdate(true)
setInitialExpandedItemsNeedUpdate(true);
}
}, []);

Expand Down

0 comments on commit 8dda990

Please sign in to comment.