Skip to content

Unify stateful components around a single value field #182

@jlowin

Description

@jlowin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImprovement to existing functionality or new capabilities.jsonRelated to the JSON protocol or component schema.pythonRelated to the Python SDK: components, actions, serialization.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions