-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathNavListItem.js
57 lines (49 loc) · 1.38 KB
/
NavListItem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Nav List Item
*/
import React, { forwardRef } from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import css from './NavListItem.css';
import NavListSectionContext from '../NavListSection/NavListSectionContext';
const NavListItem = forwardRef(({ children, className, isActive, ...rest }, ref) => {
let Element = 'button';
if (rest.href) {
Element = 'a';
}
if (rest.to) {
Element = Link;
}
return (
<NavListSectionContext.Consumer>
{({ activeLink, striped }) => {
const active = isActive || activeLink === rest.to || activeLink === rest.href || activeLink === rest.name;
return (
<Element
data-test-nav-list-item
ref={ref}
className={
classnames(
css.NavListItem,
{ [css.isActive]: active },
{ [css.striped]: striped },
className
)
}
{...rest}
>
{children}
</Element>
);
}}
</NavListSectionContext.Consumer>
);
});
NavListItem.propTypes = {
children: PropTypes.oneOfType([PropTypes.element, PropTypes.string, PropTypes.array]),
className: PropTypes.string,
isActive: PropTypes.bool,
};
NavListItem.displayName = 'NavListItem';
export default NavListItem;