import { createContext, forwardRef, useContext } from 'react'; import { Pressable, View, type GestureResponderEvent } from 'react-native'; import * as Slot from '../slot'; import type { IndicatorProps, IndicatorRef, ItemProps, ItemRef, RootProps, RootRef, } from './radio-group.types'; const RadioGroupContext = createContext(null); const Root = forwardRef( ( { asChild, value, onValueChange, isDisabled = false, isInvalid = false, variant, ...viewProps }, ref ) => { const Component = asChild ? Slot.View : View; return ( ); } ); Root.displayName = 'HeroUINative.Primitive.RadioGroup.Root'; function useRadioGroupContext() { const context = useContext(RadioGroupContext); if (!context) { throw new Error( 'RadioGroup compound components cannot be rendered outside the RadioGroup component' ); } return context; } // -------------------------------------------------- const Item = forwardRef( ( { asChild, value: itemValue, isDisabled: disabledProp = false, onPress: onPressProp, ...props }, ref ) => { const { isDisabled, value, onValueChange } = useRadioGroupContext(); function onPress(ev: GestureResponderEvent) { if (isDisabled || disabledProp) return; onValueChange(itemValue); onPressProp?.(ev); } const Component = asChild ? Slot.Pressable : Pressable; return ( ); } ); Item.displayName = 'HeroUINative.Primitive.RadioGroup.Item'; // -------------------------------------------------- const Indicator = forwardRef( ({ asChild, ...props }, ref) => { const Component = asChild ? Slot.View : View; return ; } ); Indicator.displayName = 'HeroUINative.Primitive.RadioGroup.Indicator'; export { Indicator, Item, Root, useRadioGroupContext };