# All files / src/PillSelect PillSelect.tsx

79.48% Statements 62/78
61.53% Branches 24/39
73.68% Functions 14/19
79.31% Lines 46/58

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
    203
    204
    2057x
    7x
    7x
    7x
    7x
    7x

    7x

    7x

    24x

    10x

    4x

    8x

    7x
    8x

    7x

    7x

    7x

    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x

    10x
    10x
    10x

    10x

    10x
    4x

    8x

    4x

    10x

    10x

    10x

    10x

    4x

    4x

    6x

    7x
     import { Check } from '@procore/core-icons/dist'
    import React from 'react'
    import { Pill } from '../Pill/Pill'
    import { Select } from '../Select/Select'
    import { Tooltip } from '../Tooltip/Tooltip'
    import { OverflowObserver } from '../_hooks/OverflowObserver'
    import { DivAttributes } from '../_utils/types'
    import {
      PillSelectCheckedContainer,
      PillSelectLabel,
      PillSelectLabelWrapper,
      PillSelectOptionWrapper,
    } from './PillSelect.styles'
    import {
      PillOptGroup,
      PillOption,
      PillOptionProps,
      PillSelectProps,
    } from './PillSelect.types'

    const emptyArr: Array<any> = []

    function defaultGetId(option: PillOption | PillOptGroup) {
      return option.id
    }

    function defaultGetLabel(option: PillOption | PillOptGroup) {
      return option.label
    }

    function defaultGetGroup(option: PillOption) {
      return option.groupId
    }

    function defaultGetSuggested(option: PillOption) {
      return option.suggested
    }

    export function defaultGetColor(option: PillOption) {
      return option.color
    }

    const PillSelectOption = React.forwardRef<HTMLDivElement, PillOptionProps>(
      function PillSelectOption({ color, label, selected, ...props }, ref) {
        return (
          <Select.Option selected={selected} {...props} ref={ref}>
            <PillSelectOptionWrapper>
              <Pill color={color}>{label}</Pill>
              <PillSelectCheckedContainer>
                {selected && <Check size="sm" />}
              </PillSelectCheckedContainer>
            </PillSelectOptionWrapper>
          </Select.Option>
        )
      }
    )

    const PillSelectOptGroup = React.forwardRef<HTMLDivElement, DivAttributes>(
      function PillSelectOptGroup(props, ref) {
        return <Select.OptGroup {...props} ref={ref} />
      }
    )

    /**

     Pill Selects are used when you need a user to select between one of several
     options that are shown as pills. Often this will be a status, such as marking
     an item “closed”, “draft”, etc.

     @since 10.19.0

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

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

     */
    export const PillSelect = React.forwardRef<
      HTMLDivElement,
      PillSelectProps<any, any>
    >(function PillSelect(
      {
        afterHide: afterHide_,
        afterShow: afterShow_,
        getColor = defaultGetColor,
        getGroup = defaultGetGroup,
        getId = defaultGetId,
        getLabel = defaultGetLabel,
        getSuggested = defaultGetSuggested,
        groupGetId = defaultGetId,
        groupGetLabel = defaultGetLabel,
        optgroups,
        options = emptyArr,
        value,
        ...props
      },
      ref
    ) {
      const { color, id, label } = React.useMemo(() => {
        if (!value) {
          return {}
        }
        return {
          color: getColor(value),
          id: getId(value),
          label: getLabel(value),
        }
      }, [getColor, getId, getLabel, value])

      const optionsByGroup = React.useMemo(
        () =>
          optgroups
            ?.map((optgroup) => ({
              optgroup,
              options: options.filter(
                (option) => getGroup(option) === groupGetId(optgroup)
              ),
            }))
            .filter(({ options }) => options.length),
        [options, optgroups, groupGetId, getGroup]
      )

      const [isOpened, setIsOpened] = React.useState(false)

      const afterShow = React.useCallback(() => {
        setIsOpened(true)
        afterShow_?.()
      }, [afterShow_])

      const afterHide = React.useCallback(() => {
        setIsOpened(false)
        afterHide_?.()
      }, [afterHide_])

      return (
        <Select
          afterHide={afterHide}
          afterShow={afterShow}
          label={
            label ? (
              <OverflowObserver>
                {({ isOverflowing, ref }) => {
                  const pill = (
                    <PillSelectLabel color={color} ref={ref}>
                      {label}
                    </PillSelectLabel>
                  )

                  const showTooltip = isOverflowing && !isOpened

                  return (
                    <PillSelectLabelWrapper disabled={props.disabled}>
                      {showTooltip ? (
                        <Tooltip trigger="hover" overlay={label}>
                          {pill}
                        </Tooltip>
                      ) : (
                        pill
                      )}
                    </PillSelectLabelWrapper>
                  )
                }}
              </OverflowObserver>
            ) : null
          }
          {...props}
          ref={ref}
        >
          {optionsByGroup ? (
            optionsByGroup.map(({ optgroup, options }) => (
              <React.Fragment key={groupGetId(optgroup)}>
                <PillSelectOptGroup key={groupGetId(optgroup)}>
                  {groupGetLabel(optgroup)}
                </PillSelectOptGroup>
                {options.map((option) => (
                  <PillSelectOption
                    key={getId(option)}
                    color={getColor(option)}
                    label={getLabel(option)}
                    selected={getId(option) === id}
                    suggested={getSuggested(option)}
                    value={option}
                  />
                ))}
              </React.Fragment>
            ))
          ) : (
            <>
              {options.map((option) => (
                <PillSelectOption
                  key={getId(option)}
                  color={getColor(option)}
                  label={getLabel(option)}
                  selected={getId(option) === id}
                  suggested={getSuggested(option)}
                  value={option}
                />
              ))}
            </>
          )}
        </Select>
      )
    })

    PillSelect.displayName = 'PillSelect'