Radio Group
Radio Group Primitive
Terminal window
~/components/ui/radio-group.tsx
Demo
A group of selectable buttons, commonly referred to as radio buttons, wherein only one button can be checked simultaneously.
Installation
npx @react-native-reusables/cli@latest add radio-group
Copy/paste the following code to ~/components/ui/radio-group.tsx
:
import * as RadioGroupPrimitive from '@rn-primitives/radio-group';import * as React from 'react';import { View } from 'react-native';import { cn } from '~/lib/utils';
const RadioGroup = React.forwardRef<RadioGroupPrimitive.RootRef, RadioGroupPrimitive.RootProps>( ({ className, ...props }, ref) => { return ( <RadioGroupPrimitive.Root className={cn('web:grid gap-2', className)} {...props} ref={ref} /> ); });RadioGroup.displayName = RadioGroupPrimitive.Root.displayName;
const RadioGroupItem = React.forwardRef<RadioGroupPrimitive.ItemRef, RadioGroupPrimitive.ItemProps>( ({ className, ...props }, ref) => { return ( <RadioGroupPrimitive.Item ref={ref} className={cn( 'aspect-square h-4 w-4 native:h-5 native:w-5 rounded-full justify-center items-center border border-primary text-primary web:ring-offset-background web:focus:outline-none web:focus-visible:ring-2 web:focus-visible:ring-ring web:focus-visible:ring-offset-2', props.disabled && 'web:cursor-not-allowed opacity-50', className )} {...props} > <RadioGroupPrimitive.Indicator className='flex items-center justify-center'> <View className='aspect-square h-[9px] w-[9px] native:h-[10] native:w-[10] bg-primary rounded-full' /> </RadioGroupPrimitive.Indicator> </RadioGroupPrimitive.Item> ); });RadioGroupItem.displayName = RadioGroupPrimitive.Item.displayName;
export { RadioGroup, RadioGroupItem };
Usage
import * as React from 'react';import { View } from 'react-native';import { Label } from '~/components/ui/label';import { RadioGroup, RadioGroupItem } from '~/components/ui/radio-group';
function Example() { const [value, setValue] = React.useState('Comfortable');
function onLabelPress(label: string) { return () => { setValue(label); }; } return ( <View className='flex-1 justify-center items-center p-6'> <RadioGroup value={value} onValueChange={setValue} className='gap-3'> <RadioGroupItemWithLabel value='Default' onLabelPress={onLabelPress('Default')} /> <RadioGroupItemWithLabel value='Comfortable' onLabelPress={onLabelPress('Comfortable')} /> <RadioGroupItemWithLabel value='Compact' onLabelPress={onLabelPress('Compact')} /> </RadioGroup> </View> );}
function RadioGroupItemWithLabel({ value, onLabelPress,}: { value: string; onLabelPress: () => void;}) { return ( <View className={'flex-row gap-2 items-center'}> <RadioGroupItem aria-labelledby={`label-for-${value}`} value={value} /> <Label nativeID={`label-for-${value}`} onPress={onLabelPress}> {value} </Label> </View> );}
Props
RadioGroup
Extends View
props
Prop | Type | Note |
---|---|---|
value | string | undefined | |
onValueChange | (val: string) => void | |
asChild | boolean | (optional) |
disabled | boolean | (optional) |
RadioGroupItem
Extends Pressable
props
Prop | Type | Note |
---|---|---|
value | string | |
aria-labelledby | string | Its label’s nativeID |
asChild | boolean | (optional) |