diff --git a/.changeset/treeview-undefined4.md b/.changeset/treeview-undefined4.md new file mode 100644 index 000000000..f0fb94916 --- /dev/null +++ b/.changeset/treeview-undefined4.md @@ -0,0 +1,5 @@ +--- +'react-magma-dom': patch +--- + +fix(TreeView): Fix undefined problem diff --git a/packages/react-magma-dom/src/components/TreeView/TreeItem.tsx b/packages/react-magma-dom/src/components/TreeView/TreeItem.tsx index 6113fe95d..aad3a993a 100644 --- a/packages/react-magma-dom/src/components/TreeView/TreeItem.tsx +++ b/packages/react-magma-dom/src/components/TreeView/TreeItem.tsx @@ -387,7 +387,7 @@ export const TreeItem = React.forwardRef( {React.Children.map( children, (child: React.ReactElement, index) => { - return child.type === TreeItem ? ( + return child?.type === TreeItem ? (
    {React.cloneElement(child, { diff --git a/packages/react-magma-dom/src/components/TreeView/TreeView.stories.tsx b/packages/react-magma-dom/src/components/TreeView/TreeView.stories.tsx index 900d64500..4fafb935f 100644 --- a/packages/react-magma-dom/src/components/TreeView/TreeView.stories.tsx +++ b/packages/react-magma-dom/src/components/TreeView/TreeView.stories.tsx @@ -1049,7 +1049,7 @@ export const InvalidTreeItems = (args: Partial) => { This is an example of a tree with badly structured tree items. Expect only the following items to be expandable: Node 1, Child 1, Node 2, - Child 2, Grandchild 2. + Child 2, Grandchild 2, Node 6.

    @@ -1058,14 +1058,14 @@ export const InvalidTreeItems = (args: Partial) => { - + This is a tag as a child of Grandchild 1 - + <>Invalid child @@ -1073,10 +1073,20 @@ export const InvalidTreeItems = (args: Partial) => { - - + + Child of node 4 is just text + + {null} + + + {undefined} + + + {undefined} + + ); diff --git a/packages/react-magma-dom/src/components/TreeView/TreeView.test.js b/packages/react-magma-dom/src/components/TreeView/TreeView.test.js index d7e2747b9..33cfd80da 100644 --- a/packages/react-magma-dom/src/components/TreeView/TreeView.test.js +++ b/packages/react-magma-dom/src/components/TreeView/TreeView.test.js @@ -2492,6 +2492,29 @@ describe('TreeView', () => { expect(getByTestId('item-child2-expand')).toBeInTheDocument(); }); + it('when multiple TreeViews are passed as a child and at least one is valid and the other is undefined, the tree item is expandable', () => { + const { getByTestId } = render( + + + + {undefined} + + {undefined} + + + + + + ); + + expect(getByTestId('item1-expand')).toBeInTheDocument(); + userEvent.click(getByTestId('item1-expand')); + expect(getByTestId('item-child2-expand')).toBeInTheDocument(); + }); it('when a fragment is passed as a child, the tree item is not expandable', () => { const { queryByTestId } = render( @@ -2535,6 +2558,34 @@ describe('TreeView', () => { expect(queryByTestId('item1-expand')).not.toBeInTheDocument(); }); + it('when undefined is passed as a child, the tree item is not expandable', () => { + const { queryByTestId } = render( + + + {undefined} + + + + ); + + expect(queryByTestId('item1-expand')).not.toBeInTheDocument(); + expect(queryByTestId('item2-expand')).not.toBeInTheDocument(); + }); + + it('when null is passed as a child, the tree item is not expandable', () => { + const { queryByTestId } = render( + + + {null} + + + + ); + + expect(queryByTestId('item1-expand')).not.toBeInTheDocument(); + expect(queryByTestId('item2-expand')).not.toBeInTheDocument(); + }); + it('when a TreeView does not have a child, the tree item is not expandable', () => { const { queryByTestId } = render( diff --git a/packages/react-magma-dom/src/components/TreeView/utils.ts b/packages/react-magma-dom/src/components/TreeView/utils.ts index fbb40cd8f..35ba94cdd 100644 --- a/packages/react-magma-dom/src/components/TreeView/utils.ts +++ b/packages/react-magma-dom/src/components/TreeView/utils.ts @@ -217,7 +217,7 @@ const areChildrenValid = children => { return false; // Return false if a child is a string } - if (child.type !== TreeItem) { + if (child?.type !== TreeItem) { return hasValidChild; } // Recursively check the validity of nested children @@ -575,13 +575,12 @@ const processParentsSelection = ({ checkedStatus: TreeViewItemInterface['checkedStatus']; }) => { const item = items.find(item => item.itemId === itemId); - const { parentId } = item; - if (parentId === null) { + if (item?.parentId === null) { return items; } - const siblings = items.filter(item => item?.parentId === parentId); + const siblings = items.filter(i => i.parentId === item?.parentId); const isAllSiblingsHasTheSameStatus = siblings.every( item => (item.checkedStatus || IndeterminateCheckboxStatus.unchecked) === @@ -591,7 +590,7 @@ const processParentsSelection = ({ ? checkedStatus : IndeterminateCheckboxStatus.indeterminate; - const parent = items.find(item => item.itemId === parentId); + const parent = items.find(i => i.itemId === item?.parentId); const nextItems = items.map(item => item.itemId === parent.itemId