# All files useInlineAutoComplete.ts

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

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

     "use client";
    import { useRef, type ChangeEventHandler } from "react";
    import {
      type WhitespaceFilter,
      type CaseInsensitiveMatchOptions,
      caseInsensitiveMatch,
      toSearchQuery,
    } from "../utils/filters.js";
    import { type EditableHTMLElement } from "./utils.js";

    const noop = (): void => {
      // do nothing
    };

    export interface AutoCompleteOption<Value = string> {
      label: string;
      value: Value;
    }

    export type AutoCompleteSuggestion<Value = string> =
      | string
      | AutoCompleteOption<Value>;

    function getSuggestionText<Value>(
      suggestion: AutoCompleteSuggestion<Value>
    ): string {
      return typeof suggestion === "string" ? suggestion : suggestion.label;
    }

    export interface InlineAutoCompleteOptions<
      Value = string,
      E extends EditableHTMLElement = HTMLInputElement,
    > {
      suggestions: readonly AutoCompleteSuggestion<Value>[];
      matches?(options: CaseInsensitiveMatchOptions): boolean;
      onChange?: ChangeEventHandler<E>;
      whitespace?: WhitespaceFilter;
    }

    export interface InlineAutoCompleteImplementation<
      E extends EditableHTMLElement = HTMLInputElement,
    > {
      onChange: ChangeEventHandler<E>;
    }

    export function useInlineAutoComplete<
      Value = string,
      E extends EditableHTMLElement = HTMLInputElement,
    >(
      options: InlineAutoCompleteOptions<Value, E>
    ): InlineAutoCompleteImplementation<E> {
      const {
        matches = caseInsensitiveMatch,
        onChange = noop,
        whitespace = "keep",
        suggestions,
      } = options;

      const prevValue = useRef("");

      return {
        onChange(event) {
          onChange(event);

          const input = event.currentTarget;
          const { value } = input;
          const isBackspace = value.length < prevValue.current.length;
          prevValue.current = value;
          if (isBackspace) {
            return;
          }

          const query = toSearchQuery(value, whitespace);
          if (!query) {
            return;
          }

          const match = suggestions.find((suggestion) => {
            const text = getSuggestionText(suggestion);
            const value = toSearchQuery(text, whitespace);

            return matches({
              query,
              value,
              startsWith: true,
            });
          });

          if (!match || isBackspace) {
            return;
          }

          const nextValue = getSuggestionText(match);
          input.value = nextValue;
          input.setSelectionRange(query.length, nextValue.length);
        },
      };
    }