Skip to content

Commit

Permalink
add hint_id to pj attachment described_by
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSim authored and inseo committed Dec 19, 2024
1 parent e195829 commit 06138b6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
14 changes: 13 additions & 1 deletion app/components/attachment/edit_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,28 @@ def attachment_input_class
"attachment-input-#{attachment_id}"
end

def show_hint?
first? && !persisted?
end

def file_field_options
track_issue_with_missing_validators if missing_validators?

options = {
class: class_names("fr-upload attachment-input": true, "#{attachment_input_class}": true),
direct_upload: @direct_upload,
id: input_id,
aria: { describedby: champ&.describedby_id },
data: {
auto_attach_url:,
turbo_force: :server
}.merge(has_file_size_validator? ? { max_file_size: max_file_size } : {})
}

describedby = []
describedby << champ.describedby_id if champ&.description.present?
describedby << describedby_hint_id if show_hint?
options[:aria] = { describedby: describedby.join(' ') }

options.merge!(has_content_type_validator? ? { accept: accept_content_type } : {})
options[:multiple] = true if as_multiple?
options[:disabled] = true if (@max && @index >= @max) || persisted?
Expand Down Expand Up @@ -169,6 +177,10 @@ def error_message(attachment)

private

def describedby_hint_id
"#{input_id}-pj-hint"
end

def input_id
if champ.present?
# There is always a single input by champ, its id must match the label "for" attribute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
- if error?(attachment)
%p.fr-error-text= error_message(attachment)
- if first? && !persisted?
%p.fr-hint-text.fr-mb-1w
- if show_hint?
%p.fr-hint-text.fr-mb-1w{ id: describedby_hint_id }
- if max_file_size.present?
= t('.max_file_size', max_file_size: number_to_human_size(max_file_size))
- if allowed_formats.present?
Expand Down
10 changes: 10 additions & 0 deletions spec/components/attachment/edit_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
it 'renders allowed formats' do
expect(subject).to have_content(/Formats supportés : jpeg, png/)
end

it 'sets up its aria describedby' do
subject

hint_element = page.find('.fr-hint-text')
expect(hint_element['id']).to eq("#{champ.input_id}-pj-hint")

input_describedby = page.find('input')['aria-describedby'].split
expect(input_describedby).to eq([champ.describedby_id, "#{champ.input_id}-pj-hint"])
end
end

context 'when there is an attachment' do
Expand Down

0 comments on commit 06138b6

Please sign in to comment.