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

49 lines
1.1 KiB
Markdown
Raw Normal View History

2020-08-18 15:39:00 +02:00
Bootstrap component for file input. Includes label and has predefined sizes and
structure for using in foris forms.
2019-10-22 10:24:10 +02:00
All additional `props` are passed to the `<input type="file">` HTML component.
```js
2020-08-18 15:39:00 +02:00
import { useState } from "react";
2020-06-04 22:52:24 +02:00
2019-10-22 10:24:10 +02:00
const [files, setFiles] = useState([]);
2020-06-04 22:52:24 +02:00
// 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"
2020-08-18 15:39:00 +02:00
onChange={(event) => setFiles(event.target.files)}
2020-06-04 22:52:24 +02:00
/>
2020-08-18 15:39:00 +02:00
</form>;
2020-06-04 22:52:24 +02:00
```
### FileInput with multiple files
2020-08-18 15:39:00 +02:00
2020-06-04 22:52:24 +02:00
```js
2020-08-18 15:39:00 +02:00
import { useState } from "react";
2020-06-04 22:52:24 +02:00
const [files, setFiles] = useState([]);
// Note that files is not an array but FileList.
2020-08-18 15:39:00 +02:00
const label =
files.length > 0
? Array.from(files)
.map((file) => file.name)
.join(", ")
: "Choose files";
2020-06-04 22:52:24 +02:00
<form className="col">
<FileInput
files={files}
label={label}
helpText="Will be uploaded"
2020-08-18 15:39:00 +02:00
onChange={(event) => setFiles(event.target.files)}
2020-06-04 22:52:24 +02:00
multiple
/>
2020-08-18 15:39:00 +02:00
</form>;
2019-10-22 10:24:10 +02:00
```