Skip to content

Commit

Permalink
fix: set isDragReject true when maxFiles rejects (react-dropzone#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
morijenab authored Oct 28, 2020
1 parent 96e7c15 commit b4a1ac6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ export function useDropzone(options = {}) {
)

const fileCount = draggedFiles.length
const isDragAccept = fileCount > 0 && allFilesAccepted({ files: draggedFiles, accept, minSize, maxSize, multiple })
const isDragAccept = fileCount > 0 && allFilesAccepted({ files: draggedFiles, accept, minSize, maxSize, multiple, maxFiles })
const isDragReject = fileCount > 0 && !isDragAccept

return {
Expand Down
8 changes: 7 additions & 1 deletion src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2097,9 +2097,11 @@ describe('useDropzone() hook', () => {
const onDropRejectedSpy = jest.fn()
const ui = (
<Dropzone accept="image/*" onDrop={onDropSpy} onDropRejected={onDropRejectedSpy} multiple={true} maxFiles={1}>
{({ getRootProps, getInputProps }) => (
{({ getRootProps, getInputProps, isDragReject, isDragAccept }) => (
<div {...getRootProps()}>
<input {...getInputProps()} />
{isDragReject && 'dragReject'}
{isDragAccept && 'dragAccept'}
</div>
)}
</Dropzone>
Expand All @@ -2109,6 +2111,10 @@ describe('useDropzone() hook', () => {
fireDrop(dropzone, createDtWithFiles(images))
await flushPromises(rerender, ui)
expect(onDropRejectedSpy).toHaveBeenCalled()
fireDragEnter(dropzone, createDtWithFiles(images))
await flushPromises(rerender, ui)
expect(dropzone).toHaveTextContent('dragReject')
expect(dropzone).not.toHaveTextContent('dragAccept')
expect(onDropSpy).toHaveBeenCalledWith([], [
{
file: images[0],
Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ function isDefined(value) {
return value !== undefined && value !== null
}

export function allFilesAccepted({ files, accept, minSize, maxSize, multiple }) {
if (!multiple && files.length > 1) {
export function allFilesAccepted({ files, accept, minSize, maxSize, multiple, maxFiles }) {
if ((!multiple && files.length > 1) || (multiple && maxFiles >= 1 && files.length > maxFiles) ) {
return false;
}

Expand Down
20 changes: 20 additions & 0 deletions src/utils/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,26 @@ it('rejects file when single accept criteria as array', () => {

})

describe('allFilesAccepted()', () => {
let utils
beforeEach(async done => {
utils = await import('./index')
done()
})
it('rejects file when multiple accept criteria', () => {
const files = [createFile('hamster.pdf', 100, 'application/pdf'),createFile('fish.pdf', 100, 'application/pdf')];
const images = [createFile('cats.gif', 1234, 'image/gif'), createFile('dogs.gif', 2345, 'image/jpeg')]
expect(utils.allFilesAccepted({ files, multiple: true})).toEqual(true)
expect(utils.allFilesAccepted({ files, multiple: true, maxFiles: 10 })).toEqual(true)
expect(utils.allFilesAccepted({ files, multiple: false, maxFiles: 10 })).toEqual(false)
expect(utils.allFilesAccepted({ files, multiple: true, accept:'image/jpeg' })).toEqual(false)
expect(utils.allFilesAccepted({ files: images, multiple: true,accept:'image/*' })).toEqual(true)
expect(utils.allFilesAccepted({ files, multiple: true, minSize: 110 })).toEqual(false)
expect(utils.allFilesAccepted({ files, multiple: true, maxSize: 99 })).toEqual(false)
expect(utils.allFilesAccepted({ files, multiple: true, maxFiles: 1 })).toEqual(false)
})
})

function createFile(name, size, type) {
const file = new File([], name, { type })
Object.defineProperty(file, 'size', {
Expand Down

0 comments on commit b4a1ac6

Please sign in to comment.