From d76123831caff235105f8af04b0ebd3e7a1fba75 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 27 Oct 2023 17:39:02 +0200 Subject: [PATCH] fix(drop_down_list): fix other option with combobox and some cleanup --- .../editable_champ/drop_down_list_component.rb | 2 +- .../drop_down_list_component.html.haml | 6 +++--- app/javascript/shared/combobox.ts | 5 +++-- app/models/champ.rb | 1 - app/models/champs/drop_down_list_champ.rb | 18 +++--------------- .../champs/multiple_drop_down_list_champ.rb | 8 -------- app/models/groupe_instructeur.rb | 2 +- app/models/logic/champ_value.rb | 7 +------ app/models/type_de_champ.rb | 12 +++++------- app/views/dossiers/dossier_vide.pdf.prawn | 4 ++-- config/locales/shared.en.yml | 2 ++ config/locales/shared.fr.yml | 2 ++ .../shared/dossiers/_edit.html.haml_spec.rb | 4 ++-- 13 files changed, 25 insertions(+), 48 deletions(-) diff --git a/app/components/editable_champ/drop_down_list_component.rb b/app/components/editable_champ/drop_down_list_component.rb index e61ad7840ee..1264ff150dc 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 @@ def dsfr_champ_container 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 7e867fa5c87..d2138513f71 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 ddae8c4c2d6..9e78127ca23 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 dab924f1952..c73a6da6121 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 ee9c5b9c3f6..4d29c77413a 100644 --- a/app/models/champs/drop_down_list_champ.rb +++ b/app/models/champs/drop_down_list_champ.rb @@ -19,14 +19,6 @@ def options? 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 @@ def selected 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 924f231dbed..fb4f0e782e4 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 @@ def options? 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 d1443458669..034e02dc219 100644 --- a/app/models/groupe_instructeur.rb +++ b/app/models/groupe_instructeur.rb @@ -115,7 +115,7 @@ def routing_rule_matches_tdc? 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 bd4d31f0020..61edd5fa603 100644 --- a/app/models/logic/champ_value.rb +++ b/app/models/logic/champ_value.rb @@ -109,12 +109,7 @@ def options(type_de_champs) 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 da383554980..96338b3d19e 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -534,15 +534,13 @@ def drop_down_list_disabled_options 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 a933767059d..5dcad614d9d 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 cd59bdb0ef8..9e348cfa672 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 2cd62ffd71b..9e4f6381bcb 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 269389c4158..7814425fd0f 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 @@ 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 @@ 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 @@ 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