Repository: cisagov/manage.get.gov

Issue:
Develop infrastructure for dependent answers
- [ ] Develop infrastructure for dependent answers.
- [ ] y/n to question and something else pops up - if any particular page ended up having more than two levels of dependencies - break out


Discussion:
Should try not to nest dependent answers too far, if we can avoid it makes life easier - if not should be flagged earlier
10/18:

- Looking through USWDS, have accordion component - have some JS in place for showing/hiding things
- Maybe easiest to hook into that, will have already have accessibility, making sure form is still usable
- Make decision on whether to use that or extend that
Following up on our discussion during planning yesterday, as of right now there are only two levels for each of the dependent questions/answers. One parent question has two child questions or texts, but that's it (they are leaves).
```
       Q1
     /    \
   Q1A    Q1B
```
10/20:

- Still a WIP, PL on forms
10/26:

- Still in progress, taken PR and integrating
- Issue with Django tweaks library, doesn't have a way of putting specific classes on radio buttons
- Looking to get a PR in next few days
10/27:

- Working on this, close

Resolving patch:
diff --git a/src/registrar/forms/application_wizard.py b/src/registrar/forms/application_wizard.py
--- a/src/registrar/forms/application_wizard.py
+++ b/src/registrar/forms/application_wizard.py
@@ -42,6 +42,23 @@ class OrganizationForm(forms.Form):
         ],
         widget=forms.RadioSelect,
     )
+    federal_type = forms.ChoiceField(
+        required=False,
+        choices=[
+            ("Executive", "Executive"),
+            ("Judicial", "Judicial"),
+            ("Legislative", "Legislative"),
+        ],
+        widget=forms.RadioSelect,
+    )
+    is_election_board = forms.ChoiceField(
+        required=False,
+        choices=[
+            ("Yes", "Yes"),
+            ("No", "No"),
+        ],
+        widget=forms.RadioSelect,
+    )
 
 
 class ContactForm(forms.Form):
diff --git a/src/registrar/templatetags/debugger_tags.py b/src/registrar/templatetags/debugger_tags.py
new file mode 100644
--- /dev/null
+++ b/src/registrar/templatetags/debugger_tags.py
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+"""
+Make debugging Django templates easier.
+Example:
+    {% load debugger_tags %}
+    {{ object|ipdb }}
+
+Copyright (c) 2007 Michael Trier
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+https://github.com/django-extensions/django-extensions/
+"""
+
+from django import template
+
+
+register = template.Library()
+
+
+@register.filter
+def ipdb(obj):  # pragma: no cover
+    """Interactive Python debugger filter."""
+    __import__("ipdb").set_trace()
+    return obj
+
+
+@register.filter
+def pdb(obj):
+    """Python debugger filter."""
+    __import__("pdb").set_trace()
+    return obj
+
+
+@register.filter
+def wdb(obj):  # pragma: no cover
+    """Web debugger filter."""
+    __import__("wdb").set_trace()
+    return obj
diff --git a/src/registrar/templatetags/dynamic_question_tags.py b/src/registrar/templatetags/dynamic_question_tags.py
new file mode 100644
--- /dev/null
+++ b/src/registrar/templatetags/dynamic_question_tags.py
@@ -0,0 +1,48 @@
+from django import template
+from django.utils.html import format_html
+
+register = template.Library()
+
+
+@register.simple_tag
+def radio_buttons_by_value(boundfield):
+    """
+    Returns radio button subwidgets indexed by their value.
+
+    This makes it easier to visually identify the choices when
+    using them in templates.
+    """
+    return {w.data["value"]: w for w in boundfield.subwidgets}
+
+
+@register.simple_tag
+def trigger(boundfield, *triggers):
+    """
+    Inserts HTML to link a dynamic question to its trigger(s).
+
+    Currently only works for radio buttons.
+
+    Also checks whether the question should be hidden on page load.
+    If the question is already answered or if the question's
+    trigger is already selected, it should not be hidden.
+
+    Returns HTML attributes which will be read by a JavaScript
+    helper, if the user has JavaScript enabled.
+    """
+    ids = []
+    hide_on_load = True
+
+    for choice in boundfield.subwidgets:
+        if choice.data["selected"]:
+            hide_on_load = False
+
+    for trigger in triggers:
+        ids.append(trigger.id_for_label)
+        if trigger.data["selected"]:
+            hide_on_load = False
+
+    html = format_html(
+        'toggle-by="{}"{}', ",".join(ids), " hide-on-load" if hide_on_load else ""
+    )
+
+    return html


Test patch: