# All files / form TextField.tsx

100% Statements 274/274
50% Branches 2/4
100% Functions 2/2
100% Lines 274/274

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
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    2751x
    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
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    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
    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
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    1x
     "use client";
    import { cnb } from "cnbuilder";
    import type {
      CSSProperties,
      HTMLAttributes,
      InputHTMLAttributes,
      ReactNode,
    } from "react";
    import { forwardRef } from "react";
    import { PropsWithRef } from "../types.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { bem } from "../utils/bem.js";
    import { FormMessageContainer } from "./FormMessageContainer.js";
    import { useFormTheme } from "./FormThemeProvider.js";
    import { Label } from "./Label.js";
    import { TextFieldContainer } from "./TextFieldContainer.js";
    import type { FormFieldOptions, UserAgentAutoCompleteProps } from "./types.js";

    const styles = bem("rmd-text-field");

    /** @remarks \@since 6.0.0 */
    export interface TextFieldClassNameOptions {
      className?: string;

      /**
       * Set this value to `true` when a text field has a floating label the text
       * field is not focused or valued so that the placeholder is hidden by setting
       * the opacity to 0. This makes it so the placeholder and label to not cover
       * each other.
       *
       * @defaultValue `false`
       */
      placeholderHidden?: boolean;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export function textField(options: TextFieldClassNameOptions = {}): string {
      const { className, placeholderHidden = false } = options;

      return cnb(
        styles({
          "placeholder-hidden": placeholderHidden,
        }),
        className
      );
    }

    /**
     * These are all the "supported" input types for react-md so that they at least
     * render reasonably well by default. There is no built-in validation or
     * anything adding onto existing browser functionality for these types.
     *
     * @remarks
     * \@since 2.5.0 - `"search"` was added
     * \@since 6.0.0 Dropped support for `"week"` and `"month"` input types since
     * they are not available in Firefox and Safari at this time.
     */
    export type SupportedInputTypes =
      | "text"
      | "password"
      | "number"
      | "tel"
      | "email"
      | "date"
      | "time"
      | "datetime-local"
      | "url"
      | "color"
      | "search";

    /**
     * @remarks \@since 6.0.0 Renamed from `TextFieldAttributes` to
     * `TextFieldInputAttributes`
     */
    export type TextFieldInputAttributes = Omit<
      InputHTMLAttributes<HTMLInputElement>,
      "type"
    >;

    /**
     * @remarks \@since 6.0.0 Removed the `containerRef` prop.
     */
    export interface TextFieldProps
      extends TextFieldInputAttributes,
        UserAgentAutoCompleteProps,
        FormFieldOptions {
      /**
       * @defaultValue `"text-field-" + useId()`
       */
      id?: string;

      /**
       * Optional placeholder text to display in the text field.
       *
       * @defaultValue `label ? " " : ""`
       */
      placeholder?: string;

      /**
       * Any optional inline styles to set on the input.
       */
      inputStyle?: CSSProperties;

      /**
       * An optional `className` to add to the input.
       */
      inputClassName?: string;

      /**
       * The text field type.
       *
       * @defaultValue `"text"`
       */
      type?: SupportedInputTypes;

      /**
       * 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>;

      /**
       * The `children` will be rendered within the `TextFieldContainer` and before
       * the `<input />` element. This was added to support the new `Select`
       * component implementation
       *
       * @internal
       * @remarks \@since 6.0.0
       */
      children?: ReactNode;
    }

    /**
     * **Client Component**
     * This might be able to become a server component if I remove the useFormTheme hook
     *
     * The structure for this component is:
     * @example
     * Component Structure
     * ```tsx
     * <FormMessageContainer {...messageContainerProps}>
     *   <TextFieldContainer {...containerProps}>
     *     <input {...props} />
     *     <Label {...labelProps} />
     *   </TextFieldContainer>
     * </FormMessageContainer>
     * ```
     *
     * @example
     * Simple Example
     * ```tsx
     * import { TextField } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return (
     *     <TextField
     *       label="Name"
     *       placeholder="Bob"
     *       defaultValue=""
     *     />
     *   );
     * }
     * ```
     */
    export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(
      function TextField(props, ref) {
        const {
          id: propId,
          style,
          className,
          type = "text",
          label,
          labelProps,
          labelStyle,
          labelClassName,
          autoCompleteValue,
          autoComplete = autoCompleteValue,
          name = autoCompleteValue,
          dense = false,
          error = false,
          active = false,
          inline = false,
          stretch = false,
          leftAddon,
          rightAddon,
          disableLeftAddonStyles = false,
          disableRightAddonStyles = false,
          inputStyle,
          inputClassName,
          theme: propTheme,
          underlineDirection: propUnderlineDirection,
          messageProps,
          messageContainerProps,
          containerProps,
          children,
          ...remaining
        } = props;
        const { disabled = false, readOnly = false } = props;
        const id = useEnsuredId(propId, "text-field");
        const { theme, underlineDirection } = useFormTheme({
          theme: propTheme,
          underlineDirection: propUnderlineDirection,
        });

        let { placeholder = "" } = props;
        if (label && !placeholder) {
          // See the placeholder type definition comments for information
          placeholder = " ";
        }

        return (
          <FormMessageContainer
            {...messageContainerProps}
            messageProps={messageProps}
          >
            <TextFieldContainer
              {...containerProps}
              style={style}
              className={className}
              theme={theme}
              label={!!label}
              error={error}
              dense={dense}
              inline={inline}
              active={active}
              stretch={stretch}
              readOnly={readOnly}
              disabled={disabled}
              leftAddon={leftAddon}
              rightAddon={rightAddon}
              underlineDirection={underlineDirection}
              disableLeftAddonStyles={disableLeftAddonStyles}
              disableRightAddonStyles={disableRightAddonStyles}
            >
              {children}
              <input
                {...remaining}
                id={id}
                ref={ref}
                type={type}
                name={name}
                disabled={disabled}
                placeholder={placeholder}
                autoComplete={autoComplete}
                style={inputStyle}
                className={textField({
                  className: inputClassName,
                  placeholderHidden: !!label && !active,
                })}
              />
              {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>
        );
      }
    );