Browse Source

We still need stimulus to clear answers

pull/235/head
baarkerlounger 3 years ago
parent
commit
d741d15a19
  1. 3
      app/helpers/question_attribute_helper.rb
  2. 8
      app/views/form/_radio_question.html.erb
  3. 14
      app/webpacker/controllers/conditional_question_controller.js

3
app/helpers/question_attribute_helper.rb

@ -21,9 +21,8 @@ private
}
end
# TODO: remove this once all conditional questions no longer need JS
def conditional_html_attributes(question)
return {} if question.conditional_for.blank? || question.type == "radio"
return {} if question.conditional_for.blank?
{
"data-controller": "conditional-question",

8
app/views/form/_radio_question.html.erb

@ -14,7 +14,13 @@
<%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) %>
<% else %>
<%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) do %>
<%= render partial: "#{conditional_question.type}_question", locals: { question: conditional_question, caption: caption, page_header: page_header, f: f } %>
<%= render partial: "#{conditional_question.type}_question", locals: {
question: conditional_question,
caption: caption,
page_header: page_header,
f: f,
conditional: true
} %>
<% end %>
<% end %>
<% end %>

14
app/webpacker/controllers/conditional_question_controller.js

@ -20,21 +20,11 @@ export default class extends Controller {
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 div = document.getElementById(targetQuestion + "_div")
if (div == null) return;
let input = document.getElementById(`case-log-${targetQuestion.replaceAll("_","-")}-field`)
if(conditions.includes(selectedValue)) {
div.style.display = "block"
} else {
div.style.display = "none"
let buttons = document.getElementsByName(`case_log[${targetQuestion}]`);
if (buttons.length == 0){
buttons = document.getElementsByName(`case_log[${targetQuestion}][]`);
}
Object.entries(buttons).map(([idx, button]) => {
button.checked = false;
})
input.value = ""
}
})
}

Loading…
Cancel
Save