Skip to content

Commit

Permalink
feat(file-upload): add cancel event when cancel button is pressed (c…
Browse files Browse the repository at this point in the history
…loses #499) (#541)

* feat(file-upload): add `cancel` event when cancel button is pressed and files are removed

there is a need for the devs to know when a user clicks on the cancel button so they can handle it properly.

* chore(file-input): update unit tests to be more specific with event tests
  • Loading branch information
emoralesb05 authored and kyleledbetter committed May 7, 2017
1 parent dda9b4b commit 9e3be77
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<h3 class="md-title">Single file selection or drop:</h3>
<p>Select Event: {{fileSelectMsg}}</p>
<p>Upload Event: {{fileUploadMsg}}</p>
<td-file-upload #singleFileUpload (select)="selectEvent($event)" (upload)="uploadEvent($event)" [disabled]="disabled">
<td-file-upload #singleFileUpload (select)="selectEvent($event)" (upload)="uploadEvent($event)" (cancel)="cancelEvent()" [disabled]="disabled">
<md-icon>file_upload</md-icon><span>{{ singleFileUpload.files?.name }}</span>
<ng-template td-file-input-label>
<md-icon>attach_file</md-icon><span>Choose a file...</span>
Expand All @@ -29,7 +29,7 @@ <h3 class="md-title">Single file selection or drop:</h3>
<![CDATA[
<p>Select Event: { {fileSelectMsg} }</p>
<p>Upload Event: { {fileUploadMsg} }</p>
<td-file-upload #singleFileUpload (select)="selectEvent($event)" (upload)="uploadEvent($event)" [disabled]="disabled">
<td-file-upload #singleFileUpload (select)="selectEvent($event)" (upload)="uploadEvent($event)" (cancel)="cancelEvent()" [disabled]="disabled">
<md-icon>file_upload</md-icon><span>{ { singleFileUpload.files?.name } }</span>
<ng-template td-file-input-label>
<md-icon>attach_file</md-icon><span>Choose a file...</span>
Expand All @@ -49,11 +49,16 @@ <h3 class="md-title">Single file selection or drop:</h3>

selectEvent(file: File): void {
this.fileSelectMsg = file.name;
};
}

uploadEvent(file: File): void {
this.fileUploadMsg = file.name;
};
}

cancelEvent(): void {
this.fileSelectMsg = 'No file selected yet.';
this.fileUploadMsg = 'No file uploaded yet.';
}

toggleDisabled(): void {
this.disabled = !this.disabled;
Expand All @@ -75,7 +80,7 @@ <h3 class="md-title">Single file selection or drop:</h3>
<h3 class="md-title">Multiple selection/drop for only .sql files and custom color palette:</h3>
<p>Select Event: {{fileSelectMultipleMsg}}</p>
<p>Upload Event: {{fileUploadMultipleMsg}}</p>
<td-file-upload #fileMultipleUpload (select)="selectMultipleEvent($event)" (upload)="uploadMultipleEvent($event)"
<td-file-upload #fileMultipleUpload (select)="selectMultipleEvent($event)" (upload)="uploadMultipleEvent($event)" (cancel)="cancelMultipleEvent()"
accept=".sql" defaultColor="accent" activeColor="warn" cancelColor="primary" multiple [disabled]="disabled">
<md-icon>file_upload</md-icon>
<span>{{ fileMultipleUpload.files?.name || fileMultipleUpload.files?.length }} <span *ngIf="fileMultipleUpload.files?.length">files selected</span></span>
Expand All @@ -97,7 +102,7 @@ <h3 class="md-title">Multiple selection/drop for only .sql files and custom colo
<![CDATA[
<p>Select Event: { {fileSelectMultipleMsg} }</p>
<p>Upload Event: { {fileUploadMultipleMsg} }</p>
<td-file-upload #fileMultipleUpload (select)="selectMultipleEvent($event)" (upload)="uploadMultipleEvent($event)"
<td-file-upload #fileMultipleUpload (select)="selectMultipleEvent($event)" (upload)="uploadMultipleEvent($event)" (cancel)="cancelMultipleEvent()"
accept=".sql" defaultColor="accent" activeColor="warn" cancelColor="primary" multiple [disabled]="disabled">
<md-icon>file_upload</md-icon>
<span>
Expand Down Expand Up @@ -139,6 +144,11 @@ <h3 class="md-title">Multiple selection/drop for only .sql files and custom colo
this.fileUploadMultipleMsg = files.name;
}
}

cancelMultipleEvent(): void {
this.fileSelectMultipleMsg = 'No file(s) selected yet.';
this.fileUploadMultipleMsg = 'No file(s) uploaded yet.';
}
}
]]>
</td-highlight>
Expand Down
10 changes: 10 additions & 0 deletions src/app/components/components/file-upload/file-upload.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export class FileUploadDemoComponent {
fileUploadMultipleMsg: string = 'No file(s) uploaded yet.';
disabled: boolean = false;

cancelEvent(): void {
this.fileSelectMsg = 'No file selected yet.';
this.fileUploadMsg = 'No file uploaded yet.';
}

selectEvent(file: File): void {
this.fileSelectMsg = file.name;
}
Expand Down Expand Up @@ -52,6 +57,11 @@ export class FileUploadDemoComponent {
}
}

cancelMultipleEvent(): void {
this.fileSelectMultipleMsg = 'No file(s) selected yet.';
this.fileUploadMultipleMsg = 'No file(s) uploaded yet.';
}

toggleDisabled(): void {
this.disabled = !this.disabled;
}
Expand Down
11 changes: 8 additions & 3 deletions src/platform/core/file/file-upload/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Example for usage:

