Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Fix `form_check_class` being ignored for single checkboxes; it was only applied to `RadioSelect` and `CheckboxSelectMultiple` (#290).
- Fix `show_label` and `label_class` being ignored for the label next to a checkbox. `show_label=False` and `show_label='sr-only'` now add `sr-only`, `show_label='skip'` omits the label, and `label_class` extends `form-check-label` instead of being dropped (#127).
- Fix `size` not being applied to the input group wrapper when a field has an addon, so `input-group-sm` and `input-group-lg` were missing and the addon rendered at the wrong height (#128).
- Add a system check that warns about keys in the `BOOTSTRAP4` setting that the package does not read, such as `base_url`, dropped in 0.0.8 (`bootstrap4.W001`, #883). Silence it with `SILENCED_SYSTEM_CHECKS` if you keep extra keys deliberately.
- Document that `use_i18n` in `BOOTSTRAP4` is always overwritten by Django's `USE_I18N`.
Expand Down
12 changes: 11 additions & 1 deletion src/bootstrap4/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,22 @@ def list_to_class(self, html, klass):
pass
return str(soup)

def get_checkbox_label_class(self):
"""Return the class for the label that sits next to a checkbox inside the form-check div."""
# form-check-label is structural Bootstrap markup, so label_class extends it instead of replacing it.
label_class = add_css_class(self.label_class, "form-check-label", prepend=True)
if not self.show_label or self.show_label == "sr-only":
label_class = add_css_class(label_class, "sr-only")
return label_class

def add_checkbox_label(self, html):
if self.show_label == "skip":
return html
return html + render_label(
content=self.field.label,
label_for=self.field.id_for_label,
label_title=escape(strip_tags(self.field_help)),
label_class="form-check-label",
label_class=self.get_checkbox_label_class(),
)

def fix_date_select_input(self, html):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,21 @@ def test_show_label_skip(self):
res = render_template_with_form("{% bootstrap_form form show_label='skip' %}", {"form": form})
self.assertNotIn("<label>", res)

def test_show_label_false_for_checkbox(self):
"""show_label=False should hide the checkbox label, not be ignored (#127)."""
res = render_template_with_form("{% bootstrap_field form.cc_myself show_label=False %}")
self.assertIn('class="form-check-label sr-only"', res)

def test_show_label_skip_for_checkbox(self):
"""show_label='skip' should drop the checkbox label entirely (#127)."""
res = render_template_with_form("{% bootstrap_field form.cc_myself show_label='skip' %}")
self.assertNotIn("<label", res)

def test_label_class_for_checkbox(self):
"""label_class should extend form-check-label rather than be dropped (#127)."""
res = render_template_with_form("{% bootstrap_field form.cc_myself label_class='my-label' %}")
self.assertIn('class="form-check-label my-label"', res)

def test_for_formset(self):
TestFormSet = formset_factory(CharFieldTestForm, extra=1)
test_formset = TestFormSet()
Expand Down
Loading