Skip to content

Commit 2e72a28

Browse files
committed
refactor(CNav): update CNavLink and CNavGroup behavior
1 parent a490f3b commit 2e72a28

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

src/components/nav/CNavGroup.tsx

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import React, { CSSProperties, forwardRef, ReactNode, useContext, useRef, useState } from 'react'
1+
import React, {
2+
CSSProperties,
3+
forwardRef,
4+
ReactNode,
5+
useContext,
6+
useEffect,
7+
useRef,
8+
useState,
9+
} from 'react'
210
import PropTypes from 'prop-types'
311
import classNames from 'classnames'
412
import { Transition } from 'react-transition-group'
@@ -10,14 +18,10 @@ export interface CNavGroupProps {
1018
* A string of all className you want applied to the component. [docs]
1119
*/
1220
className?: string
13-
/**
14-
* Set component's icon. [docs]
15-
*/
16-
icon?: string | ReactNode
1721
/**
1822
* Set group toggler label. [docs]
1923
*/
20-
toggler?: string
24+
toggler?: string | ReactNode
2125
/**
2226
* Show nav group items. [docs]
2327
*/
@@ -29,10 +33,30 @@ export interface CNavGroupProps {
2933
}
3034

3135
export const CNavGroup = forwardRef<HTMLLIElement, CNavGroupProps>(
32-
({ children, toggler, className, icon, idx, visible, ...rest }, ref) => {
36+
({ children, className, idx, toggler, visible, ...rest }, ref) => {
3337
const [height, setHeight] = useState<number | string>()
3438
const navItemsRef = useRef<HTMLUListElement>(null)
3539

40+
const { visibleGroup, setVisibleGroup } = useContext(CNavContext)
41+
42+
const [_visible, setVisible] = useState(
43+
Boolean(
44+
visible || (idx && visibleGroup && visibleGroup.toString().startsWith(idx.toString())),
45+
),
46+
)
47+
48+
useEffect(() => {
49+
setVisible(Boolean(idx && visibleGroup && visibleGroup.toString().startsWith(idx.toString())))
50+
}, [visibleGroup])
51+
52+
const handleTogglerOnCLick = (event: React.MouseEvent<HTMLElement>) => {
53+
event.preventDefault()
54+
setVisibleGroup(
55+
_visible ? (idx?.toString().includes('.') ? idx.slice(0, idx.lastIndexOf('.')) : '') : idx,
56+
)
57+
setVisible(!_visible)
58+
}
59+
3660
const style: CSSProperties = {
3761
height: 0,
3862
}
@@ -67,31 +91,12 @@ export const CNavGroup = forwardRef<HTMLLIElement, CNavGroupProps>(
6791
exited: { height: height },
6892
}
6993

70-
const { visibleGroup, setVisibleGroup } = useContext(CNavContext)
71-
72-
const _visible = Boolean(
73-
visible || (idx && visibleGroup && visibleGroup.toString().startsWith(idx.toString())),
74-
)
75-
7694
const _className = classNames('nav-group', { show: _visible }, className)
7795

7896
return (
7997
<li className={_className} {...rest} ref={ref}>
8098
{toggler && (
81-
<a
82-
className="nav-link nav-group-toggle"
83-
onClick={(event) => {
84-
event.preventDefault()
85-
setVisibleGroup(
86-
_visible
87-
? idx?.toString().includes('.')
88-
? idx.slice(0, idx.lastIndexOf('.'))
89-
: ''
90-
: idx,
91-
)
92-
}}
93-
>
94-
{icon && typeof icon === 'string' ? <i className={`nav-icon ${icon}`}></i> : icon}
99+
<a className="nav-link nav-group-toggle" onClick={(event) => handleTogglerOnCLick(event)}>
95100
{toggler}
96101
</a>
97102
)}
@@ -127,9 +132,8 @@ export const CNavGroup = forwardRef<HTMLLIElement, CNavGroupProps>(
127132
CNavGroup.propTypes = {
128133
children: PropTypes.node,
129134
className: PropTypes.string,
130-
icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
131135
idx: PropTypes.string,
132-
toggler: PropTypes.string,
136+
toggler: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
133137
visible: PropTypes.bool,
134138
}
135139

src/components/nav/CNavLink.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ElementType, forwardRef, ReactNode, useContext, useEffect, useRef } from 'react'
1+
import React, { ElementType, forwardRef, useContext, useEffect, useRef } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44

@@ -25,11 +25,6 @@ export interface CNavLinkProps extends Omit<CLinkProps, 'idx'> {
2525
* Toggle the disabled state for the component. [docs]
2626
*/
2727
disabled?: boolean
28-
/**
29-
* Set component's icon. [docs]
30-
*/
31-
icon?: string | ReactNode
32-
3328
/**
3429
* @ignore
3530
*/
@@ -43,7 +38,7 @@ export interface CNavLinkProps extends Omit<CLinkProps, 'idx'> {
4338
export const CNavLink = forwardRef<
4439
HTMLButtonElement | HTMLAnchorElement | HTMLLIElement,
4540
CNavLinkProps
46-
>(({ children, className, icon, idx, ...rest }, ref) => {
41+
>(({ children, className, idx, ...rest }, ref) => {
4742
const navLinkRef = useRef<HTMLAnchorElement>(null)
4843
const forkedRef = useForkedRef(ref, navLinkRef)
4944

@@ -57,7 +52,6 @@ export const CNavLink = forwardRef<
5752

5853
return (
5954
<CLink className={_className} {...rest} ref={forkedRef}>
60-
{icon && typeof icon === 'string' ? <i className={`nav-icon ${icon}`}></i> : icon}
6155
{children}
6256
</CLink>
6357
)
@@ -66,7 +60,6 @@ export const CNavLink = forwardRef<
6660
CNavLink.propTypes = {
6761
children: PropTypes.node,
6862
className: PropTypes.string,
69-
icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
7063
idx: PropTypes.string,
7164
}
7265

0 commit comments

Comments
 (0)