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..3e28f380 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 = set(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..10ec8e6e 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 = set(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()}), + ( + '
' + '' + '
' + '" + '' + '" + '' + "
" + "
" + ), + )