Advertisement
Oxios

Untitled

Feb 20th, 2020
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ActiveLink
  2.  
  3. import React, {Children} from 'react';
  4. import {useRouter} from 'next/router';
  5. import PropTypes from 'prop-types';
  6. import Link from 'next/link';
  7.  
  8. const ActiveLink = ({children, notActive, activeClassName, href, as}) => {
  9.     const router = useRouter();
  10.     const child = Children.only(children);
  11.     const childClassName = child.props.className || '';
  12.     let className;
  13.  
  14.     className = router.asPath === as
  15.         ? `${childClassName} ${activeClassName}`.trim()
  16.         : childClassName;
  17.  
  18.     return (
  19.         <Link href={href} as={as}>
  20.             {React.cloneElement(child, {
  21.                 className: className || null,
  22.             })}
  23.         </Link>
  24.     )
  25. };
  26.  
  27. ActiveLink.propTypes = {
  28.     activeClassName: PropTypes.string.isRequired,
  29. };
  30.  
  31. export default ActiveLink
  32.  
  33. routesConfig
  34.  
  35. {
  36.         href: '/portfolio',
  37.         as: '/portfolio',
  38.         label: 'Портфолио',
  39.         subMenu: [
  40.             {
  41.                 label: 'Фото:',
  42.                 subMenu: [
  43.                     {
  44.                         href: '/portfolio/[categories]',
  45.                         as: '/portfolio/paired-photos',
  46.                         label: 'Парные'
  47.                     },
  48.                     {
  49.                         href: '/portfolio/[categories]',
  50.                         as: '/portfolio/wedding-photos',
  51.                         label: 'Свадебные'
  52.                     },
  53.                     {
  54.                         href: '/portfolio/[categories]',
  55.                         as: '/portfolio/family-photos',
  56.                         label: 'Семейные'
  57.                     },
  58.                 ]
  59.             },
  60.             {
  61.                 href: '/portfolio',
  62.                 as: '/portfolio#portfolio-video',
  63.                 label: 'Видео',
  64.             },
  65.         ]
  66.     },
  67.  
  68. render
  69.  
  70. <ul className='menu-list' ref={menuListRef}>
  71.                             {data.map(item => {
  72.                                 return (
  73.                                     <li key={item.label} className='menu-list__item'>
  74.                                     <span onClick={() => handleClickMenuItem(item.href)}>
  75.                                         <ActiveLink activeClassName="active-link" href={item.href} as={item.as}>
  76.                                             <a className="nav-link">{item.label}</a>
  77.                                         </ActiveLink>
  78.                                     </span>
  79.                                         {item.subMenu &&
  80.                                         <ul className='menu-list menu-list_nested'>
  81.                                             {item.subMenu.map(item => {
  82.                                                 return (
  83.                                                     <li key={item.label} className='menu-list__item'>
  84.                                                         <span onClick={() => handleClickMenuItem(item.href)}>
  85.                                                             {!item.href
  86.                                                                 ? <a>{item.label}</a>
  87.                                                                 : <ActiveLink
  88.                                                                     notActive={item.notActive}
  89.                                                                     activeClassName='active-link'
  90.                                                                     href={item.href}
  91.                                                                     as={item.as} >
  92.                                                                     <a className="nav-link">{item.label}</a>
  93.                                                                 </ActiveLink>}
  94.                                                         </span>
  95.                                                     </li>
  96.                                                 )
  97.                                             })}
  98.                                         </ul>}
  99.                                     </li>
  100.                                 )
  101.                             })}
  102.                         </ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement