# All files / src/TieredSelect TieredSelect.tsx

94.11% Statements 80/85
75% Branches 42/56
90% Functions 18/20
92.53% Lines 62/67

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
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    2038x

    8x

    8x

    166x

    322x

    269x

    267x

    27x

    27x
    17x

    38x

    19x

    19x

    19x

    17x

    8x
    74x
    74x
    74x
    74x
    74x

    74x
    74x
    23x

    74x
    74x
    17x

    74x
    74x
    74x
    29x

    74x
    74x
    3x

    74x
    74x
    21x

    8x

    8x

    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x

    19x

    1x

    19x

     import React from 'react'
    import { MenuRef } from '../MenuImperative/MenuImperative.types'
    import { LeafSelectionTieredSelect } from './LeafSelectionTieredSelect'
    import {
      GetValueStringFn,
      Id,
      Tier,
      TieredSelectApi,
      TieredSelectProps,
    } from './TieredSelect.types'
    import { TierSelectionTieredSelect } from './TierSelectionTieredSelect'

    function defaultGetGroupId(tier: Tier) {
      return tier.groupId
    }

    function defaultGetId(tier: Tier) {
      return tier.id
    }

    function defaultGetLabel(tier: Tier) {
      return tier.label
    }

    function defaultGetNextGroupId(tier: Tier) {
      return tier.nextGroupId as Id
    }

    function getFullPath<T>(
      IgetLabel: TieredSelectProps['getLabel'] = defaultGetLabel
    ): GetValueStringFn {
      return function getValueString(value: T[]) {
        return value.map((tier) => getLabel!(tier)).join(' > ')
      }
    }

    function getLeafOnly<T>(
      IgetLabel: TieredSelectProps['getLabel'] = defaultGetLabel
    ): GetValueStringFn {
      return function getValueString(value: T[]) {
        return value.length ? getLabel(value[value.length - 1]) : ''
      }
    }

    function getValueStringFn(
      valueStringResolution: TieredSelectProps['getValueString'],
      getLabel: TieredSelectProps['getLabel'] = defaultGetLabel
    ): GetValueStringFn {
      Iif (valueStringResolution === 'full-path') {
        return getFullPath(getLabel)
      }

      Iif (valueStringResolution === 'leaf-only') {
        return getLeafOnly(getLabel)
      }

      return valueStringResolution!
    }

    function noop() {}

    function always() {
      return true
    }

    export const useHighlightItemEffects = ({
      currentTier,
      searchValue,
      menuRef,
      loading,
      loadingMore,
    }: {
      currentTier: Id
      searchValue: string
      menuRef: React.RefObject<MenuRef>
      loading: TieredSelectProps['loading']
      loadingMore: TieredSelectProps['loadingMore']
    }) => {
      const [previousTier, setPreviousTier] = React.useState<Id>()
      React.useEffect(() => {
        setPreviousTier(currentTier)
      }, [currentTier])

      const [previousSearch, setPreviousSearch] = React.useState<String>('')
      React.useEffect(() => {
        setPreviousSearch(searchValue)
      }, [searchValue])

      const currentMenuRef = menuRef.current
      React.useEffect(() => {
        if (previousSearch !== searchValue || previousTier !== currentTier) {
          currentMenuRef?.highlightFirst()
        }
      }, [currentMenuRef, currentTier, previousTier, searchValue, previousSearch])

      React.useEffect(() => {
        if (loadingMore) {
          currentMenuRef?.highlightLast(false)
        }
      }, [currentMenuRef, loadingMore])

      React.useEffect(() => {
        if (!loading && !currentMenuRef?.highlighted()) {
          currentMenuRef?.highlightFirst()
        }
      }, [currentMenuRef, loading])
    }

    export const TieredSelectContext = React.createContext<TieredSelectApi>({
      afterHide: noop,
      afterShow: noop,
      beforeHide: always,
      beforeShow: always,
      getGroupId: defaultGetGroupId,
      getId: defaultGetId,
      getLabel: defaultGetLabel,
      getNextGroupId: defaultGetNextGroupId,
      getValueString: getFullPath(defaultGetLabel),
      i18nScope: 'core.tieredSelect',
      onChange: noop,
    })

    /**

     We use tiered selects to allow users to choose a single parent or child option
     that is nested within a tiered list. For example, choosing a location from
     within a list of tiered locations. We typically see these selects on forms.

     Other select components are group select, multi select, and select.
     @since 10.19.0

     @see [Storybook](https://procore.github.io/core/core-react/latest/?path=/story/demos-tieredselect--demo)

     @see [Design Guidelines](https://design.procore.com/tiered-select)

     */
    export const TieredSelect = React.forwardRef<HTMLDivElement, TieredSelectProps>(
      function TieredSelect(
        {
          afterHide = noop,
          afterShow = noop,
          beforeHide = always,
          beforeShow = always,
          block,
          getGroupId = defaultGetGroupId,
          getId = defaultGetId,
          getLabel = defaultGetLabel,
          getNextGroupId = defaultGetNextGroupId,
          getValueString = getFullPath(getLabel),
          i18nScope = 'core.tieredSelect',
          onChange = noop,
          onClear,
          onNavigate,
          onQuickCreate,
          onScrollBottom,
          onSearch,
          selectableTiers = true,
          tabIndex,
          ...props
        },
        ref
      ) {
        const isLeaf = React.useCallback(
          function (tier: Tier) {
            return !getNextGroupId(tier)
          },
          [getNextGroupId]
        )

        return (
          <TieredSelectContext.Provider
            value={{
              afterHide,
              afterShow,
              beforeHide,
              beforeShow,
              block,
              getGroupId,
              getId,
              getLabel,
              getNextGroupId,
              getValueString: getValueStringFn(getValueString),
              i18nScope,
              isLeaf,
              onChange,
              onClear,
              onNavigate,
              onQuickCreate,
              onScrollBottom,
              onSearch,
              tabIndex,
            }}
          >
            {selectableTiers ? (
              <TierSelectionTieredSelect ref={ref} {...props} />
            ) : (
              <LeafSelectionTieredSelect ref={ref} {...props} />
            )}
          </TieredSelectContext.Provider>
        )
      }
    )