# All files / form Option.tsx

95.79% Statements 205/214
91.3% Branches 21/23
100% Functions 3/3
95.79% Lines 205/214

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
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    2151x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    9x
    9x
    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
    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
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    4x
    96x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    92x
    92x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    100x
    4x
    4x

    4x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    9x
    9x
    9x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    100x
    1x
     "use client";
    import { cnb } from "cnbuilder";
    import { forwardRef, Fragment, type ReactNode } from "react";
    import { getIcon } from "../icon/iconConfig.js";
    import { icon } from "../icon/styles.js";
    import {
      TextIconSpacing,
      type TextIconSpacingProps,
    } from "../icon/TextIconSpacing.js";
    import { getListItemHeight } from "../list/getListItemHeight.js";
    import { ListItemText } from "../list/ListItemText.js";
    import { MenuItem, type MenuItemProps } from "../menu/MenuItem.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { identity } from "../utils/identity.js";
    import { option } from "./optionStyles.js";
    import { useListboxContext } from "./useListboxProvider.js";
    import { triggerManualChangeEvent } from "./utils.js";

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

    /**
     * This icon is used while the option is unselected so that the selected and
     * unselected options have the same alignment.
     *
     * @since 6.0.0
     */
    export const DEFAULT_OPTION_UNSELECTED_ICON = (
      <span className={icon({ type: "svg" })} />
    );

    /**
     * @since 6.0.0 removed the `selected` and `focused` props.
     * @since 6.0.0 Added the `value`, `selectedIcon`, `unselectedIcon`,
     * `selectedIconAfter`, and `iconSpacingProps` props.
     */
    export interface OptionProps extends MenuItemProps {
      /**
       * @defaultValue `"option"`
       */
      role?: string;

      value: string | number;

      /**
       * @defaultValue `getIcon("selected")`
       */
      selectedIcon?: ReactNode;

      /**
       * @see {@link DEFAULT_OPTION_UNSELECTED_ICON}
       * @defaultValue `<span className="rmd-icon rmd-icon--svg" />`
       */
      unselectedIcon?: ReactNode;

      /**
       * Set this to `true` of the {@link selectedIcon}/{@link unselectedIcon}
       * should appear as the {@link rightAddon} instead of the {@link leftAddon}.
       *
       * @defaultValue `false`
       */
      selectedIconAfter?: boolean;

      /**
       * Since the `selectedIcon`/`unselectedIcon` are rendered as
       * `leftAddon`/`rightAddon`, the provided `leftAddon`/`rightAddon` will be
       * wrapped in the {@link TextIconSpacing} component to maintain the correct
       * spacing. You can use this prop to provide any additional configuration to
       * the spacing.
       *
       * @example
       * ```tsx
       * <Option
       *   leftAddon={<Avatar>A</Avatar>}
       *   leftAddonType="avatar"
       *   value={0}
       *   textIconSpacingProps={{
       *     beforeClassName: "my-custom-class-name",
       *   }}
       * >
       *   Some Content
       * </Option>
       * ```
       */
      textIconSpacingProps?: Omit<TextIconSpacingProps, "icon" | "children">;
    }

    /**
     * **Client Component**
     *
     * This component is a wrapper around the {@link MenuItem} to implement custom
     * select option behavior.
     *
     * @since 6.0.0 removed the `selected` and `focused` props.
     * @since 6.0.0 Added the `value`, `selectedIcon`, `unselectedIcon`,
     * `selectedIconAfter`, and `iconSpacingProps` props.
     */
    export const Option = forwardRef<HTMLLIElement, OptionProps>(
      function Option(props, ref) {
        const {
          id: propId,
          role = "option",
          value,
          children: propChildren,
          onClick = noop,
          className,
          selectedIcon: propSelectedIcon,
          unselectedIcon: propUnselectedIcon,
          selectedIconAfter: propSelectedIconAfter,
          textIconSpacingProps,
          leftAddon: propLeftAddon,
          leftAddonType,
          leftAddonClassName,
          rightAddon: propRightAddon,
          rightAddonType,
          rightAddonClassName,
          secondaryText,
          height: propHeight,
          disableTextChildren: propDisableTextChildren,
          ...remaining
        } = props;

        const id = useEnsuredId(propId, "option");
        const {
          inputRef,
          extractor = identity,
          currentValue,
          disableSelectedIcon,
          selectedIconAfter: contextSelectedIconAfter,
        } = useListboxContext();
        const selectedIconAfter = propSelectedIconAfter ?? contextSelectedIconAfter;
        const selected = value === currentValue;
        const selectedIcon = getIcon(
          "selected",
          disableSelectedIcon ? null : propSelectedIcon
        );
        const unselectedIcon = disableSelectedIcon
          ? null
          : propUnselectedIcon ?? DEFAULT_OPTION_UNSELECTED_ICON;
        const icon = selected ? selectedIcon : unselectedIcon;

        let leftAddon = propLeftAddon;
        let rightAddon = propRightAddon;
        let children = propChildren;
        let disableTextChildren = propDisableTextChildren;
        if (!selectedIconAfter && icon) {
          leftAddon = icon;
          if (propLeftAddon) {
            disableTextChildren = true;
            const Wrapper = propDisableTextChildren ? Fragment : ListItemText;

            children = (
              <TextIconSpacing {...textIconSpacingProps} icon={propLeftAddon}>
                <Wrapper>{children}</Wrapper>
              </TextIconSpacing>
            );
          }
        } else if (icon) {
          rightAddon = icon;
          if (propRightAddon) {
            disableTextChildren = true;
            const Wrapper = propDisableTextChildren ? Fragment : ListItemText;

            children = (
              <TextIconSpacing {...textIconSpacingProps} icon={propRightAddon}>
                <Wrapper>{children}</Wrapper>
              </TextIconSpacing>
            );
          }
        }

        const height = getListItemHeight({
          height: propHeight,
          leftAddon: leftAddon === icon ? null : leftAddon,
          leftAddonType,
          rightAddon: rightAddon === icon ? null : rightAddon,
          rightAddonType,
          secondaryText,
        });

        return (
          <MenuItem
            {...remaining}
            aria-selected={selected || undefined}
            id={id}
            ref={ref}
            role={role}
            onClick={(event) => {
              onClick(event);
              triggerManualChangeEvent(inputRef.current, extractor(value));
            }}
            className={option({ icon: !!icon, selected, className })}
            secondaryText={secondaryText}
            height={height}
            leftAddon={leftAddon}
            leftAddonType={leftAddonType}
            leftAddonClassName={cnb(
              leftAddon === icon && "rmd-option__icon",
              leftAddonClassName
            )}
            rightAddon={rightAddon}
            rightAddonType={rightAddonType}
            rightAddonClassName={cnb(
              rightAddon === icon && "rmd-option__icon",
              rightAddonClassName
            )}
            disableTextChildren={disableTextChildren}
          >
            {children}
          </MenuItem>
        );
      }
    );