Skip to content

Commit

Permalink
Correct focus for file upload in error summary
Browse files Browse the repository at this point in the history
Adjustments made to the the file upload so that in the event of an error
the correct part of the page can be linked to.
  • Loading branch information
patrickpatrickpatrick committed Jan 21, 2025
1 parent 8251405 commit 94a9f5c
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export class FileUpload extends ConfigurableComponent {
/** @private */
i18n

/** @private */
id

/**
* @param {Element | null} $root - File input element
* @param {FileUploadConfig} [config] - File Upload config
Expand Down Expand Up @@ -59,7 +62,11 @@ export class FileUpload extends ConfigurableComponent {
})

this.$label = this.findLabel()
this.$label.setAttribute('id', `${this.$root.getAttribute('id')}-label`)

// we need to copy the 'id' of the root element
// to the new button replacement element
// so that focus will work in the error summary
this.$root.id = `${this.id}-input`

// Wrapping element. This defines the boundaries of our drag and drop area.
const $wrapper = document.createElement('div')
Expand All @@ -69,6 +76,7 @@ export class FileUpload extends ConfigurableComponent {
const $button = document.createElement('button')
$button.classList.add('govuk-file-upload__button')
$button.type = 'button'
$button.id = this.id

const buttonSpan = document.createElement('span')
buttonSpan.className =
Expand Down Expand Up @@ -268,7 +276,7 @@ export class FileUpload extends ConfigurableComponent {
* When the button is clicked, emulate clicking the actual, hidden file input
*/
onClick() {
this.$label.click()
this.$root.click()
}

/**
Expand Down

0 comments on commit 94a9f5c

Please sign in to comment.