@@ -56,38 +56,60 @@ export default class extends Controller {
5656 this . inputTarget . value = '' ;
5757 this . inputTarget . style . display = 'block' ;
5858 this . placeholderTarget . style . display = 'block' ;
59+ this . previewTarget . innerHTML = '' ;
5960 this . previewTarget . style . display = 'none' ;
60- this . previewImageTarget . style . display = 'none' ;
61- this . previewImageTarget . style . backgroundImage = 'none' ;
62- this . previewFilenameTarget . textContent = '' ;
6361
6462 this . dispatchEvent ( 'clear' ) ;
6563 }
6664
6765 onInputChange ( event : any ) {
68- const file = event . target . files [ 0 ] ;
69- if ( typeof file === 'undefined' ) {
66+ const files = event . target . files ;
67+ if ( files . length === 0 ) {
68+ this . previewClearButtonTarget . style . display = 'none' ;
7069 return ;
7170 }
7271
7372 // Hide the input and placeholder
7473 this . inputTarget . style . display = 'none' ;
7574 this . placeholderTarget . style . display = 'none' ;
7675
77- // Show the filename in preview
78- this . previewFilenameTarget . textContent = file . name ;
79- this . previewTarget . style . display = 'flex' ;
76+ // Clear previous previews
77+ this . previewTarget . innerHTML = '' ;
8078
81- // If the file is an image, load it and display it as preview
82- this . previewImageTarget . style . display = 'none' ;
83- if ( file . type && file . type . indexOf ( 'image' ) !== - 1 ) {
84- this . _populateImagePreview ( file ) ;
79+ for ( const file of files ) {
80+ // Create a container for each file preview
81+ const filePreviewContainer = document . createElement ( 'div' ) ;
82+ filePreviewContainer . classList . add ( 'dropzone-preview-file' ) ;
83+
84+ // Create a filename preview element
85+ const fileNameElement = document . createElement ( 'span' ) ;
86+ fileNameElement . textContent = file . name ;
87+ filePreviewContainer . appendChild ( fileNameElement ) ;
88+
89+ // Create an image preview element if the file is an image, else a default svg file icon
90+ if ( file . type ) {
91+ const imagePreviewElement = document . createElement ( 'div' ) ;
92+ if ( file . type . indexOf ( 'image' ) !== - 1 ) {
93+ imagePreviewElement . classList . add ( 'dropzone-preview-image' ) ;
94+ this . _populateImagePreview ( file , imagePreviewElement ) ;
95+ } else {
96+ imagePreviewElement . classList . add ( 'dropzone-preview-svg' ) ;
97+ }
98+
99+ filePreviewContainer . appendChild ( imagePreviewElement ) ;
100+ }
101+
102+ // Append the file preview container to the main preview target
103+ this . previewTarget . appendChild ( filePreviewContainer ) ;
104+
105+ this . dispatchEvent ( 'change' , file ) ;
85106 }
86107
87- this . dispatchEvent ( 'change' , file ) ;
108+ // Show the preview container
109+ this . previewTarget . style . display = 'grid' ;
88110 }
89111
90- _populateImagePreview ( file : Blob ) {
112+ _populateImagePreview ( file : Blob , imagePreviewElement : HTMLElement ) {
91113 if ( typeof FileReader === 'undefined' ) {
92114 // FileReader API not available, skip
93115 return ;
@@ -96,8 +118,8 @@ export default class extends Controller {
96118 const reader = new FileReader ( ) ;
97119
98120 reader . addEventListener ( 'load' , ( event : any ) => {
99- this . previewImageTarget . style . display = 'block' ;
100- this . previewImageTarget . style . backgroundImage = `url(" ${ event . target . result } ")` ;
121+ imagePreviewElement . style . backgroundImage = `url(" ${ event . target . result } ")` ;
122+ imagePreviewElement . style . display = 'block' ;
101123 } ) ;
102124
103125 reader . readAsDataURL ( file ) ;
@@ -107,6 +129,7 @@ export default class extends Controller {
107129 this . inputTarget . style . display = 'block' ;
108130 this . placeholderTarget . style . display = 'block' ;
109131 this . previewTarget . style . display = 'none' ;
132+ this . element . classList . add ( 'dropzone-on-drag-enter' ) ;
110133 }
111134
112135 onDragLeave ( event : any ) {
@@ -117,6 +140,7 @@ export default class extends Controller {
117140 this . inputTarget . style . display = 'none' ;
118141 this . placeholderTarget . style . display = 'none' ;
119142 this . previewTarget . style . display = 'block' ;
143+ this . element . classList . add ( 'dropzone-on-drag-leave' ) ;
120144 }
121145 }
122146
0 commit comments