StatelessFIleInput: add maxSize and showIcon (#544)

This commit is contained in:
John Paul
2022-06-20 17:04:08 -04:00
committed by GitHub
parent 405a3eda60
commit 69a437a1a8
4 changed files with 89 additions and 43 deletions

27
plugins/fileUtils.js Normal file
View File

@@ -0,0 +1,27 @@
import { formatBytes } from '~/plugins/shorthands'
/**
* @param {File | Blob} file the file to validate
* @param {{ maxSize: number, alertOnInvalid: boolean }} validationOptions the
* constraints to validate the file against
* @param validationOptions.maxSize the max file size in bytes
* @param validationOptions.alertOnInvalid if an alert should pop up describing
* each validation error
* @returns `true` if the file is valid; `false` otherise
*/
export const fileIsValid = (file, validationOptions) => {
const { maxSize, alertOnInvalid } = validationOptions
if (maxSize !== null && maxSize !== undefined && file.size > maxSize) {
console.log(`File size: ${file.size}, max size: ${maxSize}`)
if (alertOnInvalid) {
alert(
`File ${file.name} is too big! Must be less than ${formatBytes(
maxSize
)}`
)
}
return false
}
return true
}