feat: Add dynamic component (#627)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Juro Oravec 2024-08-29 11:28:00 +02:00 committed by GitHub
parent 8c5b088c31
commit e76227b8df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 503 additions and 34 deletions

View file

@ -205,14 +205,17 @@ class Component(Generic[ArgsType, KwargsType, DataType, SlotsType], metaclass=Co
return self.registered_name or self.__class__.__name__
@property
def input(self) -> Optional[RenderInput[ArgsType, KwargsType, SlotsType]]:
def input(self) -> RenderInput[ArgsType, KwargsType, SlotsType]:
"""
Input holds the data (like arg, kwargs, slots) that were passsed to
the current execution of the `render` method.
"""
if not len(self._render_stack):
raise RuntimeError(f"{self.name}: Tried to access Component input while outside of rendering execution")
# NOTE: Input is managed as a stack, so if `render` is called within another `render`,
# the propertes below will return only the inner-most state.
return self._render_stack[-1] if len(self._render_stack) else None
return self._render_stack[-1]
def get_context_data(self, *args: Any, **kwargs: Any) -> DataType:
return cast(DataType, {})
@ -526,6 +529,8 @@ class Component(Generic[ArgsType, KwargsType, DataType, SlotsType], metaclass=Co
component_name=self.name,
context_data=slot_context_data,
fill_content=fill_content,
# Dynamic component has a special mark do it doesn't raise certain errors
is_dynamic_component=getattr(self, "_is_dynamic_component", False),
)
# Available slot fills - this is internal to us