# All files FileInput.tsx

100% Statements 216/216
93.33% Branches 14/15
100% Functions 3/3
100% Lines 216/216

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
    2171x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    47x
    47x
    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
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    6x
    6x
    6x
    6x
    6x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    24x
    47x
    24x
    24x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    1x
     "use client";
    import { cnb } from "cnbuilder";
    import {
      forwardRef,
      type InputHTMLAttributes,
      type LabelHTMLAttributes,
      type ReactNode,
    } from "react";
    import {
      button,
      type ButtonClassNameOptions,
      type ButtonClassNameThemeOptions,
    } from "../button/buttonStyles.js";
    import { getIcon } from "../icon/iconConfig.js";
    import { useElementInteraction } from "../interaction/useElementInteraction.js";
    import { type PropsWithRef } from "../types.js";
    import { SrOnly } from "../typography/SrOnly.js";
    import { useEnsuredId } from "../useEnsuredId.js";

    /** @remarks \@since 6.0.0 */
    export type FileInputClassNameOptions = ButtonClassNameOptions;

    /** @remarks \@since 6.0.0 */
    export function fileInput(options: FileInputClassNameOptions = {}): string {
      return cnb("rmd-file-input", button(options));
    }

    /** @remarks \@since 6.0.0 */
    export type FileInputHTMLAttributes = Omit<
      InputHTMLAttributes<HTMLInputElement>,
      "type"
    >;

    /**
     * @remarks \@since 6.0.0 Removed the `disableIconSpacing` prop since it is no
     * longer required.
     */
    export interface FileInputProps
      extends ButtonClassNameThemeOptions,
        FileInputHTMLAttributes {
      /**
       * @defaultValue `"Upload"`
       */
      srOnlyLabel?: ReactNode;

      /**
       * Any additional props to provide to the container `<label>` element since
       * most props get passed to the `<input type="file">`. So this would be useful
       * for inline style or click handlers.
       */
      labelProps?: PropsWithRef<
        LabelHTMLAttributes<HTMLLabelElement>,
        HTMLLabelElement
      >;

      /**
       * An optional icon to display for the file input.
       *
       * @defaultValue `getIcon("upload")`
       */
      icon?: ReactNode;

      /**
       * Boolean if the icon should appear after the children in the label.
       *
       * @defaultValue `false`
       */
      iconAfter?: boolean;

      /**
       * Boolean if the file input should no longer allow the same file to be
       * selected multiple times and trigger the `onChange` each time it is
       * selected.
       *
       * @defaultValue `false`
       */
      disableRepeatableFiles?: boolean;

      /**
       * Children should generally be provided when the {@link buttonType} is
       * set to `"text"`. This defaults to a screen-reader only accessible text.
       *
       * @defaultValue `<SrOnly phoneOnly={responsive}>Upload</SrOnly>`
       */
      children?: ReactNode;
    }

    /**
     * **Client Component**
     * This might be able to become a server component if I remove the getIcon hook
     *
     * @example
     * Simple Example
     * ```tsx
     * import { FileInput, Form } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * const extensions = [
     *   "svg",
     *   "jpeg",
     *   "jpg",
     *   "png",
     *   "apng",
     *   "mkv",
     *   "mp4",
     *   "mpeg",
     *   "mpg",
     *   "webm",
     *   "mov",
     * ] as const;
     *
     * const FOUR_HUNDRED_MB = 400 * 1024 * 1024;
     * const maxFiles = 10;
     *
     * function Example(): ReactElement {
     *   const { stats, errors, onChange, clearErrors, reset, remove, accept } =
     *     useFileUpload({
     *       maxFiles,
     *       maxFileSize: FOUR_HUNDRED_MB,
     *       extensions,
     *     });
     *
     *   return (
     *     <Form>
     *       <FileInput accept={accept} multiple={maxFiles > 1} onChange={onChange} />
     *       <Button onClick={reset} disabled={!stats.length}>
     *         Remove all files
     *       </Button>
     *     </Form>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0 Added additional support for `iconSize` and
     * `responsive`. Also removed the `disableIconSpacing` prop since it is no
     * longer required.
     */
    export const FileInput = forwardRef<HTMLInputElement, FileInputProps>(
      function FileInput(props, ref) {
        const {
          id: propId,
          className,
          children: propChildren,
          icon: propIcon,
          iconAfter = false,
          srOnlyLabel = "Upload",
          disableRepeatableFiles = false,
          labelProps,
          theme = "primary",
          themeType = "contained",
          buttonType = propChildren ? "text" : "icon",
          disabled = false,
          iconSize,
          responsive,
          multiple = false,
          ...remaining
        } = props;
        const id = useEnsuredId(propId, "file-input");
        const { pressed, pressedClassName, ripples, handlers } =
          useElementInteraction({
            ...labelProps,
            onClick(event) {
              labelProps?.onClick?.(event);

              // stop propagation so 2 ripples are not created
              event.stopPropagation();
            },
            disabled,
          });

        const icon = getIcon("upload", propIcon);
        let children = propChildren;
        if (
          typeof propChildren === "undefined" &&
          !props["aria-label"] &&
          !props["aria-labelledby"]
        ) {
          children = <SrOnly phoneOnly={responsive}>{srOnlyLabel}</SrOnly>;
        }

        return (
          <label
            {...labelProps}
            {...handlers}
            className={fileInput({
              theme,
              themeType,
              buttonType,
              disabled,
              iconSize,
              pressed,
              responsive,
              pressedClassName,
              className: className || labelProps?.className,
            })}
          >
            {!iconAfter && icon}
            {children}
            {iconAfter && icon}
            <input
              {...remaining}
              id={id}
              ref={ref}
              value={
                disableRepeatableFiles || !props.onChange ? remaining.value : ""
              }
              type="file"
              className="rmd-hidden-input"
              disabled={disabled}
              multiple={multiple}
            />
            {ripples}
          </label>
        );
      }
    );