Skip to content
Snippets Groups Projects
Commit 04833031 authored by Tim Fechner's avatar Tim Fechner
Browse files

Add more widgets that needs class="input"

parent 44ffb521
No related branches found
No related tags found
1 merge request!2Change class rendering.
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.html import format_html, format_html_join from django.utils.html import format_html, format_html_join
from django.template import defaulttags from django.template import defaulttags
from django.forms import widgets
def render_form_field(field, label=None): def render_form_field(field, label=None):
...@@ -88,7 +89,12 @@ class BulmaFieldMarkup(object): ...@@ -88,7 +89,12 @@ class BulmaFieldMarkup(object):
@classmethod @classmethod
def select(cls, field, content): def select(cls, field, content):
"""Dropdowns have an extra wrapping <div class="select">""" """Dropdowns have an extra wrapping <div class="select">"""
return cls.label(field.label, cls.div_control(format_html('<div class="select">{}</div>', content))) if isinstance(field.field.widget, widgets.SelectMultiple):
css_classes = 'select is-multiple'
else:
css_classes = 'select'
return cls.label(field.label, cls.div_control(format_html('<div class="{}">{}</div>', css_classes, content)))
@classmethod @classmethod
def checkbox(cls, field, content): def checkbox(cls, field, content):
...@@ -142,3 +148,10 @@ def render_form_errors(errors): ...@@ -142,3 +148,10 @@ def render_form_errors(errors):
""", """,
error_tags=error_tags, error_tags=error_tags,
) )
def instance_of(field_widget, widget_list):
for widget in widget_list:
if isinstance(field_widget, widget):
return True
return False
from django import template from django import template
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.forms.widgets import TextInput, Textarea from django.forms import widgets
from hshassets.forms.utils import render_form_field, render_form_generics, render_form_errors
from hshassets.forms.utils import render_form_field, render_form_generics, render_form_errors, instance_of
from hshassets.forms.elements import Field from hshassets.forms.elements import Field
register = template.Library() register = template.Library()
...@@ -17,9 +18,12 @@ def bulma_form_fields(form): ...@@ -17,9 +18,12 @@ def bulma_form_fields(form):
widget_classes = list() widget_classes = list()
# not every form element in bulma has the 'input' css class, so we need to differ here # not every form element in bulma has the 'input' css class, so we need to differ here
if isinstance(field.field.widget, TextInput):
field_widget = field.field.widget
if instance_of(field_widget, [widgets.TextInput, widgets.NumberInput, widgets.EmailInput, widgets.URLInput, widgets.PasswordInput]):
widget_classes.append("input") widget_classes.append("input")
elif isinstance(field.field.widget, Textarea): elif isinstance(field.field.widget, widgets.Textarea):
widget_classes.append("textarea") widget_classes.append("textarea")
# highlight fields with errors # highlight fields with errors
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment