# All files / src/autocomplete useInlineAutocomplete.ts

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

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

     "use client";
    import { type HTMLAttributes } from "react";
    import { type EditableHTMLElement } from "../form/utils.js";
    import { caseInsensitiveSearch } from "../searching/caseInsensitive.js";
    import { type WhitespaceFilter } from "../searching/types.js";
    import { type NonNullMutableRef, type TextExtractor } from "../types.js";
    import { type AutocompleteOption } from "./types.js";
    import { useInlineSelection } from "./useInlineSelection.js";
    import { defaultAutocompleteExtractor, isTextMovementEvent } from "./utils.js";

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

    export type InlineAutocompleteHandlers<
      E extends EditableHTMLElement = HTMLInputElement,
    > = Pick<HTMLAttributes<E>, "onKeyDown" | "onFocus" | "onBlur" | "onChange">;

    /**
     * @remarks \@since 6.0.0
     */
    export interface InlineAutocompleteOptions<
      T = AutocompleteOption,
      E extends EditableHTMLElement = HTMLInputElement,
    > extends InlineAutocompleteHandlers<E> {
      options: readonly T[];
      extractor?: TextExtractor<T>;
      whitespace?: WhitespaceFilter;
      defaultValue?: string;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface InlineAutocompleteProps<
      E extends EditableHTMLElement = HTMLInputElement,
    > extends Required<InlineAutocompleteHandlers<E>> {
      "aria-autocomplete": "inline";
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface InlineAutocompleteImplementation<
      E extends EditableHTMLElement = HTMLInputElement,
    > {
      inputProps: InlineAutocompleteProps<E>;
      typedValue: NonNullMutableRef<string>;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export function useInlineAutocomplete<
      T extends AutocompleteOption,
      E extends EditableHTMLElement = HTMLInputElement,
    >(
      options: Omit<InlineAutocompleteOptions<T, E>, "extractor">
    ): InlineAutocompleteImplementation<E>;
    export function useInlineAutocomplete<
      T,
      E extends EditableHTMLElement = HTMLInputElement,
    >(
      options: InlineAutocompleteOptions<T, E> & { extractor: TextExtractor<T> }
    ): InlineAutocompleteImplementation<E>;
    export function useInlineAutocomplete<
      T,
      E extends EditableHTMLElement = HTMLInputElement,
    >(
      options: InlineAutocompleteOptions<T, E>
    ): InlineAutocompleteImplementation<E> {
      const {
        options: suggestions,
        extractor = defaultAutocompleteExtractor("useInlineAutocomplete"),
        onChange = noop,
        whitespace = "keep",
      } = options;

      const { typedValue, handleSelection } = useInlineSelection(options);
      return {
        typedValue,
        inputProps: {
          "aria-autocomplete": "inline",
          onChange(event) {
            onChange(event);
            handleSelection({
              event,
              search: (query) => {
                const match = caseInsensitiveSearch({
                  type: "search",
                  list: suggestions,
                  query,
                  extractor,
                  whitespace,
                  startsWith: true,
                });

                return (match && extractor(match)) || "";
              },
            });
          },
          onBlur(event) {
            typedValue.current = event.currentTarget.value;
          },
          onFocus(event) {
            typedValue.current = event.currentTarget.value;
          },
          onKeyDown(event) {
            if (isTextMovementEvent(event)) {
              typedValue.current = event.currentTarget.value;
            }
          },
        },
      };
    }