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:
Martin 2022-07-08 16:44:21 +02:00 committed by mfo
parent 7ad47f3eae
commit 4c0aac8e0c
6 changed files with 30 additions and 10 deletions

View file

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