diff --git a/app/javascript/controllers/file_input_reset_controller.ts b/app/javascript/controllers/file_input_reset_controller.ts index 0b26c6c84..cbce6ab6b 100644 --- a/app/javascript/controllers/file_input_reset_controller.ts +++ b/app/javascript/controllers/file_input_reset_controller.ts @@ -2,18 +2,36 @@ import { ApplicationController } from './application_controller'; import { hide, show } from '@utils'; export class FileInputResetController extends ApplicationController { - static targets = ['input', 'reset']; + static targets = ['reset']; - declare readonly inputTarget: HTMLInputElement; declare readonly resetTarget: HTMLElement; + connect() { + this.on('change', (event) => { + if (event.target == this.fileInput) { + this.showResetButton(); + } + }); + } + reset(event: Event) { event.preventDefault(); - this.inputTarget.value = ''; + this.fileInput.value = ''; hide(this.resetTarget); } showResetButton() { show(this.resetTarget); } + + private get fileInput() { + const inputs = + this.element.querySelectorAll('input[type="file"]'); + if (inputs.length == 0) { + throw new Error('No file input found'); + } else if (inputs.length > 1) { + throw new Error('Multiple file inputs found'); + } + return inputs[0]; + } } diff --git a/app/views/shared/dossiers/messages/_form.html.haml b/app/views/shared/dossiers/messages/_form.html.haml index eea4ac1eb..b831092ef 100644 --- a/app/views/shared/dossiers/messages/_form.html.haml +++ b/app/views/shared/dossiers/messages/_form.html.haml @@ -9,8 +9,9 @@ = render Dsfr::InputComponent.new(form: f, attribute: :body, input_type: :text_area, opts: { rows: 5, placeholder: placeholder, title: placeholder, class: 'fr-input message-textarea'}) .fr-mt-3w{ data: { controller: "file-input-reset" } } - %button.hidden.fr-btn.fr-btn--tertiary.fr-btn--sm.fr-icon-delete-line.fr-mr-1w{ 'aria-label': t('views.shared.messages.remove_file'), data: { 'file-input-reset-target': 'reset', action: 'file-input-reset#reset' } } - = f.file_field :piece_jointe, direct_upload: true, data: { 'file-input-reset-target': 'input', action: 'change->file-input-reset#showResetButton' } + = render Attachment::EditComponent.new(attached_file: commentaire.piece_jointe) + %button.hidden.fr-btn.fr-btn--tertiary-no-outline.fr-btn--icon-left.fr-icon-delete-line{ data: { 'file-input-reset-target': 'reset', action: 'file-input-reset#reset' } } + = t('views.shared.messages.remove_file') .fr-mt-3w = f.submit t('views.shared.dossiers.messages.form.send_message'), class: 'fr-btn', data: { disable: true } diff --git a/config/locales/views/shared/en.yml b/config/locales/views/shared/en.yml index ddf077d85..749f4bc18 100644 --- a/config/locales/views/shared/en.yml +++ b/config/locales/views/shared/en.yml @@ -21,3 +21,5 @@ en: already_user: "I already have an account" create: 'Create an account' signin: 'Sign in' + messages: + remove_file: 'Remove file' diff --git a/config/locales/views/shared/fr.yml b/config/locales/views/shared/fr.yml index 33ef71b89..a8af2562b 100644 --- a/config/locales/views/shared/fr.yml +++ b/config/locales/views/shared/fr.yml @@ -22,4 +22,4 @@ fr: create: 'Créer un compte' signin: 'Se connecter' messages: - remove_file: 'Supprime le fichier' + remove_file: 'Supprimer le fichier'