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'
Un archivo, con reglas
Recibe un CSV de hasta 5 MB. Prueba otro tipo de archivo: dice qué recibe en su lugar.
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.
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.
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:
acceptpara el tipo de archivo,maxSizepara 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
| FileDrop | Tipo | Por defecto |
|---|---|---|
acceptWhat it takes: extensions (`.csv`) or types (`text/csv`, `image/*`). Anything else is refused with a message. | string[] | |
aria-describedby | string | |
aria-labelThe name for screen readers when there is no Field around it. | string | |
aria-labelledby | string | |
childrenThe sentence inside the zone. By default «Drop a file here or choose one». | ReactNode | |
className | string | |
disabled | boolean | |
filesThe chosen files. Leave it out to let the component keep them. | File[] | |
idInside a Field it comes from the Field. | string | |
invalidMarks the value as wrong. Inside a `<Field error>` it is set for you. | boolean | |
maxSizeThe largest file it takes, in bytes. | number | |
multipleTakes several files. With one, a new file replaces the old one. | boolean | |
onChange | ((files: File[]) => void) |