Synertech Web/ui v0.57.5 Site Storybook

Documentation / Forms

FileSelector

Client@synertech/uiOpen in Storybook

The file selector: a drop zone (drag and drop or click) or a plain button, which validates each file against accept and maxSize (10 MB by default) and lists the selection with each file’s size. mode picks what it accepts: files, a folder (traversed recursively, through webkitdirectory), or both.

Controlled (value + onChange, which returns a File[]) or not. A rejected file does not enter the selection and is not reported on screen: onReject(file, reason) is what lets you announce it, for instance in error. Beyond maxFiles, the extra files are silently ignored.

Client component ("use client"). Its texts, rejection reasons included, are fileSelector… labels: English by default, French under <UILabelsProvider labels={frenchUILabels}>. For one instance, placeholder, browseLabel, browseFolderLabel and the labels prop win over the provider.

Examples

Basic usage

Uncontrolled: the drop zone keeps the selection, onChange passes it on at each addition.

Open in a tab
import { FileSelector } from "@synertech/ui";

export function Basic() {
  return (
    <FileSelector
      label="Pièce jointe"
      placeholder="Glissez un fichier ici, ou"
      browseLabel="parcourir"
      onChange={(files) =>
        console.log(
          "Sélection :",
          files.map((f) => f.name)
        )
      }
    />
  );
}

Several files, controlled

multiple and maxFiles cap the selection; onReject explains the rejections.

Open in a tab
import { useState } from "react";
import { FileSelector } from "@synertech/ui";

export function Multiple() {
  const [files, setFiles] = useState<File[]>([]);
  const [rejection, setRejection] = useState<string>();

  return (
    <FileSelector
      label="Justificatifs"
      multiple
      maxFiles={5}
      maxSize={5 * 1024 * 1024}
      accept=".pdf,image/*"
      placeholder="Déposez vos justificatifs ici, ou"
      browseLabel="parcourir"
      value={files}
      onChange={(next) => {
        setFiles(next);
        setRejection(undefined);
      }}
      onReject={(file) => setRejection(`« ${file.name} » refusé : PDF ou image de 5 Mo maximum.`)}
      error={rejection}
      helperText={`${files.length} / 5 fichiers`}
    />
  );
}

Button variant

variant="button" replaces the drop zone with a compact button, for a dense form.

Open in a tab
import { FileSelector } from "@synertech/ui";

export function ButtonVariant() {
  return (
    <FileSelector
      variant="button"
      size="sm"
      label="CV"
      accept=".pdf,.docx"
      browseLabel="Choisir un fichier"
      helperText="PDF ou Word."
    />
  );
}

Folders

mode="directory" selects a whole folder; mode="both" offers both buttons. A folder is a list of files: both modes keep all of them, with or without multiple.

Open in a tab
import { FileSelector } from "@synertech/ui";

export function Directories() {
  return (
    <div className="flex flex-col gap-6">
      <FileSelector
        mode="directory"
        label="Dossier du projet"
        placeholder="Glissez un dossier ici, ou"
        browseLabel="choisir un dossier"
      />
      <FileSelector
        mode="both"
        multiple
        label="Documents à importer"
        placeholder="Glissez des fichiers ou des dossiers ici, ou"
        browseLabel="Fichiers"
        browseFolderLabel="Dossier"
      />
    </div>
  );
}

Uploading an import

A real case: the controlled selection is sent then cleared; readOnly freezes it during the upload.

Open in a tab
import { useState } from "react";
import { Button, FileSelector } from "@synertech/ui";

export function UploadForm() {
  const [files, setFiles] = useState<File[]>([]);
  const [sending, setSending] = useState(false);

  const send = async () => {
    setSending(true);
    const body = new FormData();
    files.forEach((file) => body.append("files", file));
    await fetch("/api/imports", { method: "POST", body });
    setSending(false);
    setFiles([]);
  };

  return (
    <form
      className="flex flex-col gap-3"
      onSubmit={(event) => {
        event.preventDefault();
        void send();
      }}
    >
      <FileSelector
        label="Fichiers CSV"
        multiple
        accept=".csv"
        placeholder="Glissez vos exports CSV ici, ou"
        browseLabel="parcourir"
        value={files}
        onChange={setFiles}
        readOnly={sending}
      />
      <Button type="submit" loading={sending} disabled={files.length === 0} className="self-end">
        Importer {files.length > 0 && `(${files.length})`}
      </Button>
    </form>
  );
}

API reference

Components

FileSelector

ComponentClient
import { FileSelector } from "@synertech/ui";

Props — FileSelectorProps

PropTypeDefaultDescription
accept string —

Accepted file types (MIME types or extensions, e.g. "image/*,.pdf,.docx"), checked on every file, dropped ones included. Checked in the browser only: a name and a type are what the reader's machine says they are, and the server that receives the files checks again.

browseFolderLabel string —

Custom text for the browse folder button (used in "directory" and "both" modes)

browseLabel string —

Custom text for the browse button

className string —

Additional class names

disabled boolean false

Disabled state

error boolean | string —

true marks the field invalid; a message also does, and is written under the field in place of helperText. The controls are then described by the message, and the file input is aria-invalid. false and "" are no error.

helperText string —

Written under the field; a message passed as error takes its place.

icon ReactNode —

Custom icon for the dropzone

label string —

Label text

labels UILabelsOf<"fileSelector"> —

The strings the selector writes itself, for this instance: any fileSelector… key of UILabels, the reasons handed to onReject included. Wins over UILabelsProvider; placeholder, browseLabel and browseFolderLabel win over both.

maxFiles number —

Maximum number of files, wherever several can be selected

maxSize number 10 * 1024 * 1024

Maximum file size in bytes (default: 10MB). Checked in the browser only, like accept

mode "file" | "directory" | "both" "file"

Selection mode: file only, directory only, or both. A directory is handed to onChange as all the files it holds, whatever multiple says. A dropped folder is read until it has given maxFiles accepted files, and never past 10,000 entries: read to the end, a large one held the tab

multiple boolean false

Allow several files to be selected. Only mode="file" can mean a single one: a folder is a list of files, so directory and both always keep every file they are given

onChange (files: File[]) => void —

Callback when files change

onReject (file: File, reason: string) => void —

Called for each file left out — too large, of a type accept does not name, or already in the list (same name, path, size and date) — with the reason, translated.

placeholder string —

Custom placeholder text for dropzone

readOnly boolean false

Read-only state (shows files but can't change)

showFileList boolean true

Show file list below the selector

size "sm" | "md" | "lg" "md"

Component size

value File[] —

Currently selected files (controlled mode)

variant "dropzone" | "button" "dropzone"

Visual variant

Types

SelectedFile

Interface
interface SelectedFile
FieldTypeDefaultDescription
file• File —
id• string —
error string —