# All files / src/form useEditableCombobox.ts

0% Statements 0/139
0% Branches 0/1
0% Functions 0/1
0% Lines 0/139

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

     "use client";
    import { useRef } from "react";
    import { isTypeEvent } from "../movement/utils.js";
    import {
      useCombobox,
      type BaseComboboxOptions,
      type ComboboxImplementation,
    } from "./useCombobox.js";
    import { isChangeableHTMLElement, triggerManualChangeEvent } from "./utils.js";

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

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

    /**
     * @remarks \@since 6.0.0
     */
    export function useEditableCombobox<
      ComboboxEl extends HTMLElement = HTMLInputElement,
      PopupEl extends HTMLElement = HTMLElement,
    >(
      options: EditableComboboxOptions<ComboboxEl, PopupEl> = {}
    ): EditableComboboxImplementation<ComboboxEl, PopupEl> {
      const focusFirst = useRef(false);
      const { getMenuProps, ...combobox } = useCombobox({
        ...options,
        loopable: true,
        searchable: false,
        isNegativeOneAllowed: true,
        getEnterDefaultFocusedIndex(options) {
          const { focusLast, focusables, currentFocusIndex } = options;
          if (focusFirst.current) {
            focusFirst.current = false;
            return 0;
          }

          if (focusLast) {
            return focusables.length - 1;
          }

          return currentFocusIndex;
        },
        extendKeyDown(movementData) {
          const {
            event,
            show,
            hide,
            visible,
            currentFocusIndex,
            setActiveDescendantId,
          } = movementData;

          const resetFocus = (): void => {
            currentFocusIndex.current = -1;
            setActiveDescendantId("");
          };

          switch (event.key) {
            case "Escape":
              if (!visible && isChangeableHTMLElement(event.currentTarget)) {
                event.preventDefault();
                event.stopPropagation();
                triggerManualChangeEvent(event.currentTarget, "");
              }
              break;
            case "Home":
            case "End":
              // do not attempt to jump to the start or end of the listbox so
              // natural text editing occurs
              event.stopPropagation();
              if (visible) {
                resetFocus();
              }
              break;
            case "ArrowLeft":
            case "ArrowRight":
              if (visible) {
                resetFocus();
              }
              break;
            case "ArrowUp":
              // Note: The non-altKey version is handled in the `useCombobox` hook
              //
              // if the popup is available and the altKey was pressed, return focus
              // to the combobox without closing. If the focus was already on the
              // combobox, hide the popup
              if (visible && event.altKey) {
                event.preventDefault();
                event.stopPropagation();

                if (currentFocusIndex.current !== -1) {
                  resetFocus();
                } else {
                  hide();
                }
              }
              break;
            case "ArrowDown":
              // if the popup is available, display the popup keep the focus on the
              // combobox if the altKey was pressed. Otherwise, move focus into the
              // listbox
              if (!visible) {
                event.preventDefault();
                event.stopPropagation();
                focusFirst.current = !event.altKey;
                show();
              }
              break;
            default:
              if (isTypeEvent(event)) {
                show();
                resetFocus();
              }
          }
        },
      });

      return {
        ...combobox,
        getMenuProps(props) {
          return {
            preventOverlap: true,
            ...getMenuProps(props),
          };
        },
      };
    }