Skip to content

Commit

Permalink
Explicit is better than implicit
Browse files Browse the repository at this point in the history
  • Loading branch information
dyve committed Apr 15, 2021
1 parent 6bb8fc7 commit be2d724
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/django_bootstrap5/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"javascript_in_head": False,
"horizontal_label_class": "col-sm-2",
"horizontal_field_class": "col-sm-10",
"horizontal_field_offset_class": "offset-sm-2",
"set_placeholder": True,
"required_css_class": "",
"error_css_class": "is-invalid",
Expand Down
25 changes: 11 additions & 14 deletions src/django_bootstrap5/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def __init__(self, *args, **kwargs):
self.horizontal_field_class = kwargs.get(
"horizontal_field_class", get_bootstrap_setting("horizontal_field_class")
)
self.horizontal_field_offset_class = kwargs.get(
"horizontal_field_offset_class", get_bootstrap_setting("horizontal_field_offset_class")
)
self.inline_field_class = kwargs.get("inline_field_class", get_bootstrap_setting("inline_field_class"))
self.error_css_class = kwargs.get("error_css_class", None)
self.required_css_class = kwargs.get("required_css_class", None)
Expand Down Expand Up @@ -260,19 +263,8 @@ def __init__(self, field, *args, **kwargs):
self.help_text = text_value(field.help_text) if self.show_help and field.help_text else ""
self.field_errors = [conditional_escape(text_value(error)) for error in field.errors]
self.form_check_class = kwargs.get("form_check_class", "form-check")
self.horizontal_offset_class = self.horizontal_label_class.replace("col", "offset")

if "placeholder" in kwargs:
# Find the placeholder in kwargs, even if it's empty
self.placeholder = kwargs["placeholder"]
elif get_bootstrap_setting("set_placeholder"):
# If not found, see if we set the label
self.placeholder = field.label
else:
# Or just set it to empty
self.placeholder = ""
if self.placeholder:
self.placeholder = text_value(self.placeholder)

self.placeholder = text_value(kwargs.get("placeholder", self.default_placeholder))

self.addon_before = kwargs.get("addon_before", self.widget.attrs.pop("addon_before", ""))
self.addon_after = kwargs.get("addon_after", self.widget.attrs.pop("addon_after", ""))
Expand Down Expand Up @@ -311,6 +303,11 @@ def __init__(self, field, *args, **kwargs):
def is_floating(self):
return super().is_floating and self.can_widget_float(self.widget) and self.is_widget_form_control(self.widget)

@property
def default_placeholder(self):
"""Return default placeholder for field."""
return self.field.label if get_bootstrap_setting("set_placeholder") else ""

def restore_widget_attrs(self):
self.widget.attrs = self.initial_attrs.copy()

Expand Down Expand Up @@ -477,7 +474,7 @@ def render(self):
label = mark_safe("")
if isinstance(self.widget, CheckboxInput):
field = format_html('<div class="form-check">{}</div>', field)
horizontal_class = merge_css_classes(self.horizontal_field_class, self.horizontal_offset_class)
horizontal_class = merge_css_classes(self.horizontal_field_class, self.horizontal_field_offset_class)
else:
label = self.get_label_html(horizontal=self.is_horizontal)
horizontal_class = self.horizontal_field_class
Expand Down

0 comments on commit be2d724

Please sign in to comment.