Skip to content

Commit

Permalink
Use fileRejections which includes files and why they were rejected
Browse files Browse the repository at this point in the history
Fix type errors
Only use maxSize in string if it's a number
Use readonly for FileRejections
Check if object isEmpty for custom message check
  • Loading branch information
kangaree committed Dec 19, 2024
1 parent 825f722 commit e437d36
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
29 changes: 17 additions & 12 deletions playbook/app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useCallback, useRef } from 'react'
import { useDropzone, DropzoneInputProps, DropzoneRootProps } from 'react-dropzone'
import { useDropzone, DropzoneInputProps, DropzoneRootProps, FileRejection } from 'react-dropzone'
import classnames from 'classnames'

import { buildCss, buildDataProps, noop, buildHtmlProps } from '../utilities/props'
Expand All @@ -9,6 +9,8 @@ import type { Callback } from '../types'
import Body from '../pb_body/_body'
import Card from '../pb_card/_card'

import { isEmpty } from '../utilities/object'

type FileUploadProps = {
accept?: Record<string, string[]>,
className?: string,
Expand All @@ -19,7 +21,7 @@ type FileUploadProps = {
acceptedFilesDescription?: string,
maxSize?: number,
onFilesAccepted: Callback<File, File>,
onFilesRejected: (error: string, files: File[]) => void,
onFilesRejected: (error: string, files: readonly FileRejection[]) => void,
}

const getFormattedFileSize = (fileSize: number): string => {
Expand Down Expand Up @@ -48,27 +50,30 @@ const FileUpload = (props: FileUploadProps): React.ReactElement => {
getRootProps: () => DropzoneRootProps & any;
getInputProps: () => DropzoneInputProps & any;
isDragActive: boolean;
rejectedFiles: File[];
fileRejections: readonly FileRejection[];
}

const { getRootProps, getInputProps, isDragActive, rejectedFiles }: DropZoneProps = useDropzone({
const { getRootProps, getInputProps, isDragActive, fileRejections }: DropZoneProps = useDropzone({
accept,
maxSize,
onDrop,
})

const prevRejected = useRef<File[] | null>(null);
const prevRejected = useRef<readonly FileRejection[] | null>(null);

const maxFileSizeText = `Max file size is ${getFormattedFileSize(maxSize)}.`
let maxFileSizeText = ''
if (maxSize !== undefined) {
maxFileSizeText = `Max file size is ${getFormattedFileSize(maxSize)}.`
}

useEffect(() => {
if (rejectedFiles === prevRejected.current) return
const isFileTooLarge = maxSize && rejectedFiles.length > 0 && rejectedFiles[0].size > maxSize;
if (fileRejections === prevRejected.current) return
const isFileTooLarge = maxSize && fileRejections.length > 0 && fileRejections[0].file.size > maxSize;
if (isFileTooLarge) {
onFilesRejected(`File size is too large! ${maxFileSizeText}`, rejectedFiles)
onFilesRejected(`File size is too large! ${maxFileSizeText}`, fileRejections)
}
prevRejected.current = rejectedFiles
}, [maxFileSizeText, maxSize, onFilesRejected, rejectedFiles])
prevRejected.current = fileRejections
}, [maxFileSizeText, maxSize, onFilesRejected, fileRejections])

const acceptedFileTypes = () => {
if (!accept) {
Expand All @@ -90,7 +95,7 @@ const FileUpload = (props: FileUploadProps): React.ReactElement => {
const getDescription = () => {
return customMessage
? customMessage
: `Choose a file or drag it here.${accept === null ? '' : ` The accepted file types are: ${acceptedFilesDescription || acceptedFileTypes()}.`}${maxSize ? ` ${maxFileSizeText}` : ''}`;
: `Choose a file or drag it here.${isEmpty(accept) ? '' : ` The accepted file types are: ${acceptedFilesDescription || acceptedFileTypes()}.`}${maxSize ? ` ${maxFileSizeText}` : ''}`;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const AcceptedFilesList = ({ files }) => (
const RejectedFilesList = ({ files }) => (
<List>
{files.map((file) => (
<ListItem key={file.name}><Body color="error">{`${file.name} (file too large)`}</Body></ListItem>
<ListItem key={file.file.name}><Body color="error">{`${file.file.name} (file too large)`}</Body></ListItem>
))}
</List>
)
Expand Down

0 comments on commit e437d36

Please sign in to comment.