Skip to content

Commit

Permalink
fix(UIShell): avoid spreading isSideNavExpanded to non Carbon eleme…
Browse files Browse the repository at this point in the history
…nts (#8135)

* fix(UIShell): avoid spreading `isSideNavExpanded` to non Carbon elements

* refactor(SideNavLink): remove unneeded destructuring

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
emyarod and kodiakhq[bot] committed Mar 24, 2021
1 parent cfcda43 commit e13a36a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
8 changes: 7 additions & 1 deletion packages/react/src/components/UIShell/SideNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { settings } from 'carbon-components';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
import { CARBON_SIDENAV_ITEMS } from './_utils';
// TO-DO: comment back in when footer is added for rails
// import SideNavFooter from './SideNavFooter';

Expand Down Expand Up @@ -87,8 +88,13 @@ const SideNav = React.forwardRef(function SideNav(props, ref) {
let currentExpansionState = controlled
? expandedViaHoverState || expanded
: expanded;
// avoid spreading `isSideNavExpanded` to non-Carbon UI Shell children
return React.cloneElement(child, {
isSideNavExpanded: currentExpansionState,
...(CARBON_SIDENAV_ITEMS.includes(child.type?.displayName)
? {
isSideNavExpanded: currentExpansionState,
}
: {}),
});
});
}
Expand Down
10 changes: 9 additions & 1 deletion packages/react/src/components/UIShell/SideNavItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { settings } from 'carbon-components';
import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import { CARBON_SIDENAV_ITEMS } from './_utils';

const { prefix } = settings;

Expand All @@ -20,7 +21,14 @@ const SideNavItems = ({
const className = cx([`${prefix}--side-nav__items`], customClassName);
const childrenWithExpandedState = React.Children.map(children, (child) => {
if (React.isValidElement(child)) {
return React.cloneElement(child, { isSideNavExpanded });
// avoid spreading `isSideNavExpanded` to non-Carbon UI Shell children
return React.cloneElement(child, {
...(CARBON_SIDENAV_ITEMS.includes(child.type?.displayName)
? {
isSideNavExpanded,
}
: {}),
});
}
});
return <ul className={className}>{childrenWithExpandedState}</ul>;
Expand Down
2 changes: 0 additions & 2 deletions packages/react/src/components/UIShell/SideNavLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const SideNavLink = React.forwardRef(function SideNavLink(
renderIcon: IconElement,
isActive,
large,
// eslint-disable-next-line no-unused-vars
isSideNavExpanded,
...rest
},
ref
Expand Down
6 changes: 6 additions & 0 deletions packages/react/src/components/UIShell/_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const CARBON_SIDENAV_ITEMS = [
'SideNavFooter',
'SideNavHeader',
'SideNavItems',
'SideNavMenu',
];

0 comments on commit e13a36a

Please sign in to comment.