Skip to content

Radio

Overview

Radio is a single selection form control based on the Angular Material radio component.

Examples

import mesop as me


@me.stateclass
class State:
  radio_value: str = "2"


def on_change(event: me.RadioChangeEvent):
  s = me.state(State)
  s.radio_value = event.value


@me.page(
  security_policy=me.SecurityPolicy(
    allowed_iframe_parents=["https://google.github.io"]
  ),
  path="/radio",
)
def app():
  s = me.state(State)
  me.text("Horizontal radio options")
  me.radio(
    on_change=on_change,
    options=[
      me.RadioOption(label="Option 1", value="1"),
      me.RadioOption(label="Option 2", value="2"),
    ],
    value=s.radio_value,
  )
  me.text(text="Selected radio value: " + s.radio_value)

API

radio

Creates a Radio component.

PARAMETER DESCRIPTION
options

List of radio options

TYPE: Iterable[RadioOption] DEFAULT: ()

on_change

Event emitted when the group value changes. Change events are only emitted when the value changes due to user interaction with a radio button (the same behavior as <input type-"radio">).

TYPE: Callable[[RadioChangeEvent], Any] | None DEFAULT: None

color

Theme color for all of the radio buttons in the group.

TYPE: Literal['primary', 'accent', 'warn'] | None DEFAULT: None

label_position

Whether the labels should appear after or before the radio-buttons. Defaults to 'after'

TYPE: Literal['before', 'after'] DEFAULT: 'after'

value

Value for the radio-group. Should equal the value of the selected radio button if there is a corresponding radio button with a matching value.

TYPE: str DEFAULT: ''

disabled

Whether the radio group is disabled.

TYPE: bool DEFAULT: False

style

Style for the component.

TYPE: Style | None DEFAULT: None

key

The component key.

TYPE: str | None DEFAULT: None

RadioOption dataclass

ATTRIBUTE DESCRIPTION
label

Content to show for the radio option

TYPE: str | None

value

The value of this radio button.

TYPE: str | None

RadioChangeEvent dataclass

Bases: MesopEvent

Event representing a change in the radio component's value.

ATTRIBUTE DESCRIPTION
value

The new value of the radio component after the change.

TYPE: str

key

key of the component that emitted this event.

TYPE: str