refactor(drop_down_list_champ): other option

This commit is contained in:
Paul Chavard 2021-10-26 20:42:17 +02:00 committed by kara Diaby
parent 8154daf847
commit 7ebc439630
6 changed files with 24 additions and 30 deletions

View file

@ -0,0 +1,20 @@
import { delegate, show, hide } from '@utils';
delegate(
'change',
'.editable-champ-drop_down_list select, .editable-champ-drop_down_list input[type="radio"]',
(event) => {
const parent = event.target.closest('.editable-champ-drop_down_list');
const inputGroup = parent?.querySelector('.drop_down_other');
if (inputGroup) {
const input = inputGroup.querySelector('input');
if (event.target.value === '__other__') {
show(inputGroup);
input.disabled = false;
} else {
hide(inputGroup);
input.disabled = true;
}
}
}
);

View file

@ -20,30 +20,3 @@ delegate('click', '.dropdown-button', (event) => {
button.setAttribute('aria-expanded', !buttonExpanded); button.setAttribute('aria-expanded', !buttonExpanded);
} }
}); });
function onChangeSelectWithOther(target) {
const parent = target.closest('.editable-champ-drop_down_list');
const inputGroup = parent?.querySelector('.drop_down_other');
if (inputGroup) {
const input = inputGroup.querySelector('input');
if (target.value === '__other__') {
show(inputGroup);
input.disabled = false;
} else {
hide(inputGroup);
input.disabled = true;
}
}
}
delegate('change', '.editable-champ-drop_down_list select', (event) => {
onChangeSelectWithOther(event.target);
});
delegate(
'click',
'.editable-champ-drop_down_list input[type="radio"]',
(event) => {
onChangeSelectWithOther(event.target);
}
);

View file

@ -27,6 +27,7 @@ import '../new_design/dossiers/auto-upload';
import '../new_design/champs/carte'; import '../new_design/champs/carte';
import '../new_design/champs/linked-drop-down-list'; import '../new_design/champs/linked-drop-down-list';
import '../new_design/champs/repetition'; import '../new_design/champs/repetition';
import '../new_design/champs/drop-down-list';
import { import {
toggleCondidentielExplanation, toggleCondidentielExplanation,

View file

@ -52,7 +52,7 @@ class Champs::DropDownListChamp < Champ
end end
def other_value_present? def other_value_present?
value.present? && drop_down_list_options.exclude?(value) drop_down_other? && value.present? && drop_down_list_options.exclude?(value)
end end
def drop_down_other? def drop_down_other?

View file

@ -1,4 +1,4 @@
.drop_down_other{ class: champ.other_value_present? ? '' : 'hidden' } .drop_down_other{ class: champ.other_value_present? ? '' : 'hidden' }
.notice .notice
%p Veuillez saisir votre autre choix %p Veuillez saisir votre autre choix
= form.text_field :value_other, { maxlength: "200", placeholder: "Saisissez ici", disabled: !champ.other_value_present? } = form.text_field :value_other, maxlength: "200", placeholder: "Saisissez ici", disabled: !champ.other_value_present?

View file

@ -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? } = form.select :value, champ.options, selected: champ.selected, required: champ.mandatory?
- if champ.drop_down_other? - if champ.drop_down_other?
= render partial: "shared/dossiers/drop_down_other_input", locals: { form: form, champ: champ } = render partial: "shared/dossiers/drop_down_other_input", locals: { form: form, champ: champ }