Radio Group
Composable single-choice controls with controlled and uncontrolled state, radio semantics, and 48dp touch targets.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add radio-groupSource files
Expand a file to read the source the installer copies into your app.
components/ui/radio-group/RadioGroup.tsx
import * as React from 'react'
import { Pressable, View, type PressableProps, type ViewProps } from 'react-native'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'
interface RadioGroupContextValue {
value: string | undefined
disabled: boolean
select: (value: string) => void
}
const RadioGroupContext = React.createContext<RadioGroupContextValue | null>(null)
export interface RadioGroupProps extends ViewProps {
className?: string
value?: string
defaultValue?: string
onValueChange?: (value: string) => void
disabled?: boolean
}
const RadioGroup = React.forwardRef<React.ComponentRef<typeof View>, RadioGroupProps>(
({ value, defaultValue, onValueChange, disabled = false, className, accessibilityState, ...props }, ref) => {
const [internal, setInternal] = React.useState(defaultValue)
const resolved = value ?? internal
function select(next: string) {
if (disabled || next === resolved) return
if (value === undefined) setInternal(next)
onValueChange?.(next)
}
return (
<RadioGroupContext.Provider value={{ value: resolved, disabled, select }}>
<View
ref={ref}
{...props}
accessible={false}
accessibilityRole="radiogroup"
accessibilityState={{ ...accessibilityState, disabled }}
className={cn('gap-[8px]', className)}
/>
</RadioGroupContext.Provider>
)
},
)
RadioGroup.displayName = 'RadioGroup'
export interface RadioGroupItemProps extends PressableProps {
value: string
label: string
children?: React.ReactNode
className?: string
}
const RadioGroupItem = React.forwardRef<React.ComponentRef<typeof Pressable>, RadioGroupItemProps>(
({ value, label, children, className, disabled, onPress, accessibilityLabel, accessibilityState, ...props }, ref) => {
const group = React.useContext(RadioGroupContext)
if (!group) throw new Error('RadioGroupItem must be inside RadioGroup')
const checked = group.value === value
const isDisabled = group.disabled || !!disabled
return (
<Pressable
ref={ref}
{...props}
accessibilityRole="radio"
accessibilityLabel={accessibilityLabel ?? label}
accessibilityState={{ ...accessibilityState, checked, disabled: isDisabled }}
disabled={isDisabled}
onPress={(event) => {
if (isDisabled) return
group.select(value)
onPress?.(event)
}}
className={cn(
'active:bg-muted min-h-[48px] flex-row items-center gap-3 rounded-md px-3 py-3',
isDisabled && 'opacity-50',
className,
)}
>
<View
aria-hidden
className={cn(
'size-[20px] items-center justify-center rounded-full border',
checked ? 'border-primary' : 'border-muted-foreground',
)}
>
{checked ? <View className="bg-primary size-[10px] rounded-full" /> : null}
</View>
<View pointerEvents="none" aria-hidden className="min-w-0 flex-1 gap-1">
<Text className="font-medium">{label}</Text>
{children}
</View>
</Pressable>
)
},
)
RadioGroupItem.displayName = 'RadioGroupItem'
export { RadioGroup, RadioGroupItem }
components/ui/radio-group/index.ts
export { RadioGroup, RadioGroupItem, type RadioGroupProps, type RadioGroupItemProps } from './RadioGroup'