Ir al contenido

FileDrop

Un lugar para soltar archivos del computador, con un botón para quien prefiera elegirlos. Lista lo elegido con su tamaño, y dice por qué se rechazó un archivo.

import { FileDrop } from '@sypnia/ui/react'

En esta página
  1. Un archivo, con reglas
  2. Varios archivos
  3. Desactivado
  4. Cuándo usarlo
  5. Para todos
  6. Props

Un archivo, con reglas

Recibe un CSV de hasta 5 MB. Prueba otro tipo de archivo: dice qué recibe en su lugar.

Suelta un archivo aquí o elige uno

A CSV file of up to 5 MB, with the names of the columns in the first line.

Código
import { useState } from 'react';
import { Field, FileDrop } from '@sypnia/ui/react';

export default function Basic() {
  const [files, setFiles] = useState<File[]>([]);
  return (
    <Field label="Rows to import" hint={files.length ? 'The first line must be the names of the columns.' : 'A CSV file of up to 5 MB, with the names of the columns in the first line.'}>
      <FileDrop files={files} onChange={setFiles} accept={['.csv']} maxSize={5 * 1024 * 1024} />
    </Field>
  );
}

Varios archivos

Cada archivo nuevo se agrega a la lista, y cada uno se puede quitar.

Drop the screenshots here or choose them

Pictures or PDF, up to 10 MB each.

Código
import { Field, FileDrop } from '@sypnia/ui/react';

export default function Several() {
  return (
    <Field label="Attachments" hint="Pictures or PDF, up to 10 MB each.">
      <FileDrop multiple accept={['image/*', '.pdf']} maxSize={10 * 1024 * 1024}>Drop the screenshots here or choose them</FileDrop>
    </Field>
  );
}

Desactivado

Dice por qué no se puede soltar nada.

Suelta un archivo aquí o elige uno

An import is already running. Wait for it to finish.

Código
import { Field, FileDrop } from '@sypnia/ui/react';

export default function States() {
  return (
    <Field label="Rows to import" hint="An import is already running. Wait for it to finish.">
      <FileDrop disabled accept={['.csv']} />
    </Field>
  );
}

Cuándo usarlo

Úsalo cuando

  • La persona trae un archivo de su computador: un CSV para importar, un logo, un certificado.
  • Hay reglas sobre lo que se recibe: accept para el tipo de archivo, maxSize para su peso.

No lo uses cuando

  • El archivo es una imagen que se debe ver y recortar: ImageUpload o AvatarUpload.
  • Es un archivo pequeño en un formulario apretado: FileInput ocupa una línea.
  • El contenido es un texto corto que la persona podría pegar: Textarea o CodeInput.

Para todos

  • Todo se puede hacer sin arrastrar: el botón abre el selector de archivos del dispositivo, y es la parte que recibe el foco.
  • Un archivo rechazado se anuncia de inmediato como alerta que nombra el archivo, dice qué está mal y qué elegir en su lugar.
  • Cada archivo elegido tiene su propio botón, llamado «Quitar» más el nombre del archivo.
  • Mientras se arrastra un archivo encima, la zona se ilumina, así queda claro dónde soltarlo.
  • Dentro de un Field toma la etiqueta, la pista y el error por sí solo; suelto, dale un aria-label.

Props

FileDropTipoPor defecto
accept

What it takes: extensions (`.csv`) or types (`text/csv`, `image/*`). Anything else is refused with a message.

string[]
aria-describedby string
aria-label

The name for screen readers when there is no Field around it.

string
aria-labelledby string
children

The sentence inside the zone. By default «Drop a file here or choose one».

ReactNode
className string
disabled boolean
files

The chosen files. Leave it out to let the component keep them.

File[]
id

Inside a Field it comes from the Field.

string
invalid

Marks the value as wrong. Inside a `<Field error>` it is set for you.

boolean
maxSize

The largest file it takes, in bytes.

number
multiple

Takes several files. With one, a new file replaces the old one.

boolean
onChange ((files: File[]) => void)
  1. Un archivo, con reglas
  2. Varios archivos
  3. Desactivado
  4. Cuándo usarlo
  5. Para todos
  6. Props

    Busca páginas, secciones y términos