From 96f45eebdf6e2ed6b0f9957044fe7d104661a616 Mon Sep 17 00:00:00 2001 From: Josh Black Date: Fri, 29 Oct 2021 20:02:00 -0500 Subject: [PATCH] refactor(react): update HeaderNavigation to functional component (#9911) * refactor(react): update HeaderNavigation to functional component * chore: remove ref from refactor Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../migration/11.x-carbon-components-react.md | 8 +-- .../react/src/components/UIShell/index.js | 6 +- .../UIShell/next/HeaderNavigation.js | 56 +++++++++++++++++ .../next/__tests__/HeaderNavigation-test.js | 63 +++++++++++++++++++ .../src/components/UIShell/next/index.js | 1 + 5 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 packages/react/src/components/UIShell/next/HeaderNavigation.js create mode 100644 packages/react/src/components/UIShell/next/__tests__/HeaderNavigation-test.js diff --git a/docs/migration/11.x-carbon-components-react.md b/docs/migration/11.x-carbon-components-react.md index f3949f1f6153..25dbd939920c 100644 --- a/docs/migration/11.x-carbon-components-react.md +++ b/docs/migration/11.x-carbon-components-react.md @@ -8,7 +8,6 @@ ## Table of Contents - [Components](#components) - - [SideNavMenu](#sidenavmenu) @@ -32,8 +31,9 @@ For a full overview of changes to components, checkout our ## Components -| Component | Changes | -| :------------ | :-------------------------------------------------------------- | -| `SideNavMenu` | Updated from a class to functional component. No other changes. | +| Component | Changes | +| :----------------- | :-------------------------------------------------------------- | +| `HeaderNavigation` | Updated from a class to functional component. No other changes. | +| `SideNavMenu` | Updated from a class to functional component. No other changes. | ## FAQ diff --git a/packages/react/src/components/UIShell/index.js b/packages/react/src/components/UIShell/index.js index de6b4e84a17c..4a404c0cc13f 100644 --- a/packages/react/src/components/UIShell/index.js +++ b/packages/react/src/components/UIShell/index.js @@ -6,6 +6,8 @@ */ import * as FeatureFlags from '@carbon/feature-flags'; +import HeaderNavigationClassic from './HeaderNavigation'; +import { HeaderNavigation as HeaderNavigationNext } from './next'; import SideNavMenuClassic from './SideNavMenu'; import { SideNavMenu as SideNavMenuNext } from './next/SideNavMenu'; @@ -19,7 +21,9 @@ export HeaderMenu from './HeaderMenu'; export HeaderMenuButton from './HeaderMenuButton'; export HeaderMenuItem from './HeaderMenuItem'; export HeaderName from './HeaderName'; -export HeaderNavigation from './HeaderNavigation'; +export const HeaderNavigation = FeatureFlags.enabled('enable-v11-release') + ? HeaderNavigationNext + : HeaderNavigationClassic; export HeaderPanel from './HeaderPanel'; export HeaderSideNavItems from './HeaderSideNavItems'; export Switcher from './Switcher'; diff --git a/packages/react/src/components/UIShell/next/HeaderNavigation.js b/packages/react/src/components/UIShell/next/HeaderNavigation.js new file mode 100644 index 000000000000..6db04f8fb1e5 --- /dev/null +++ b/packages/react/src/components/UIShell/next/HeaderNavigation.js @@ -0,0 +1,56 @@ +/** + * Copyright IBM Corp. 2016, 2018 + * + * 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 React from 'react'; +import PropTypes from 'prop-types'; +import { AriaLabelPropType } from '../../../prop-types/AriaPropTypes'; +import { usePrefix } from '../../../internal/usePrefix'; + +function HeaderNavigation(props) { + const { + 'aria-label': ariaLabel, + 'aria-labelledby': ariaLabelledBy, + children, + className: customClassName, + ...rest + } = props; + const prefix = usePrefix(); + const className = cx(`${prefix}--header__nav`, customClassName); + // Assign both label strategies in this option, only one should be defined + // so when we spread that should be the one that is applied to the node + const accessibilityLabel = { + 'aria-label': ariaLabel, + 'aria-labelledby': ariaLabelledBy, + }; + + return ( + + ); +} + +HeaderNavigation.propTypes = { + /** + * Required props for accessibility label on the underlying menu + */ + ...AriaLabelPropType, + + /** + * Provide valid children of HeaderNavigation, for example `HeaderMenuItem` + * or `HeaderMenu` + */ + children: PropTypes.node, + + /** + * Optionally provide a custom class to apply to the underlying