# All files / autocomplete useInlineAutocomplete.ts

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

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

     "use client";
    import { useRef, type ChangeEventHandler } from "react";
    import { type EditableHTMLElement } from "../form/utils.js";
    import { caseInsensitiveSearch } from "../searching/caseInsensitive.js";
    import { type WhitespaceFilter } from "../searching/types.js";
    import { type AutocompleteSuggestion } from "./types.js";
    import { getSuggestionText } from "./utils.js";

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

    export interface InlineAutocompleteOptions<
      Value = string,
      E extends EditableHTMLElement = HTMLInputElement,
    > {
      onChange?: ChangeEventHandler<E>;
      whitespace?: WhitespaceFilter;
      suggestions: readonly AutocompleteSuggestion<Value>[];
      defaultValue?: string;
    }

    export interface InlineAutocompleteImplementation<
      E extends EditableHTMLElement = HTMLInputElement,
    > {
      "aria-autocomplete": "inline";
      onChange: ChangeEventHandler<E>;
    }

    export function useInlineAutocomplete<
      Value = string,
      E extends EditableHTMLElement = HTMLInputElement,
    >(
      options: InlineAutocompleteOptions<Value, E>
    ): InlineAutocompleteImplementation<E> {
      const {
        onChange = noop,
        whitespace = "keep",
        suggestions,
        defaultValue = "",
      } = options;

      const prevValue = useRef(defaultValue);

      return {
        "aria-autocomplete": "inline",
        onChange(event) {
          onChange(event);

          const input = event.currentTarget;
          const { value } = input;

          const match = caseInsensitiveSearch({
            type: "search",
            list: suggestions,
            query: value,
            extractor: getSuggestionText,
            whitespace,
            startsWith: true,
          });

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

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

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

          if (match && value.length > prevValue.current.length) {
            const nextValue = getSuggestionText(match);
            input.value = nextValue;
            input.setSelectionRange(value.length, nextValue.length);
          }

          prevValue.current = value;
        },
      };
    }