From b95aab5a1358f7bc228100171facd422787bc0db Mon Sep 17 00:00:00 2001 From: Nikita Frolenkov Date: Wed, 1 Nov 2023 21:41:22 +0500 Subject: [PATCH] Add "input_class" argument to "bootstrap_field (#525) --- CHANGELOG.md | 1 + src/django_bootstrap5/renderers.py | 4 +++- src/django_bootstrap5/templatetags/django_bootstrap5.py | 5 +++++ tests/test_bootstrap_field.py | 6 ++++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 263424e9..0a752a61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Drop support for Python 3.7 in test matrix (#533). - Fix support for Django 4.2 in test matrix (#533). - Pass "horizontal_field_offset_class" to child renderers (#391, #521). +- Add "input_class" argument to "bootstrap_field (#525)". ## 23.3 (2023-06-03) diff --git a/src/django_bootstrap5/renderers.py b/src/django_bootstrap5/renderers.py index 0a3f2d95..ec26f80f 100644 --- a/src/django_bootstrap5/renderers.py +++ b/src/django_bootstrap5/renderers.py @@ -246,6 +246,8 @@ def __init__(self, field, **kwargs): else success_css_class ) + self.input_class = kwargs.get("input_class", "") + @property def is_floating(self): return ( @@ -302,7 +304,7 @@ def add_widget_class_attrs(self, widget=None): size_prefix = None before = [] - classes = [widget.attrs.get("class", "")] + classes = [widget.attrs.get("class", ""), self.input_class] if ReadOnlyPasswordHashWidget is not None and isinstance(widget, ReadOnlyPasswordHashWidget): before.append("form-control-static") diff --git a/src/django_bootstrap5/templatetags/django_bootstrap5.py b/src/django_bootstrap5/templatetags/django_bootstrap5.py index 1186cacf..1d23bd5e 100644 --- a/src/django_bootstrap5/templatetags/django_bootstrap5.py +++ b/src/django_bootstrap5/templatetags/django_bootstrap5.py @@ -472,6 +472,11 @@ def bootstrap_field(field, **kwargs): :default: ``'has-success'``. Can be changed :doc:`settings` + input_class + CSS class added to the input html element + + :default: ``''`` + **Usage**:: {% bootstrap_field field %} diff --git a/tests/test_bootstrap_field.py b/tests/test_bootstrap_field.py index 39432fa2..779f2ace 100644 --- a/tests/test_bootstrap_field.py +++ b/tests/test_bootstrap_field.py @@ -68,6 +68,12 @@ def _test_size_medium(param): _test_size_medium("md") _test_size_medium("") + def test_input_class(self): + self.assertIn( + 'class="form-control foobar-class"', + self.render('{% bootstrap_field form.subject input_class="foobar-class" %}', {"form": SubjectTestForm()}), + ) + def test_label(self): self.assertEqual( self.render('{% bootstrap_label "foobar" label_for="subject" %}'),