[#11103] Adapt ToggleComponent to be used with form_with helper

This commit is contained in:
Mathieu Magnin 2024-12-03 16:30:16 +01:00
parent 950e3ba859
commit ecb5eee26a
No known key found for this signature in database
GPG key ID: 8DCAFC82D7BA654E
3 changed files with 22 additions and 3 deletions

View file

@ -15,6 +15,13 @@
.fr-tabs__panel.fr-pb-8w.fr-tabs__panel--selected{ id: "tabpanel-standard#{@count}-panel", role: "tabpanel", "aria-labelledby": "tabpanel-standard#{@count}", tabindex: "0" }
= form_with url: download_export_path, namespace: "export#{@count}", data: { turbo_method: :post, turbo: true } do |f|
.fr-pb-2w
= render Dsfr::ToggleComponent.new(form: f,
target: :include_archived_dossiers,
title: "Include les dossiers archivés",
label_side: :right)
= f.hidden_field :statut, value: @statut
%fieldset.fr-fieldset#radio-hint{ "aria-labelledby": "radio-hint-legend" }
%legend.fr-fieldset__legend--regular.fr-fieldset__legend#radio-hint-legend Sélectionner le format de l'export

View file

@ -22,7 +22,19 @@ class Dsfr::ToggleComponent < ApplicationComponent
private
def label_for
return input_id if @form.object.present?
return "#{@form.options[:namespace]}_#{target}" if @form.options[:namespace].present?
target.to_s
end
def input_id
if @form.object.present?
dom_id(@form.object, target)
else
target.to_s
end
end
end

View file

@ -1,8 +1,8 @@
%div{ class: "fr-toggle fr-toggle--label-left #{extra_class_names}" }
= @form.check_box target, class: 'fr-toggle__input', disabled:, data:, id: input_id
= @form.label target,
title,
for: input_id,
title || html_title&.html_safe,
for: label_for,
data: { 'fr-checked-label': toggle_labels[:checked], 'fr-unchecked-label': toggle_labels[:unchecked] },
class: 'fr-toggle__label'