Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot specify as props for CNavLink in CNavItem #409

Open
mst-mkt opened this issue Sep 18, 2024 · 0 comments
Open

Cannot specify as props for CNavLink in CNavItem #409

mst-mkt opened this issue Sep 18, 2024 · 0 comments

Comments

@mst-mkt
Copy link

mst-mkt commented Sep 18, 2024

Environment

  • OS: ArchLinux (likely occurs with other OS as well)
  • Browser: Vivaldi (Chromium-based) 6.9.3447.44 (likely occurs with other browsers)

Source Code

export const CNavItem: PolymorphicRefForwardingComponent<'li', CNavItemProps> = forwardRef<
HTMLLIElement,
CNavItemProps
>(({ children, as: Component = 'li', className, ...rest }, ref) => {
return (
<Component className={classNames('nav-item', className)} ref={ref}>
{rest.href || rest.to ? (
<CNavLink className={className} {...rest}>
{children}
</CNavLink>
) : (
children
)}
</Component>
)
})

Description

It is not possible to pass as props to <CNavLink> inside a <CNavItem>, which was previously possible. This makes it impossible to use the Router's <Link> (e.g. <NavLink> from react-router-dom) component within <CNav>.

Problem Explanation

This issue likely occurred due to the following changes:
2652a91?diff=split&w=0#diff-1e4fdbb5559a22bedb4e23de9ec5e8452d240aef08945cc08c18168f68a051d6L7-R33

The as prop that was previously passed to <CNavLink> as rest is now used in the part that wraps (previously li). This change has caused the as prop to no longer function correctly when passing <Link>.

Proposed Solution

One possible solution is to add a navLinkAs prop, as shown below.

import React, { ElementType, forwardRef } from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'

import { CNavLink, CNavLinkProps } from './CNavLink'

import { PolymorphicRefForwardingComponent } from '../../helpers'

export interface CNavItemProps extends Omit<CNavLinkProps, 'component'> {
  as?: ElementType
  navLinkAs?: ElementType
}

export const CNavItem: PolymorphicRefForwardingComponent<'li', CNavItemProps> = forwardRef<
  HTMLLIElement,
  CNavItemProps
>(({ children, as: Component = 'li', navLinkAs, className, ...rest }, ref) => {
  return (
    <Component className={classNames('nav-item', className)} ref={ref}>
      {rest.href || rest.to ? (
        <CNavLink className={className} as={navLinkAs} {...rest}>
          {children}
        </CNavLink>
      ) : (
        children
      )}
    </Component>
  )
})

CNavItem.propTypes = {
  as: PropTypes.elementType,
  navLinkAs: PropTypes.elementType,
  children: PropTypes.node,
  className: PropTypes.string,
}

If necessary, I can create a PR with the above changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant