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]);
|
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) {
|
function selectOptions(selectElement, options) {
|
||||||
selectElement.innerHTML = '';
|
selectElement.innerHTML = '';
|
||||||
|
if (selectElement.required) {
|
||||||
|
selectElement.appendChild(makeOption(''));
|
||||||
|
}
|
||||||
for (let option of options) {
|
for (let option of options) {
|
||||||
let element = document.createElement('option');
|
selectElement.appendChild(makeOption(option));
|
||||||
element.textContent = option;
|
|
||||||
element.value = option;
|
|
||||||
selectElement.appendChild(element);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
selectElement.selectedIndex = 0;
|
selectElement.selectedIndex = 0;
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
class Champs::DropDownListChamp < Champ
|
class Champs::DropDownListChamp < Champ
|
||||||
THRESHOLD_NB_OPTIONS_AS_RADIO = 5
|
THRESHOLD_NB_OPTIONS_AS_RADIO = 5
|
||||||
OTHER = '__other__'
|
OTHER = '__other__'
|
||||||
|
delegate :options_without_empty_value_when_mandatory, to: :type_de_champ
|
||||||
|
|
||||||
def render_as_radios?
|
def render_as_radios?
|
||||||
enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_RADIO
|
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)
|
self.drop_down_options = parse_drop_down_list_value(value)
|
||||||
end
|
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?
|
def drop_down_list_options?
|
||||||
drop_down_list_options.any?
|
drop_down_list_options.any?
|
||||||
end
|
end
|
||||||
|
@ -354,10 +362,11 @@ class TypeDeChamp < ApplicationRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
DEFAULT_EMPTY = ['']
|
||||||
def parse_drop_down_list_value(value)
|
def parse_drop_down_list_value(value)
|
||||||
value = value ? value.split("\r\n").map(&:strip).join("\r\n") : ''
|
value = value ? value.split("\r\n").map(&:strip).join("\r\n") : ''
|
||||||
result = value.split(/[\r\n]|[\r]|[\n]|[\n\r]/).reject(&:empty?)
|
result = value.split(/[\r\n]|[\r]|[\n]|[\n\r]/).reject(&:empty?)
|
||||||
result.blank? ? [] : [''] + result
|
result.blank? ? [] : DEFAULT_EMPTY + result
|
||||||
end
|
end
|
||||||
|
|
||||||
def populate_stable_id
|
def populate_stable_id
|
||||||
|
|
|
@ -30,10 +30,15 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
|
||||||
tags
|
tags
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_blank_option_when_not_mandatory(options)
|
||||||
|
return options if mandatory
|
||||||
|
options.unshift('')
|
||||||
|
end
|
||||||
|
|
||||||
def primary_options
|
def primary_options
|
||||||
primary_options = unpack_options.map(&:first)
|
primary_options = unpack_options.map(&:first)
|
||||||
if primary_options.present?
|
if primary_options.present?
|
||||||
primary_options.unshift('')
|
primary_options = add_blank_option_when_not_mandatory(primary_options)
|
||||||
end
|
end
|
||||||
primary_options
|
primary_options
|
||||||
end
|
end
|
||||||
|
@ -53,7 +58,7 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
|
||||||
chunked = options.slice_before(PRIMARY_PATTERN)
|
chunked = options.slice_before(PRIMARY_PATTERN)
|
||||||
chunked.map do |chunk|
|
chunked.map do |chunk|
|
||||||
primary, *secondary = chunk
|
primary, *secondary = chunk
|
||||||
secondary.unshift('')
|
secondary = add_blank_option_when_not_mandatory(secondary)
|
||||||
[PRIMARY_PATTERN.match(primary)&.[](1), secondary]
|
[PRIMARY_PATTERN.match(primary)&.[](1), secondary]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
= form.radio_button :value, Champs::DropDownListChamp::OTHER, checked: champ.other_value_present?
|
= form.radio_button :value, Champs::DropDownListChamp::OTHER, checked: champ.other_value_present?
|
||||||
Autre
|
Autre
|
||||||
- else
|
- 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?
|
- if champ.drop_down_other?
|
||||||
= render partial: "shared/dossiers/editable_champs/drop_down_other_input", locals: { form: form, champ: champ }
|
= 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'])
|
expect(page).to have_select("Valeur secondaire dépendant de la première", options: ['', 'Secondary 1.1', 'Secondary 1.2'])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def log_in(email, password, procedure)
|
def log_in(email, password, procedure)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue