Support component create methods as keys in global styles (#2498)

This commit is contained in:
Nikhil Rao 2024-02-01 03:25:18 +07:00 committed by GitHub
parent 80c9eb34e4
commit 5176a7cb14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 5 deletions

View File

@ -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.

View File

@ -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

View File

@ -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 ----------------------",

View File

@ -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.