# All files / src/NextMenu NextMenu.tsx

75% Statements 39/52
35.48% Branches 11/31
46.15% Functions 6/13
80.48% Lines 33/41

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
    2x
    2x
    2x

    2x

    2x

    2x
    3x
    39x

    3x

    2x
    3x

    2x

    2x

    2x

    2x

    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    2x

    1x
    1x
    1x
    1x
    1x

    1x

    10x

    1x

    1x

     // TODO figure out what to do with this component RE SuperSelect
    import React from 'react'
    import { GroupedVirtuoso, Virtuoso } from 'react-virtuoso'
    import {
      defaultMenuHeight,
      optionMinHeight,
      StyledOptions,
    } from './NextMenu.styles'
    import { NextMenuProps } from './NextMenu.types'
    import { NextMenuGroup, NextMenuItem } from './NextMenuItem'

    /**
     * Count items in groups by id received from getItemGroupId
     *
     * @param items_
     * @param groups_
     * @param getItemGroupId
     * @return number[]
     * */
    export const getGroupCounts = (
      items_: any[],
      groups_: any[],
      getItemGroupId: (item: any) => string | number
    ): number[] => {
      return groups_.map((group) => {
        const groupedItems = items_.filter(
          (item) => getItemGroupId(item) === group.id
        )
        return groupedItems.length
      })
    }

    /**
     * Get the height of the item list, used to make menu grow to size of item list
     * (or to a max height if applied by implementer) with a minimum of 32px
     * @param height;
     * @returns number
     */
    function calculateHeight(height: number) {
      return Math.max(height, optionMinHeight)
    }

    const Options = React.forwardRef<HTMLDivElement, any>((props, ref) => {
      return <StyledOptions ref={ref} {...props} />
    })

    const flatComponents = { Item: NextMenuItem, List: Options }

    const groupedComponents = {
      Group: NextMenuGroup,
      Item: NextMenuItem,
      List: Options,
    }

    /**
     Menus are used in conjunction with components that contain dropdowns.
     For example, multi select, single select, and dropdown.

     @since 11.1.1
     @see [Storybook](https://procore.github.io/core/core-react/latest/?path=/story/coverage-nextmenu--with-groups)
     @see [Design Guidelines](https://design.procore.com/menu)
     */
    export const NextMenu = React.forwardRef<
      React.ReactElement,
      NextMenuProps<any, any>
    >(({ style, ...props }, menuRef) => {
      const [adjustedHeight, setAdjustedHeight] = React.useState(defaultMenuHeight)

      const sharedMenuConfiguration = {
        ref: menuRef,
        style: {
          height: adjustedHeight,
          minHeight: optionMinHeight,
          ...style,
        },
        totalListHeightChanged: (height: number) =>
          setAdjustedHeight(calculateHeight(height)),
      }

      Iif (!props.groups?.length) {
        return <NextMenuFlat {...sharedMenuConfiguration} {...props} />
      }
      return <NextMenuGrouped {...sharedMenuConfiguration} {...props} />
    })

    /**
     * Flat version of menu list
     * */
    export const NextMenuFlat = React.forwardRef<
      React.ReactElement,
      NextMenuProps<any, any> & { totalListHeightChanged: (height: number) => void }
    >(
      (
        {
          items = [],
          overscan = 20,
          style,
          totalListHeightChanged,
          onEndReached = () => {},
          components = {},
          getItemLabel = (item: any) => item?.label,
        },
        menuRef
      ) => {
        return (
          <Virtuoso
            ref={menuRef as any}
            components={{ ...flatComponents, ...components } as any}
            context={{ items, getItemLabel }}
            data={items}
            overscan={overscan}
            style={style}
            totalListHeightChanged={totalListHeightChanged}
            endReached={onEndReached}
          />
        )
      }
    )

    /**
     * Grouped version of menu list
     * Provide getItemGroupId to group items
     * @see getGroupCounts
     * */
    export const NextMenuGrouped = React.forwardRef<
      React.ReactElement,
      NextMenuProps<any, any> & { totalListHeightChanged: (height: number) => void }
    >((props, menuRef) => {
      const {
        children,
        components = {},
        items = [],
        groups = [],
        getItemGroupId = () => -1,
        overscan = 20,
        onSelect: onSelect_,
        onEndReached: onEndReached_ = () => {},
        style,
        totalListHeightChanged,
      } = props

      const groupCounts = getGroupCounts(items, groups, getItemGroupId)

      return (
        <GroupedVirtuoso
          ref={menuRef as any}
          style={style}
          components={{ ...groupedComponents, ...components } as any}
          context={{ items }}
          groupCounts={groupCounts}
          overscan={overscan}
          endReached={onEndReached_}
          totalListHeightChanged={totalListHeightChanged}
        />
      )
    })