diff --git a/app/components/editable_champ/drop_down_list_component.rb b/app/components/editable_champ/drop_down_list_component.rb index e61ad7840..1264ff150 100644 --- a/app/components/editable_champ/drop_down_list_component.rb +++ b/app/components/editable_champ/drop_down_list_component.rb @@ -13,6 +13,6 @@ class EditableChamp::DropDownListComponent < EditableChamp::EditableChampBaseCom def contains_long_option? max_length = 100 - @champ.options.any? { _1.size > max_length } + @champ.enabled_non_empty_options.any? { _1.size > max_length } end end diff --git a/app/components/editable_champ/drop_down_list_component/drop_down_list_component.html.haml b/app/components/editable_champ/drop_down_list_component/drop_down_list_component.html.haml index 7e867fa5c..d2138513f 100644 --- a/app/components/editable_champ/drop_down_list_component/drop_down_list_component.html.haml +++ b/app/components/editable_champ/drop_down_list_component/drop_down_list_component.html.haml @@ -17,12 +17,12 @@ .fr-radio-group = @form.radio_button :value, Champs::DropDownListChamp::OTHER, checked: @champ.other?, id: "#{@champ.id}_radio_option_other" %label.fr-label{ for: "#{@champ.id}_radio_option_other" } - Autre + = t('shared.champs.drop_down_list.other') - elsif @champ.render_as_combobox? - = render Dsfr::ComboboxComponent.new form: @form, name: :value, options: @champ.enabled_non_empty_options, selected: @champ.selected, id: @champ.input_id, class: select_class_names, describedby: @champ.describedby_id + = render Dsfr::ComboboxComponent.new form: @form, name: :value, options: @champ.enabled_non_empty_options(other: true), selected: @champ.selected, id: @champ.input_id, class: select_class_names, describedby: @champ.describedby_id - else = @form.select :value, - @champ.options.compact_blank, + @champ.enabled_non_empty_options(other: true), { selected: @champ.selected, include_blank: true }, required: @champ.required?, id: @champ.input_id, diff --git a/app/javascript/shared/combobox.ts b/app/javascript/shared/combobox.ts index ddae8c4c2..9e78127ca 100644 --- a/app/javascript/shared/combobox.ts +++ b/app/javascript/shared/combobox.ts @@ -151,6 +151,8 @@ export class Combobox { this.#open = false; this.#focusedOption = null; this._render(Action.Close); + } else { + this._render(Action.Update); } } @@ -196,7 +198,6 @@ export class Combobox { } close() { - if (!this.#open) return; this.#open = false; this.#focusedOption = null; if (!this.#allowsCustomValue && !this.#selectedOption) { @@ -238,7 +239,7 @@ export class Combobox { return this.#options; } - return matchSorter(this.#options, this.#inputValue, { keys: ['value'] }); + return matchSorter(this.#options, this.#inputValue, { keys: ['label'] }); } private get _focusedOptionIndex(): number { diff --git a/app/models/champ.rb b/app/models/champ.rb index dab924f19..c73a6da61 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -20,7 +20,6 @@ class Champ < ApplicationRecord :drop_down_list_options, :drop_down_other?, :drop_down_list_options?, - :drop_down_list_disabled_options, :drop_down_list_enabled_non_empty_options, :drop_down_secondary_libelle, :drop_down_secondary_description, diff --git a/app/models/champs/drop_down_list_champ.rb b/app/models/champs/drop_down_list_champ.rb index ee9c5b9c3..4d29c7741 100644 --- a/app/models/champs/drop_down_list_champ.rb +++ b/app/models/champs/drop_down_list_champ.rb @@ -19,14 +19,6 @@ class Champs::DropDownListChamp < Champ drop_down_list_options? end - def options - if drop_down_other? - drop_down_list_options + [["Autre", OTHER]] - else - drop_down_list_options - end - end - def html_label? !render_as_radios? end @@ -39,16 +31,12 @@ class Champs::DropDownListChamp < Champ other? ? OTHER : value end - def disabled_options - drop_down_list_disabled_options - end - - def enabled_non_empty_options - drop_down_list_enabled_non_empty_options + def enabled_non_empty_options(other: false) + drop_down_list_enabled_non_empty_options(other:) end def other? - drop_down_other? && (other || (value.present? && drop_down_list_options.exclude?(value))) + drop_down_other? && (other || (value.present? && enabled_non_empty_options.exclude?(value))) end def value=(value) diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index 924f231db..fb4f0e782 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -5,14 +5,6 @@ class Champs::MultipleDropDownListChamp < Champ drop_down_list_options? end - def options - drop_down_list_options - end - - def disabled_options - drop_down_list_disabled_options - end - def enabled_non_empty_options drop_down_list_enabled_non_empty_options end diff --git a/app/models/groupe_instructeur.rb b/app/models/groupe_instructeur.rb index d14434586..034e02dc2 100644 --- a/app/models/groupe_instructeur.rb +++ b/app/models/groupe_instructeur.rb @@ -115,7 +115,7 @@ class GroupeInstructeur < ApplicationRecord when TypeDeChamp.type_champs.fetch(:regions) APIGeoService.regions.map { _1[:code] } when TypeDeChamp.type_champs.fetch(:drop_down_list) - routing_tdc.options_with_drop_down_other + routing_tdc.drop_down_list_enabled_non_empty_options(other: true).map { _1.is_a?(Array) ? _1.last : _1 } end routing_rule.right.value.in?(options) end diff --git a/app/models/logic/champ_value.rb b/app/models/logic/champ_value.rb index bd4d31f00..61edd5fa6 100644 --- a/app/models/logic/champ_value.rb +++ b/app/models/logic/champ_value.rb @@ -109,12 +109,7 @@ class Logic::ChampValue < Logic::Term when MANAGED_TYPE_DE_CHAMP.fetch(:regions) APIGeoService.regions.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } else - opts = tdc.drop_down_list_enabled_non_empty_options.map { |option| [option, option] } - if tdc.drop_down_other? - opts + [["Autre", Champs::DropDownListChamp::OTHER]] - else - opts - end + tdc.drop_down_list_enabled_non_empty_options(other: true).map { _1.is_a?(Array) ? _1 : [_1, _1] } end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index da3835549..96338b3d1 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -534,15 +534,13 @@ class TypeDeChamp < ApplicationRecord drop_down_list_options.filter { |v| (v =~ /^--.*--$/).present? } end - def drop_down_list_enabled_non_empty_options - (drop_down_list_options - drop_down_list_disabled_options).reject(&:empty?) - end + def drop_down_list_enabled_non_empty_options(other: false) + list_options = (drop_down_list_options - drop_down_list_disabled_options).reject(&:empty?) - def options_with_drop_down_other - if drop_down_other? - drop_down_options + [Champs::DropDownListChamp::OTHER] + if other && drop_down_other? + list_options + [[I18n.t('shared.champs.drop_down_list.other'), Champs::DropDownListChamp::OTHER]] else - drop_down_options + list_options end end diff --git a/app/views/dossiers/dossier_vide.pdf.prawn b/app/views/dossiers/dossier_vide.pdf.prawn index a93376705..5dcad614d 100644 --- a/app/views/dossiers/dossier_vide.pdf.prawn +++ b/app/views/dossiers/dossier_vide.pdf.prawn @@ -154,7 +154,7 @@ def render_single_champ(pdf, champ) add_libelle(pdf, champ) add_optionnal_description(pdf, champ) add_explanation(pdf, 'Cochez la mention applicable, une seule valeur possible') - champ.options.compact_blank.each do |option| + champ.enabled_non_empty_options.each do |option| format_with_checkbox(pdf, option) end pdf.text "\n" @@ -162,7 +162,7 @@ def render_single_champ(pdf, champ) add_libelle(pdf, champ) add_optionnal_description(pdf, champ) add_explanation(pdf, 'Cochez la mention applicable, plusieurs valeurs possibles') - champ.options.compact_blank.each do |option| + champ.enabled_non_empty_options.each do |option| format_with_checkbox(pdf, option) end pdf.text "\n" diff --git a/config/locales/shared.en.yml b/config/locales/shared.en.yml index cd59bdb0e..9e348cfa6 100644 --- a/config/locales/shared.en.yml +++ b/config/locales/shared.en.yml @@ -48,3 +48,5 @@ en: fetching_data: "Fetching data for INE %{ine}." data_fetched: "Data concerning %{sources} linked to the INE %{ine} has been received from the MESRI." data_fetched_title: "Data received from the MESRI" + drop_down_list: + other: Other diff --git a/config/locales/shared.fr.yml b/config/locales/shared.fr.yml index 2cd62ffd7..9e4f6381b 100644 --- a/config/locales/shared.fr.yml +++ b/config/locales/shared.fr.yml @@ -50,3 +50,5 @@ fr: fetching_data: "La récupération automatique des données pour l’INE %{ine} est en cours." data_fetched: "Des données concernant %{sources} liées à l’INE %{ine} ont été reçues depuis le MESRI." data_fetched_title: "Données obtenues du MESRI" + drop_down_list: + other: Autre diff --git a/spec/views/shared/dossiers/_edit.html.haml_spec.rb b/spec/views/shared/dossiers/_edit.html.haml_spec.rb index 269389c41..7814425fd 100644 --- a/spec/views/shared/dossiers/_edit.html.haml_spec.rb +++ b/spec/views/shared/dossiers/_edit.html.haml_spec.rb @@ -41,7 +41,6 @@ describe 'shared/dossiers/edit', type: :view do let(:dossier) { create(:dossier) } let(:type_de_champ) { create(:type_de_champ_drop_down_list, mandatory: mandatory, procedure: dossier.procedure) } let(:champ) { create(:champ_drop_down_list, dossier: dossier, type_de_champ: type_de_champ, value: value) } - let(:options) { type_de_champ.drop_down_list_options } let(:enabled_options) { type_de_champ.drop_down_list_enabled_non_empty_options } let(:mandatory) { true } @@ -49,6 +48,7 @@ describe 'shared/dossiers/edit', type: :view do context 'when the list is short' do let(:value) { 'val1' } + it 'renders the list as radio buttons' do expect(subject).to have_selector('input[type=radio]', count: enabled_options.count) end @@ -68,7 +68,7 @@ describe 'shared/dossiers/edit', type: :view do let(:type_de_champ) { create(:type_de_champ_drop_down_list, :long, procedure: dossier.procedure) } it 'renders the list as a dropdown' do - expect(subject).to have_select(type_de_champ.libelle, options: options) + expect(subject).to have_select(type_de_champ.libelle, options: enabled_options + ['']) end end end