All files / modules/10-common/navigation/NavExpandable NavExpandable.tsx

68.42% Statements 13/19
84.62% Branches 11/13
20% Functions 1/5
72.22% Lines 13/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 58 59 60 61 62 63              56x 56x 56x 56x 56x                 56x             15x 15x 15x 15x   15x             15x                                           56x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useState } from 'react'
import cx from 'classnames'
import { useLocation, matchPath } from 'react-router-dom'
import { Layout, Text } from '@wings-software/uicore'
import css from './NavExpandable.module.scss'
 
interface NavExpandableProps {
  title: string
  route: string
  className?: string
  withoutBorder?: boolean
}
 
const NavExpandable: React.FC<React.PropsWithChildren<NavExpandableProps>> = ({
  title,
  route,
  children,
  className,
  withoutBorder = false
}) => {
  const [mouseEnter, setMouseEnter] = useState<boolean>(false)
  const { pathname } = useLocation()
  const isSelected = matchPath(pathname, route)
  const timerRef = React.useRef<number | null>(null)
 
  const handleMouseEvent = (val: boolean): void => {
    if (timerRef.current) window.clearTimeout(timerRef.current)
    timerRef.current = window.setTimeout(() => {
      setMouseEnter(val)
    }, 300)
  }
 
  return (
    <div>
      {!withoutBorder && <div className={css.border} />}
      <Layout.Vertical
        className={cx(className, css.main)}
        onMouseEnter={() => handleMouseEvent(true)}
        onMouseLeave={() => handleMouseEvent(false)}
      >
        <Text
          rightIcon={isSelected || mouseEnter ? 'chevron-up' : 'chevron-down'}
          className={css.text}
          font="xsmall"
          flex={{ alignItems: 'flex-start', justifyContent: 'space-between' }}
        >
          {title}
        </Text>
        {isSelected || mouseEnter ? children : null}
      </Layout.Vertical>
    </div>
  )
}
 
export default NavExpandable