-
-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…3588) * move props parsing into separate class derived from dict * replace use of old `_parse_props` * tiny fix * extend principle to classes and style * fix linting errors in query.py * fix `ui.tree` and `ui.menu` * remove obsolete pylint markers * parameterize parsing tests * use new API where possible
- Loading branch information
1 parent
ed6a955
commit 392e0d0
Showing
20 changed files
with
378 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import TYPE_CHECKING, Generic, List, Optional, TypeVar | ||
|
||
if TYPE_CHECKING: | ||
from .element import Element | ||
|
||
T = TypeVar('T', bound='Element') | ||
|
||
|
||
class Classes(list, Generic[T]): | ||
|
||
def __init__(self, *args, element: T, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
self.element = element | ||
|
||
def __call__(self, | ||
add: Optional[str] = None, *, | ||
remove: Optional[str] = None, | ||
replace: Optional[str] = None) -> T: | ||
"""Apply, remove, or replace HTML classes. | ||
This allows modifying the look of the element or its layout using `Tailwind <https://tailwindcss.com/>`_ or `Quasar <https://quasar.dev/>`_ classes. | ||
Removing or replacing classes can be helpful if predefined classes are not desired. | ||
:param add: whitespace-delimited string of classes | ||
:param remove: whitespace-delimited string of classes to remove from the element | ||
:param replace: whitespace-delimited string of classes to use instead of existing ones | ||
""" | ||
new_classes = self.update_list(self, add, remove, replace) | ||
if self != new_classes: | ||
self[:] = new_classes | ||
self.element.update() | ||
return self.element | ||
|
||
@staticmethod | ||
def update_list(classes: List[str], | ||
add: Optional[str] = None, | ||
remove: Optional[str] = None, | ||
replace: Optional[str] = None) -> List[str]: | ||
"""Update a list of classes.""" | ||
class_list = classes if replace is None else [] | ||
class_list = [c for c in class_list if c not in (remove or '').split()] | ||
class_list += (add or '').split() | ||
class_list += (replace or '').split() | ||
return list(dict.fromkeys(class_list)) # NOTE: remove duplicates while preserving order |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.