Radio
Standalone radio button for use in any layout, offering predefined choices for selection.
Installation
API Reference
This is an illustration of Radio component.
size
isInvalid
isDisabled
<RadioGroup><Radio value="change" size="md" isInvalid={false} isDisabled={false}><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Label</RadioLabel></Radio></RadioGroup>
Installation
CLI
Manual
Run the following command:
npx gluestack-ui add radio
API Reference
To use this component in your project, include the following import statement in your file.
import { Radio } from "@gluestack-ui/themed"
export default () => (<RadioGroup><Radio><RadioIndicator><RadioIcon /></RadioIndicator><RadioLabel /></Radio></RadioGroup>)
Component Props
This section provides a comprehensive reference list for the component props, detailing descriptions, properties, types, and default behavior for easy project integration.
Radio
Contains all Radio related layout style props and actions.
It inherits all the properties of React Native's View component.
Prop | Type | Default | Description |
---|---|---|---|
value | string | - | The value to be used in the radio input. This is the value that will be returned on form submission. |
onChange | function | - | Function called when the state of the radio changes. |
isDisabled | bool | false | To manually set disable to the radio. |
isInvalid | bool | false | To manually set invalid to the radio. |
isHovered | bool | false | To manually set hover to the radio. |
isFocusVisible | bool | false | To manually set focus visible state to the radio. |
isIndeterminate | bool | false | To manually set indeterminate to the radio. |
RadioIndicator
Contains all Indicator related layout style props and actions. It inherits all the properties of React Native's View component.
RadioIcon
Contains all Icon related layout style props and actions. It inherits all the properties of gluestack Style's AsForwarder component.
Prop | Type | Default | Description |
---|---|---|---|
forceMount | boolean | false | Forces mounting when more control is needed, useful for animations with React libraries. |
RadioLabel
Contains all Label related layout style props and actions. It inherits all the properties of React Native's Text component.
RadioGroup
Contains all Group related layout style props and actions. It inherits all the properties of React Native's View component.
Prop | Type | Default | Description |
---|---|---|---|
value | string | - | The value of the radio group. |
onChange | function | - | The callback fired when any children Radio is checked or
unchecked. |
isReadOnly | bool | false | To manually set read-only to the radio group. |
Features
- Keyboard support for actions.
- Support for hover, focus and active states.
Accessibility
We have outlined the various features that ensure the Radio component is accessible to all users, including those with disabilities. These features help ensure that your application is inclusive and meets accessibility standards. Adheres to the WAI-ARIA design pattern.
Keyboard
- Tab: Moves focus to the next focusable element.
- Shift + Tab: Moves focus to the previous focusable element.
- Space: To check or uncheck focused radio.
Screen Reader
- VoiceOver: When the radio is focused, the screen reader will announce it's a radio and it's current state (check or uncheck) and it's label.
Focus Management
- The onFocus and onBlur props manage focus states and provide visual cues to users. This is especially important for users who rely on keyboard navigation.
States
- In error state, aria-invalid will be passed to indicate that the radio input has an error, and providing support for an aria-errormessage to describe the error in more detail.
- In disabled state, aria-hidden will be passed to make radio input not focusable.
- In required state, aria-required will be passed to indicate that the radio input is required.
Props
Radio component is created using Pressable component from react-native. It extends all the props supported by React Native Pressable.
Data Attributes Table
Component receives states as props as boolean values, which are applied as data-* attributes. These attributes are then used to style the component via classNames, enabling state-based styling.
State | Data Attribute | Values |
---|---|---|
hover | data-hover | true | false |
active | data-active | true | false |
disabled | data-disabled | true | false |
focusVisible | data-focus-visible | true | false |
invalid | data-invalid | true | false |
Examples
The Examples section provides visual representations of the different variants of the component, allowing you to quickly and easily determine which one best fits your needs. Simply copy the code and integrate it into your project.
Multiple Radio
Radio buttons provide a mutually exclusive selection mechanism, allowing users to choose a single option from a set of related choices.
function App() {const [values, setValues] = React.useState("Eng")return (<RadioGroup value={values} onChange={setValues}><VStack space="sm"><Radio value="Eng"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>English</RadioLabel></Radio><Radio value="Fre"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>French</RadioLabel></Radio><Radio value="Ger"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>German</RadioLabel></Radio></VStack></RadioGroup>)}
Horizontal
Radio buttons can be used horizontally.
function App() {const [values, setValues] = React.useState("Cash On Delivery")return (<RadioGroup value={values} onChange={setValues}><HStack space="2xl"><Radio value="Credit Card"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Credit Card</RadioLabel></Radio><Radio value="Cash On Delivery"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Cash On Delivery</RadioLabel></Radio></HStack></RadioGroup>)}
With help text
Radio buttons can be used with helper text.
function App() {const [values, setValues] = React.useState("Read-only")return (<RadioGroup value={values} onChange={setValues}><VStack space="2xl"><Box><Radio value="Read-only" size="md"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Extended coverage</RadioLabel></Radio><Text size="$sm" className="ml-7 text-typography-500">Extra services included</Text></Box><Box><Radio value="Write" size="md"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Basic coverage</RadioLabel></Radio><Text size="$sm" className="ml-7 text-typography-500">Nothing extra included</Text></Box></VStack></RadioGroup>)}
Form Control
Radio buttons can be used inside FormControl for better control of states and functionality.
<FormControl><VStack space="md"><Heading size="sm">Which time slot works best for you?</Heading><RadioGroup><VStack space="sm"><Radio value="Monday" size="md"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Monday</RadioLabel></Radio><Radio value="Tuesday" size="md"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Tuesday</RadioLabel></Radio><Radio value="Wednesday" size="md"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Wednesday</RadioLabel></Radio></VStack></RadioGroup><Text className="text-sm text-light-500">Choose a time slot for the meeting</Text></VStack></FormControl>
Label left
Radio buttons can be used horizontally
function App() {const [values, setValues] = React.useState("Monday")return (<RadioGroup value={values} onChange={setValues}><VStack space="lg" className="w-40"><Radio value="Monday" className="justify-between"><RadioLabel>Jane Cooper</RadioLabel><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator></Radio><Radio value="Tuesday" className="justify-between"><RadioLabel>Wade Warren</RadioLabel><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator></Radio></VStack></RadioGroup>)}
Controlled
The Radio components can be used with a controlled state, enabling precise management of the selected option through external state management.
function App() {const [values, setValues] = React.useState("Apartments")return (<RadioGroup value={values} onChange={setValues}><VStack space="md"><Radio value="Apartments"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Apartments</RadioLabel></Radio><Radio value="Houses"><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Houses</RadioLabel></Radio></VStack></RadioGroup>)}
Uncontrolled
The Radio components can be used with either a ref or no state, providing an uncontrolled state where the selected option is managed internally.
function App() {const radioRef = React.useRef(null)const handleRadioChange = (e) => {e.preventDefault()const checkboxValue = radioRef.current.checked}return (<RadioGroup><VStack space="md"><Radio value="Hotels" ref={radioRef} onChange={handleRadioChange}><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Hotels</RadioLabel></Radio><Radiovalue="Living quarters"ref={radioRef}onChange={handleRadioChange}><RadioIndicator><RadioIcon as={CircleIcon} /></RadioIndicator><RadioLabel>Living quarters</RadioLabel></Radio></VStack></RadioGroup>)}
Radio group
The Radio group component allows users to group radio and display them in a horizontal or vertical row for better visual representation and functionality.
function RadioGroupExample() {const [values, setValues] = React.useState("1st")return (<RadioGroup value={values} onChange={setValues}><VStack space="