duck.html.components.select¶
Select HTML Component.
This module provides reusable Select and Option components for creating dropdown menus in HTML.
Module Contents¶
Classes¶
Represents an individual option within a |
|
A reusable HTML |
API¶
- class duck.html.components.select.Option(element: Optional[str] = None, properties: Optional[Dict[str, str]] = None, props: Optional[Dict[str, str]] = None, style: Optional[Dict[str, str]] = None, inner_html: Optional[Union[str, str, float]] = None, children: Optional[List[duck.html.components.HtmlComponent]] = None, **kwargs)[source]¶
Bases:
duck.html.components.InnerComponentRepresents an individual option within a
Selectdropdown.This component is used to define selectable items inside a
Selectcomponent.- Parameters:
text – The option’s display text (or pass
inner_htmldirectly).value – Optional. The option’s
valueattribute, if it differs from its text.selected – Optional. Whether this option is selected by default.
Example Usage:
option = Option(inner_html="Option 1") select.add_child(option)This generates:
<option>Option 1</option>Returns: - An
<option>HTML element.Initialization
Initialize an HTML component.
- Parameters:
element – The HTML element tag name (e.g., textarea, input, button). Can be None, but make sure element is returned by get_element method.
accept_inner_html – Whether the HTML component accepts an inner body (e.g., inner-body-here).
inner_html – Inner html to add to the HTML component. Defaults to None.
properties – Dictionary for properties to initialize the component with.
props – Just same as properties argument (added for simplicity).
style – Dictionary for style to initialize the component with.
event_handlers – Events to automatically bind. Each dictionary maps an event name to its handler and may include additional keyword arguments forwarded to
bind(), e.g.event_handlers=[{"click": on_button_click, **extra_kwargs}].**kwargs – Extra keyword arguments
- Raises:
HtmlComponentError – If ‘element’ is not a string or ‘inner_html’ is set but ‘accept_inner_html’ is False.
- class duck.html.components.select.Select(element: Optional[str] = None, properties: Optional[Dict[str, str]] = None, props: Optional[Dict[str, str]] = None, style: Optional[Dict[str, str]] = None, inner_html: Optional[Union[str, str, float]] = None, children: Optional[List[duck.html.components.HtmlComponent]] = None, **kwargs)[source]¶
Bases:
duck.html.components.InnerComponentA reusable HTML
<select>component for creating dropdown menus.This component generates a customizable
<select>dropdown with options.- Parameters:
name – Optional. The
nameattribute for the select field.options – Optional. A list of options, where each item is either a string/int/float (used as the option text), a dict of Option constructor kwargs, or an
Optioncomponent instance.
Styling:
Uses default styling based on the
Theme.currentclass, falling back to sensible defaults when the active theme doesn’t define them.Can be overridden per-instance via the
stylekwarg, or with CSS.
Initialization
Initialize an HTML component.
- Parameters:
element – The HTML element tag name (e.g., textarea, input, button). Can be None, but make sure element is returned by get_element method.
accept_inner_html – Whether the HTML component accepts an inner body (e.g., inner-body-here).
inner_html – Inner html to add to the HTML component. Defaults to None.
properties – Dictionary for properties to initialize the component with.
props – Just same as properties argument (added for simplicity).
style – Dictionary for style to initialize the component with.
event_handlers – Events to automatically bind. Each dictionary maps an event name to its handler and may include additional keyword arguments forwarded to
bind(), e.g.event_handlers=[{"click": on_button_click, **extra_kwargs}].**kwargs – Extra keyword arguments
- Raises:
HtmlComponentError – If ‘element’ is not a string or ‘inner_html’ is set but ‘accept_inner_html’ is False.
- DEFAULT_STYLE¶
None
- to_option(option) duck.html.components.select.Option[source]¶
Normalizes a raw option value into an
Optioncomponent.- Parameters:
option – A string/int/float, a two-item tuple/list of
(value, text), a dict of Option kwargs, or an existingOptioncomponent.- Returns:
An
Optioncomponent.- Raises:
ComponentError – If
optionisn’t one of the supported types, or a tuple/list isn’t exactly two items.