1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2024-11-14 17:35:35 +01:00
foris-js/src/bootstrap/FileInput.md
2020-08-18 16:17:00 +02:00

1.1 KiB

Bootstrap component for file input. Includes label and has predefined sizes and structure for using in foris forms.

All additional props are passed to the <input type="file"> HTML component.

import { useState } from "react";

const [files, setFiles] = useState([]);

// Note that files is not an array but FileList.
const label = files.length === 1 ? files[0].name : "Choose file";

<form className="col">
    <FileInput
        files={files}
        label={label}
        helpText="Will be uploaded"
        onChange={(event) => setFiles(event.target.files)}
    />
</form>;

FileInput with multiple files

import { useState } from "react";

const [files, setFiles] = useState([]);

// Note that files is not an array but FileList.
const label =
    files.length > 0
        ? Array.from(files)
              .map((file) => file.name)
              .join(", ")
        : "Choose files";

<form className="col">
    <FileInput
        files={files}
        label={label}
        helpText="Will be uploaded"
        onChange={(event) => setFiles(event.target.files)}
        multiple
    />
</form>;