Skip to content

Commit

Permalink
Replace international investment filters accordion with DS accordion
Browse files Browse the repository at this point in the history
  • Loading branch information
boxadesign committed Nov 15, 2024
1 parent 94053ea commit a6ac257
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,101 +1,10 @@
{% load investment_filters %}
<h2 class="govuk-heading-m govuk-!-margin-bottom-4">Filter results</h2>
<form class="magna-header__search govuk-!-margin-bottom-4"
action=""
method="get">
<div class="govuk-accordion"
data-module="govuk-accordion"
id="accordion-default">
{% if form.investment_type %}
<div class="govuk-accordion__section">
<div class="govuk-accordion__section-header">
<h2 class="govuk-accordion__section-heading">
<span class="govuk-accordion__section-button"
id="accordion-default-heading-1">
<span class="govuk-heading-s">Investment type</span>
</span>
</h2>
</div>
<div id="accordion-default-content-1"
class="govuk-accordion__section-content govuk-!-padding-left-2 fixed-height-scroll">
<div class="govuk-checkboxes govuk-checkboxes--small"
data-module="govuk-checkboxes">
{% for action in form.investment_type %}
<div class="govuk-checkboxes__item">
<input {% if action.data.value in form.investment_type.value %}checked{% endif %}
type="checkbox"
name="{{ form.investment_type.name }}"
value="{{ action.data.value }}"
class="govuk-checkboxes__input"
id="{{ action.id_for_label }}">
<label class="govuk-label govuk-checkboxes__label"
for="{{ action.id_for_label }}">{{ action.choice_label }}</label>
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}
{% if form.sector %}
<div class="govuk-accordion__section">
<div class="govuk-accordion__section-header">
<h2 class="govuk-accordion__section-heading">
<span class="govuk-accordion__section-button"
id="accordion-default-heading-2">
<span class="govuk-heading-s">Sector</span>
</span>
</h2>
</div>
<div id="accordion-default-content-2"
class="govuk-accordion__section-content govuk-!-padding-left-2 fixed-height-scroll">
<div class="govuk-checkboxes govuk-checkboxes--small"
data-module="govuk-checkboxes">
{% for action in form.sector %}
<div class="govuk-checkboxes__item">
<input {% if action.data.value in form.sector.value %}checked{% endif %}
type="checkbox"
name="{{ form.sector.name }}"
value="{{ action.data.value }}"
class="govuk-checkboxes__input"
id="{{ action.id_for_label }}">
<label class="govuk-label govuk-checkboxes__label"
for="{{ action.id_for_label }}">{{ action.choice_label }}</label>
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}
{% if form.region %}
<div class="govuk-accordion__section">
<div class="govuk-accordion__section-header">
<h2 class="govuk-accordion__section-heading">
<span class="govuk-accordion__section-button"
id="accordion-default-heading-3">
<span class="govuk-heading-s">UK nation or region</span>
</span>
</h2>
</div>
<div id="accordion-default-content-3"
class="govuk-accordion__section-content govuk-!-padding-left-2 fixed-height-scroll">
<div class="govuk-checkboxes govuk-checkboxes--small"
data-module="govuk-checkboxes">
{% for action in form.region %}
<div class="govuk-checkboxes__item">
<input {% if action.data.value in form.region.value %}checked{% endif %}
type="checkbox"
name="{{ form.region.name }}"
value="{{ action.data.value }}"
class="govuk-checkboxes__input"
id="{{ action.id_for_label }}">
<label class="govuk-label govuk-checkboxes__label"
for="{{ action.id_for_label }}">{{ action.choice_label }}</label>
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}
</div>
{% get_filter_accordion_items form as accordion_items %}
{% include '_accordion.html' with id='accordion-default' items=accordion_items headingClasses='govuk-accordion__section-button--size-19' %}
{% include 'international/includes/submit_button.html' with button_text='Update results' %}
<div>
<a href="?page=1" class="govuk-link">Reset</a>
Expand Down
6 changes: 2 additions & 4 deletions international_investment/templates/investment/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load static %}
{% load wagtailimages_tags %}
{% load company_filters %}
{% load investment_filters %}
{% block head_title %}
{% if form.errors %}Error:{% endif %}
Investment opportunity search
Expand Down Expand Up @@ -46,7 +47,4 @@ <h3 class="govuk-heading-m">No opportunities match your search</h3>
</div>
</section>
{% endblock %}
{% block body_js %}
{{ block.super }}
<script>document.getElementsByClassName("govuk-accordion__controls")[0].style.display = 'none';</script>
{% endblock %}
{% block body_js %}{{ block.super }}{% endblock %}
Empty file.
57 changes: 57 additions & 0 deletions international_investment/templatetags/investment_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django import template
from django.template.defaultfilters import urlencode

register = template.Library()


@register.filter
def get_url(value, param=None):
if param is None:
return value
return f"{value}?{urlencode(param)}"


@register.simple_tag
def get_filter_accordion_items(form):
items = []

for field in form:
if field.field.widget.input_type == 'checkbox':
items.append(
{
'heading': {'text': field.label},
'content': {'html': _create_checkbox_html(field, _get_field_values(field))},
}
)

return items


def _get_field_values(field):
if hasattr(field, 'value'):
if callable(field.value):
return field.value() or []
return field.value or []
return []


def _create_checkbox_html(field, field_values):
checkboxes_html = (
'<div class="govuk-checkboxes govuk-checkboxes--small fixed-height-scroll govuk-!-padding-left-2" '
'data-module="govuk-checkboxes">'
)

for action in field:
value = action.data['value'] if isinstance(action.data, dict) else action.data.value
checked = 'checked' if value in field_values else ''
checkboxes_html += (
f'<div class="govuk-checkboxes__item">'
f'<input {checked} type="checkbox" name="{field.name}" '
f'value="{value}" class="govuk-checkboxes__input" '
f'id="{action.id_for_label}">'
f'<label class="govuk-label govuk-checkboxes__label" '
f'for="{action.id_for_label}">{action.choice_label}</label>'
f'</div>'
)

return checkboxes_html + '</div>'

0 comments on commit a6ac257

Please sign in to comment.