import { createContext, forwardRef, useContext } from 'react'; import { type GestureResponderEvent, Pressable, View } from 'react-native'; import type { PressableRef, SlottablePressableProps, } from '../../helpers/types'; import * as Slot from '../slot'; import type { IndicatorProps, IndicatorRef, RootProps, RootRef, } from './checkbox.types'; interface RootContext extends RootProps { nativeID?: string; } const CheckboxContext = createContext(null); const Root = forwardRef( ( { isDisabled = false, isSelected, onSelectedChange, isInvalid = false, isOnSurface, nativeID, ...props }, ref ) => { return ( ); } ); Root.displayName = 'HeroUINative.Primitive.Checkbox.Root'; function useCheckboxContext() { const context = useContext(CheckboxContext); if (!context) { throw new Error( 'Checkbox compound components cannot be rendered outside the Checkbox component' ); } return context; } // -------------------------------------------------- const Trigger = forwardRef( ({ asChild, onPress: onPressProp, ...props }, ref) => { const { isDisabled, isSelected, onSelectedChange, nativeID } = useCheckboxContext(); function onPress(ev: GestureResponderEvent) { if (isDisabled) return; const newValue = !isSelected; onSelectedChange?.(newValue); onPressProp?.(ev); } const Component = asChild ? Slot.Pressable : Pressable; return ( ); } ); Trigger.displayName = 'HeroUINative.Primitive.Checkbox.Trigger'; // -------------------------------------------------- const Indicator = forwardRef( ({ asChild, ...props }, ref) => { const { isSelected, isDisabled } = useCheckboxContext(); const Component = asChild ? Slot.View : View; return ( ); } ); Indicator.displayName = 'HeroUINative.Primitive.Checkbox.Indicator'; export { Indicator, Root, useCheckboxContext };