Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/components/editable_champ/drop_down_list_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/shared/combobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export class Combobox {
this.#open = false;
this.#focusedOption = null;
this._render(Action.Close);
} else {
this._render(Action.Update);
}
}

Expand Down Expand Up @@ -196,7 +198,6 @@ export class Combobox {
}

close() {
if (!this.#open) return;
this.#open = false;
this.#focusedOption = null;
if (!this.#allowsCustomValue && !this.#selectedOption) {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion app/models/champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 3 additions & 15 deletions app/models/champs/drop_down_list_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions app/models/champs/multiple_drop_down_list_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/groupe_instructeur.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions app/models/logic/champ_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 5 additions & 7 deletions app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions app/views/dossiers/dossier_vide.pdf.prawn
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ 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"
when 'Champs::MultipleDropDownListChamp'
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"
Expand Down
2 changes: 2 additions & 0 deletions config/locales/shared.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions config/locales/shared.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions spec/views/shared/dossiers/_edit.html.haml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
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 }

before { dossier.champs_public << champ }

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
Expand All @@ -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
Expand Down