Skip to content

Commit

Permalink
radio_select: allow to disable a single option
Browse files Browse the repository at this point in the history
  • Loading branch information
xavfernandez committed Jul 4, 2024
1 parent 2a0b9cc commit 8d78e72
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}>
<label class="form-check-label" for="{{ option.attrs.id }}">{{ option.label }}</label>
</div>
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}>
<label class="btn btn-outline-primary" for="{{ option.attrs.id }}">{{ option.label }}</label>
{% endfor %}
Expand Down
46 changes: 46 additions & 0 deletions tests/test_bootstrap_field_radio_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -111,3 +136,24 @@ def test_disabled_select(self):
"</div>"
),
)

def test_single_disabled_option(self):
"""Test field with a disabled option."""
self.assertHTMLEqual(
self.render("{% bootstrap_field form.test %}", context={"form": DisabledOptionSelectTestForm()}),
(
'<div class="django_bootstrap5-req mb-3">'
'<label class="form-label">Test</label>'
'<div class="" required id="id_test">'
'<div class="form-check">'
'<input class="form-check-input" disabled type="radio" name="test" id="id_test_0" value="1">'
'<label class="form-check-label" for="id_test_0">one</label>'
"</div>"
'<div class="form-check">'
'<input class="form-check-input" type="radio" name="test" id="id_test_1" value="2">'
'<label class="form-check-label" for="id_test_1">two</label>'
"</div>"
"</div>"
"</div>"
),
)
45 changes: 45 additions & 0 deletions tests/test_bootstrap_field_radio_select_button_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -105,3 +131,22 @@ def test_disabled_select(self):
"</div>"
),
)

def test_single_disabled_option(self):
"""Test field with a disabled option."""
self.assertHTMLEqual(
self.render("{% bootstrap_field form.test %}", context={"form": DisabledOptionSelectTestForm()}),
(
'<div class="django_bootstrap5-req mb-3">'
'<label class="form-label">Test</label>'
'<div id="id_test" class="btn-group" role="group">'
'<input type="radio" class=" btn-check" autocomplete="off" name="test" id="id_test_0" value="1" '
"disabled required>"
'<label class="btn btn-outline-primary" for="id_test_0">one</label>'
'<input type="radio" class=" btn-check" autocomplete="off" name="test" id="id_test_1" value="2" '
"required>"
'<label class="btn btn-outline-primary" for="id_test_1">two</label>'
"</div>"
"</div>"
),
)

0 comments on commit 8d78e72

Please sign in to comment.