From 7f23f34b14497dd3da4386833887f2549cf1215b Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Tue, 25 Jan 2022 16:40:07 +0000 Subject: [PATCH] Clear conditional date fields if invalidated by top level answer --- .../conditional_question_controller.js | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/webpacker/controllers/conditional_question_controller.js b/app/webpacker/controllers/conditional_question_controller.js index 138efd2e9..41bb3c45a 100644 --- a/app/webpacker/controllers/conditional_question_controller.js +++ b/app/webpacker/controllers/conditional_question_controller.js @@ -16,15 +16,30 @@ export default class extends Controller { } } + clearTextNumericInput(input) { + input.value = "" + } + + clearDateInputs(inputs) { + inputs.forEach((input) => { input.value = "" }) + } + displayConditionalRadio() { if(this.element.checked) { let selectedValue = this.element.value let conditional_for = JSON.parse(this.element.dataset.info) Object.entries(conditional_for).map(([targetQuestion, conditions]) => { - let input = document.getElementById(`case-log-${targetQuestion.replaceAll("_","-")}-field`) - if(conditions.includes(selectedValue)) { - } else { - input.value = "" + if(conditions.includes(selectedValue)) { + } else { + const textNumericInput = document.getElementById(`case-log-${targetQuestion.replaceAll("_","-")}-field`) + if (textNumericInput == null) { + const dateInputs = [1,2,3].map((idx) => { + return document.getElementById(`case_log_${targetQuestion}_${idx}i`) + }) + this.clearDateInputs(dateInputs) + } else { + this.clearTextNumericInput(textNumericInput) + } } }) }