Skip to content

Commit

Permalink
Eureka
Browse files Browse the repository at this point in the history
  • Loading branch information
dyve committed Apr 11, 2021
1 parent 87e7bca commit 990e40e
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/django_bootstrap5/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def add_widget_attrs(self):
elif isinstance(widget, ClearableFileInput):
widget.template_name = "django_bootstrap5/widgets/clearable_file_input.html"

def get_label_class(self):
def get_label_class(self, horizontal=False):
"""Return CSS class for label."""
label_classes = [text_value(self.label_class)]
if not self.show_label:
Expand All @@ -382,7 +382,7 @@ def get_label_class(self):
elif self.is_inline:
widget_label_class = "visually-hidden"
else:
widget_label_class = "form-label"
widget_label_class = "col-sm-2 col-form-label" if horizontal else "form-label"
label_classes = [widget_label_class] + label_classes
return merge_css_classes(*label_classes)

Expand All @@ -393,11 +393,15 @@ def get_field_html(self):
self.restore_widget_attrs()
return field_html

def get_label_html(self):
def get_label_html(self, horizontal=False):
"""Return value for label."""
label_html = "" if self.show_label == "skip" else self.field.label
if label_html:
label_html = render_label(label_html, label_for=self.field.id_for_label, label_class=self.get_label_class())
label_html = render_label(
label_html,
label_for=self.field.id_for_label,
label_class=self.get_label_class(horizontal=horizontal),
)
return label_html

def get_help_html(self):
Expand Down Expand Up @@ -464,19 +468,29 @@ def render(self):
return text_value(self.field)

field = self.get_field_html()
if self.field_before_label():
label = self.get_label_html()
field = field + label
label = mark_safe("")
if isinstance(self.widget, CheckboxInput):
field = format_html('<div class="form-check">{}</div>', field)
if self.is_horizontal:
field = format_html('<div class="col-sm-10 offset-sm-2">{}</div>', field)
horizontal_class = "col-sm-10 offset-sm-2"
else:
label = self.get_label_html(horizontal=self.is_horizontal)
horizontal_class = "col-sm-10"

label = self.get_label_html()

field_with_label = field + label if self.field_before_label() else label + field

if isinstance(self.widget, CheckboxInput):
field_with_label = format_html('<div class="form-check">{}</div>', field_with_label)
field_with_help_and_errors = format_html("{}{}{}", field, self.get_help_html(), self.get_errors_html())
if self.is_horizontal:
field_with_help_and_errors = format_html(
'<div class="{}">{}</div>', horizontal_class, field_with_help_and_errors
)

return format_html(
'<{tag} class="{wrapper_classes}">{field_with_label}{help}{errors}</{tag}>',
'<{tag} class="{wrapper_classes}">{label}{field_with_help_and_errors}</{tag}>',
tag=WRAPPER_TAG,
wrapper_classes=self.get_wrapper_classes(),
field_with_label=field_with_label,
help=self.get_help_html(),
errors=self.get_errors_html(),
label=label,
field_with_help_and_errors=field_with_help_and_errors,
)

0 comments on commit 990e40e

Please sign in to comment.