xnano.components.dropdown
xnano.components.dropdown
¶
xnano.components.dropdown
Choose an item from a collapsible, searchable list.
Classes:
-
Dropdown–Collapsible choice list.
Dropdown
dataclass
¶
Dropdown(
items: Sequence[OptionItem] = (),
query: str = "",
filter: FilterMode = "fuzzy",
accept: AcceptPolicy = "replace",
searchable: bool = True,
selected: int = 0,
direction: OptionsDirection = "top_to_bottom",
foreground: ColorLike | None = None,
background: ColorLike | None = None,
highlight_color: ColorLike = "black",
highlight_background: ColorLike = "white",
highlight_symbol: str = "> ",
hovered: int | None = None,
hover_symbol: str = "· ",
match_color: ColorLike | None = "cyan",
repeat_highlight_symbol: bool = False,
focusable: bool = True,
passthrough: Sequence[str] = (),
open: bool = False,
placeholder: str | Any | None = None,
max_visible: int | None = None,
close_on_select: bool = True,
open_keys: Sequence[str] = ("enter", "space", "down"),
close_keys: Sequence[str] = ("enter", "escape"),
*,
visible: bool = True,
z: int = 0,
fit_content: bool = True
)
Bases: Options
Collapsible choice list.
When closed, the dropdown shows the selected label or placeholder. When open, users can search and move through the available options.
class Form(BaseGrid):
theme: Dropdown = Field(
default=Dropdown(
items=("dark", "light", "system"),
placeholder="pick a theme",
),
)
@on_keyboard("enter")
def _choose(self, ctx: Context) -> None:
if not self.theme.open:
apply_theme(self.theme.value)
Example
Dropdown(items=("dark", "light"), placeholder="Choose a theme")
Attributes:
-
open(bool) –Whether the options list is expanded.
-
placeholder(str | Any | None) –Closed-row text when nothing is selected.
-
max_visible(int | None) –Cap on open-list rows around the selection.
-
close_on_select(bool) –Close after enter accepts the selection.
-
open_keys(Sequence[str]) –Bindings that open a closed dropdown.
-
close_keys(Sequence[str]) –Bindings that close an open dropdown (enter also accepts the current selection first).
Methods:
-
handle_keyboard–Open, close, search, or move through the dropdown.
-
compose–Compose closed label or open options content.
-
component_post_init–Initialize subclass state after dataclass fields are assigned.
-
get_frame–Optional frame/panel chrome around composed content.
-
get_size–Return the preferred cell size of this component.
-
before_render–Called before rendering; returns the effective render area.
-
after_render–Record the painted area so hover can map a pointer to a row.
-
compose_extra_small–Compose content when the viewport is extra small (< 40 cols).
-
compose_small–Compose content when the viewport is small (40–79 cols).
-
compose_medium–Compose content when the viewport is medium (80–119 cols).
-
compose_large–Compose content when the viewport is large (120–159 cols).
-
compose_extra_large–Compose content when the viewport is extra large (>= 160 cols).
-
handle_paste–Optional paste handler while focused.
-
select_value–Select the visible item whose stored value equals
value. -
resolve_submission–Reconcile typed composer text with the selection per
accept. -
move–Move
selectedbydelta, skipping disabled entries. -
select–Set
selectedtoindexwithin the filtered view. -
clear_query–Clear the filter query and reset selection to the first row.
-
handle_hover–Mark the row under the pointer as hovered (mouse hover).
searchable
class-attribute
instance-attribute
¶
searchable: bool = True
Whether typing while open edits query.
A dropdown is a search box when expanded, so unlike a plain Options
list it defaults to True; set False for a pick-only dropdown.
placeholder
class-attribute
instance-attribute
¶
Closed-row text when nothing is selected (string or Text).
max_visible
class-attribute
instance-attribute
¶
max_visible: int | None = None
Cap on open-list rows around the selection; None shows all.
close_on_select
class-attribute
instance-attribute
¶
close_on_select: bool = True
Close after enter accepts the current selection.
open_keys
class-attribute
instance-attribute
¶
Bindings that open a closed dropdown.
close_keys
class-attribute
instance-attribute
¶
Bindings that close an open dropdown.
visible
class-attribute
instance-attribute
¶
visible: bool = dataclasses.field(
default=True, kw_only=True
)
Whether this component paints at all.
z
class-attribute
instance-attribute
¶
z: int = dataclasses.field(default=0, kw_only=True)
Stacking order among sibling content.
fit_content
class-attribute
instance-attribute
¶
fit_content: bool = dataclasses.field(
default=True, kw_only=True
)
When True, paint at natural size inside a larger slot.
items
class-attribute
instance-attribute
¶
items: Sequence[OptionItem] = ()
Entries to pick from (plain strings, Text, or Option).
query
class-attribute
instance-attribute
¶
query: str = ''
Filter text. Edited by typing while focused when searchable;
assign it from a hook to filter reactively.
filter
class-attribute
instance-attribute
¶
filter: FilterMode = 'fuzzy'
How query narrows the visible items — see FilterMode.
"fuzzy"/True fuzzy-match, "prefix" prefix-match, "none"/
False show everything (for externally filtered items), or a
(query, item_text) -> bool callable.
accept
class-attribute
instance-attribute
¶
accept: AcceptPolicy = 'replace'
How resolve_submission reconciles typed text with the selection.
selected
class-attribute
instance-attribute
¶
selected: int = 0
Selection index within the filtered view.
direction
class-attribute
instance-attribute
¶
direction: OptionsDirection = 'top_to_bottom'
Visual order of option rows in the list.
foreground
class-attribute
instance-attribute
¶
foreground: ColorLike | None = None
Default foreground color for unselected rows (deprecated alias:
color).
background
class-attribute
instance-attribute
¶
background: ColorLike | None = None
Default background color for unselected rows.
highlight_color
class-attribute
instance-attribute
¶
highlight_color: ColorLike = 'black'
Foreground of the selected row.
highlight_background
class-attribute
instance-attribute
¶
highlight_background: ColorLike = 'white'
Background of the selected row.
highlight_symbol
class-attribute
instance-attribute
¶
highlight_symbol: str = '> '
Symbol prepended to the selected row.
hovered
class-attribute
instance-attribute
¶
hovered: int | None = None
Row under the pointer (filtered-view index), or None.
Set by :meth:handle_hover when mouse events are enabled; drawn as a
dim hover_symbol so it reads as distinct from the selection.
hover_symbol
class-attribute
instance-attribute
¶
hover_symbol: str = '· '
Symbol prepended to the hovered row (dim), distinct from selection.
match_color
class-attribute
instance-attribute
¶
match_color: ColorLike | None = 'cyan'
Emphasis color for characters matched by query.
repeat_highlight_symbol
class-attribute
instance-attribute
¶
repeat_highlight_symbol: bool = False
When True, every row shows the highlight symbol.
focusable
class-attribute
instance-attribute
¶
focusable: bool = True
Whether this Options participates in field focus (tab order).
passthrough
class-attribute
instance-attribute
¶
Key bindings this list never captures while focused.
owns_cursor
class-attribute
instance-attribute
¶
owns_cursor: bool = dataclasses.field(
default=True, init=False
)
Whether selection replaces the hardware caret.
filtered
property
¶
Indices into items currently visible, in display order.
Use this with visible_items to inspect the current filtered view.
visible_items
property
¶
Plain text of the currently visible items, in display order.
selected_item
property
¶
selected_item: OptionItem | None
The selected item object, or None when the view is empty.
selected_value
property
¶
selected_value: Any | None
Stable stored value of the selection (alias of value).
Use with select_value to preserve a selection across item
rebuilds by value/id rather than by fragile filtered index.
selected_label
property
¶
selected_label: str
Plain display text of the selected item, or "" when empty.
handle_keyboard
¶
handle_keyboard(keyboard: 'KeyboardEventData') -> bool
Open, close, search, or move through the dropdown.
Opening keys expand a closed dropdown. Escape closes it without
changing the selection. Enter accepts the selected value and closes
it when close_on_select is enabled.
Parameters:
-
keyboard('KeyboardEventData') –The keyboard event payload.
Returns:
-
bool–Truewhen the event was consumed.
Source code in xnano/components/dropdown.py
compose
¶
compose(ctx: 'ComponentRenderContext') -> Any
Compose closed label or open options content.
Parameters:
-
ctx('ComponentRenderContext') –Render-time scope for this paint.
Returns:
-
Closed(Any) –a single
TextBlockrow. Open: the same content -
Any–tree
Optionswould produce, optionally windowed.
Source code in xnano/components/dropdown.py
component_post_init
¶
Initialize subclass state after dataclass fields are assigned.
Override this method instead of __post_init__.
get_size
¶
get_size(ctx: ComponentRenderContext[StateT]) -> Size
before_render
¶
before_render(
ctx: ComponentRenderContext[StateT], area: "Area"
) -> "Area"
compose_extra_small
¶
compose_extra_small(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is extra small (< 40 cols).
Optional responsive counterpart to :meth:compose. When
overridden, it is used instead of compose while the window
is in this size tier. Overriding any compose_* variant opts the
component into breakpoint dispatch; a component that overrides none
pays no per-frame cost.
Source code in xnano/components/component.py
compose_small
¶
compose_small(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is small (40–79 cols).
See :meth:compose_extra_small.
compose_medium
¶
compose_medium(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is medium (80–119 cols).
See :meth:compose_extra_small.
compose_large
¶
compose_large(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is large (120–159 cols).
See :meth:compose_extra_small.
compose_extra_large
¶
compose_extra_large(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is extra large (>= 160 cols).
See :meth:compose_extra_small.
Source code in xnano/components/component.py
handle_paste
¶
select_value
¶
Select the visible item whose stored value equals value.
Returns True when a match is found. The stable way to keep a
selection pinned across item rebuilds: store the value, rebuild
items, then re-select by value.
Source code in xnano/components/options.py
resolve_submission
¶
Reconcile typed composer text with the selection per accept.
Removes the userland "did they Tab-complete this row or type past
it?" logic. See AcceptPolicy for the per-policy behavior.
Source code in xnano/components/options.py
move
¶
move(delta: int) -> None
Move selected by delta, skipping disabled entries.
Movement stays within the currently filtered choices.
Parameters:
-
delta(int) –Steps to move; negative moves toward the start.
Source code in xnano/components/options.py
select
¶
select(index: int) -> None
Set selected to index within the filtered view.
Disabled entries are rejected; the selection is left unchanged
when index points at a disabled item. Out-of-range indices
are clamped.
Parameters:
-
index(int) –Target index in the filtered view.
Source code in xnano/components/options.py
clear_query
¶
handle_hover
¶
Mark the row under the pointer as hovered (mouse hover).
Hover is a distinct indicator from the selection: the hovered row
is set here and drawn with a dim hover_symbol. Moving off the
rows clears it. Returns whether the hovered row changed.