Skip to content

RadioGroup

Group of radio buttons with shared state management.

Also known as: option group, single select group, radio list

View in Storybook

When to use

  • Group of radio buttons for single selection. For cards with descriptions, use RadioCard.
  • For dropdown-style, use Select.

Import

tsx
import { RadioGroup } from '@vacano/ui'

Usage

tsx
const [value, setValue] = useState<string | null>('option1')

const options = [
  { value: 'option1', label: 'Option 1' },
  { value: 'option2', label: 'Option 2' },
  { value: 'option3', label: 'Option 3' },
]

<RadioGroup
  options={options}
  value={value}
  onChange={setValue}
/>

Type-safe Values

RadioGroup is generic -- the value type is inferred from options:

tsx
type Color = 'red' | 'green' | 'blue'

const [color, setColor] = useState<Color | null>('red')

const options: RadioGroupOption<Color>[] = [
  { value: 'red', label: 'Red' },
  { value: 'green', label: 'Green' },
  { value: 'blue', label: 'Blue' },
]

<RadioGroup<Color>
  options={options}
  value={color}     // Color | null
  onChange={setColor} // (value: Color) => void
/>

With Label

tsx
<RadioGroup
  label="Select an option"
  options={options}
  value={value}
  onChange={setValue}
/>

With Name

tsx
<RadioGroup
  name="preferences"
  options={options}
  value={value}
  onChange={setValue}
/>

Disabled

tsx
<RadioGroup
  disabled
  options={options}
  value="option1"
  onChange={() => {}}
/>

Props

PropTypeDefaultDescription
optionsRadioGroupOption<T>[]requiredArray of options
valueT | nullrequiredSelected value
onChange(value: T) => voidrequiredChange handler
labelstring-Group label
namestring-Input name for all radios
variant'normal' | 'error''normal'Visual variant for all radios
disabledbooleanfalseDisable all radios
classNamestring-CSS class for root element
classnamesRadioGroupClassNames-Custom class names for inner elements
refRef<HTMLDivElement>-Ref to the root container element
data-test-idstring-Test identifier for automated testing

T extends string and defaults to string.

Option Type

tsx
type RadioGroupOption<T extends string = string> = {
  label: string
  value: T
}

ClassNames

KeyDescription
labelGroup label
optionsOptions container
radioIndividual radio

Released under the MIT License.