Component | Radio Group
Render a radio group.
- TypeScript / JavaScript
- Python
ui.radioGroup("radio-group-key", ["A", "B", "C", "D"], {
label: "Select an option",
onChange: (value) => console.log(value)
})
ui.radio_group(
"radio-group-key",
["A", "B", "C", "D"],
label="Select an option",
on_change=lambda value: print(value)
)
Learn more about working with inputs.
Options
Select components support both a "basic" and "detailed" format for options.
- TypeScript / JavaScript
- Python
Basic
Quick and easy. Pass an array of strings, such as ["A", "B", "C", "D"]
.
ui.radioGroup("radio-group-key", ["A", "B", "C", "D"], {
label: "Select an option",
onChange: (value) => console.log(value)
})
ui.radio_group(
"radio-group-key",
["A", "B", "C", "D"],
label="Select an option",
on_change=lambda value: print(value)
)
Detailed
Use separate label
and value
fields for each option. The label
field will be displayed to the user, and the value
field will be passed to hooks such as onChange
and validate
.
const options = [
{ label: "Google", value: "GOOG" },
{ label: "Apple", value: "APPL" },
{
label: "Microsoft",
value: "MSFT",
// The detailed format also supports an optional "description" field
// to display beneath an option.
description: "Current stock price: $423"
}
]
ui.radioGroup("company-select", options, {
label: "Select a company",
onChange: (value) => console.log(value) // Will be "GOOG", "APPL", or "MSFT"
})
Use separate label
and value
fields for each option. The label
field will be displayed to the user, and the value
field will be passed to hooks such as on_change
and validate
.
options = [
{ "label": "Google", "value": "GOOG" },
{ "label": "Apple", "value": "APPL" },
{
"label": "Microsoft",
"value": "MSFT",
# The detailed format also supports an optional "description" field
# to display beneath an option.
"description": "Current stock price: $423"
}
]
ui.radio_group(
"company-select",
options,
label="Select an option",
on_change=lambda value: print(value) # Will be "GOOG", "APPL", or "MSFT"
)
Detailed option object properties
label
required stringlabel
required strvalue
required string | number | booleanThe internal value to assign to the option and use when submitting a form or executing callbacks such as onChange
.
value
required str | float | int | boolThe internal value to assign to the option and use when submitting a form or executing callbacks such as on_change
.
description
optional stringdescription
optional strAPI reference
Function signature
- TypeScript / JavaScript
- Python
type OptionValue = string | number | boolean
type DetailedOption = {
label: string,
value: OptionValue,
description?: string
}
ui.radioGroup(
id: string,
options: Array<OptionValue | DetailedOption>
properties: Partial<{
label: string,
description: string,
required: boolean,
initialValue: OptionValue | null,
validate: (value: OptionValue | null) => string | void,
onChange: (value: OptionValue | null) => void,
style: Style
}>
)
OptionValue = str | float | int | bool
class DetailedOption(TypedDict):
label: str
value: OptionValue
description: NotRequired[str]
ui.radio_group(
id: str,
options: list[OptionValue | DetailedOption],
*,
label: str | None = None,
description: str | None = None,
required: bool = True,
initial_value: OptionValue | None = None,
validate: Callable[[OptionValue | None], str | None] = None,
on_change: Callable[[OptionValue | None], None] = None,
style: Style | None = None
)
Parameters
id
required stringid
required strproperties.label
optional stringThe label to display above the radio group. If not provided, the label will be inferred from the id
.
label
optional strThe label to display above the radio group. If not provided, the label will be inferred from the id
.
properties.description
optional stringA description to display between the label and radio group.
description
optional strA description to display between the label and radio group.
properties.required
optional booleanValidate that the radio group is not empty before submitting the form it's part of or calling it's onChange
hook. Defaults to true
.
required
optional boolValidate that the radio group is not empty before submitting the form it's part of or calling it's on_change
hook. Defaults to True
.
properties.initialValue
optional string | number | boolean | nullThe initial value of the radio group. Defaults to null
. You can also update the value of the radio group at any time using the page.setInputs()
method.
initial_value
optional str | float | int | bool | NoneThe initial value of the radio group. Defaults to None
. You can also update the value of the radio group at any time using the page.set_inputs()
method.
properties.validate
optional (value: string | number | boolean | null) => string | voidvalidate
optional Callable[[str | float | int | bool | None], str | None]properties.onChange
optional (value: string | number | boolean | null) => voidon_change
optional Callable[[str | float | int | bool | None], None]properties.style
optional StyleDirectly style the underlying element using CSS. See the styling guide for more details on available style properties.
style
optional Style | NoneDirectly style the underlying element using CSS. See the styling guide for more details on available style properties.