edifice.component#
- edifice.component(f)[source]#
Decorator which turns a render function of props into an
Element.The first argument of the render function must be
self, for internal technical reasons.- Return type:
Callable[[ParamSpec(P)],Element]
Props#
The props are the arguments passed to the
@componentfunction.The
@componentwill be re-rendered when some of its props are not__eq__to the props from the last time the@componentrendered. If the props are all__eq__, the@componentwill not re-render.Arguments with a leading underscore
_will not count as props. They are normal function arguments, and changing their value will not cause the@componentto re-render.Render#
The @component function must render exactly one
Element. In the @component function, declare a tree ofElementwith a single root.a tree with a single root#@component def MySimpleComp(self, prop1, prop2, prop3): with VBoxView(): Label(text=prop1) Label(text=prop2) Label(text=prop3)
State#
The
@componentfunction is a stateless function from props to anElement. To introduce state into a@component, use Hooks.Composition#
An
@component’s children can be passed to it as props. This allows a@componentto act as a parent container for otherElements, and to render them in a specific layout.There are two features to accomplish this.
The special
childrenprop is a tuple ofElements. The specialchildrenprop must be declared as a keyword argument with a default empty tuple, like thischildren:tuple[Element, ...]=().Do not explicitly pass the
childrenprop when calling the@component. The children will be passed implicitly.The
child_place()function is used to place thechildrenin the parent@component’s render function.
With these two features, you can declare how the parent container
@componentwill render its children.Example ContainerComponent with children props#@component def ContainerComponent(self:Element, children:tuple[Element, ...]=()): with VBoxView(): for child in children: with VBoxView(): child_place(child)
Example ContainerComponent usage#with ContainerComponent(): Label(text="First Child") Label(text="Second Child") Label(text="Third Child")
- type f:
Callable[[Concatenate[TypeVar(selfT, bound=Element),ParamSpec(P)]],None]- param f:
The render function to wrap. Its first argument must be
self. Subsequent arguments are props.