Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NovoFileInputElement): Allow for same file delete/upload, allow file list clear from patchValue(). #1300

Merged
merged 5 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// NG
import { async, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NovoDragulaElement } from '../../../../elements/dragula/Dragula';
import { NovoDragulaService } from '../../../../elements/dragula/DragulaService';
import { DecodeURIPipe } from '../../../../pipes/decode-uri/DecodeURI';
Expand Down Expand Up @@ -225,16 +226,33 @@ describe('Elements: NovoFileInputElement', () => {
// });
// });
//
// describe('Method: writeValue()', () => {
// it('should be defined.', () => {
// expect(component.writeValue).toBeDefined();
// });
//
// it('should change the value', () => {
// component.writeValue(10);
// expect(component.model).toBe(10);
// });
// });
describe('Method: writeValue()', () => {
it('should change the value', () => {
component.writeValue(10);
expect(component.model).toBe(10);
});

it('should empty the file list if a falsey value is programmatically set', () => {
component.writeValue(undefined);
expect(component.files).toEqual([]);
});
});
describe('Method: check(files)', () => {
beforeEach(() => {
component.layoutOptions = {};
});

it('should clear the input value after processing', () => {
component.check({target: { files: [], value: 'test.txt'}});
fixture.detectChanges();
fixture.whenStable().then(() => {
let input = fixture.debugElement.query(By.css('#file'));
let el = input.nativeElement;

expect(el.value).toBe('');
});
});
});
//
// describe('Method: registerOnChange()', () => {
// it('should be defined.', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const NovoFileInputMixins: CanUpdateErrorStateCtor & typeof NovoFileInputBase =
<ng-template #fileInput>
<div class="file-input-group" [class.disabled]="disabled" [class.active]="active">
<input
#inputElement
*ngIf="!layoutOptions.customActions"
type="file"
[name]="name"
Expand All @@ -62,6 +63,7 @@ const NovoFileInputMixins: CanUpdateErrorStateCtor & typeof NovoFileInputBase =
[attr.data-feature-id]="dataFeatureId"
/>
<input
#inputElement
*ngIf="layoutOptions.customActions"
type="file"
[name]="name"
Expand Down Expand Up @@ -152,7 +154,7 @@ const NovoFileInputMixins: CanUpdateErrorStateCtor & typeof NovoFileInputBase =
</div>
</div>
</ng-template>
`,
`,
})
export class NovoFileInputElement extends NovoFileInputMixins implements NovoFieldControl<any>, ControlValueAccessor, OnInit, OnDestroy {
private _uniqueId: string = `novo-file-input-${++nextId}`;
Expand Down Expand Up @@ -183,6 +185,7 @@ export class NovoFileInputElement extends NovoFileInputMixins implements NovoFie
fileOutput: TemplateRef<any>;
@ViewChild('container', { read: ViewContainerRef, static: true })
container: ViewContainerRef;
@ViewChild('inputElement') inputElement: ElementRef<HTMLInputElement>;

@Input()
multiple: boolean = false;
Expand Down Expand Up @@ -385,6 +388,8 @@ export class NovoFileInputElement extends NovoFileInputMixins implements NovoFie

writeValue(model: any): void {
this.model = model;
// If model is cleared programmatically (E.g. form.patchValue({file: undefined})), empty file list.
this.files = !model ? [] : this.files;
}

registerOnChange(fn: Function): void {
Expand All @@ -397,6 +402,8 @@ export class NovoFileInputElement extends NovoFileInputMixins implements NovoFie

check(event) {
this.process(Array.from(event.target.files));
// After processing file upload, clear input element value. Allows for delete and upload of same file.
event.target.value = '';
}

validate(files): boolean {
Expand Down