Skip to content

Commit

Permalink
fix: widget css classes (#997)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav authored Jan 23, 2025
1 parent 386f94d commit b9ea7df
Showing 1 changed file with 146 additions and 27 deletions.
173 changes: 146 additions & 27 deletions src/unfold/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,26 +256,53 @@

class UnfoldAdminTextInputWidget(AdminTextInputWidget):
def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:
super().__init__(attrs={"class": " ".join(INPUT_CLASSES), **(attrs or {})})
super().__init__(
attrs={
"class": " ".join(
[*INPUT_CLASSES, attrs.get("class", "") if attrs else ""]
),
**(attrs or {}),
}
)


class UnfoldAdminURLInputWidget(AdminURLFieldWidget):
template_name = "unfold/widgets/url.html"

def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:
super().__init__(attrs={"class": " ".join(INPUT_CLASSES), **(attrs or {})})
super().__init__(
attrs={
"class": " ".join(
[*INPUT_CLASSES, attrs.get("class", "") if attrs else ""]
),
**(attrs or {}),
}
)


class UnfoldAdminColorInputWidget(AdminTextInputWidget):
def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:
super().__init__(
attrs={"type": "color", "class": " ".join(COLOR_CLASSES), **(attrs or {})}
attrs={
"type": "color",
"class": " ".join(
[*COLOR_CLASSES, attrs.get("class", "") if attrs else ""]
),
**(attrs or {}),
}
)


class UnfoldAdminUUIDInputWidget(AdminUUIDInputWidget):
def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:
super().__init__(attrs={"class": " ".join(INPUT_CLASSES), **(attrs or {})})
super().__init__(
attrs={
"class": " ".join(
[*INPUT_CLASSES, attrs.get("class", "") if attrs else ""]
),
**(attrs or {}),
}
)


class UnfoldAdminIntegerRangeWidget(MultiWidget):
Expand All @@ -285,7 +312,9 @@ def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:
if attrs is None:
attrs = {}

attrs["class"] = " ".join(INPUT_CLASSES)
attrs["class"] = " ".join(
[*INPUT_CLASSES, attrs.get("class", "") if attrs else ""]
)

_widgets = (NumberInput(attrs=attrs), NumberInput(attrs=attrs))

Expand All @@ -299,7 +328,14 @@ def decompress(self, value: Union[str, None]) -> Tuple[Optional[Callable], ...]:

class UnfoldAdminEmailInputWidget(AdminEmailInputWidget):
def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:
super().__init__(attrs={"class": " ".join(INPUT_CLASSES), **(attrs or {})})
super().__init__(
attrs={
"class": " ".join(
[*INPUT_CLASSES, attrs.get("class", "") if attrs else ""]
),
**(attrs or {}),
}
)


class FileFieldMixin:
Expand Down Expand Up @@ -341,7 +377,13 @@ def __init__(
self, attrs: Optional[Dict[str, Any]] = None, format: Optional[str] = None
) -> None:
attrs = {
"class": "vDateField " + " ".join(DATETIME_CLASSES),
"class": " ".join(
[
"vDateField",
*DATETIME_CLASSES,
attrs.get("class", "") if attrs else "",
]
),
"size": "10",
**(attrs or {}),
}
Expand All @@ -355,7 +397,13 @@ def __init__(
self, attrs: Optional[Dict[str, Any]] = None, format: Optional[str] = None
) -> None:
attrs = {
"class": "vDateField " + " ".join(DATETIME_CLASSES),
"class": " ".join(
[
"vDateField",
*DATETIME_CLASSES,
attrs.get("class", "") if attrs else "",
]
),
"size": "10",
**(attrs or {}),
}
Expand All @@ -369,7 +417,13 @@ def __init__(
self, attrs: Optional[Dict[str, Any]] = None, format: Optional[str] = None
) -> None:
attrs = {
"class": "vTimeField " + " ".join(DATETIME_CLASSES),
"class": " ".join(
[
"vTimeField",
*DATETIME_CLASSES,
attrs.get("class", "") if attrs else "",
]
),
"size": "8",
**(attrs or {}),
}
Expand All @@ -383,7 +437,13 @@ def __init__(
self, attrs: Optional[Dict[str, Any]] = None, format: Optional[str] = None
) -> None:
attrs = {
"class": "vTimeField " + " ".join(DATETIME_CLASSES),
"class": " ".join(
[
"vTimeField",
*DATETIME_CLASSES,
attrs.get("class", "") if attrs else "",
]
),
"size": "8",
**(attrs or {}),
}
Expand All @@ -398,7 +458,13 @@ def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:

super().__init__(
attrs={
"class": "vLargeTextField " + " ".join(TEXTAREA_CLASSES),
"class": " ".join(
[
"vLargeTextField",
*TEXTAREA_CLASSES,
attrs.get("class", "") if attrs else "",
]
),
**(attrs or {}),
}
)
Expand All @@ -414,10 +480,14 @@ def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:

super().__init__(
attrs={
"class": "vLargeTextField "
+ " ".join(TEXTAREA_CLASSES)
+ " "
+ " ".join(TEXTAREA_EXPANDABLE_CLASSES),
"class": " ".join(
[
"vLargeTextField",
*TEXTAREA_CLASSES,
*TEXTAREA_EXPANDABLE_CLASSES,
attrs.get("class", "") if attrs else "",
]
),
**(attrs or {}),
}
)
Expand Down Expand Up @@ -471,25 +541,48 @@ def get_context(

class UnfoldAdminIntegerFieldWidget(AdminIntegerFieldWidget):
def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:
super().__init__(attrs={"class": " ".join(INPUT_CLASSES), **(attrs or {})})
super().__init__(
attrs={
"class": " ".join(
[*INPUT_CLASSES, attrs.get("class", "") if attrs else ""]
),
**(attrs or {}),
}
)


class UnfoldAdminDecimalFieldWidget(AdminIntegerFieldWidget):
def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:
super().__init__(attrs={"class": " ".join(INPUT_CLASSES), **(attrs or {})})
super().__init__(
attrs={
"class": " ".join(
[*INPUT_CLASSES, attrs.get("class", "") if attrs else ""]
),
**(attrs or {}),
}
)


class UnfoldAdminBigIntegerFieldWidget(AdminBigIntegerFieldWidget):
def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:
super().__init__(attrs={"class": " ".join(INPUT_CLASSES), **(attrs or {})})
super().__init__(
attrs={
"class": " ".join(
[*INPUT_CLASSES, attrs.get("class", "") if attrs else ""]
),
**(attrs or {}),
}
)


class UnfoldAdminNullBooleanSelectWidget(NullBooleanSelect):
def __init__(self, attrs=None):
if attrs is None:
attrs = {}

attrs["class"] = " ".join(SELECT_CLASSES)
attrs["class"] = " ".join(
[*SELECT_CLASSES, attrs.get("class", "") if attrs else ""]
)
super().__init__(attrs)


Expand All @@ -498,7 +591,9 @@ def __init__(self, attrs=None, choices=()):
if attrs is None:
attrs = {}

attrs["class"] = " ".join([*SELECT_CLASSES, attrs.get("class", "")])
attrs["class"] = " ".join(
[*SELECT_CLASSES, attrs.get("class", "") if attrs else ""]
)
super().__init__(attrs, choices)


Expand All @@ -507,7 +602,9 @@ def __init__(self, attrs=None, choices=()):
if attrs is None:
attrs = {}

attrs["class"] = " ".join([*SELECT_CLASSES, attrs.get("class", "")])
attrs["class"] = " ".join(
[*SELECT_CLASSES, attrs.get("class", "") if attrs else ""]
)
super().__init__(attrs, choices)


Expand Down Expand Up @@ -537,7 +634,9 @@ class UnfoldAdminCheckboxSelectMultiple(CheckboxSelectMultiple):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.attrs = {"class": " ".join(CHECKBOX_CLASSES)}
self.attrs = {
"class": " ".join([*CHECKBOX_CLASSES, self.attrs.get("class", "")])
}


try:
Expand Down Expand Up @@ -575,7 +674,9 @@ def __init__(
super().__init__(
{
**(attrs or {}),
"class": " ".join(CHECKBOX_CLASSES + [attrs.get("class", "")]),
"class": " ".join(
[*CHECKBOX_CLASSES, attrs.get("class", "") if attrs else ""]
),
},
check_test,
)
Expand All @@ -586,7 +687,13 @@ def __init__(
self, attrs: Optional[Dict[str, Any]] = None, check_test: Callable = None
) -> None:
super().__init__(
attrs={"class": " ".join(SWITCH_CLASSES), **(attrs or {})}, check_test=None
attrs={
"class": " ".join(
[*SWITCH_CLASSES, attrs.get("class", "") if attrs else ""]
),
**(attrs or {}),
},
check_test=None,
)


Expand All @@ -605,14 +712,26 @@ def __init__(
using: Optional[Any] = None,
) -> None:
attrs = {
"class": " ".join(["vForeignKeyRawIdAdminField"] + INPUT_CLASSES),
**(attrs or {}),
"class": " ".join(
[
"vForeignKeyRawIdAdminField",
*INPUT_CLASSES,
attrs.get("class", "") if attrs else "",
]
)
** (attrs or {}),
}
super().__init__(rel, admin_site, attrs, using)


class UnfoldAdminPasswordInput(PasswordInput):
def __init__(self, attrs=None, render_value=False):
super().__init__(
{"class": " ".join(INPUT_CLASSES), **(attrs or {})}, render_value
{
"class": " ".join(
[*INPUT_CLASSES, attrs.get("class", "") if attrs else ""]
),
**(attrs or {}),
},
render_value,
)

0 comments on commit b9ea7df

Please sign in to comment.