# All files / src/form useSelectCombobox.ts

100% Statements 70/70
100% Branches 12/12
100% Functions 3/3
100% Lines 70/70

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
    711x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    87x
    87x
    87x
    87x
    87x
    87x
    87x
    87x
    87x
    87x
    87x
    19x
    19x
    13x
    13x
    6x
    6x
    19x
    19x
    19x
    3x
    3x
    3x
    3x
    3x
    19x
    19x
    87x
    17x
    17x
    1x
    1x
    16x
    16x
    16x
    16x
    16x
    16x
    87x
    87x
     "use client";
    import {
      useCombobox,
      type BaseComboboxOptions,
      type ComboboxImplementation,
    } from "./useCombobox.js";

    /**
     * @remarks \@since 6.0.0
     */
    export interface SelectComboboxOptions<
      ComboboxEl extends HTMLElement = HTMLInputElement,
      PopupEl extends HTMLElement = HTMLElement,
    > extends BaseComboboxOptions<ComboboxEl, PopupEl> {
      value: string;
      values: readonly string[];
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface SelectComboboxImplementation<
      ComboboxEl extends HTMLElement = HTMLInputElement,
      PopupEl extends HTMLElement = HTMLElement,
    > extends ComboboxImplementation<ComboboxEl, PopupEl> {}

    /**
     * @remarks \@since 6.0.0
     */
    export function useSelectCombobox<
      ComboboxEl extends HTMLElement = HTMLInputElement,
      PopupEl extends HTMLElement = HTMLElement,
    >(
      options: SelectComboboxOptions<ComboboxEl, PopupEl>
    ): SelectComboboxImplementation<ComboboxEl, PopupEl> {
      const { value, values, ...comboboxOptions } = options;

      return useCombobox({
        ...comboboxOptions,
        searchable: true,
        extendKeyDown(movementData) {
          const { event, show, focusLast, visible } = movementData;
          if (visible) {
            return;
          }

          switch (event.key) {
            case " ":
            case "Home":
            case "End":
              event.preventDefault();
              event.stopPropagation();
              focusLast.current = event.key === "End";
              show();
              break;
          }
        },
        getEnterDefaultFocusedIndex(options) {
          const { focusLast } = options;
          if (focusLast && !value) {
            return values.length - 1;
          }

          return Math.max(
            0,
            values.findIndex((option) => option === value)
          );
        },
      });
    }