```html
<td-file-upload #fileUpload defaultColor="accent" activeColor="warn" cancelColor="primary" (select)="selectEvent($event)"
(upload)="uploadEvent($event)" accept=".ext,.anotherExt" [disabled]="disabled" multiple>
(upload)="uploadEvent($event)" (cancel)="cancelEvent()" accept=".ext,.anotherExt" [disabled]="disabled" multiple>
<md-icon>file_upload</md-icon><span>{{ fileUpload.files?.name }}</span>
<ng-template td-file-input-label>
<md-icon>attach_file</md-icon><span>Choose a file...</span>
Expand All @@ -27,15 +27,19 @@ export class Demo {
} else {
...
}
};
}

uploadEvent(files: FileList | File): void {
if (files instanceof FileList) {
...
} else {
...
}
};
}

cancelEvent(): void {
...
}
}
```

Expand All @@ -53,6 +57,7 @@ Properties:
| `disabled` | `boolean` | Disables [TdFileUploadComponent] and clears selected/dropped files.
| `upload` | `function($event)` | Event emitted when upload button is clicked. Emits a [File or FileList] object.
| `select` | `function($event)` | Event emitted when a file is selected. Emits a [File or FileList] object.
| `cancel` | `function()` | Event emitted when cancel button is clicked.

## Setup

Expand Down
75 changes: 74 additions & 1 deletion src/platform/core/file/file-upload/file-upload.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,59 @@ describe('Component: FileUpload', () => {
async(inject([], () => {
let fixture: ComponentFixture<any> = TestBed.createComponent(TdFileUploadBasicTestComponent);
let component: TdFileUploadBasicTestComponent = fixture.debugElement.componentInstance;

let eventSpy: jasmine.Spy = spyOn(component, 'selectEvent');

component.multiple = false;
component.disabled = false;
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(fixture.debugElement.query(By.css('td-file-input'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('.td-file-upload'))).toBeFalsy();
expect(eventSpy.calls.count()).toBe(0);
fixture.debugElement.query(By.directive(TdFileUploadComponent)).componentInstance.handleSelect([{}]);
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(eventSpy.calls.count()).toBe(1);
});
});
})));

it('should mimic file selection, upload click and throw (upload) event',
async(inject([], () => {
let fixture: ComponentFixture<any> = TestBed.createComponent(TdFileUploadBasicTestComponent);
let component: TdFileUploadBasicTestComponent = fixture.debugElement.componentInstance;

let eventSpy: jasmine.Spy = spyOn(component, 'uploadEvent');

component.multiple = false;
component.disabled = false;
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(fixture.debugElement.query(By.css('td-file-input'))).toBeTruthy();
expect(fixture.debugElement.query(By.css('.td-file-upload'))).toBeFalsy();
expect(eventSpy.calls.count()).toBe(0);
fixture.debugElement.query(By.directive(TdFileUploadComponent)).componentInstance.handleSelect([{}]);
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.selectFiles).toBeTruthy();
expect(eventSpy.calls.count()).toBe(0);
fixture.debugElement.query(By.css('.td-file-upload')).triggerEventHandler('click', new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(eventSpy.calls.count()).toBe(1);
});
});
});
})));

it('should mimic file selection, cancel click and throw (cancel) event',
async(inject([], () => {
let fixture: ComponentFixture<any> = TestBed.createComponent(TdFileUploadBasicTestComponent);
let component: TdFileUploadBasicTestComponent = fixture.debugElement.componentInstance;

let eventSpy: jasmine.Spy = spyOn(component, 'cancelEvent');

component.multiple = false;
component.disabled = false;
fixture.detectChanges();
Expand All @@ -126,6 +179,12 @@ describe('Component: FileUpload', () => {
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.selectFiles).toBeTruthy();
expect(eventSpy.calls.count()).toBe(0);
fixture.debugElement.query(By.css('.td-file-upload-cancel')).triggerEventHandler('click', new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(eventSpy.calls.count()).toBe(1);
});
});
});
})));
Expand All @@ -135,7 +194,8 @@ describe('Component: FileUpload', () => {
@Component({
selector: 'td-file-upload-basic-test',
template: `
<td-file-upload #fileUpload [multiple]="multiple" [disabled]="disabled" (select)="selectFiles = $event" (upload)="files = $event">
<td-file-upload #fileUpload [multiple]="multiple" [disabled]="disabled" (select)="selectEvent($event)"
(upload)="uploadEvent($event)" (cancel)="cancelEvent()">
<span>{{ fileUpload.files?.name }}</span>
<ng-template td-file-input-label>
<span>Choose a file...</span>
Expand All @@ -149,4 +209,17 @@ class TdFileUploadBasicTestComponent {
disabled: boolean;
files: File | FileList;
selectFiles: File | FileList;

selectEvent(files: File | FileList): void {
this.selectFiles = files;
}

uploadEvent(files: File | FileList): void {
this.files = files;
}

cancelEvent(): void {
this.selectFiles = undefined;
this.files = undefined;
}
}
7 changes: 7 additions & 0 deletions src/platform/core/file/file-upload/file-upload.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ export class TdFileUploadComponent {
*/
@Output('upload') onUpload: EventEmitter<File | FileList> = new EventEmitter<File | FileList>();

/**
* cancel?: function
* Event emitted when cancel button is clicked.
*/
@Output('cancel') onCancel: EventEmitter<void> = new EventEmitter<void>();

constructor(private _changeDetectorRef: ChangeDetectorRef) {

}
Expand Down Expand Up @@ -111,6 +117,7 @@ export class TdFileUploadComponent {
*/
cancel(): void {
this.files = undefined;
this.onCancel.emit(undefined);
this._changeDetectorRef.markForCheck();
}
}

0 comments on commit 9e3be77

Please sign in to comment.