-
Notifications
You must be signed in to change notification settings - Fork 356
/
file-input.component.ts
173 lines (156 loc) · 4.36 KB
/
file-input.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import {
Component,
Directive,
Input,
Output,
EventEmitter,
ChangeDetectionStrategy,
ViewChild,
ElementRef,
Renderer2,
TemplateRef,
ViewContainerRef,
ChangeDetectorRef,
forwardRef,
OnInit,
OnDestroy,
NgZone,
} from '@angular/core';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { TemplatePortalDirective } from '@angular/cdk/portal';
import { ENTER } from '@angular/cdk/keycodes';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { fromEvent, merge, Subject } from 'rxjs';
import { filter, takeUntil } from 'rxjs/operators';
import {
ICanDisable,
IControlValueAccessor,
mixinControlValueAccessor,
mixinDisabled,
} from '@covalent/core/common';
@Directive({
selector: '[tdFileInputLabel]ng-template',
})
export class TdFileInputLabelDirective extends TemplatePortalDirective {
constructor(
templateRef: TemplateRef<unknown>,
viewContainerRef: ViewContainerRef
) {
super(templateRef, viewContainerRef);
}
}
export class TdFileInputBase {
constructor(public _changeDetectorRef: ChangeDetectorRef) {}
}
export const _TdFileInputMixinBase = mixinControlValueAccessor(
mixinDisabled(TdFileInputBase)
);
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => TdFileInputComponent),
multi: true,
},
],
selector: 'td-file-input',
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
inputs: ['disabled', 'value'],
styleUrls: ['./file-input.component.scss'],
templateUrl: './file-input.component.html',
})
export class TdFileInputComponent
extends _TdFileInputMixinBase
implements OnInit, OnDestroy, IControlValueAccessor, ICanDisable
{
private _multiple = false;
/** The native `<button class="td-file-input"></button>` element */
@ViewChild('fileInputButton', { static: true, read: ElementRef })
_inputButton!: ElementRef<HTMLElement>;
/** The native `<input type="file"> element */
@ViewChild('fileInput', { static: true })
_inputElement!: ElementRef<HTMLInputElement>;
get inputElement(): HTMLInputElement {
return this._inputElement.nativeElement;
}
/**
* color?: 'accent' | 'primary' | 'warn'
* Sets button color. Uses same color palette accepted as [MatButton].
*/
@Input() color?: 'accent' | 'primary' | 'warn';
/**
* multiple?: boolean
* Sets if multiple files can be dropped/selected at once in [TdFileInputComponent].
*/
@Input()
set multiple(multiple: boolean | string) {
this._multiple = coerceBooleanProperty(multiple);
}
get multiple(): boolean {
return this._multiple;
}
/**
* accept?: string
* Sets files accepted when opening the file browser dialog.
* Same as 'accept' attribute in <input/> element.
*/
@Input() accept?: string;
/**
* select?: function
* Event emitted a file is selected
* Emits a [File | FileList] object.
*/
@Output() selectFile: EventEmitter<File | FileList> = new EventEmitter<
File | FileList
>();
private _destroy$ = new Subject<void>();
constructor(
private _ngZone: NgZone,
private _renderer: Renderer2,
_changeDetectorRef: ChangeDetectorRef
) {
super(_changeDetectorRef);
}
ngOnInit(): void {
this._ngZone.runOutsideAngular(() => {
merge(
fromEvent(this._inputButton.nativeElement, 'click'),
fromEvent<KeyboardEvent>(this._inputButton.nativeElement, 'keyup').pipe(
filter((event) => event.keyCode === ENTER)
)
)
.pipe(takeUntil(this._destroy$))
.subscribe(() => this._inputElement.nativeElement.click());
});
}
ngOnDestroy(): void {
this._destroy$.next();
}
/**
* Method executed when a file is selected.
*/
handleSelect(files: File | FileList): void {
this.writeValue(files);
this.selectFile.emit(files);
}
/**
* Used to clear the selected files from the [TdFileInputComponent].
*/
clear(): void {
this.writeValue(undefined);
this._renderer.setProperty(this.inputElement, 'value', '');
}
/** Method executed when the disabled value changes */
override onDisabledChange(v: boolean): void {
if (v) {
this.clear();
}
}
/**
* Sets disable to the component. Implemented as part of ControlValueAccessor.
*/
override setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}
}