Skip to content

Commit

Permalink
fix: Reject all files in single file drop (#641)
Browse files Browse the repository at this point in the history
If multiple files were dropped and `multiple == false`,
react-dropzone will reject all files. Previously it would
accept one file and reject the rest.

BREAKING CHANGE: This can break applications that rely on the current behavior.

Closes #458
  • Loading branch information
Schmitze333 authored and Andrey Okonetchnikov committed Aug 17, 2018
1 parent 71a54ad commit da18e76
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -196,10 +196,10 @@ class Dropzone extends React.Component {
}
})

if (!multiple) {
if (!multiple && acceptedFiles.length > 1) {
// if not in multi mode add any extra accepted files to rejected.
// This will allow end users to easily ignore a multi file drop in "single" mode.
rejectedFiles.push(...acceptedFiles.splice(1))
rejectedFiles.push(...acceptedFiles.splice(0))
}

if (onDrop) {
Expand Down
25 changes: 14 additions & 11 deletions src/index.spec.js
Expand Up @@ -622,20 +622,14 @@ describe('Dropzone', () => {
expect(child).toHaveProp('isDragReject', false)
})

it('should add valid files to rejected files on a multple drop when multiple false', async () => {
const dropzone = mount(<Dropzone accept="image/*" onDrop={dropSpy} multiple={false} />)
await dropzone.simulate('drop', { dataTransfer: { files: images } })
const rejected = dropSpy.firstCall.args[0]
expect(rejected.length).toEqual(1)
})

it('should add invalid files to rejected when multiple is false', async () => {
it('should reject invalid file when multiple is false', async () => {
const dropzone = mount(<Dropzone accept="image/*" onDrop={dropSpy} multiple={false} />)
await dropzone.simulate('drop', {
dataTransfer: { files: images.concat(files) }
dataTransfer: { files }
})
const rejected = dropSpy.firstCall.args[1]
expect(rejected.length).toEqual(2)
const [accepted, rejected] = dropSpy.firstCall.args
expect(rejected.length).toEqual(1)
expect(accepted.length).toEqual(0)
})

it('should allow single files to be dropped if multiple is false', async () => {
Expand All @@ -647,6 +641,15 @@ describe('Dropzone', () => {
expect(rejected.length).toEqual(0)
})

it('should reject multiple files to be dropped if multiple is false', async () => {
const dropzone = mount(<Dropzone accept="image/*" onDrop={dropSpy} multiple={false} />)

await dropzone.simulate('drop', { dataTransfer: { files: images } })
const [accepted, rejected] = dropSpy.firstCall.args
expect(accepted.length).toEqual(0)
expect(rejected.length).toEqual(2)
})

it('should take all dropped files if multiple is true', async () => {
const dropzone = mount(<Dropzone onDrop={dropSpy} multiple />)
await dropzone.simulate('drop', { dataTransfer: { files: images } })
Expand Down

0 comments on commit da18e76

Please sign in to comment.