-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Every stateful component needs an initial value for .rx and state seeding, but they all spell it differently: value (Input, Slider, Textarea, Calendar), checked (Checkbox, Switch), default_value/defaultValue (Tabs, Pages). This means StatefulMixin has a _get_initial_value() method that every subclass overrides with a one-liner returning self.value or self.checked or self.default_value.
Instead, StatefulMixin should define value: Any = None directly. Subclasses narrow the type:
class Slider(StatefulMixin, Component):
value: float | list[float] | None = Field(default=None)
class Checkbox(StatefulMixin, Component):
value: bool = Field(default=False, alias="checked")Pydantic v2 handles field narrowing, alias overrides, and validation_alias=AliasChoices(...) for accepting multiple kwarg names. Friendly attributes like .checked and .default_value become simple properties over self.value. _get_initial_value() goes away — render_previews.py just reads .value.
Protocol change: Align the protocol too — defaultValue on Tabs/Pages becomes value, checked on Checkbox/Switch becomes value. One field name across all stateful components, both Python and protocol side.