Skip to content

Commit 527a484

Browse files
authored
update checkbox, radioselect styles (#5)
1 parent 1469d9a commit 527a484

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/django_formify/tailwind/formify_helper.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,16 @@
1111

1212

1313
class CSSContainer:
14-
def __init__(self, css_styles, error_styles):
15-
self.css_styles = css_styles.copy()
16-
self.error_styles = error_styles.copy()
14+
def __init__(self, css_styles):
15+
for key, value in css_styles.items():
16+
setattr(self, key, value)
1717

18-
def get_input_class(self, field):
18+
def get_field_class(self, field):
1919
widget_cls = field.field.widget.__class__.__name__
2020
key = camel_to_snake(widget_cls)
21-
css_classes = getattr(self, "css_styles", {}).get(key, "")
21+
css_classes = getattr(self, key, "")
2222
return css_classes
2323

24-
def get_error_class(self, field):
25-
widget_cls = field.field.widget.__class__.__name__
26-
key = camel_to_snake(widget_cls)
27-
error_classes = getattr(self, "error_styles", {}).get(key, "")
28-
return error_classes
29-
3024

3125
class FormifyHelper:
3226
"""
@@ -66,13 +60,19 @@ class FormifyHelper:
6660
"time_input": common_style,
6761
"date_time_input": common_style,
6862
"clearable_file_input": "w-full overflow-clip rounded-lg border border-gray-300 bg-gray-50/50 text-gray-600 file:mr-4 file:cursor-pointer file:border-none file:bg-gray-50 file:px-4 file:py-2 file:font-medium file:text-gray-900 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-black disabled:cursor-not-allowed disabled:opacity-75 dark:border-gray-700 dark:bg-gray-900/50 dark:text-gray-300 dark:file:bg-gray-900 dark:file:text-white dark:focus-visible:outline-white",
63+
"radio_select_option_label": "inline-flex items-center gap-2 text-gray-700",
64+
"checkbox_label": "inline-flex items-center gap-2 text-gray-700",
6965
}
7066

7167
default_error_styles = {
7268
# border-red-300
7369
"clearable_file_input": "w-full overflow-clip rounded-lg border border-red-300 bg-gray-50/50 text-gray-600 file:mr-4 file:cursor-pointer file:border-none file:bg-gray-50 file:px-4 file:py-2 file:font-medium file:text-gray-900 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-black disabled:cursor-not-allowed disabled:opacity-75 dark:border-gray-700 dark:bg-gray-900/50 dark:text-gray-300 dark:file:bg-gray-900 dark:file:text-white dark:focus-visible:outline-white",
7470
}
7571

72+
css_container = None
73+
74+
error_css_container = None
75+
7676
################################################################################
7777
# label
7878
################################################################################
@@ -96,10 +96,11 @@ class FormifyHelper:
9696
layout = None
9797

9898
def __init__(self):
99-
self.css_container = self.get_css_container()
99+
self.prepare_css_container()
100100

101-
def get_css_container(self):
102-
return CSSContainer(self.default_styles, self.default_error_styles)
101+
def prepare_css_container(self):
102+
self.css_container = CSSContainer(self.default_styles)
103+
self.error_css_container = CSSContainer(self.default_error_styles)
103104

104105
def get_context_data(self, context_data) -> Context:
105106
if isinstance(context_data, Context):
@@ -267,12 +268,11 @@ def render_as_tailwind_field(self, context):
267268
# if class is not set, then add additional css classes
268269

269270
# add default input class
270-
css_container = self.css_container
271-
css = " " + css_container.get_input_class(field)
271+
css = " " + self.css_container.get_field_class(field)
272272
css_class += css
273273

274274
if field.errors:
275-
error_css = css_container.get_error_class(field)
275+
error_css = self.error_css_container.get_field_class(field)
276276
if error_css:
277277
css_class = error_css
278278
else:

src/django_formify/templates/formify/tailwind/checkbox_input.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{% endcall %}
66

77
{% call field_component.input %}
8-
<label for="{{ field.id_for_label }}" class="inline-flex items-center gap-2 text-gray-700">
8+
<label for="{{ field.id_for_label }}" class="{{ formify_helper.css_container.checkbox_label|default_if_none:'' }}">
99
{{ field_html }}
1010
{% comment %}line break here would cause extra white space around the checkbox{% endcomment %}
1111
{{ field.label|safe }}

src/django_formify/templates/formify/tailwind/checkbox_select_multiple.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
{% call field_component.input %}
1010
<div class="{{ formify_helper.field_class }}" {% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
1111
{% for choice in field.field.choices %}
12-
<div class="mr-3">
13-
<label class="inline-flex items-center gap-2 text-gray-700" for="id_{{ field.html_name }}_{{ forloop.counter }}">
14-
<input type="checkbox" class="{{ css_container.checkboxselectmultiple }}"{% if choice.0 in field.value or choice.0|stringformat:"s" in field.value or choice.0|stringformat:"s" == field.value|default_if_none:""|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
12+
<div>
13+
<label class="{{ formify_helper.css_container.checkbox_label|default_if_none:'' }}" for="id_{{ field.html_name }}_{{ forloop.counter }}">
14+
<input type="checkbox" class="{{ formify_helper.css_container.checkboxselectmultiple }}"{% if choice.0 in field.value or choice.0|stringformat:"s" in field.value or choice.0|stringformat:"s" == field.value|default_if_none:""|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
1515
{% comment %}line break here would cause extra white space around the checkbox{% endcomment %}
1616
{{ choice.1|unlocalize }}
1717
</label>

src/django_formify/templates/formify/tailwind/radio_select.html

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
{% call field_component.input %}
1010
<div {% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
1111
{% for choice in field.field.choices %}
12-
<label for="id_{{ field.html_name }}_{{ forloop.counter }}" class="{% if css_container.option_label %}{{ css_container.option_label }}{% else %}block text-gray-700{% endif %} mr-3">
13-
<input type="radio" class="{{ css_container.radioselect }}"{% if choice.0|stringformat:"s" == field.value|default_if_none:""|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
14-
{{ choice.1|unlocalize }}
15-
</label>
12+
<div>
13+
<label for="id_{{ field.html_name }}_{{ forloop.counter }}" class="{{ formify_helper.css_container.radio_select_option_label|default_if_none:'' }}">
14+
<input type="radio" class="{{ formify_helper.css_container.radio_select_option_input }}"{% if choice.0|stringformat:"s" == field.value|default_if_none:""|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
15+
{{ choice.1|unlocalize }}
16+
</label>
17+
</div>
1618
{% endfor %}
1719
</div>
1820
{% endcall %}

0 commit comments

Comments
 (0)