Skip to content

Commit

Permalink
fix(TreeView): Fix undefined problem (#1579)
Browse files Browse the repository at this point in the history
  • Loading branch information
silvalaura authored Nov 19, 2024
1 parent 73b6aaa commit febf617
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/treeview-undefined-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-magma-dom': patch
---

fix(TreeView): Fix undefined problem
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export function useTreeView(props: UseTreeViewProps) {
}, [items, rawInitialExpandedItems]);

const itemToFocus = React.useMemo(() => {
const enabledItems = items.filter(item => !item.isDisabled);
const enabledItems = items.filter(item => !item?.isDisabled);
const [firstItem] = enabledItems;

if (selectable === TreeViewSelectable.off) {
Expand Down Expand Up @@ -230,13 +230,13 @@ export function useTreeView(props: UseTreeViewProps) {
item => item.itemId === prevItem.itemId
);

if (itemWithUpdatedDisabledState?.isDisabled === prevItem.isDisabled) {
if (itemWithUpdatedDisabledState?.isDisabled === prevItem?.isDisabled) {
return prevItem;
}

return {
...prevItem,
isDisabled: itemWithUpdatedDisabledState.isDisabled,
isDisabled: itemWithUpdatedDisabledState?.isDisabled,
};
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/react-magma-dom/src/components/TreeView/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ const getIsDisabled = ({
const isDisabled =
preselectedItem?.isDisabled !== undefined
? preselectedItem?.isDisabled
: props.isDisabled;
: props?.isDisabled;

if (selectable === TreeViewSelectable.multi && !checkChildren) {
return isDisabled;
Expand Down Expand Up @@ -281,7 +281,7 @@ const processItemCheckedStatus = ({
}) => {
const item = items.find(item => item.itemId === itemId);

if (item.isDisabled && !forceCheckedStatusForDisabled) {
if (item?.isDisabled && !forceCheckedStatusForDisabled) {
return items;
}

Expand Down Expand Up @@ -682,7 +682,7 @@ export const toggleAllMulti = ({
} & Pick<UseTreeViewProps, 'checkChildren' | 'checkParents'>) => {
if (!checkChildren) {
return items.map(item => {
if (item.isDisabled) {
if (item?.isDisabled) {
return item;
}

Expand Down

2 comments on commit febf617

@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.