Radio Button

Radio buttons are used when the user needs to select one option from a set.

This UI component is best used when there is enough space, since all the options are visible at once. If you need to save space, a better UI choice would be the Dropdown, which only shows the selected item and the others are hidden after the selection.

Basic Usage

The following example will render a simple Radio button with a label.

JSX
<Radio value="1" name="myRadio" label="One" />

Radio buttons can be used individually or can be grouped logically inside a Radio Group component. The Radio Group component, also helps to specify the group name only once, otherwise it needs to be added to every radio button.

Radio Groups
<RadioGroup name="gender">
    <Radio value="f" label="Female" />
    <Radio value="m" label="Male" />
    <Radio value="o" label="Other" />
</RadioGroup>

Value binding

Controlled component

Radio buttons can be used as controlled components by providing the checked and the onChange props.

Controlled component
function App() {
    const [gender, setGender] = React.useState('f');
    const genderChange = (ev) => {
        setGender(ev.target.value);
    }

    return <RadioGroup name="genderGroup">
        <Radio value="f" checked={gender === 'f'} onChange={genderChange} label="Female" />
        <Radio value="m" checked={gender === 'm'} onChange={genderChange} label="Male" />
        <Radio value="o" checked={gender === 'o'} onChange={genderChange} label="Other" />
    </RadioGroup>
}

Uncontrolled component

When using the Radio as uncontrolled component, a ref can be used to access the component instance. The checked property on the component will be true, when the radio is selected.

Uncontrolled component
function App() {
    const ref = React.useRef(null);
    const checkOutTheRadio = () => {
        console.log(ref.current.checked); // should print out the checked state of the radio to the console
    };

    return <RadioGroup name="uncontrolled">
        <Radio value="one" defaultChecked={true} ref={oneRef} label="One or the" />
        <Radio value="other" label="Other" />
        <Button onClick={checkOutTheRadio}>Which one is selected?</Button>
    </RadioGroup>
}

For many more examples - simple and complex use-cases - check out the radio button demos for react.

Options

Name Type Default value Description
checked Boolean undefined When true, sets the group to this Radio's value.
defaultChecked Boolean undefined Defines the initial selected value of the group, when using the Radios as uncontrolled components.
color String undefined A predefined color to style the component.
Supported values are:
  • primary
  • secondary
  • success
  • danger
  • warning
  • info
  • dark
  • light
description String undefined A description that shows up under the label of the component.
disabled Boolean false Initial disabled state of the component.
label String undefined Sets the label of component.
name String undefined The name attribute for the Radio button. Each unique name defines a group, and the same name should be used on each component that goes into the same group.
value String undefined The value attribute is a string containing the Radio button's value.
onChange Function undefined An event handler that is called every time the radio group changes value. The event is passed as the first parameter and it's target value property is set to the new value.
Ex.
const handler = (ev) => { setState({ myValue: ev.target.value }); }
<Radio onChange={handler} checked={state.myValue === 'value1'} />
position String 'end' Sets the position of the switch handle depending on the RTL setting. It can be 'start' or 'end'.
When in LTR mode, the 'start' will set the handle position to the left (a.k.a. from left to right the switch will start with the handle) and the 'end' will set it to the right - so the handle goes to the end of the switch.
In RTL mode, the 'start' will position the hande to the right. The 'end' will position the handle to the left in this case.
rtl Boolean false Right to left display.
theme String undefined

Sets the visual appearance of the component.

If it is 'auto' or undefined, the theme will automatically be chosen based on the platform. If custom themes are also present, they will take precedence over the built in themes, e.g. if there's an iOS based custom theme, it will be chosen on the iOS platform instead of the default iOS theme.

Supplied themes:
  • 'ios' - iOS theme
  • 'material' - Material theme
  • 'windows' - Windows theme
It's possible to modify theme colors or create custom themes.
Make sure that the theme you set is included in the downloaded package.
themeVariant String undefined

Controls which variant of the theme will be used (light or dark).

Possible values:
  • 'light' - Use the light variant of the theme.
  • 'dark' - Use the dark variant of the theme.
  • 'auto' or undefined - Detect the preferred system theme on devices where this is supported.

To use the option with custom themes, make sure to create two custom themes, where the dark version has the same name as the light one, suffixed with '-dark', e.g.: 'my-theme' and 'my-theme-dark'.

Customizing the appearance

While the provided pre-built themes are enough in many use cases, most of the times on top of adapting to a specific platform, you'd also like to match a brand or color scheme. Mobiscroll provides various ways to achieve this:

Override the Sass Color Variables

A convenient way to customize the colors of the Mobiscroll components is to override the Sass color variables.

Let's say your branding uses a nice red accent color, and you'd like that color to appear on the Mobiscroll components as well, while still using platform specific themes (e.g. ios on iOS devices, material on Android devices, and mobiscroll on desktop). You can override the accent color for every theme:

$mbsc-ios-accent: #e61d2a;
$mbsc-material-accent: #e61d2a;
$mbsc-mobiscroll-accent: #e61d2a;

@import "~@mobiscroll/React/dist/css/mobiscroll.react.scss"
It's important that you override the variables BEFORE the scss file import, otherwise it won't make any difference.
Here's a complete guide on how to set up Mobiscroll with SASS support

You can also customize the colors on many levels:

  1. Theme specific variables (ex. $mbsc-material-background, $mbsc-ios-dark-text) are applied to all components in a theme. Complete list of variables here.
  2. Component specific global variables (ex. $mbsc-card-background-light, $mbsc-listview-text-dark) are applied to all themes for a specific component.
  3. Component and theme specific variables (ex. $mbsc-ios-dark-form-background, $mbsc-material-input-text) are applied to a specific theme and a specific component.

Hereinafter you will see all the variables that are specific to the Radio component or affect its look:

For many more examples - simple and complex use-cases - check out the radio button demos for react.