Support component create methods as keys in global styles (#2498)
This commit is contained in:
parent
80c9eb34e4
commit
5176a7cb14
@ -2,7 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from typing import Any, Type
|
from typing import Any, Callable, Type
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from pydantic.fields import ModelField
|
from pydantic.fields import ModelField
|
||||||
@ -290,7 +290,7 @@ def create_theme(style: ComponentStyle) -> dict:
|
|||||||
The base style for the app.
|
The base style for the app.
|
||||||
"""
|
"""
|
||||||
# Get the global style from the style dict.
|
# Get the global style from the style dict.
|
||||||
style_rules = Style({k: v for k, v in style.items() if not isinstance(k, type)})
|
style_rules = Style({k: v for k, v in style.items() if not isinstance(k, Callable)})
|
||||||
|
|
||||||
root_style = {
|
root_style = {
|
||||||
# Root styles.
|
# Root styles.
|
||||||
|
@ -115,7 +115,7 @@ class BaseComponent(Base, ABC):
|
|||||||
|
|
||||||
|
|
||||||
# Map from component to styling.
|
# Map from component to styling.
|
||||||
ComponentStyle = Dict[Union[str, Type[BaseComponent]], Any]
|
ComponentStyle = Dict[Union[str, Type[BaseComponent], Callable], Any]
|
||||||
ComponentChild = Union[types.PrimitiveType, Var, BaseComponent]
|
ComponentChild = Union[types.PrimitiveType, Var, BaseComponent]
|
||||||
|
|
||||||
|
|
||||||
@ -600,10 +600,13 @@ class Component(BaseComponent, ABC):
|
|||||||
Returns:
|
Returns:
|
||||||
The component with the additional style.
|
The component with the additional style.
|
||||||
"""
|
"""
|
||||||
|
component_style = None
|
||||||
if type(self) in style:
|
if type(self) in style:
|
||||||
# Extract the style for this component.
|
# Extract the style for this component.
|
||||||
component_style = Style(style[type(self)])
|
component_style = Style(style[type(self)])
|
||||||
|
if self.create in style:
|
||||||
|
component_style = Style(style[self.create])
|
||||||
|
if component_style is not None:
|
||||||
# Only add style props that are not overridden.
|
# Only add style props that are not overridden.
|
||||||
component_style = {
|
component_style = {
|
||||||
k: v for k, v in component_style.items() if k not in self.style
|
k: v for k, v in component_style.items() if k not in self.style
|
||||||
|
@ -704,7 +704,6 @@ class PyiGenerator:
|
|||||||
|
|
||||||
def _write_pyi_file(self, module_path: Path, source: str):
|
def _write_pyi_file(self, module_path: Path, source: str):
|
||||||
relpath = str(_relative_to_pwd(module_path)).replace("\\", "/")
|
relpath = str(_relative_to_pwd(module_path)).replace("\\", "/")
|
||||||
print(f"Writing {relpath}")
|
|
||||||
pyi_content = [
|
pyi_content = [
|
||||||
f'"""Stub file for {relpath}"""',
|
f'"""Stub file for {relpath}"""',
|
||||||
"# ------------------- DO NOT EDIT ----------------------",
|
"# ------------------- DO NOT EDIT ----------------------",
|
||||||
|
@ -268,6 +268,23 @@ def test_add_style(component1, component2):
|
|||||||
assert c2.style["color"] == "black"
|
assert c2.style["color"] == "black"
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_style_create(component1, component2):
|
||||||
|
"""Test that adding style works with the create method.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
component1: A test component.
|
||||||
|
component2: A test component.
|
||||||
|
"""
|
||||||
|
style = {
|
||||||
|
component1.create: Style({"color": "white"}),
|
||||||
|
component2.create: Style({"color": "black"}),
|
||||||
|
}
|
||||||
|
c1 = component1().add_style(style) # type: ignore
|
||||||
|
c2 = component2().add_style(style) # type: ignore
|
||||||
|
assert c1.style["color"] == "white"
|
||||||
|
assert c2.style["color"] == "black"
|
||||||
|
|
||||||
|
|
||||||
def test_get_imports(component1, component2):
|
def test_get_imports(component1, component2):
|
||||||
"""Test getting the imports of a component.
|
"""Test getting the imports of a component.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user