From 73127304626311fe4fcaca4bafb727dd65ae89d7 Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Thu, 27 Jun 2024 10:54:59 +0200 Subject: [PATCH] radio_select: allow to disable a single option --- .../widgets/radio_select.html | 2 +- .../widgets/radio_select_button_group.html | 2 +- tests/test_bootstrap_field_radio_select.py | 46 +++++++++++++++++++ ...otstrap_field_radio_select_button_group.py | 45 ++++++++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html b/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html index a6441a56..cd45e8ab 100644 --- a/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html +++ b/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html @@ -12,7 +12,7 @@ id="{{ option.attrs.id }}" {% if option.value != None %} value="{{ option.value|stringformat:'s' }}" {% if option.attrs.checked %} checked="checked"{% endif %}{% endif %} - {% if widget.attrs.disabled %} disabled{% endif %}> + {% if widget.attrs.disabled or option.attrs.disabled %} disabled{% endif %}> {% endfor %} diff --git a/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select_button_group.html b/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select_button_group.html index 625d093c..df18fc12 100644 --- a/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select_button_group.html +++ b/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select_button_group.html @@ -8,7 +8,7 @@ id="{{ option.attrs.id }}" {% if option.value != None %} value="{{ option.value|stringformat:'s' }}" {% if option.attrs.checked %} checked="checked"{% endif %}{% endif %} - {% if widget.attrs.disabled %} disabled{% endif %} + {% if widget.attrs.disabled or option.attrs.disabled %} disabled{% endif %} {% if widget.required %} required{% endif %}> {% endfor %} diff --git a/tests/test_bootstrap_field_radio_select.py b/tests/test_bootstrap_field_radio_select.py index dc1b0a86..b1566c8f 100644 --- a/tests/test_bootstrap_field_radio_select.py +++ b/tests/test_bootstrap_field_radio_select.py @@ -24,6 +24,31 @@ class DisabledSelectTestForm(forms.Form): ) +class RadioSelectWithDisabledOptions(forms.RadioSelect): + + def __init__(self, attrs=None, choices=(), *, disabled_values=()): + super().__init__(attrs) + self.choices = choices + self.disabled_values = disabled_values + + def create_option(self, name, value, *args, **kwargs): + option = super().create_option(name, value, *args, **kwargs) + if value in self.disabled_values: + option["attrs"]["disabled"] = True + return option + + +class DisabledOptionSelectTestForm(forms.Form): + test = forms.ChoiceField( + choices=( + (1, "one"), + (2, "two"), + ), + widget=RadioSelectWithDisabledOptions(disabled_values=(1,)), + ) + + + class BootstrapFieldSelectTestCase(BootstrapTestCase): def test_select(self): """Test field with select widget.""" @@ -111,3 +136,24 @@ def test_disabled_select(self): "" ), ) + + def test_single_disabled_option(self): + """Test field with a disabled option.""" + self.assertHTMLEqual( + self.render("{% bootstrap_field form.test %}", context={"form": DisabledOptionSelectTestForm()}), + ( + '
' + '' + '
' + '
' + '' + '' + "
" + '
' + '' + '' + "
" + "
" + "
" + ), + ) diff --git a/tests/test_bootstrap_field_radio_select_button_group.py b/tests/test_bootstrap_field_radio_select_button_group.py index 2fbd9c48..b63e0750 100644 --- a/tests/test_bootstrap_field_radio_select_button_group.py +++ b/tests/test_bootstrap_field_radio_select_button_group.py @@ -26,6 +26,32 @@ class DisabledSelectTestForm(forms.Form): ) +class RadioSelectButtonGroupWithDisabledOptions(RadioSelectButtonGroup): + + def __init__(self, attrs=None, choices=(), *, disabled_values=()): + super().__init__(attrs) + self.choices = choices + self.disabled_values = disabled_values + + def create_option(self, name, value, *args, **kwargs): + option = super().create_option(name, value, *args, **kwargs) + if value in self.disabled_values: + option["attrs"]["disabled"] = True + return option + + +class DisabledOptionSelectTestForm(forms.Form): + test = forms.ChoiceField( + choices=( + (1, "one"), + (2, "two"), + ), + widget=RadioSelectButtonGroupWithDisabledOptions(disabled_values=(1,)), + ) + + + + class BootstrapFieldSelectTestCase(BootstrapTestCase): def test_select(self): """Test field with select widget.""" @@ -105,3 +131,22 @@ def test_disabled_select(self): "" ), ) + + def test_single_disabled_option(self): + """Test field with a disabled option.""" + self.assertHTMLEqual( + self.render("{% bootstrap_field form.test %}", context={"form": DisabledOptionSelectTestForm()}), + ( + '
' + '' + '
' + '" + '' + '" + '' + "
" + "
" + ), + )