# All files / src/SuperSelect SuperSelect.tsx

33.33% Statements 11/33
0% Branches 0/25
0% Functions 0/10
34.37% Lines 11/32

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

    2x
    2x

    2x

    2x

    2x
     import { FloatingFocusManager, FloatingPortal } from '@floating-ui/react'
    import React from 'react'
    import { DragDropContext, Droppable } from 'react-beautiful-dnd'
    import { Virtuoso } from 'react-virtuoso'
    import { presets } from './presets'
    import { DraggableOptionContent, OptionContent } from './SuperSelect.components'
    import {
      SuperSelectApi,
      SuperSelectConfig,
      SuperSelectProps,
    } from './SuperSelect.types'
    import { SuperSelectContext, useSuperSelect } from './useSuperSelect'
    import { useVirtuosoResizeErrorFix } from './useVirtuosoResizeErrorFix'

    export const InternalDragDropContext = React.createContext({
      isDraggingOver: false,
    })

    const InternalDragDropProvider = ({
      isDraggingOver,
      children,
    }: {
      isDraggingOver: boolean
      children: React.ReactNode
    }) => {
      const value = React.useMemo(() => {
        return {
          isDraggingOver,
        }
      }, [isDraggingOver])

      return (
        <InternalDragDropContext.Provider value={value}>
          {children}
        </InternalDragDropContext.Provider>
      )
    }

    function SelectComponent(
      props: SuperSelectProps,
      ref: React.ForwardedRef<SuperSelectApi>
    ) {
      useVirtuosoResizeErrorFix()

      const search = props.search === undefined || props.search === true

      const preset = props.preset || ''

      const config: SuperSelectConfig = {
        ...presets[preset],
        ...props,
        search,
      }

      const ctx = useSuperSelect(config)

      React.useImperativeHandle(ref, () => ctx)

      React.useEffect(() => {
        Iif (props.value && props.onChange === undefined) {
          console.warn(
            'Must provide both a `value` and `onChange` prop for a controlled Select'
          )
        }
      }, [])

      return (
        <SuperSelectContext.Provider value={ctx}>
          <ctx.components.Trigger
            ref={ctx.refs.floating.reference}
            {...ctx.props.trigger()}
          />
          <FloatingPortal>
            {ctx.state.open && (
              <FloatingFocusManager
                context={ctx.refs.floating.context}
                order={
                  config.search
                    ? ['content', 'reference']
                    : ['reference', 'content']
                }
                initialFocus={config.multiple ? undefined : 0}
              >
                <ctx.components.Overlay
                  ref={ctx.refs.floating.floating}
                  {...ctx.props.overlay()}
                >
                  {(config.search === true ||
                    (config.search === undefined && !config.multiple)) && (
                    <ctx.components.SearchContainer
                      {...ctx.props.searchContainer()}
                    >
                      <ctx.components.Search {...ctx.props.search()} />
                    </ctx.components.SearchContainer>
                  )}
                  {config.header && (
                    <ctx.components.Header {...ctx.props.header()}>
                      {config.header}
                    </ctx.components.Header>
                  )}
                  {ctx.state.options.length > 0 ? (
                    <ctx.components.Options>
                      {config.draggable ? (
                        <DragDropContext onDragEnd={ctx.state.onDragEnd}>
                          <Droppable
                            droppableId="droppable"
                            mode="virtual"
                            renderClone={(provided, snapshot, dragged) => {
                              const option = ctx.state.options[dragged.source.index]

                              return (
                                <ctx.components.Item
                                  isDragging
                                  data-index={dragged.source.index}
                                  {...provided.draggableProps}
                                >
                                  <ctx.components.DragHandle />
                                  <ctx.components.OptionLabel option={option} />
                                </ctx.components.Item>
                              )
                            }}
                          >
                            {(provided, snapshot) => {
                              return (
                                <InternalDragDropProvider
                                  isDraggingOver={snapshot.isDraggingOver}
                                >
                                  <Virtuoso
                                    {...ctx.props.virtuoso()}
                                    scrollerRef={(el) =>
                                      provided.innerRef(el as HTMLElement)
                                    }
                                    itemContent={(i, data) => (
                                      <DraggableOptionContent
                                        index={i}
                                        option={data}
                                      />
                                    )}
                                  />
                                </InternalDragDropProvider>
                              )
                            }}
                          </Droppable>
                        </DragDropContext>
                      ) : (
                        <Virtuoso
                          {...ctx.props.virtuoso()}
                          ref={ctx.refs.virtuoso}
                          itemContent={(i, option) => (
                            <OptionContent option={option} />
                          )}
                        />
                      )}
                    </ctx.components.Options>
                  ) : (
                    <ctx.components.EmptyMessage {...ctx.props.emptyMessage()} />
                  )}
                  {config.footer && (
                    <ctx.components.Footer {...ctx.props.footer()} />
                  )}
                </ctx.components.Overlay>
              </FloatingFocusManager>
            )}
          </FloatingPortal>
        </SuperSelectContext.Provider>
      )
    }

    export const SuperSelect = React.forwardRef(SelectComponent)