1 import { Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core'
2 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3 import { Notifier } from '@app/core'
4 import { I18n } from '@ngx-translate/i18n-polyfill'
7 selector: 'my-reactive-file',
8 styleUrls: [ './reactive-file.component.scss' ],
9 templateUrl: './reactive-file.component.html',
12 provide: NG_VALUE_ACCESSOR,
13 useExisting: forwardRef(() => ReactiveFileComponent),
18 export class ReactiveFileComponent implements OnInit, ControlValueAccessor {
19 @Input() inputLabel: string
20 @Input() inputName: string
21 @Input() extensions: string[] = []
22 @Input() maxFileSize: number
23 @Input() displayFilename = false
25 @Output() fileChanged = new EventEmitter<Blob>()
27 allowedExtensionsMessage = ''
33 private notifier: Notifier,
38 if (!this.file) return ''
44 this.allowedExtensionsMessage = this.extensions.join(', ')
47 fileChange (event: any) {
48 if (event.target.files && event.target.files.length) {
49 const [ file ] = event.target.files
51 if (file.size > this.maxFileSize) {
52 this.notifier.error(this.i18n('This file is too large.'))
58 this.propagateChange(this.file)
59 this.fileChanged.emit(this.file)
63 propagateChange = (_: any) => { /* empty */ }
65 writeValue (file: any) {
68 if (!this.file) this.fileInputValue = null
71 registerOnChange (fn: (_: any) => void) {
72 this.propagateChange = fn
75 registerOnTouched () {