1- import { Component , Input , OnInit } from "@angular/core" ;
1+ import { Component , Input , OnDestroy , OnInit } from "@angular/core" ;
22import { storiesOf , moduleMetadata } from "@storybook/angular" ;
33
44import { action } from "@storybook/addon-actions" ;
@@ -20,6 +20,7 @@ import { NotificationService } from "../notification/notification.service";
2020import * as fileType from "file-type" ;
2121import { DocumentationModule } from "../documentation-component/documentation.module" ;
2222import { FormBuilder , FormControl , FormGroup , ReactiveFormsModule , Validators } from "@angular/forms" ;
23+ import { Subject , Subscription } from "rxjs" ;
2324
2425@Component ( {
2526 selector : "app-file-uploader" ,
@@ -65,7 +66,7 @@ class FileUploaderStory {
6566 }
6667
6768 onUpload ( ) {
68- this . files . forEach ( fileItem => {
69+ this . files . forEach ( ( fileItem : any ) => {
6970 if ( ! fileItem . uploaded ) {
7071 if ( fileItem . file . size < this . maxSize ) {
7172 fileItem . state = "upload" ;
@@ -177,7 +178,7 @@ class DragAndDropStory {
177178 }
178179
179180 onUpload ( ) {
180- this . files . forEach ( fileItem => {
181+ this . files . forEach ( ( fileItem : any ) => {
181182 if ( ! fileItem . uploaded ) {
182183 if ( fileItem . file . size < this . maxSize ) {
183184 fileItem . state = "upload" ;
@@ -250,14 +251,14 @@ class NgModelFileUploaderStory {
250251 }
251252
252253 onUpload ( ) {
253- this . model . forEach ( fileItem => {
254+ this . model . forEach ( ( fileItem : any ) => {
254255 if ( ! fileItem . uploaded ) {
255256 if ( fileItem . file . size < this . maxSize ) {
256257 fileItem . state = "upload" ;
257258 setTimeout ( ( ) => {
258259 fileItem . state = "complete" ;
259260 fileItem . uploaded = true ;
260- console . log ( fileItem ) ;
261+ console . log ( "Uploaded file:" , fileItem ) ;
261262 } , 1500 ) ;
262263 }
263264
@@ -314,7 +315,7 @@ class NgModelFileUploaderStory {
314315 </form>
315316 `
316317} )
317- class ReactiveFormsStory implements OnInit {
318+ class ReactiveFormsStory implements OnInit , OnDestroy {
318319 static notificationCount = 0 ;
319320 public formGroup : FormGroup ;
320321 public disabledFormGroup : FormGroup ;
@@ -331,6 +332,7 @@ class ReactiveFormsStory implements OnInit {
331332 @Input ( ) disabled = false ;
332333
333334 protected maxSize = 500000 ;
335+ private subscribers : Subscription [ ] = [ ] ;
334336
335337 constructor (
336338 protected notificationService : NotificationService ,
@@ -347,6 +349,15 @@ class ReactiveFormsStory implements OnInit {
347349 files : new FormControl ( new Set < any > ( ) , [ Validators . required ] )
348350 } ) ;
349351 this . disabledFormGroup . disable ( ) ;
352+
353+ const sub = this . formGroup . valueChanges . subscribe ( this . fileChangesObserver ) ;
354+ this . subscribers . push ( sub ) ;
355+ }
356+
357+ ngOnDestroy ( ) {
358+ this . subscribers . forEach ( sub => {
359+ sub . unsubscribe ( ) ;
360+ } ) ;
350361 }
351362
352363 onUpload ( ) {
@@ -357,6 +368,7 @@ class ReactiveFormsStory implements OnInit {
357368 setTimeout ( ( ) => {
358369 fileItem . state = "complete" ;
359370 fileItem . uploaded = true ;
371+ console . log ( "Uploaded file:" , fileItem ) ;
360372 } , 1500 ) ;
361373 }
362374
@@ -371,6 +383,10 @@ class ReactiveFormsStory implements OnInit {
371383 }
372384 } ) ;
373385 }
386+
387+ private fileChangesObserver = ( value : { files : Set < File > } ) => {
388+ console . log ( "files:" , value . files ) ;
389+ }
374390}
375391
376392
0 commit comments