# All files NativeSelect.tsx

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

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
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227

     import {
      forwardRef,
      type CSSProperties,
      type HTMLAttributes,
      type ReactNode,
      type SelectHTMLAttributes,
    } from "react";
    import { getIcon } from "../icon/iconConfig.js";
    import { type PropsWithRef } from "../types.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { FormMessageContainer } from "./FormMessageContainer.js";
    import { Label } from "./Label.js";
    import { TextFieldContainer } from "./TextFieldContainer.js";
    import { getFormConfig } from "./formConfig.js";
    import { nativeSelect, nativeSelectContainer } from "./nativeSelectStyles.js";
    import {
      type FormFieldOptions,
      type UserAgentAutoCompleteProps,
    } from "./types.js";

    export interface NativeSelectProps
      extends SelectHTMLAttributes<HTMLSelectElement>,
        UserAgentAutoCompleteProps,
        FormFieldOptions {
      /**
       * A custom dropdown icon to use instead of the browser's default select
       * dropdown icon.
       *
       * Set this to `null` if the browser's default icon should be used instead.
       *
       * @defaultValue `getIcon("dropdown")`
       */
      icon?: ReactNode;

      /**
       * This applies custom inline styles to the `<select>` element since the
       * `style` prop is applied to the container element instead.
       */
      selectStyle?: CSSProperties;

      /**
       * This applies custom className to the `<select>` element since the
       * `className` prop is applied to the container element instead.
       */
      selectClassName?: string;

      /**
       * This should be a list of `<option>` elements for specific values within the
       * `<select>`.
       *
       * Check out the {@link NativeSelect} for examples around using "placeholder"
       * text and requiring a value to be selected.
       */
      children: ReactNode;

      /**
       * Optional props to provide to the {@link TextFieldContainer} component.
       * There probably isn't any real use for this prop other than if you need to
       * add a `ref` for some DOM behavior.
       */
      containerProps?: PropsWithRef<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
    }

    /**
     * **Server Component**
     *
     * This component is a wrapper for the native `<select>` field that applies the
     * same theming as `TextField` and `TextArea` components. This component might
     * not be used much since the `Select` offers more styling options.
     *
     * @example
     * Simple Example
     * ```tsx
     * <NativeSelect label="Label">
     *   <option value="a">Value 1</option>
     *   <option value="b">Value 2</option>
     *   <option value="c">Value 3</option>
     *   <option value="d">Value 4</option>
     * </NativeSelect>
     * ```
     *
     * @example
     * Required Value Example
     * ```tsx
     * function Example(): ReactElement {
     *   // using `defaultValue=""` makes it so the first option selected by default
     *   // and considered an "invalid" value since it is `disabled`
     *   //
     *   // a `name` must be set with `required` so that the form validation will
     *   // fire if the value is still the empty string when the form is submitted
     *   //
     *   // the first `<option>` is kind of like placeholder text since it doesn't
     *   // have a value and is disabled by default
     *
     *   return (
     *     <NativeSelect
     *       label="State"
     *       name="state"
     *       required
     *       defaultValue=""
     *     >
     *       <option value="" disabled>Choose a state</option>
     *       {states.map(({ name, code }) => (
     *         <option key={code} value={code}>{name}</option>
     *       ))}
     *     </NativeSelect>
     *   );
     * }
     * ```
     */
    export const NativeSelect = forwardRef<HTMLSelectElement, NativeSelectProps>(
      function NativeSelect(props, ref) {
        const {
          id: propId,
          style,
          className,
          icon: propIcon,
          label,
          labelProps,
          labelStyle,
          labelClassName,
          selectStyle,
          selectClassName,
          autoCompleteValue,
          autoComplete = autoCompleteValue,
          name = autoCompleteValue,
          dense = false,
          error = false,
          active = false,
          inline = false,
          stretch = false,
          leftAddon,
          rightAddon: propRightAddon,
          leftAddonProps,
          rightAddonProps,
          disableLeftAddonStyles = false,
          disableRightAddonStyles = false,
          theme: propTheme,
          underlineDirection: propUnderlineDirection,
          messageProps,
          messageContainerProps,
          containerProps,
          children,
          ...remaining
        } = props;
        const { disabled = false, readOnly = false, multiple = false } = props;
        const id = useEnsuredId(propId, "select");
        const theme = getFormConfig("theme", propTheme);
        const underlineDirection = getFormConfig(
          "underlineDirection",
          propUnderlineDirection
        );
        const icon = getIcon("dropdown", propIcon);
        const underlined = theme === "underline" || theme === "filled";

        let rightAddon = propRightAddon;
        if (typeof propRightAddon === "undefined" && !multiple) {
          rightAddon = icon;
        }

        return (
          <FormMessageContainer
            {...messageContainerProps}
            messageProps={messageProps}
          >
            <TextFieldContainer
              {...containerProps}
              style={style}
              className={nativeSelectContainer({
                label: !!label,
                multiple,
                underlined,
                className,
              })}
              theme={theme}
              label={!!label}
              error={error}
              dense={dense}
              inline={inline}
              active={active}
              stretch={stretch}
              readOnly={readOnly}
              disabled={disabled}
              leftAddon={leftAddon}
              leftAddonProps={leftAddonProps}
              rightAddon={rightAddon}
              rightAddonProps={rightAddonProps}
              underlineDirection={underlineDirection}
              disableLeftAddonStyles={disableLeftAddonStyles}
              disableRightAddonStyles={disableRightAddonStyles}
            >
              <select
                {...remaining}
                id={id}
                ref={ref}
                autoComplete={autoComplete}
                name={name}
                disabled={disabled}
                style={selectStyle}
                className={nativeSelect({
                  icon: !!icon,
                  className: selectClassName,
                })}
              >
                {children}
              </select>
              {label && (
                <Label
                  {...labelProps}
                  htmlFor={id}
                  style={labelProps?.style ?? labelStyle}
                  className={labelProps?.className ?? labelClassName}
                  floating
                  dense={dense}
                  error={error}
                  active={active}
                  disabled={disabled}
                >
                  {label}
                </Label>
              )}
            </TextFieldContainer>
          </FormMessageContainer>
        );
      }
    );