Skip to content

Commit 0fca143

Browse files
authored
fix: upload field checkbox widget (#240)
1 parent e2eaa45 commit 0fca143

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

src/unfold/static/unfold/css/styles.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/unfold/templates/admin/widgets/clearable_file_input.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
{% if widget.is_initial and not widget.required %}
1313
<div class="bg-gray-50 border-r flex flex-none items-center self-stretch px-3 dark:bg-white/[.02] dark:border-gray-700 dark:text-gray-400">
1414
<label for="{{ widget.checkbox_id }}" class="flex items-center">
15-
<input type="checkbox" class="form-check-input" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}" />
15+
<input type="checkbox"{% if widget.class %} class="{{ widget.class }}"{% endif %} name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}" />
1616

1717
<span class="ml-2 text-gray-500 dark:text-gray-400">
1818
{{ widget.clear_checkbox_label }}

src/unfold/templates/unfold/widgets/clearable_file_input_small.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<div class="flex flex-row">
44
<div class="border flex items-center overflow-hidden rounded-md shadow-sm text-sm max-w-2xl w-full focus-within:ring focus-within:ring-primary-300 focus-within:border-primary-600 dark:border-gray-700 dark:focus-within:border-primary-600 dark:focus-within:ring-primary-700 dark:focus-within:ring-opacity-50">
55
{% if widget.is_initial and not widget.required %}
6-
<div class="bg-gray-50 border-r flex flex-none items-center self-stretch px-3 dark:bg-gray-900">
6+
<div class="bg-gray-50 border-r flex flex-none items-center self-stretch px-3 dark:bg-gray-900 dark:border-r-gray-700">
77
<label for="{{ widget.checkbox_id }}" class="flex items-center">
8-
<input type="checkbox" class="form-check-input" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}" />
8+
<input type="checkbox"{% if widget.class %} class="{{ widget.class }}"{% endif %} name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}" />
99

10-
<span class="ml-2 text-gray-500">
10+
<span class="ml-2 text-gray-500 dark:text-gray-400">
1111
{{ widget.clear_checkbox_label }}
1212
</span>
1313
</label>
@@ -19,19 +19,19 @@
1919
<div class="flex flex-none items-center leading-none self-stretch">
2020
<input id="{{ widget.name }}" type="{{ widget.type }}" name="{{ widget.name }}" class="opacity-0 pointer-events-none" {% include "django/forms/widgets/attrs.html" %} />
2121

22-
<label for="{{ widget.name }}" class="cursor-pointer text-gray-400 px-3 dark:text-gray-400">
22+
<label for="{{ widget.name }}" class="cursor-pointer text-gray-400 px-3 dark:text-gray-500">
2323
<span class="material-symbols-outlined">file_upload</span>
2424
</label>
2525
</div>
2626
</div>
2727

2828
{% if widget.attrs.accept == 'image/*' and widget.is_initial %}
29-
<div class="bg-gray-50 border h-9 ml-3 overflow-hidden relative rounded-md shadow-sm w-9 dark:bg-gray-900 dark:border-gray-700 dark:text-gray-500">
30-
<div class="absolute flex font-medium h-full items-center left-0 justify-center text-gray-400 top-0 w-full z-10 dark:text-gray-500">
29+
<div class="h-9.5 ml-3 relative w-9.5">
30+
<div class="absolute border flex font-medium h-full items-center left-0 rounded-md justify-center shadow-sm text-gray-400 top-0 w-full z-10 dark:bg-gray-900 dark:border-gray-700 dark:text-gray-500">
3131
?
3232
</div>
3333

34-
<a href="{{ widget.value.url }}" target="_blank" class="bg-center bg-cover bg-no-repeat block h-9 relative w-9 z-20" style="background-image: url('{{ widget.value.url }}')">
34+
<a href="{{ widget.value.url }}" target="_blank" class="bg-center bg-cover bg-no-repeat block h-9.5 overflow-hidden relative rounded-md w-9.5 z-20" style="background-image: url('{{ widget.value.url }}')">
3535
</a>
3636
</div>
3737
{% endif %}

src/unfold/widgets.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,24 @@ def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:
281281
super().__init__(attrs={"class": " ".join(INPUT_CLASSES), **(attrs or {})})
282282

283283

284-
class UnfoldAdminImageFieldWidget(AdminFileWidget):
284+
class FileFieldMixin:
285+
def get_context(self, name, value, attrs):
286+
widget = super().get_context(name, value, attrs)
287+
widget["widget"].update(
288+
{"class": " ".join([*CHECKBOX_CLASSES, *["form-check-input"]])}
289+
)
290+
return widget
291+
292+
293+
class UnfoldAdminImageFieldWidget(FileFieldMixin, AdminFileWidget):
285294
pass
286295

287296

288-
class UnfoldAdminFileFieldWidget(AdminFileWidget):
297+
class UnfoldAdminFileFieldWidget(FileFieldMixin, AdminFileWidget):
289298
template_name = "unfold/widgets/clearable_file_input_small.html"
290299

291300

292-
class UnfoldAdminImageSmallFieldWidget(AdminFileWidget):
301+
class UnfoldAdminImageSmallFieldWidget(FileFieldMixin, AdminFileWidget):
293302
template_name = "unfold/widgets/clearable_file_input_small.html"
294303

295304

0 commit comments

Comments
 (0)