Browse Source
* Use Stimulus controller for showing and hiding conditional questions for radio questionspull/33/head
Daniel Baark
3 years ago
committed by
GitHub
15 changed files with 1553 additions and 1384 deletions
@ -0,0 +1,15 @@
|
||||
module ConditionalQuestionsHelper |
||||
def conditional_questions_for_page(page) |
||||
page["questions"].values.map { |question| |
||||
question["conditional_for"] |
||||
}.compact.map(&:keys).flatten |
||||
end |
||||
|
||||
def display_question_key_div(page_info, question_key) |
||||
if conditional_questions_for_page(page_info).include?(question_key) |
||||
"<div id=#{question_key}_div style='display:none;'>".html_safe |
||||
else |
||||
"<div id=#{question_key}_div>".html_safe |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,27 @@
|
||||
import { Controller } from "@hotwired/stimulus" |
||||
|
||||
export default class extends Controller { |
||||
initialize() { |
||||
this.displayConditional() |
||||
} |
||||
|
||||
displayConditional() { |
||||
if(this.element.checked) { |
||||
let selected = this.element.value |
||||
let conditional_for = JSON.parse(this.element.dataset.info) |
||||
|
||||
Object.entries(conditional_for).forEach(([key, values]) => { |
||||
let div = document.getElementById(key + "_div") |
||||
if(values.includes(selected)) { |
||||
div.style.display = "block" |
||||
} else { |
||||
div.style.display = "none" |
||||
let buttons = document.getElementsByName(key) |
||||
Object.entries(buttons).forEach(([idx, button]) => { |
||||
button.checked = false; |
||||
}) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
} |
@ -1,7 +0,0 @@
|
||||
import { Controller } from "@hotwired/stimulus" |
||||
|
||||
export default class extends Controller { |
||||
connect() { |
||||
this.element.textContent = "Hello World!" |
||||
} |
||||
} |
@ -1,7 +1,9 @@
|
||||
// This file is auto-generated by ./bin/rails stimulus:manifest:update
|
||||
// Run that command whenever you add a new controller
|
||||
// Load all the controllers within this directory and all subdirectories.
|
||||
// Controller files must be named *_controller.js.
|
||||
|
||||
import { application } from "./application" |
||||
import { Application } from "@hotwired/stimulus" |
||||
import { definitionsFromContext } from "@hotwired/stimulus-webpack-helpers" |
||||
|
||||
import HelloController from "./hello_controller" |
||||
application.register("hello", HelloController) |
||||
const application = Application.start() |
||||
const context = require.context("controllers", true, /_controller\.js$/) |
||||
application.load(definitionsFromContext(context)) |
||||
|
@ -0,0 +1,28 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe ConditionalQuestionsHelper do |
||||
let(:form) { Form.new(2021, 2022) } |
||||
let(:page_key) { "armed_forces" } |
||||
let(:page) { form.all_pages[page_key] } |
||||
|
||||
describe "conditional questions for page" do |
||||
let(:conditional_pages) { %w[armed_forces_active armed_forces_injured] } |
||||
|
||||
it "returns the question keys of all conditional questions on the given page" do |
||||
expect(conditional_questions_for_page(page)).to eq(conditional_pages) |
||||
end |
||||
end |
||||
|
||||
describe "display question key div" do |
||||
let(:question_key) { "armed_forces" } |
||||
let(:conditional_question_key) { "armed_forces_injured" } |
||||
|
||||
it "returns a non visible div for conditional questions" do |
||||
expect(display_question_key_div(page, conditional_question_key)).to match("style='display:none;'") |
||||
end |
||||
|
||||
it "returns a visible div for conditional questions" do |
||||
expect(display_question_key_div(page, question_key)).not_to match("style='display:none;'") |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue