diff --git a/controlpanel/templates/controlpanel/_form_fields.html b/controlpanel/templates/controlpanel/_form_fields.html index 7b31099..75b4c70 100644 --- a/controlpanel/templates/controlpanel/_form_fields.html +++ b/controlpanel/templates/controlpanel/_form_fields.html @@ -12,18 +12,6 @@ {% endfor %} {% for field in form %}
- {% if field.field.widget.input_type == "checkbox" %} - - {% else %} - - {{ field|daisy }} - {% endif %} - {% if field.help_text %}{{ field.help_text }}{% endif %} - {% for error in field.errors %}{{ error }}{% endfor %} + {% form_field field %}
{% endfor %} diff --git a/controlpanel/templates/controlpanel/club_form.html b/controlpanel/templates/controlpanel/club_form.html index dc6ebd1..be9a906 100644 --- a/controlpanel/templates/controlpanel/club_form.html +++ b/controlpanel/templates/controlpanel/club_form.html @@ -17,14 +17,7 @@
{% for field in form %} -
- - {{ field|daisy }} - {% if field.help_text and not field.errors %}{{ field.help_text }}{% endif %} - {% for error in field.errors %}{{ error }}{% endfor %} -
+ {% form_field field %} {% endfor %}
diff --git a/controlpanel/templates/templatetags/field.html b/controlpanel/templates/templatetags/field.html new file mode 100644 index 0000000..6fe2429 --- /dev/null +++ b/controlpanel/templates/templatetags/field.html @@ -0,0 +1,228 @@ +{% load i18n %} + +
+
+ {% if show_label %} +
+
+ {{ field.label }} +
+ + {% if field.field.required %} +
+ {% translate "required" %} +
+ {% endif %} +
+ {% endif %} + +
+ {% if field_type == "input" %} + + + {% elif field_type == "checkbox" %} +
+ + + {% if show_help_text %} +
+ {{ field.help_text }} +
+ {% endif %} +
+ + {% elif field_type == "textarea" %} + + + {% elif field_type == "select" %} + + + {% elif field_type == "file" %} + + + {% if field.value %} + {% translate "Current file:" %} {{ field.value }} + {% endif %} + {% endif %} +
+ + {% if field_type != "checkbox" and field.help_text and show_help_text or field.errors %} +
+ {% if field.errors %} +
+ {{ field.errors }} +
+ {% endif %} + + {% if field.help_text %} +
+ {{ field.help_text }} +
+ {% endif %} +
+ {% endif %} +
+
+ + +{% comment %}
+ +
{% endcomment %} \ No newline at end of file diff --git a/controlpanel/templatetags/ui.py b/controlpanel/templatetags/ui.py index ec17cbb..96e5113 100644 --- a/controlpanel/templatetags/ui.py +++ b/controlpanel/templatetags/ui.py @@ -1,6 +1,7 @@ """Template helpers for the daisyUI-based UI.""" from django import forms, template +from django.forms import BoundField register = template.Library() @@ -130,3 +131,58 @@ def daisy(field, css=None): attrs = dict(widget.attrs) attrs["class"] = " ".join(part for part in [widget.attrs.get("class", ""), css] if part) return field.as_widget(attrs=attrs) + + +@register.inclusion_tag("templatetags/field.html") +def form_field( + field: BoundField, + label: str | None = None, + help_text: str | None = None, + show_label: bool = True, + show_help_text: bool = True, + show_placeholder: bool = True, + show_as_toggle: bool = False, + size: str = "full", +) -> dict: + if label is not None: + field.label = label + + if help_text is not None: + field.help_text = help_text + + field_type = None + match field.widget_type: + case "select" | "nullbooleanselect" | "radioselect": + field_type = "select" + case "checkbox": + field_type = "checkbox" + case "textarea" | "markdownx": + field_type = "textarea" + case "clearablefile": + field_type = "file" + case "selectmultiple": + field_type = "select" + case _: + field_type = "input" + + size_modifier = None + match size: + case "extra-small": + size_modifier = "xs" + case "small": + size_modifier = "sm" + case _: + pass + + # if field.widget_type == "number": + # field.value = str(field.value()) + + return { + "field": field, + "show_label": show_label, + "show_help_text": show_help_text, + "show_placeholder": show_placeholder, + "show_as_toggle": show_as_toggle, + "field_type": field_type, + "size_modifier": size_modifier, + }