|
| 1 | +/** |
| 2 | + * Copyright IBM Corp. 2016, 2018 |
| 3 | + * |
| 4 | + * This source code is licensed under the Apache-2.0 license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import { ChevronDown20 } from '@carbon/icons-react'; |
| 9 | +import cx from 'classnames'; |
| 10 | +import PropTypes from 'prop-types'; |
| 11 | +import React, { useState } from 'react'; |
| 12 | +import SideNavIcon from '../SideNavIcon'; |
| 13 | +import { keys, match } from '../../../internal/keyboard'; |
| 14 | +import { usePrefix } from '../../../internal/usePrefix'; |
| 15 | + |
| 16 | +const SideNavMenu = React.forwardRef(function SideNavMenu(props, ref) { |
| 17 | + const { |
| 18 | + className: customClassName, |
| 19 | + children, |
| 20 | + defaultExpanded = false, |
| 21 | + isActive = false, |
| 22 | + large = false, |
| 23 | + renderIcon: IconElement, |
| 24 | + isSideNavExpanded, |
| 25 | + title, |
| 26 | + } = props; |
| 27 | + const prefix = usePrefix(); |
| 28 | + const [isExpanded, setIsExpanded] = useState(defaultExpanded); |
| 29 | + const [prevExpanded, setPrevExpanded] = useState(defaultExpanded); |
| 30 | + const className = cx({ |
| 31 | + [`${prefix}--side-nav__item`]: true, |
| 32 | + [`${prefix}--side-nav__item--active`]: |
| 33 | + isActive || (hasActiveChild(children) && !isExpanded), |
| 34 | + [`${prefix}--side-nav__item--icon`]: IconElement, |
| 35 | + [`${prefix}--side-nav__item--large`]: large, |
| 36 | + [customClassName]: !!customClassName, |
| 37 | + }); |
| 38 | + |
| 39 | + if (isSideNavExpanded === false && isExpanded === true) { |
| 40 | + setIsExpanded(false); |
| 41 | + setPrevExpanded(true); |
| 42 | + } else if (isSideNavExpanded === true && prevExpanded === true) { |
| 43 | + setIsExpanded(true); |
| 44 | + setPrevExpanded(false); |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions |
| 49 | + <li |
| 50 | + className={className} |
| 51 | + onKeyDown={(event) => { |
| 52 | + if (match(event, keys.Escape)) { |
| 53 | + setIsExpanded(false); |
| 54 | + } |
| 55 | + }}> |
| 56 | + <button |
| 57 | + aria-expanded={isExpanded} |
| 58 | + className={`${prefix}--side-nav__submenu`} |
| 59 | + onClick={() => { |
| 60 | + setIsExpanded(!isExpanded); |
| 61 | + }} |
| 62 | + ref={ref} |
| 63 | + type="button"> |
| 64 | + {IconElement && ( |
| 65 | + <SideNavIcon> |
| 66 | + <IconElement /> |
| 67 | + </SideNavIcon> |
| 68 | + )} |
| 69 | + <span className={`${prefix}--side-nav__submenu-title`}>{title}</span> |
| 70 | + <SideNavIcon className={`${prefix}--side-nav__submenu-chevron`} small> |
| 71 | + <ChevronDown20 /> |
| 72 | + </SideNavIcon> |
| 73 | + </button> |
| 74 | + <ul className={`${prefix}--side-nav__menu`}>{children}</ul> |
| 75 | + </li> |
| 76 | + ); |
| 77 | +}); |
| 78 | + |
| 79 | +SideNavMenu.propTypes = { |
| 80 | + /** |
| 81 | + * Provide <SideNavMenuItem>'s inside of the `SideNavMenu` |
| 82 | + */ |
| 83 | + children: PropTypes.node, |
| 84 | + |
| 85 | + /** |
| 86 | + * Provide an optional class to be applied to the containing node |
| 87 | + */ |
| 88 | + className: PropTypes.string, |
| 89 | + |
| 90 | + /** |
| 91 | + * Specify whether the menu should default to expanded. By default, it will |
| 92 | + * be closed. |
| 93 | + */ |
| 94 | + defaultExpanded: PropTypes.bool, |
| 95 | + |
| 96 | + /** |
| 97 | + * Specify whether the `SideNavMenu` is "active". `SideNavMenu` should be |
| 98 | + * considered active if one of its menu items are a link for the current |
| 99 | + * page. |
| 100 | + */ |
| 101 | + isActive: PropTypes.bool, |
| 102 | + |
| 103 | + /** |
| 104 | + * Property to indicate if the side nav container is open (or not). Use to |
| 105 | + * keep local state and styling in step with the SideNav expansion state. |
| 106 | + */ |
| 107 | + isSideNavExpanded: PropTypes.bool, |
| 108 | + |
| 109 | + /** |
| 110 | + * Specify if this is a large variation of the SideNavMenu |
| 111 | + */ |
| 112 | + large: PropTypes.bool, |
| 113 | + |
| 114 | + /** |
| 115 | + * Pass in a custom icon to render next to the `SideNavMenu` title |
| 116 | + */ |
| 117 | + renderIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), |
| 118 | + |
| 119 | + /** |
| 120 | + * Provide the text for the overall menu name |
| 121 | + */ |
| 122 | + title: PropTypes.string.isRequired, |
| 123 | +}; |
| 124 | + |
| 125 | +function hasActiveChild(children) { |
| 126 | + // if we have children, either a single or multiple, find if it is active |
| 127 | + if (Array.isArray(children)) { |
| 128 | + return children.some((child) => { |
| 129 | + if (!child.props) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + if (child.props.isActive === true) { |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + if (child.props['aria-current']) { |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + return false; |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + if (children.props) { |
| 146 | + if (children.props.isActive === true || children.props['aria-current']) { |
| 147 | + return true; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return false; |
| 152 | +} |
| 153 | + |
| 154 | +export { SideNavMenu }; |
0 commit comments