RadioGroup
Basic control to allow selecting only one item from a set.
#
Documentation
#
#
Usage
#
Use RadioGroup and Radio components together to create sets of radio buttons.
"use client";
import type { ComponentPropsWithoutRef } from "react";
import { Field, Radio, RadioGroup } from "@optiaxiom/react";
export function App({
description,
error,
label = "Input label",
required = false,
}: Pick<
ComponentPropsWithoutRef<typeof Field>,
"description" | "error" | "label" | "required"
>) {
return (
<Field
description={description}
error={error}
label={label}
required={required}
>
<RadioGroup name="usage">
<Radio value="one">Option One</Radio>
<Radio value="two">Option Two</Radio>
<Radio value="three">Option Three</Radio>
</RadioGroup>
</Field>
);
}#
Label
#
The children prop on Radio is used as the label of the radio control.
import { Radio, RadioGroup } from "@optiaxiom/react";
export function App() {
return (
<RadioGroup name="label-usage">
<Radio value="one" />
<Radio value="two">Option Two</Radio>
</RadioGroup>
);
}#
Controlled
#
Use the value and defaultValue props on RadioGroup to toggle between controlled and uncontrolled usage. And combine it with onValueChange to listen for changes to the value.
"use client";
import { Button, Flex, Radio, RadioGroup } from "@optiaxiom/react";
import { useState } from "react";
export function App() {
const [value, setValue] = useState("");
return (
<Flex>
<RadioGroup
name="controlled-usage"
onValueChange={setValue}
value={value}
>
<Radio value="one">Option One</Radio>
<Radio value="two">Option Two</Radio>
<Radio value="three">Option Three</Radio>
</RadioGroup>
<Button disabled={!value} onClick={() => setValue("")}>
Reset
</Button>
</Flex>
);
}#
Disabled state
#
Enable the disabled prop on the RadioGroup to toggle the disabled state of all the radio controls.
import { Radio, RadioGroup } from "@optiaxiom/react";
export function App() {
return (
<RadioGroup disabled name="disabled-group-usage" value="one">
<Radio value="one">Option One</Radio>
<Radio value="two">Option Two</Radio>
</RadioGroup>
);
}You can also enable the disabled prop on each Radio to toggle the disabled state of each radio control.
import { Radio, RadioGroup } from "@optiaxiom/react";
export function App() {
return (
<RadioGroup name="disabled-usage">
<Radio defaultChecked value="one">
Option One
</Radio>
<Radio disabled value="two">
Option Two
</Radio>
<Radio value="three">Option Three</Radio>
</RadioGroup>
);
}#
Description
#
Use the description prop to add helper text after the label.
import { Radio, RadioGroup } from "@optiaxiom/react";
export function App() {
return (
<RadioGroup name="description-usage">
<Radio description="Helper Text" value="one">
Option One
</Radio>
<Radio value="two">Option Two</Radio>
</RadioGroup>
);
}#
Related
#
Checkbox
Basic control to allow selecting one or more items from a set.
Select
Select a value from a list of options inside a dropdown menu.
Switch
Control to allow toggling between checked and not checked state.
#
Props
#
#
RadioGroup
#
Supports all Box props in addition to its own. Renders a <div> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
className
|
defaultValueThe initial selected value in uncontrolled mode.
|
disabledWhether the input is disabled and in read only mode.
|
nameThe name of the form control elements. Will override any name specified on the inner Radio components.
|
onBlurHandler for Useful for integrating with third party form libraries.
|
onChangeHandler for Useful for integrating with third party form libraries.
|
onValueChangeHandler that is called when the selected value changes.
|
valueThe selected value in controlled mode.
|
#
Radio
#
Supports all Box props in addition to its own. Renders a <div> element but forwards all props to a hidden <input> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
className
|
descriptionAdd secondary text after the label.
|
onCheckedChangeHandler that is called when the checked state changes.
|
#
Changelog
#
#
0.2.0
#
- Renamed
RadioGroupItemtoRadio
#
0.1.0
#
- Added component