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
|
|
|
```
|