fix(drop_down_list): fix other option with combobox and some cleanup

This commit is contained in:
Paul Chavard 2023-10-27 17:39:02 +02:00
parent 4a698f8264
commit d76123831c
13 changed files with 25 additions and 48 deletions

View file

@ -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

View file

@ -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,

View file

@ -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 {

View file

@ -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,

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -50,3 +50,5 @@ fr:
fetching_data: "La récupération automatique des données pour lINE %{ine} est en cours."
data_fetched: "Des données concernant %{sources} liées à lINE %{ine} ont été reçues depuis le MESRI."
data_fetched_title: "Données obtenues du MESRI"
drop_down_list:
other: Autre

View file

@ -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