From c89388a58a493fadc787716b9283a341a96645ba Mon Sep 17 00:00:00 2001 From: Nirajsah Date: Sun, 8 Oct 2023 00:20:14 +0530 Subject: [PATCH] feat(component): add SideNavItems.tsx component --- .all-contributorsrc | 9 +++ README.md | 1 + .../src/components/UIShell/SideNavItems.tsx | 77 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 packages/react/src/components/UIShell/SideNavItems.tsx diff --git a/.all-contributorsrc b/.all-contributorsrc index 6e3ad91a49d7..0d2efe5dfd47 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1268,6 +1268,15 @@ "contributions": [ "code" ] + }, + { + "login": "Nirajsah", + "name": "Niraj Sah", + "avatar_url": "https://avatars.githubusercontent.com/u/51414373?v=4", + "profile": "https://github.com/Nirajsah", + "contributions": [ + "code" + ] } ], "commitConvention": "none" diff --git a/README.md b/README.md index 2118c5c3ba11..0c10e11ef7e4 100644 --- a/README.md +++ b/README.md @@ -254,6 +254,7 @@ check out our [Contributing Guide](/.github/CONTRIBUTING.md) and our
cordesmj

💻
Aziz Chebbi

💻
Michał Konopski

💻 +
Niraj Sah

💻 diff --git a/packages/react/src/components/UIShell/SideNavItems.tsx b/packages/react/src/components/UIShell/SideNavItems.tsx new file mode 100644 index 000000000000..c0032ca4d6c3 --- /dev/null +++ b/packages/react/src/components/UIShell/SideNavItems.tsx @@ -0,0 +1,77 @@ +/** + * Copyright IBM Corp. 2016, 2023 + * + * This source code is licensed under the Apache-2.0 license found in the + * LICENSE file in the root directory of this source tree. + */ + +import cx from 'classnames'; +import PropTypes from 'prop-types'; +import React from 'react'; +import { CARBON_SIDENAV_ITEMS } from './_utils'; +import { usePrefix } from '../../internal/usePrefix'; + +interface SideNavItemsProps { + /** + * Provide an optional class to be applied to the containing node + */ + className?: string; + /** + * Provide a single icon as the child to `SideNavIcon` to render in the + * container, and it is required + */ + children: React.ReactNode; + /** + * Property to indicate if the side nav container is open (or not). Use to + * keep local state and styling in step with the SideNav expansion state. + */ + isSideNavExpanded?: boolean; +} + +const SideNavItems: React.FC = ({ + className: customClassName, + children, + isSideNavExpanded, +}) => { + const prefix = usePrefix(); + const className = cx([`${prefix}--side-nav__items`], customClassName); + const childrenWithExpandedState = React.Children.map(children, (child) => { + // avoid spreading `isSideNavExpanded` to non-Carbon UI Shell children + if (React.isValidElement(child)) { + const childType = child.type as React.ComponentType; + if (childType && childType.displayName) { + return React.cloneElement(child, { + ...(CARBON_SIDENAV_ITEMS.includes(childType.displayName) + ? { + isSideNavExpanded, + } + : {}), + }); + } + } + }); + return
    {childrenWithExpandedState}
; +}; + +SideNavItems.displayName = 'SideNavItems'; + +SideNavItems.propTypes = { + /** +// * Provide a single icon as the child to `SideNavIcon` to render in the +// * container +// */ + children: PropTypes.node.isRequired, + // + // /** + // * Provide an optional class to be applied to the containing node + // */ + className: PropTypes.string, + + // /** + // * Property to indicate if the side nav container is open (or not). Use to + // * keep local state and styling in step with the SideNav expansion state. + // */ + isSideNavExpanded: PropTypes.bool, +}; + +export default SideNavItems;