Skip to main content

Component | Radio Group

Render a radio group.

ui.radioGroup("radio-group-key", ["A", "B", "C", "D"], { 
label: "Select an option",
onChange: (value) => console.log(value)
})

Learn more about working with inputs.

Options

Select components support both a "basic" and "detailed" format for options.

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)
})

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"
})

Detailed option object properties

label

required string
The user-facing label to use for the option.

value

required string | number | boolean

The internal value to assign to the option and use when submitting a form or executing callbacks such as onChange.


description

optional string
Helpful text to display beneath the label.

API reference

Function signature

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
}>
)

Parameters

id

required string
A unique identifier for the component. This is necessary so that Compose can properly pass user actions back to the component.

options

required Array<OptionValue | DetailedValue>

The list of options that can be selected. See the options section for more info.


properties.label

optional string

The label to display above the radio group. If not provided, the label will be inferred from the id.


properties.description

optional string

A description to display between the label and radio group.


properties.required

optional boolean

Validate that the radio group is not empty before submitting the form it's part of or calling it's onChange hook. Defaults to true.


properties.initialValue

optional string | number | boolean | null

The 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.


properties.validate

optional (value: string | number | boolean | null) => string | void
Validate the radio group value before submitting it. If the function returns a string, it will be displayed as an error message.

properties.onChange

optional (value: string | number | boolean | null) => void
A callback function that is called when the user changes the value of the radio group.

properties.style

optional Style

Directly style the underlying element using CSS. See the styling guide for more details on available style properties.