Skip to main content

Overview

The RadioGroup class creates a group of radio buttons where only one option can be selected at a time. It provides a clear visual representation of all available options and is ideal for mutually exclusive choices.

Inheritance

ObjectBase → NodeBase → SelectableNode → RadioGroup

Constructor

new RadioGroup(
  id: string,
  placeholder?: string,
  value?: OptionChild[] | Promise<OptionChild[]> | Observable<OptionChild[]>,
  selected?: string,
  singleLine?: boolean,
  icon?: string,
  errorMessage?: string,
  disabled?: boolean,
  validator?: Validator | AbstractControlOptions,
  asyncValidator?: AsyncValidatorFn,
  action?: Action | Action[]
)

Parameters

id
string
required
The unique identifier for the radio group node in the DOM.
placeholder
string
The label text to display for the radio group.
value
OptionChild[] | Promise<OptionChild[]> | Observable<OptionChild[]>
The radio button options to display. Can be an array, Promise, or Observable of OptionChild objects.
selected
string
The initial selected value of the radio group.
singleLine
boolean
default:"false"
Whether to display the node in a single line layout.
icon
string
Material icon name to display with the radio group.
errorMessage
string
Custom error message to display when validation fails.
disabled
boolean
Whether the radio group is disabled.
validator
Validator | AbstractControlOptions
Synchronous validator function(s) or control options.
asyncValidator
AsyncValidatorFn
Asynchronous validator function(s) for the radio group.
action
Action | Action[]
Action(s) to execute when the selection changes.

Properties

PropertyTypeDescription
idstringThe unique identifier for the node
placeholderstringThe label text for the radio group
type'radiogroup'The node type (read-only)
valueOptionChild[]The radio button options
selectedValuestringThe currently selected value
singleLinebooleanWhether displayed in single line layout
iconstringMaterial icon name
errorMessagestringCustom error message
disabledbooleanWhether the node is disabled
validatorValidator | AbstractControlOptionsSynchronous validators
asyncValidatorAsyncValidatorFnAsynchronous validators
actionAction | Action[]Associated actions
hintstringHint text to display
autoFocusbooleanWhether to auto-focus on load

Methods

getOptions()

Returns the current list of radio button options.
getOptions(): OptionChild[]

getNativeElement()

Returns the native DOM element for this node.
getNativeElement(): HTMLElement | null

Usage Example

import { RadioGroup, OptionChild } from '@ng-mdf/core';
import { Validators } from '@angular/forms';

// Simple radio group with static options
const genderRadioGroup = new RadioGroup(
  'gender',
  'Select your gender',
  [
    new OptionChild('male', 'Male'),
    new OptionChild('female', 'Female'),
    new OptionChild('other', 'Other'),
    new OptionChild('prefer-not', 'Prefer not to say')
  ],
  'prefer-not',  // preselected value
  false,
  undefined,
  'Please select a gender',
  false,
  Validators.required
);

// Radio group with action
import { Action } from '@ng-mdf/core';

const subscriptionRadioGroup = new RadioGroup(
  'subscription',
  'Choose your plan',
  [
    new OptionChild('free', 'Free - $0/month'),
    new OptionChild('basic', 'Basic - $9.99/month'),
    new OptionChild('pro', 'Pro - $19.99/month'),
    new OptionChild('enterprise', 'Enterprise - Contact us')
  ],
  'free',
  false,
  'payment',
  undefined,
  false,
  Validators.required,
  undefined,
  new Action('change', (event) => {
    console.log('Selected plan:', event.value);
    // Update pricing display or load additional options
  })
);

// Radio group with async options from API
const categoryRadioGroup = new RadioGroup(
  'category',
  'Select a category',
  fetch('/api/categories').then(res => res.json()),
  undefined,
  false,
  'category'
);

Best Practices

When to Use RadioGroup

  • 2-5 options: Radio groups work best with a small number of visible options
  • Mutually exclusive: When only one choice should be selected
  • Important choices: When you want all options visible at once
  • Quick selection: When users should see and compare all options immediately

When to Use Alternatives

Accessibility

RadioGroup nodes are rendered as native radio button groups with proper ARIA attributes and keyboard navigation support. Users can navigate options using arrow keys and select with Enter/Space.

Build docs developers (and LLMs) love