Skip to content

Commit 5b20c47

Browse files
committed
Move BoundFormField to nhsuk_forms
1 parent dbe4b4f commit 5b20c47

5 files changed

Lines changed: 36 additions & 183 deletions

File tree

lung_cancer_screening/core/form_fields.py

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -33,102 +33,6 @@ def widget_attrs(self, widget):
3333
return attrs
3434

3535

36-
class BoundChoiceField(forms.BoundField):
37-
"""
38-
Specialisation of BoundField that can deal with conditionally shown fields,
39-
and divider content between choices.
40-
This can be used to render a set of radios or checkboxes with text boxes to capture
41-
more details.
42-
"""
43-
44-
def __init__(self, form: forms.Form, field: "ChoiceField", name: str):
45-
super().__init__(form, field, name)
46-
47-
self._conditional_html = {}
48-
self.dividers = {}
49-
50-
def add_conditional_html(self, value, html):
51-
if isinstance(self.field.widget, widgets.Select):
52-
raise ValueError(
53-
"select component does not support conditional fields")
54-
55-
self._conditional_html[value] = html
56-
57-
def conditional_html(self, value):
58-
return self._conditional_html.get(value)
59-
60-
def add_divider_after(self, previous, divider):
61-
self.dividers[previous] = divider
62-
63-
def get_divider_after(self, previous):
64-
return self.dividers.get(previous)
65-
66-
67-
class ChoiceField(forms.ChoiceField):
68-
"""
69-
A ChoiceField that renders using NHS.UK design system radios/select
70-
components.
71-
"""
72-
73-
widget = widgets.RadioSelect
74-
bound_field_class = BoundChoiceField
75-
76-
def __init__(
77-
self,
78-
*args,
79-
hint=None,
80-
label_classes=None,
81-
classes=None,
82-
**kwargs,
83-
):
84-
kwargs["template_name"] = ChoiceField._template_name(
85-
kwargs.get("widget", self.widget)
86-
)
87-
88-
self.hint = hint
89-
self.classes = classes
90-
self.label_classes = label_classes
91-
92-
super().__init__(*args, **kwargs)
93-
94-
@staticmethod
95-
def _template_name(widget):
96-
if (isinstance(widget, type) and widget is widgets.RadioSelect) or isinstance(
97-
widget, widgets.RadioSelect
98-
):
99-
return "forms/radios.jinja"
100-
elif (isinstance(widget, type) and widget is widgets.Select) or isinstance(
101-
widget, widgets.Select
102-
):
103-
return "forms/select.jinja"
104-
105-
106-
class MultipleChoiceField(forms.MultipleChoiceField):
107-
"""
108-
A MultipleChoiceField that renders using the NHS.UK design system checkboxes
109-
component.
110-
"""
111-
112-
widget = widgets.CheckboxSelectMultiple
113-
bound_field_class = BoundChoiceField
114-
115-
def __init__(
116-
self,
117-
*args,
118-
hint=None,
119-
label_classes=None,
120-
classes=None,
121-
**kwargs,
122-
):
123-
kwargs["template_name"] = "forms/checkboxes.jinja"
124-
125-
self.hint = hint
126-
self.classes = classes
127-
self.label_classes = label_classes
128-
129-
super().__init__(*args, **kwargs)
130-
131-
13236
class ImperialHeightWidget(widgets.MultiWidget):
13337
"""
13438
A widget that splits height into feet and inches inputs.

lung_cancer_screening/core/tests/unit/not_test_form_fields.py

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from django import forms
2+
from django.forms import widgets
3+
from django.utils.translation import gettext_lazy as _
4+
5+
6+
class BoundChoiceField(forms.BoundField):
7+
"""
8+
Specialisation of BoundField that can deal with conditionally shown fields,
9+
and divider content between choices.
10+
This can be used to render a set of radios or checkboxes with text boxes to capture
11+
more details.
12+
"""
13+
14+
def __init__(self, form: forms.Form, field: "ChoiceField", name: str):
15+
super().__init__(form, field, name)
16+
17+
self._conditional_html = {}
18+
self.dividers = {}
19+
20+
def add_conditional_html(self, value, html):
21+
if isinstance(self.field.widget, widgets.Select):
22+
raise ValueError(
23+
"select component does not support conditional fields")
24+
25+
self._conditional_html[value] = html
26+
27+
def conditional_html(self, value):
28+
return self._conditional_html.get(value)
29+
30+
def add_divider_after(self, previous, divider):
31+
self.dividers[previous] = divider
32+
33+
def get_divider_after(self, previous):
34+
return self.dividers.get(previous)

lung_cancer_screening/nhsuk_forms/choice_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.forms import widgets
33
from django.utils.translation import gettext_lazy as _
44

5-
from lung_cancer_screening.core.form_fields import BoundChoiceField
5+
from .bound_choice_field import BoundChoiceField
66

77
class ChoiceField(forms.ChoiceField):
88
"""

lung_cancer_screening/nhsuk_forms/typed_choice_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.forms import widgets
44
from django.utils.translation import gettext_lazy as _
55

6-
from lung_cancer_screening.core.form_fields import BoundChoiceField
6+
from .bound_choice_field import BoundChoiceField
77

88
class TypedChoiceField(forms.TypedChoiceField):
99
"""

0 commit comments

Comments
 (0)