[ADD] survey_dropdown_simple_choice
Display "Multiple choice: only one answer" (simple_choice) survey questions as a dropdown list instead of radio buttons, per question. The select is mirrored on hidden radios so that the native survey form pipeline (validation, conditional questions, "Other" comment, AJAX submit, correct answers display) keeps working. Native radio rendering is kept when suggested answers contain images and in readonly mode. Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import SurveyFormWidget from "@survey/js/survey_form";
|
||||
|
||||
// Keep native survey answer pipeline intact (validation, conditional
|
||||
// questions, AJAX submit): the dropdown change is mapped onto the hidden
|
||||
// mirror radios that survey_form.js knows about.
|
||||
SurveyFormWidget.include({
|
||||
events: Object.assign({}, SurveyFormWidget.prototype.events, {
|
||||
'change select.o_survey_dropdown_choice': '_onDropdownChoiceChange',
|
||||
}),
|
||||
|
||||
/**
|
||||
* Arrow left/right on a focused dropdown must not navigate between pages.
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
_onKeyDown: function (event) {
|
||||
if (event.target.matches?.('select.o_survey_dropdown_choice')
|
||||
&& ['ArrowLeft', 'ArrowRight'].includes(event.key)) {
|
||||
return;
|
||||
}
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
|
||||
/**
|
||||
* Correct answers are colored on the mirror radios: show them instead of
|
||||
* the dropdown so the participant sees the correction.
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
_showQuestionAnswer: function (correctAnswers, questionId) {
|
||||
this._super.apply(this, arguments);
|
||||
const questionWrapper = this.el.querySelector(`.js_question-wrapper[id="${questionId}"]`);
|
||||
const select = questionWrapper && questionWrapper.querySelector('select.o_survey_dropdown_choice');
|
||||
if (select) {
|
||||
select.disabled = true;
|
||||
questionWrapper.querySelector('.o_survey_dropdown_select').classList.add('d-none');
|
||||
questionWrapper.querySelector('.o_survey_dropdown_mirror').classList.remove('d-none');
|
||||
}
|
||||
},
|
||||
|
||||
_onDropdownChoiceChange: function (ev) {
|
||||
const select = ev.currentTarget;
|
||||
const group = select.closest('.o_survey_form_choice');
|
||||
if (!group || this.readonly) {
|
||||
return;
|
||||
}
|
||||
const previous = group.querySelector('input.o_survey_form_choice_item:checked');
|
||||
group.querySelectorAll('input.o_survey_form_choice_item').forEach((input) => {
|
||||
input.checked = false;
|
||||
input.classList.remove('o_survey_form_choice_item_selected');
|
||||
});
|
||||
// Labels' 'o_survey_selected' class is left untouched: the native
|
||||
// handler relies on it to find the previous answer (conditional
|
||||
// questions) and moves it itself.
|
||||
const value = select.value;
|
||||
const target = value && group.querySelector(
|
||||
`input.o_survey_form_choice_item[value="${value}"]`
|
||||
);
|
||||
if (target) {
|
||||
target.checked = true;
|
||||
target.classList.add('o_survey_form_choice_item_selected');
|
||||
}
|
||||
// Native change listener (jQuery delegated) handles: comment area
|
||||
// visibility, conditional questions, and auto-submit when
|
||||
// users_can_go_back on page_per_question layouts. On reset to the
|
||||
// empty option, it is fired on the previous (now unchecked) radio,
|
||||
// like the native untick of a radio.
|
||||
const changed = target || previous;
|
||||
if (changed) {
|
||||
changed.dispatchEvent(new Event('change', { bubbles: true }));
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user