Skip to content

Fix: Subwidgets are now rendered using their own type #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions crispy_tailwind/tailwind.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import re

from django.forms import Field, Widget, boundfield


class CSSContainer:
def __init__(self, css_styles):
Expand Down Expand Up @@ -67,6 +69,15 @@ def __sub__(self, other):
setattr(self, field, new_classes)
return self

def get_input_class(self, field):
widget_name = re.sub(r"widget$|input$", "", field.field.widget.__class__.__name__.lower())
def get_input_class(self, obj):
# Following check is used to keep backward compatibility with older versions
if isinstance(obj, boundfield.BoundField):
widget = obj.field.widget
elif isinstance(obj, Field):
widget = obj.widget
elif isinstance(obj, Widget):
widget = obj
else:
raise ValueError("Object must be a BoundField, Field or Widget")
widget_name = re.sub(r"widget$|input$", "", widget.__class__.__name__.lower())
return getattr(self, widget_name, "")
4 changes: 2 additions & 2 deletions crispy_tailwind/templatetags/tailwind_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import re

from crispy_forms.utils import TEMPLATE_PACK, get_template_pack
from django import forms, template
from django.conf import settings
from django.template import Context, loader

from crispy_forms.utils import TEMPLATE_PACK, get_template_pack
from crispy_tailwind.tailwind import CSSContainer

register = template.Library()
Expand Down Expand Up @@ -161,7 +161,7 @@ def render(self, context): # noqa: C901
if template_pack == "tailwind" and '"class"' not in attr.keys():
css_container = context.get("css_container", self.default_container)
if css_container:
css = " " + css_container.get_input_class(field)
css = " " + css_container.get_input_class(widget)
css_class += css
if field.errors:
error_border_class = css_container.error_border
Expand Down
Loading