fix(linked_dropdown/dropdown): for dropdown_list, drop blank option because rails add it when the field is required. kind of same behaviour for linked_dropdown_list [but requires to manage it on the front too]
This commit is contained in:
parent
7ad47f3eae
commit
4c0aac8e0c
6 changed files with 30 additions and 10 deletions
|
@ -13,15 +13,19 @@ delegate('change', PRIMARY_SELECTOR, (evt) => {
|
|||
|
||||
selectOptions(secondary, options[primary.value]);
|
||||
});
|
||||
|
||||
function makeOption(option) {
|
||||
let element = document.createElement('option');
|
||||
element.textContent = option;
|
||||
element.value = option;
|
||||
return element;
|
||||
}
|
||||
function selectOptions(selectElement, options) {
|
||||
selectElement.innerHTML = '';
|
||||
|
||||
if (selectElement.required) {
|
||||
selectElement.appendChild(makeOption(''));
|
||||
}
|
||||
for (let option of options) {
|
||||
let element = document.createElement('option');
|
||||
element.textContent = option;
|
||||
element.value = option;
|
||||
selectElement.appendChild(element);
|
||||
selectElement.appendChild(makeOption(option));
|
||||
}
|
||||
|
||||
selectElement.selectedIndex = 0;
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
class Champs::DropDownListChamp < Champ
|
||||
THRESHOLD_NB_OPTIONS_AS_RADIO = 5
|
||||
OTHER = '__other__'
|
||||
delegate :options_without_empty_value_when_mandatory, to: :type_de_champ
|
||||
|
||||
def render_as_radios?
|
||||
enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_RADIO
|
||||
|
|
|
@ -297,6 +297,14 @@ class TypeDeChamp < ApplicationRecord
|
|||
self.drop_down_options = parse_drop_down_list_value(value)
|
||||
end
|
||||
|
||||
# historicaly we added a blank ("") option by default to avoid wrong selection
|
||||
# see self.parse_drop_down_list_value
|
||||
# then rails decided to add this blank ("") option when the select is required
|
||||
# so we revert this change
|
||||
def options_without_empty_value_when_mandatory(options)
|
||||
mandatory? ? options.reject(&:blank?) : options
|
||||
end
|
||||
|
||||
def drop_down_list_options?
|
||||
drop_down_list_options.any?
|
||||
end
|
||||
|
@ -354,10 +362,11 @@ class TypeDeChamp < ApplicationRecord
|
|||
|
||||
private
|
||||
|
||||
DEFAULT_EMPTY = ['']
|
||||
def parse_drop_down_list_value(value)
|
||||
value = value ? value.split("\r\n").map(&:strip).join("\r\n") : ''
|
||||
result = value.split(/[\r\n]|[\r]|[\n]|[\n\r]/).reject(&:empty?)
|
||||
result.blank? ? [] : [''] + result
|
||||
result.blank? ? [] : DEFAULT_EMPTY + result
|
||||
end
|
||||
|
||||
def populate_stable_id
|
||||
|
|
|
@ -30,10 +30,15 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
|
|||
tags
|
||||
end
|
||||
|
||||
def add_blank_option_when_not_mandatory(options)
|
||||
return options if mandatory
|
||||
options.unshift('')
|
||||
end
|
||||
|
||||
def primary_options
|
||||
primary_options = unpack_options.map(&:first)
|
||||
if primary_options.present?
|
||||
primary_options.unshift('')
|
||||
primary_options = add_blank_option_when_not_mandatory(primary_options)
|
||||
end
|
||||
primary_options
|
||||
end
|
||||
|
@ -53,7 +58,7 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
|
|||
chunked = options.slice_before(PRIMARY_PATTERN)
|
||||
chunked.map do |chunk|
|
||||
primary, *secondary = chunk
|
||||
secondary.unshift('')
|
||||
secondary = add_blank_option_when_not_mandatory(secondary)
|
||||
[PRIMARY_PATTERN.match(primary)&.[](1), secondary]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
= form.radio_button :value, Champs::DropDownListChamp::OTHER, checked: champ.other_value_present?
|
||||
Autre
|
||||
- else
|
||||
= form.select :value, champ.options, { selected: champ.selected}, required: champ.mandatory?, id: champ.input_id, aria: { describedby: champ.describedby_id }
|
||||
= form.select :value, champ.options_without_empty_value_when_mandatory(champ.options), { selected: champ.selected }, required: champ.mandatory?, id: champ.input_id, aria: { describedby: champ.describedby_id }
|
||||
|
||||
- if champ.drop_down_other?
|
||||
= render partial: "shared/dossiers/editable_champs/drop_down_other_input", locals: { form: form, champ: champ }
|
||||
|
|
|
@ -64,6 +64,7 @@ describe 'linked dropdown lists' do
|
|||
expect(page).to have_select("Valeur secondaire dépendant de la première", options: ['', 'Secondary 1.1', 'Secondary 1.2'])
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log_in(email, password, procedure)
|
||||
|
|
Loading…
Reference in a new issue