Skip to content

Commit

Permalink
Update FileDropzone API (#678)
Browse files Browse the repository at this point in the history
Adds new arguments `@queue` and `@filter` to FileDropzone.

Includes basic test coverage of these new APIs while deprecated API tests still pass.

Adds typing to FileDropzone component and DataTransferWrapper internal utility. I could have taken this further but I think it's good to do this process incrementally. Honestly there's a lot to untangle in some of these internal utilities (regarding typing support).

Registers/unregisters the FileDropzone as an event listener on the queue in the method from #654.

Also drops interim file-filtering techniques (in onDrop and onSelect). Closes #679
  • Loading branch information
gilest authored Feb 21, 2022
1 parent 8cddd56 commit 840f8d9
Show file tree
Hide file tree
Showing 13 changed files with 674 additions and 736 deletions.
23 changes: 9 additions & 14 deletions docs/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,24 @@ For more details see the [MDN accept attribute reference](https://developer.mozi

## Custom validation

Both components provide callback arguments with which you may implement custom validation of chosen files.
Both `<FileUpload>` and `<FileDropzone>` components accept the `@filter` argument with which you may implement custom validation of chosen files.

`<FileUpload>` may be passed `@onSelect` and `<FileDropzone>` may be passed `@onDrop`.

These are called *after* files have been chosen and *before* `@onFileAdd` is called by the queue.

To implement validation, you may (optionally) return a subset of files from these callbacks to restrict which files are queued for upload.
This are called *after* files have been chosen and *before* `fileAdded` is called by the queue.

See the example below where the same validation callback is used for both selection methods.

Commonly validated file properties are `type`, `name` and `size`. For more details see the [MDN File reference](https://developer.mozilla.org/en-US/docs/Web/API/File).

```hbs
<FileDropzone
@name="photos"
@onDrop={{this.validateFiles}}
as |dropzone queue|
@queue={{queue}}
@filter={{this.validateFile}}
as |dropzone|
>
...
<FileUpload
@name="photos"
@onSelect={{this.validateFiles}}
@onFileAdd={{perform this.uploadImage}}
@queue={{queue}}
@filter={{this.validateFile}}
/>
...
</FileDropzone>
Expand All @@ -50,8 +45,8 @@ const allowedTypes = [

export default class ExampleComponent extends Component {
...
validateFiles(files) {
return files.filter((file) => allowedTypes.includes(file.type));
validateFile(file) {
return allowedTypes.includes(file.type);
}
}
```
7 changes: 1 addition & 6 deletions ember-file-upload/addon/components/file-dropzone.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
dragover=this.didDragOver
drop=this.didDrop
}}
{{update-queue
this.queue
multiple=@multiple
disabled=@disabled
onFileAdd=@onFileAdd
}}
{{this.bindListeners}}
>
{{yield
(hash supported=this.supported active=this.active)
Expand Down
301 changes: 0 additions & 301 deletions ember-file-upload/addon/components/file-dropzone.js

This file was deleted.

Loading

0 comments on commit 840f8d9

Please sign in to comment.