Atoms / File input

File input

A button and a drop target, the list of chosen files with a remove button each, and the limits.

import { FileInput, formatFileSize } from "@forgeron/ui";
/01 → Attachments
Drop files here, orimage/*, .pdf · up to 5 MB
import { FileInput, type FileRejection } from "@forgeron/ui";
import { useState } from "react";

export default function Attachments() {
  const [refused, setRefused] = useState<FileRejection[]>([]);
  return (
    <div style={{ display: "grid", gap: 8 }}>
      <FileInput
        label="Attachments"
        multiple
        accept="image/*,.pdf"
        maxSize={5 * 1024 * 1024}
        maxFiles={3}
        onReject={setRefused}
      />
      {refused.length > 0 ? (
        <code>{refused.map((r) => `${r.file.name}: ${r.reason}`).join(", ")}</code>
      ) : null}
    </div>
  );
}
API → Props
PropTypeDefaultDescription
labelstring—Names the field for assistive technology.
value / defaultValueFile[]—Controlled or not.
onChange(files: File[]) => void—On every change.
acceptstring—`image/*,.pdf`. Checked on a drop too.
multiplebooleanfalseSeveral files; else a new one replaces the old.
maxSize / maxFilesnumber—Bytes, and a count.
onReject(rejections) => void—{ file, reason: "type" | "size" | "count" }[]: tell the person.
hintstring—The line in the drop target.
disabledboolean—Not editable.
classNamestring—Extra classes.