Skip to content

ChoiceEditor

Base classes

Name Children Inherits
Editor
promptantic.textual.editors
Base of all editors: a node, its path in the value, and an error line.

β‹” Inheritance diagram

graph TD
  436348671["editors.ChoiceEditor"]
  2997256436["editors.Editor"]
  4188532303["widget.Widget"]
  3946681048["dom.DOMNode"]
  53685856["message_pump.MessagePump"]
  2751680898["builtins.object"]
  2997256436 --> 436348671
  4188532303 --> 2997256436
  3946681048 --> 4188532303
  53685856 --> 3946681048
  2751680898 --> 53685856

πŸ›ˆ DocStrings

Bases: Editor

A select of the allowed values; just the value when there's only one (const).

Source code in src/promptantic/textual/editors.py
class ChoiceEditor(Editor):
    """A select of the allowed values; just the value when there's only one (``const``)."""

    node: ChoiceNode

    def compose(self) -> ComposeResult:
        choices = self.node.choices
        if self.node.fixed:
            yield Label(choices[0].label, classes="pa-fixed")
        else:
            values = [choice.value for choice in choices]
            index = values.index(self.initial) if self.initial in values else Select.NULL
            yield Select(
                [(choice.label, i) for i, choice in enumerate(choices)],
                value=index,
                prompt="(none)" if self.node.nullable else "Select…",
                compact=True,
            )
        yield self.errors_widget()

    def get_value(self) -> Any | Unset:
        if self.node.fixed:
            return self.node.choices[0].value
        index = self.query_one(Select).value
        if index is Select.NULL:
            return None if self.node.nullable else UNSET
        return self.node.choices[cast("int", index)].value

    def on_select_changed(self, event: Select.Changed) -> None:
        event.stop()
        self.check()

:fa-brands-github: Show source on GitHub