Skip to content

Commit

Permalink
Added support for custom widgets (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
smithdc1 authored Apr 13, 2021
1 parent 5ea1042 commit 91c3547
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,27 @@ Documentation
The documentation for this project is available here:
https://django-crispy-forms.github.io/crispy-tailwind/index.html

FAQs
----

What about custom widgets?
==========================

The template pack includes default styles for widgets included in Django
itself. `Styling of widget instances`_ can be done by using the ``widget.attrs``
argument when creating the widget.

For example the following form will render
``<input type="text" name="name" class="customtextwidget custom-css" required id="id_name">``::

class CustomTextWidget(forms.TextInput):
pass

class CustomTextWidgetForm(forms.Form):
name = forms.CharField(
widget=CustomTextWidget(attrs={"class": "custom-css"})
)

.. _`Styling of widget instances` : https://docs.djangoproject.com/en/dev/ref/forms/widgets/#styling-widget-instances
.. _Tailwind CSS: https://tailwindcss.com/
.. _django-crispy-forms: https://github.com/django-crispy-forms/django-crispy-forms
2 changes: 1 addition & 1 deletion crispy_tailwind/tailwind.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ def __sub__(self, other):

def get_input_class(self, field):
widget_name = re.sub(r"widget$|input$", "", field.field.widget.__class__.__name__.lower())
return getattr(self, widget_name)
return getattr(self, widget_name, "")
1 change: 1 addition & 0 deletions docs/examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ A formset with ``non_form_errors`` and a field error. See the

.. image:: img/formset_failing.png
:width: 50%

9 changes: 9 additions & 0 deletions tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ class SelectForm(forms.Form):
widget=forms.Select(),
choices=(("accepted", "Accepted"), ("not_accepted", "Not accepted")),
)


class CustomTextWidget(forms.TextInput):
pass


class CustomTextWidgetForm(forms.Form):
first_name = forms.CharField(widget=CustomTextWidget())
last_name = forms.CharField(widget=CustomTextWidget(attrs={"class": "custom-css"}))
13 changes: 12 additions & 1 deletion tests/test_tailwind.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout
from crispy_forms.utils import render_crispy_form
from crispy_tailwind.tailwind import CSSContainer

from .forms import SampleForm
from .forms import CustomTextWidgetForm, SampleForm

individual_inputs = {"text": "text", "radioselect": "radio"}

Expand Down Expand Up @@ -57,3 +58,13 @@ def test_form():
context = Context({"form": SampleForm(), "form_helper": form_helper})
html = template.render(context)
assert "base" in html


def test_custom_widget():
form = CustomTextWidgetForm()
form.helper = FormHelper()
html = render_crispy_form(form)
assert '<input type="text" name="first_name" class="customtextwidget " required id="id_first_name">' in html
assert (
'<input type="text" name="last_name" class="custom-css customtextwidget " required id="id_last_name">' in html
)

0 comments on commit 91c3547

Please sign in to comment.