FileInput
Basic Usage
import { FileInput } from '@krakentech/coral';
<FileInput />Click or drag files to this area to upload
Only .jpg, .png, .doc and .pdf files will be accepted.
Properties
accept
Warning Consumers should still run server-side validation on files to ensure that the correct file formats have been uploaded.
<FileInput accept="image/png" />Click or drag files to this area to upload
Only .jpg, .png, .doc and .pdf files will be accepted.
automaticUpload / automaticUploadAction
const automaticUploadAction = async (files) => {}
<FileInput automaticUpload automaticUploadAction={automaticUploadAction} />buttonLabel
<FileInput buttonLabel="Custom button label" />Click or drag files to this area to upload
Only .jpg, .png, .doc and .pdf files will be accepted.
buttonPosition
<FileInput buttonPosition="right" />Click or drag files to this area to upload
Only .jpg, .png, .doc and .pdf files will be accepted.
disabled
<FileInput disabled />Click or drag files to this area to upload
Only .jpg, .png, .doc and .pdf files will be accepted.
dropzoneTitle
<FileInput dropzoneTitle="Drop files here" />Drop files here
Only .jpg, .png, .doc and .pdf files will be accepted.
dropzoneCaption
<FileInput dropzoneCaption="A bit of extra text about file limits or file types accepted" />Click or drag files to this area to upload
A bit of extra text about file limits or file types accepted
error / errorMessage
<FileInput error errorMessage="Something went wrong" />Click or drag files to this area to upload
Only .jpg, .png, .doc and .pdf files will be accepted.
Something went wrong
maxFiles / multiple
<FileInput multiple maxFiles="5" />Click or drag files to this area to upload
Only .jpg, .png, .doc and .pdf files will be accepted.
name
Note: if you’re using
multipleyour name should be appended with[]to enable multiple files to be passed through the form data.
<FileInput name="inputName" />Click or drag files to this area to upload
Only .jpg, .png, .doc and .pdf files will be accepted.
uploadingLabel
<FileInput uploadingLabel="Custom uploading message..." />Async automatic upload
const asyncOnChangeUpload = async (files: FileList | null) => {
const fileStatus: FileInputUploadStatus[] = [];
if (files?.length) {
for (let i = 0; i < files.length; i++) {
const response: FileInputUploadStatus = {
name: files[i].name,
status: 'uploading',
};
const data = new FormData();
data.append('file', files[i]);
try {
await fetch('{endpoint}', {
method: 'POST',
body: data,
})
.then((res) => res.json())
.then((res) => {
/**
* The response from your endpoint could be structured like:
* 200 (successful upload): { "uploaded": true }
* 200 (failed upload): { "uploaded": false, "message": "Too big" }
*/
const success = res.uploaded;
if (!success) {
response.status = 'error';
response.message = res.message;
} else {
response.status = 'success';
}
});
} catch (error) {
response.status = 'error';
response.message = 'Something went wrong';
}
fileStatus.push(response);
}
return fileStatus;
}
return [];
};
<FileInput
automaticUpload
automaticUploadAction={asyncOnChangeUpload}
multiple
maxFiles={20}
/>;Event Handlers
- Files in
onChangeis a complete list of all files that have been uploaded, regardless of whether they are pending or not. If you upload one file then another after, unlikeautomaticUploadAction, you will receive both items infiles, not just the last one.onRemoveprovides only the file that you have removed in the callback.
<FileInput
onChange={(files) => console.log(files)}
onRemove={(file) => console.log(file)}
/>Full API
| Name | Type | Default |
|---|---|---|
accept | stringWhat filetypes can be uploaded. Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept | image/png,image/jpg,image/jpeg,.doc,.pdf |
automaticUpload | booleanIf | false |
automaticUploadAction | (pendingFiles: FileList | null) => Promise<FileInputUploadStatus[]>A function that returns a promise containing an array of FileInputUploadStatus objects for the upload status of each file to render. | |
buttonLabel | stringThe label for the button. | Choose files(s) |
buttonPosition | "left" | "center" | "right"Position of the button. | left |
disabled | booleanIf | false |
dropzoneTitle | stringTitle of the dropzone element. | Click or drag files to this area to upload |
dropzoneCaption | stringCaption of the dropzone element. | Only .jpg, .png, .doc and .pdf files will be accepted. |
error | booleanIf | false |
errorMessage | stringIf set, the | Invalid input |
id | stringThe id for the input. | file-input |
maxFiles | numberThe maximum number of files allowed to be uploaded. | 1 |
multiple | booleanIf | false |
name | stringThe field name used on the POST request when the form is submitted. | |
onChange | (files: FileList | null) => voidCallback fired whenever the input element loses user focus.
| |
onRemove | (file: File | null) => voidCallback fired when a file is removed from the input.
| |
uploadingLabel | stringThe text to display while files are being uploaded. | Uploading... |
attributes | AttributesThis prop can be used to pass HTML attributes directly to the component.
We currently allow passing Example usage: |