11/**
22 * Parse XML response from AWS S3 and return the file key.
33 *
4- * @param {string } responseText - XML response form AWS S3.
4+ * @param {string } responseText - XML response from AWS S3.
55 * @return {string } - Key from response.
66 */
77export function getKeyFromResponse ( responseText ) {
@@ -16,6 +16,7 @@ export function getKeyFromResponse(responseText) {
1616 * @extends HTMLElement
1717 */
1818export class S3FileInput extends globalThis . HTMLElement {
19+ static passThroughAttributes = [ "accept" , "required" , "multiple" , "class" , "style" ]
1920 constructor ( ) {
2021 super ( )
2122 this . keys = [ ]
@@ -34,21 +35,21 @@ export class S3FileInput extends globalThis.HTMLElement {
3435
3536 connectedCallback ( ) {
3637 // Create a hidden file input for the file picker functionality
37- this . _hiddenInput = document . createElement ( "input" )
38- this . _hiddenInput . type = "file"
38+ this . _fileInput = document . createElement ( "input" )
39+ this . _fileInput . type = "file"
3940
4041 // Sync attributes to hidden input
4142 this . _syncAttributesToHiddenInput ( )
4243
4344 // Listen for file selection on hidden input
44- this . _hiddenInput . addEventListener ( "change" , ( ) => {
45- this . _files = this . _hiddenInput . files
45+ this . _fileInput . addEventListener ( "change" , ( ) => {
46+ this . _files = this . _fileInput . files
4647 this . dispatchEvent ( new Event ( "change" , { bubbles : true } ) )
4748 this . changeHandler ( )
4849 } )
4950
5051 // Append elements
51- this . appendChild ( this . _hiddenInput )
52+ this . appendChild ( this . _fileInput )
5253
5354 // Setup form event listeners
5455 this . form ?. addEventListener ( "formdata" , this . fromDataHandler . bind ( this ) )
@@ -62,18 +63,17 @@ export class S3FileInput extends globalThis.HTMLElement {
6263 * Sync attributes from custom element to hidden input.
6364 */
6465 _syncAttributesToHiddenInput ( ) {
65- if ( ! this . _hiddenInput ) return
66+ if ( ! this . _fileInput ) return
6667
67- const attrsToSync = [ "accept" , "required" , "multiple" ]
68- attrsToSync . forEach ( ( attr ) => {
68+ S3FileInput . passThroughAttributes . forEach ( ( attr ) => {
6969 if ( this . hasAttribute ( attr ) ) {
70- this . _hiddenInput . setAttribute ( attr , this . getAttribute ( attr ) )
70+ this . _fileInput . setAttribute ( attr , this . getAttribute ( attr ) )
7171 } else {
72- this . _hiddenInput . removeAttribute ( attr )
72+ this . _fileInput . removeAttribute ( attr )
7373 }
7474 } )
7575
76- this . _hiddenInput . disabled = this . hasAttribute ( "disabled" )
76+ this . _fileInput . disabled = this . hasAttribute ( "disabled" )
7777 }
7878
7979 /**
@@ -106,8 +106,8 @@ export class S3FileInput extends globalThis.HTMLElement {
106106 // Setting value on file inputs is restricted for security
107107 if ( val === "" || val === null ) {
108108 this . _files = [ ]
109- if ( this . _hiddenInput ) {
110- this . _hiddenInput . value = ""
109+ if ( this . _fileInput ) {
110+ this . _fileInput . value = ""
111111 }
112112 }
113113 }
@@ -190,8 +190,8 @@ export class S3FileInput extends globalThis.HTMLElement {
190190 }
191191
192192 click ( ) {
193- if ( this . _hiddenInput ) {
194- this . _hiddenInput . click ( )
193+ if ( this . _fileInput ) {
194+ this . _fileInput . click ( )
195195 }
196196 }
197197
@@ -216,15 +216,15 @@ export class S3FileInput extends globalThis.HTMLElement {
216216 */
217217 async submitHandler ( event ) {
218218 event . preventDefault ( )
219- this . form . dispatchEvent ( new window . CustomEvent ( "upload" ) )
220- await Promise . all ( this . form . pendingRquests )
221- this . form . requestSubmit ( event . submitter )
219+ this . form ? .dispatchEvent ( new window . CustomEvent ( "upload" ) )
220+ await Promise . all ( this . form ? .pendingRquests )
221+ this . form ? .requestSubmit ( event . submitter )
222222 }
223223
224224 uploadHandler ( ) {
225225 if ( this . files . length && ! this . upload ) {
226226 this . upload = this . uploadFiles ( )
227- this . form . pendingRquests = this . form . pendingRquests || [ ]
227+ this . form . pendingRquests = this . form ? .pendingRquests || [ ]
228228 this . form . pendingRquests . push ( this . upload )
229229 }
230230 }
@@ -289,7 +289,7 @@ export class S3FileInput extends globalThis.HTMLElement {
289289 * Called when observed attributes change.
290290 */
291291 static get observedAttributes ( ) {
292- return [ "name" , "accept" , "required" , "multiple" , "disabled" , " id"]
292+ return this . passThroughAttributes . concat ( [ "name" , "id" ] )
293293 }
294294
295295 attributeChangedCallback ( name , oldValue , newValue ) {
0 commit comments