Skip to content

Commit

Permalink
feat: choices support in ArrayWidget (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav authored Jul 7, 2024
1 parent d22f633 commit 195ee2d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/unfold/contrib/forms/templates/unfold/forms/array.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

<template x-for="(item, index) in items" :key="item.key">
<div class="flex flex-row">
{% include template.template_name with widget=template %}
{% with widget=template %}
{% include template.template_name %}
{% endwith %}

<a x-on:click="items.splice(index, 1)" class="bg-white border cursor-pointer flex items-center h-9.5 justify-center ml-2 rounded shadow-sm shrink-0 text-red-600 text-sm w-9.5 dark:bg-gray-900 dark:border-gray-700 dark:text-red-500">
<span class="material-symbols-outlined text-sm">delete</span>
Expand Down
31 changes: 21 additions & 10 deletions src/unfold/contrib/forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from django.forms import MultiWidget, Widget
from django.http import QueryDict
from django.utils.datastructures import MultiValueDict
from unfold.widgets import PROSE_CLASSES, UnfoldAdminTextInputWidget
from unfold.widgets import (
PROSE_CLASSES,
UnfoldAdminSelectWidget,
UnfoldAdminTextInputWidget,
)

WYSIWYG_CLASSES = [
*PROSE_CLASSES,
Expand All @@ -27,19 +31,26 @@

class ArrayWidget(MultiWidget):
template_name = "unfold/forms/array.html"
widget_class = UnfoldAdminTextInputWidget

def __init__(self, *args: Any, **kwargs: Any) -> None:
widgets = [self.widget_class]
if "choices" in kwargs:
self.choices = kwargs["choices"]

widgets = [self.get_widget_instance()]
super().__init__(widgets)

def get_widget_instance(self) -> Any:
if hasattr(self, "choices"):
return UnfoldAdminSelectWidget(choices=self.choices)

return UnfoldAdminTextInputWidget()

def get_context(self, name: str, value: str, attrs: Dict) -> Dict:
self._resolve_widgets(value)
context = super().get_context(name, value, attrs)
template_widget = UnfoldAdminTextInputWidget()
template_widget.name = name

context.update({"template": template_widget})
context.update(
{"template": self.get_widget_instance().get_context(name, "", {})["widget"]}
)
return context

def value_from_datadict(
Expand Down Expand Up @@ -71,12 +82,12 @@ def _resolve_widgets(self, value: Optional[Union[List, str]]) -> None:
value = []

elif isinstance(value, List):
self.widgets = [self.widget_class for item in value]
self.widgets = [self.get_widget_instance() for item in value]
else:
self.widgets = [self.widget_class for item in value.split(",")]
self.widgets = [self.get_widget_instance() for item in value.split(",")]

self.widgets_names = ["" for i in range(len(self.widgets))]
self.widgets = [w() if isinstance(w, type) else w for w in self.widgets]
self.widgets = [w if isinstance(w, type) else w for w in self.widgets]


class WysiwygWidget(Widget):
Expand Down

0 comments on commit 195ee2d

Please sign in to comment.