Improve form style

This commit is contained in:
Edgar P. Burkhart 2025-01-01 14:56:29 +01:00
parent 804b2b38e0
commit 221dbb196e
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
5 changed files with 77 additions and 62 deletions

View file

@ -17,7 +17,7 @@ repos:
rev: v1.23.3 rev: v1.23.3
hooks: hooks:
- id: djlint-django - id: djlint-django
args: ["--reformat"] args: ["--reformat", "--lint", "--quiet"]
- repo: https://github.com/pre-commit/mirrors-prettier - repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.0-alpha.6" rev: "v3.0.0-alpha.6"
hooks: hooks:

View file

@ -6,23 +6,43 @@ form ul.errorlist {
} }
form { form {
> table > tbody > tr > th { max-width: 32rem;
background: var(--bg-01); display: grid;
border-right: 1px solid var(--gray); grid-gap: var(--gap);
}
tbody :is(input, select, textarea) { p.field {
font: inherit; display: grid;
border: none; grid-auto-rows: min-content;
background: transparent; border: 1px solid var(--gray);
width: 100%; padding: 0.5rem;
height: 100%; margin: 0;
line-height: 1.5;
} &:has(> input, > select, > textarea) > label {
input[type="checkbox"] { font-size: 0.8rem;
width: initial; line-height: 0.8rem;
} z-index: 10;
tfoot { }
text-align: right;
> input,
> select,
> textarea {
font: inherit;
line-height: initial;
border: none;
border-bottom: 1px solid var(--gray);
background: none;
z-index: 1;
&[name="value"] {
font-size: 1.5rem;
text-align: right;
}
&:focus {
outline: none;
background: var(--bg-01);
}
}
} }
} }
table.file-input { table.file-input {
@ -42,10 +62,18 @@ table.file-input {
} }
.buttons { .buttons {
display: grid;
grid-auto-columns: min-content;
grid-gap: var(--gap);
align-items: baseline;
& > * {
grid-row: 1;
}
input { input {
font: inherit; font: inherit;
line-height: 1.5; line-height: 1.5;
margin-left: var(--gap);
border-radius: var(--radius); border-radius: var(--radius);
padding: 0 var(--gap); padding: 0 var(--gap);
cursor: pointer; cursor: pointer;
@ -56,13 +84,16 @@ table.file-input {
&[type="submit"] { &[type="submit"] {
border: 0.1rem solid var(--green); border: 0.1rem solid var(--green);
background: var(--green-1); background: var(--green-1);
grid-column: 1;
} }
&[type="reset"] { &[type="reset"] {
border: 0.1rem solid var(--red); border: 0.1rem solid var(--red);
background: var(--red-1); background: var(--red-1);
grid-column: 2;
} }
} }
a.del { a.del {
color: var(--red); color: var(--red);
grid-column: 3;
} }
} }

View file

@ -2,42 +2,28 @@
{% load main_extras %} {% load main_extras %}
{% block fields %} {% block fields %}
{% if form.non_field_errors %} {% if form.non_field_errors %}
<ul class='errorlist'> <ul class="errorlist">
{% for error in form.non_field_errors %}<li>{{ error }}</li>{% endfor %} {% for error in form.non_field_errors %}<li>{{ error }}</li>{% endfor %}
</ul> </ul>
{% endif %} {% endif %}
<table> {% for field in form %}
<tbody> {% if field.errors %}<p class="error">{{ field.errors }}</p>{% endif %}
{% for field in form %} <p class="field">
{% if field.errors %} <label for="{{ field.id_for_label }}">{{ field.label }}</label>
<tr> {{ field }}
<td colspan="2">{{ field.errors }}</td> </p>
</tr> {% endfor %}
{% endif %} <div class="buttons">
<tr> {% block buttons %}
<th class="l"> {% if not form.instance|adding %}
<label for="{{ field.id_for_label }}">{{ field.label }}</label> <a class="del" href="{{ form.instance.get_delete_url }}">{% translate "Delete" %}</a>
</th> {% endif %}
<td>{{ field }}</td> <input type="reset" value="{% translate "Reset" %}">
</tr> {% if form.instance|adding %}
{% endfor %} <input type="submit" value="{% translate "Create" %}">
</tbody> {% else %}
<tfoot> <input type="submit" value="{% translate "Save" %}">
<tr class="buttons"> {% endif %}
<td colspan="2"> {% endblock buttons %}
{% block buttons %} </div>
{% if not form.instance|adding %} {% endblock fields %}
<a class="del" href="{{ form.instance.get_delete_url }}">{% translate "Delete" %}</a>
{% endif %}
<input type="reset" value="{% translate "Reset" %}">
{% if form.instance|adding %}
<input type="submit" value="{% translate "Create" %}">
{% else %}
<input type="submit" value="{% translate "Save" %}">
{% endif %}
{% endblock %}
</td>
</tr>
</tfoot>
</table>
{% endblock %}

View file

@ -62,5 +62,8 @@ class DatalistInput(TextInput):
def get_context(self, *args): def get_context(self, *args):
context = super().get_context(*args) context = super().get_context(*args)
name = context["widget"]["name"]
context["widget"]["attrs"]["list"] = f"{name}-list"
context["widget"]["attrs"]["autocomplete"] = "off"
context["widget"]["options"] = self.options context["widget"]["options"] = self.options
return context return context

View file

@ -1,9 +1,4 @@
<input autocomplete="off" {% include "django/forms/widgets/text.html" %}
list="{{ widget.name }}-list" <datalist id="{{ widget.attrs.list }}">
type="{{ widget.type }}"
name="{{ widget.name }}"
{% if widget.value != None %}value="{{ widget.value|stringformat:'s' }}"{% endif %}
{% include "django/forms/widgets/attrs.html" %}>
<datalist id="{{ widget.name }}-list">
{% for option in widget.options %}<option value="{{ option }}"></option>{% endfor %} {% for option in widget.options %}<option value="{{ option }}"></option>{% endfor %}
</datalist> </datalist>