All files / molecule/collapsible/src index.js

81.81% Statements 18/22
64.28% Branches 18/28
50% Functions 3/6
80.95% Lines 17/21

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171                                        1x                                 4x 4x 4x   4x   6x         4x             4x 4x       4x       4x     4x         4x         4x   4x                                     1x   1x                                                                                                                                            
import {useCallback, useEffect, useState} from 'react'
 
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import useControlledState from '@s-ui/react-hooks/lib/useControlledState'
 
import {
  BASE_CLASS,
  BUTTON_CLASS,
  BUTTON_CONTENT_CLASS,
  BUTTON_TEXT_ALIGN,
  COLLAPSED_CLASS,
  CONTAINER_BUTTON_CLASS,
  CONTENT_ALIGN,
  CONTENT_CLASS,
  ICON_CLASS,
  MIN_HEIGHT
} from './settings.js'
 
const MoleculeCollapsible = ({
  onClose = () => {},
  onOpen = () => {},
  alignButtonText,
  alignContainer,
  children,
  height = MIN_HEIGHT,
  icon,
  isCollapsible = true,
  isDefaultExpanded = false,
  isExpanded,
  showText,
  hideText,
  withGradient = true,
  withOverflow = false,
  withTransition = true
}) => {
  const [expanded, setExpanded] = useControlledState(isExpanded, isDefaultExpanded)
  const [showButton, setShowButton] = useState(true)
  const [childrenHeight, setChildrenHeight] = useState(0)
 
  const nodeCallback = useCallback(
    node => {
      setChildrenHeight(node !== null ? node.getBoundingClientRect().height : 0)
    },
    [children]
  )
 
  const toggleCollapse = () => {
    if (showButton) {
      setExpanded(!expanded)
      expanded ? onClose() : onOpen()
    }
  }
 
  useEffect(() => {
    Eif (!childrenHeight || !expanded) return
    setShowButton(isCollapsible && childrenHeight >= height)
  }, [childrenHeight, expanded, height, isCollapsible, setShowButton])
 
  const wrapperClassName = cx(`${BASE_CLASS}`, {
    [`${BASE_CLASS}--withGradient`]: withGradient,
    [COLLAPSED_CLASS]: !expanded
  })
  const iconClassName = cx(`${ICON_CLASS}`, {
    [COLLAPSED_CLASS]: !expanded
  })
  const containerClassName = cx(`${CONTAINER_BUTTON_CLASS}`, {
    [`${CONTAINER_BUTTON_CLASS}--withGradient`]: withGradient,
    [`${CONTAINER_BUTTON_CLASS}--alignButtonText-${alignButtonText}`]: alignButtonText,
    [COLLAPSED_CLASS]: !expanded
  })
  const contentClassName = cx(`${CONTENT_CLASS}`, {
    [`${CONTENT_CLASS}--withTransition`]: withTransition,
    [`${CONTENT_CLASS}--withOverflow`]: withOverflow,
    [`${CONTENT_CLASS}--alignContainer-${alignContainer}`]: alignContainer
  })
  const containerHeight = !expanded ? `${height}px` : `${childrenHeight}px`
 
  return (
    <div className={wrapperClassName}>
      <div className={contentClassName} style={{maxHeight: !showButton ? 'none' : containerHeight}}>
        <div ref={nodeCallback}>{children}</div>
      </div>
      {showButton && (
        <div className={containerClassName}>
          <button type="button" className={BUTTON_CLASS} onClick={toggleCollapse}>
            <span className={BUTTON_CONTENT_CLASS} tabIndex="-1">
              {!expanded ? showText : hideText}
              <span className={iconClassName}>{icon}</span>
            </span>
          </button>
        </div>
      )}
    </div>
  )
}
 
MoleculeCollapsible.displayName = 'MoleculeCollapsible'
 
MoleculeCollapsible.propTypes = {
  /**
   * Button text align center || right || left
   */
  alignButtonText: PropTypes.oneOf(Object.values(BUTTON_TEXT_ALIGN)),
  /**
   * Container align center || right
   */
  alignContainer: PropTypes.oneOf(Object.values(CONTENT_ALIGN)),
  /**
   * Content to collapse
   */
  children: PropTypes.node.isRequired,
  /**
   * Define the min height visible
   */
  height: PropTypes.number,
  /**
   * Icon to be added on the right of the content
   */
  icon: PropTypes.node.isRequired,
  /**
   * When expanded, do not show the inner toggle button for collapsing.
   */
  isCollapsible: PropTypes.bool,
  /**
   * Make it expanded/collapsed uncontrolled
   */
  isDefaultExpanded: PropTypes.bool,
  /**
   * Make it expanded/collapsed controlled
   */
  isExpanded: PropTypes.bool,
  /**
   * Text to show when content is collapsed
   */
  showText: PropTypes.string.isRequired,
  /**
   * Text to show when content is not collapsed
   */
  hideText: PropTypes.string.isRequired,
  /**
   * Activate/deactivate gradient
   */
  withGradient: PropTypes.bool,
  /**
   * Activate/deactivate horizontal overflow
   */
  withOverflow: PropTypes.bool,
  /**
   * Activate/deactivate transition
   */
  withTransition: PropTypes.bool,
  /**
   * On open callback
   */
  onOpen: PropTypes.func,
  /**
   * On close callback
   */
  onClose: PropTypes.func
}
 
export default MoleculeCollapsible
 
export {
  CONTENT_ALIGN,
  CONTENT_ALIGN as moleculeCollapsibleContentAlign,
  BUTTON_TEXT_ALIGN as moleculeCollapsibleButtonAlign
}