# All files / form useListboxProvider.ts

96.42% Statements 54/56
50% Branches 1/2
100% Functions 1/1
96.42% Lines 54/56

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
    571x
    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
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    100x
    100x

    100x
    100x
     "use client";
    import { createContext, useContext, type RefObject } from "react";
    import { type TextExtractor } from "../types.js";
    import { type ChangeableHTMLElement } from "./utils.js";

    /**
     * @internal
     * @since 6.0.0
     */
    export interface ListboxContext {
      /**
       * This ref is used to trigger the change event when an option is clicked.
       */
      inputRef: RefObject<ChangeableHTMLElement>;

      /**
       * An optional text extractor when the {@link currentValue} is not a
       * `string | number | null` (mostly the `Autocomplete`).
       */
      extractor?: TextExtractor<unknown>;

      /**
       * This is used within the `Option` component to determine if it is currently
       * selected or not.
       *
       * This is normally a `string | number | null`
       */
      currentValue: unknown;

      /** Convenience pass-through value */
      selectedIconAfter?: boolean;
      /** Convenience pass-through value */
      disableSelectedIcon?: boolean;
    }

    const context = createContext<ListboxContext | null>(null);

    /**
     * **Client Component**
     *
     * @internal
     * @since 6.0.0
     */
    export const { Provider: ListboxProvider } = context;

    /**
     * @internal
     * @since 6.0.0
     */
    export function useListboxContext(): ListboxContext {
      const value = useContext(context);
      if (!value) {
        throw new Error("The `ListboxProvider` must be a parent component");
      }
      return value;
    }