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 end
# TODO: remove this once all conditional questions no longer need JS
def conditional_html_attributes(question) def conditional_html_attributes(question)
return {} if question.conditional_for.blank? || question.type == "radio" return {} if question.conditional_for.blank?
{ {
"data-controller": "conditional-question", "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) %> <%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) %>
<% else %> <% else %>
<%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) do %> <%= 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 %> <% end %>
<% end %> <% end %>

14
app/webpacker/controllers/conditional_question_controller.js

@ -20,21 +20,11 @@ export default class extends Controller {
if(this.element.checked) { if(this.element.checked) {
let selectedValue = this.element.value let selectedValue = this.element.value
let conditional_for = JSON.parse(this.element.dataset.info) let conditional_for = JSON.parse(this.element.dataset.info)
Object.entries(conditional_for).map(([targetQuestion, conditions]) => { Object.entries(conditional_for).map(([targetQuestion, conditions]) => {
let div = document.getElementById(targetQuestion + "_div") let input = document.getElementById(`case-log-${targetQuestion.replaceAll("_","-")}-field`)
if (div == null) return;
if(conditions.includes(selectedValue)) { if(conditions.includes(selectedValue)) {
div.style.display = "block"
} else { } else {
div.style.display = "none" input.value = ""
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;
})
} }
}) })
} }

Loading…
Cancel
Save