Skip to content

Commit 8b2c2fe

Browse files
authored
Merge branch 'master' into label-issue
2 parents 01b48ab + 1991bc5 commit 8b2c2fe

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

src/file-uploader/file-uploader.component.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,26 @@ describe("FileUploader", () => {
120120

121121
expect(element.nativeElement.querySelector(".bx--file__state-container .bx--file--invalid")).toBeTruthy();
122122
});
123+
124+
it("should correctly update this.files when onFilesAdded is called", () => {
125+
fixture = TestBed.createComponent(FileUploader);
126+
wrapper = fixture.componentInstance;
127+
fixture.detectChanges();
128+
129+
const fileAlreadyAdded = new File([""], "test-filename-added", {type: "text/html"});
130+
const currentFiles = new Set().add(wrapper.createFileItem(fileAlreadyAdded));
131+
wrapper.files = currentFiles;
132+
fixture.detectChanges();
133+
expect(wrapper.value).toBe(currentFiles);
134+
135+
const dataTransfer = new DataTransfer();
136+
const fileToAdd = new File(["test file"], "test-filename", {type: "text/html"});
137+
dataTransfer.items.add(fileToAdd);
138+
wrapper.fileInput.nativeElement.files = dataTransfer.files;
139+
fixture.detectChanges();
140+
wrapper.onFilesAdded();
141+
const filesArray: FileItem[] = Array.from(wrapper.files);
142+
expect(!!filesArray.find((fileItem: FileItem) => fileItem.file.name === fileToAdd.name)).toBe(true);
143+
});
123144
});
124145

src/file-uploader/file-uploader.component.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
EventEmitter,
77
TemplateRef
88
} from "@angular/core";
9-
import { NG_VALUE_ACCESSOR } from "@angular/forms";
9+
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
1010

1111
import { I18n } from "carbon-components-angular/i18n";
1212
import { FileItem } from "./file-item.interface";
@@ -90,7 +90,7 @@ const noop = () => { };
9090
}
9191
]
9292
})
93-
export class FileUploader {
93+
export class FileUploader implements ControlValueAccessor {
9494
/**
9595
* Counter used to create unique ids for file-uploader components
9696
*/
@@ -212,16 +212,17 @@ export class FileUploader {
212212
}
213213

214214
onFilesAdded() {
215+
const newFiles = new Set<FileItem>(this.files);
215216
if (!this.multiple) {
216-
this.files.clear();
217+
newFiles.clear();
217218
}
218219
for (let file of this.fileList) {
219220
const fileItem = this.createFileItem(file);
220-
this.files.add(fileItem);
221+
newFiles.add(fileItem);
221222
}
222223

223-
this.filesChange.emit(this.files);
224-
this.value = this.files;
224+
this.value = newFiles;
225+
this.filesChange.emit(newFiles);
225226
}
226227

227228
onDragOver(event) {
@@ -287,4 +288,15 @@ export class FileUploader {
287288
registerOnChange(fn: any) {
288289
this.onChangeCallback = fn;
289290
}
291+
292+
/**
293+
* `ControlValueAccessor` method to programmatically disable the checkbox.
294+
*
295+
* ex: `this.formGroup.get("myFileUploader").disable();`
296+
*
297+
* @param isDisabled `true` to disable the file uploader
298+
*/
299+
setDisabledState(isDisabled: boolean) {
300+
this.disabled = isDisabled;
301+
}
290302
}

src/file-uploader/file-uploader.stories.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,29 @@ class NgModelFileUploaderStory {
295295
Upload
296296
</button>
297297
</form>
298+
<form [formGroup]="disabledFormGroup" (ngSubmit)="onUpload()">
299+
<ibm-file-uploader
300+
[title]="title"
301+
[description]="description"
302+
[buttonText]="buttonText"
303+
[buttonType]="buttonType"
304+
[accept]="accept"
305+
[multiple]="multiple"
306+
[skeleton]="skeleton"
307+
[size]="size"
308+
formControlName="files">
309+
</ibm-file-uploader>
310+
<div [id]="notificationId" style="width: 300px; margin-top: 20px"></div>
311+
<button ibmButton *ngIf="disabledFormGroup.get('files').value && disabledFormGroup.get('files').value.size > 0" type="submit">
312+
Upload
313+
</button>
314+
</form>
298315
`
299316
})
300317
class ReactiveFormsStory implements OnInit {
301318
static notificationCount = 0;
302319
public formGroup: FormGroup;
320+
public disabledFormGroup: FormGroup;
303321

304322
@Input() notificationId = `notification-${FileUploaderStory.notificationCount}`;
305323
@Input() title;
@@ -325,6 +343,10 @@ class ReactiveFormsStory implements OnInit {
325343
this.formGroup = this.formBuilder.group({
326344
files: new FormControl(new Set<any>(), [Validators.required])
327345
});
346+
this.disabledFormGroup = this.formBuilder.group({
347+
files: new FormControl(new Set<any>(), [Validators.required])
348+
});
349+
this.disabledFormGroup.disable();
328350
}
329351

330352
onUpload() {

0 commit comments

Comments
 (0)