@@ -56,48 +56,75 @@ 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 = '' ;
61+ this . element . classList . remove ( 'dropzone-on-drag-enter' ) ;
6362
6463 this . dispatchEvent ( 'clear' ) ;
6564 }
6665
6766 onInputChange ( event : any ) {
68- const file = event . target . files [ 0 ] ;
69- if ( typeof file === 'undefined' ) {
67+ const files = event . target . files ;
68+ if ( files . length === 0 ) {
69+ this . previewClearButtonTarget . style . display = 'none' ;
7070 return ;
7171 }
7272
7373 // Hide the input and placeholder
7474 this . inputTarget . style . display = 'none' ;
7575 this . placeholderTarget . style . display = 'none' ;
7676
77- // Show the filename in preview
78- this . previewFilenameTarget . textContent = file . name ;
79- this . previewTarget . style . display = 'flex' ;
77+ // Clear previous previews
78+ this . previewTarget . innerHTML = '' ;
8079
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 ) ;
80+ for ( const file of files ) {
81+ // Create a container for each file preview
82+ const filePreviewContainer = document . createElement ( 'div' ) ;
83+ filePreviewContainer . classList . add ( 'dropzone-preview-file' ) ;
84+
85+ // Create a filename preview element
86+ const fileNameElement = document . createElement ( 'span' ) ;
87+ fileNameElement . textContent = file . name ;
88+ filePreviewContainer . appendChild ( fileNameElement ) ;
89+
90+ // Create an image preview element if the file is an image, else a default svg file icon
91+ if ( file . type ) {
92+ const imagePreviewElement = document . createElement ( 'div' ) ;
93+
94+ if ( file . type . indexOf ( 'image' ) !== - 1 ) {
95+ imagePreviewElement . classList . add ( 'dropzone-preview-image' ) ;
96+ this . _populateImagePreview ( file , imagePreviewElement ) ;
97+ } else {
98+ const noPreviewSvg =
99+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="currentColor" d="M14 11a3 3 0 0 1-3-3V4H7a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2h9a2 2 0 0 0 2-2v-8zm-2-3a2 2 0 0 0 2 2h3.59L12 4.41zM7 3h5l7 7v9a3 3 0 0 1-3 3H7a3 3 0 0 1-3-3V6a3 3 0 0 1 3-3"/></svg>' ;
100+ imagePreviewElement . innerHTML = noPreviewSvg ;
101+
102+ imagePreviewElement . classList . add ( 'dropzone-no-preview' ) ;
103+ }
104+
105+ filePreviewContainer . appendChild ( imagePreviewElement ) ;
106+ }
107+
108+ // Append the file preview container to the main preview target
109+ this . previewTarget . appendChild ( filePreviewContainer ) ;
110+
111+ this . dispatchEvent ( 'change' , file ) ;
85112 }
86113
87- this . dispatchEvent ( 'change' , file ) ;
114+ // Show the preview container
115+ this . previewTarget . style . display = 'grid' ;
88116 }
89117
90- _populateImagePreview ( file : Blob ) {
118+ _populateImagePreview ( file : Blob , imagePreviewElement : HTMLElement ) {
91119 if ( typeof FileReader === 'undefined' ) {
92120 // FileReader API not available, skip
93121 return ;
94122 }
95123
96124 const reader = new FileReader ( ) ;
97-
98125 reader . addEventListener ( 'load' , ( event : any ) => {
99- this . previewImageTarget . style . display = 'block' ;
100- this . previewImageTarget . style . backgroundImage = `url(" ${ event . target . result } ")` ;
126+ imagePreviewElement . style . backgroundImage = `url(" ${ event . target . result } ")` ;
127+ imagePreviewElement . style . display = 'block' ;
101128 } ) ;
102129
103130 reader . readAsDataURL ( file ) ;
@@ -107,6 +134,8 @@ export default class extends Controller {
107134 this . inputTarget . style . display = 'block' ;
108135 this . placeholderTarget . style . display = 'block' ;
109136 this . previewTarget . style . display = 'none' ;
137+ this . element . classList . add ( 'dropzone-on-drag-enter' ) ;
138+ this . element . classList . remove ( 'dropzone-on-drag-leave' ) ;
110139 }
111140
112141 onDragLeave ( event : any ) {
@@ -117,6 +146,8 @@ export default class extends Controller {
117146 this . inputTarget . style . display = 'none' ;
118147 this . placeholderTarget . style . display = 'none' ;
119148 this . previewTarget . style . display = 'block' ;
149+ this . element . classList . remove ( 'dropzone-on-drag-enter' ) ;
150+ this . element . classList . add ( 'dropzone-on-drag-leave' ) ;
120151 }
121152 }
122153
0 commit comments