Radio Group
nativeui/radio-groupFreeComponent
A radio group component for React Native applications.
Live preview
live · rendered by the registry
Rendered by nativeui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://nativeui.ribbi.ch/r/radio-group.jsonSource
1import * as React from "react"2import { View, Text, Pressable } from "react-native"3import { cn } from "@/lib/utils"45interface RadioGroupRootProps extends React.ComponentPropsWithoutRef<typeof View> {6 value?: string7 defaultValue?: string8 onValueChange?: (value: string) => void9 disabled?: boolean10}1112interface RadioGroupItemProps extends Omit<React.ComponentPropsWithoutRef<typeof Pressable>, 'children'> {13 value: string14 disabled?: boolean15 id?: string16}1718interface RadioGroupLabelProps extends React.ComponentPropsWithoutRef<typeof Text> {19 disabled?: boolean20 htmlFor?: string21}2223const RadioGroupContext = React.createContext<{24 value?: string25 onValueChange?: (value: string) => void26 disabled?: boolean27}>({})2829function RadioGroup({30 value,31 defaultValue,32 onValueChange,33 disabled = false,34 className,35 children,36 ...props37}: RadioGroupRootProps) {38 const [innerValue, setInnerValue] = React.useState<string | undefined>(value ?? defaultValue)39 const currentValue = value !== undefined ? value : innerValue4041 const handleValueChange = React.useCallback((newValue: string) => {42 if (disabled) return4344 if (value === undefined) {45 setInnerValue(newValue)46 }4748 onValueChange?.(newValue)49 }, [value, onValueChange, disabled])5051 return (52 <RadioGroupContext.Provider53 value={{ value: currentValue, onValueChange: handleValueChange, disabled }}54 >55 <View56 className={cn("space-y-4", className)}57 accessibilityRole="radiogroup"58 {...props}59 >60 {children}61 </View>62 </RadioGroupContext.Provider>63 )64}6566const RadioGroupItem = React.forwardRef<67 React.ElementRef<typeof Pressable>,68 RadioGroupItemProps69>(({ className, value, disabled, id, ...props }, ref) => {70 const { value: groupValue, onValueChange, disabled: groupDisabled } = React.useContext(RadioGroupContext)71 const isDisabled = disabled || groupDisabled72 const isChecked = value === groupValue7374 const handlePress = () => {75 if (!isDisabled) {76 onValueChange?.(value)77 }78 }7980 return (81 <Pressable82 ref={ref}83 accessibilityRole="radio"84 accessibilityState={{ checked: isChecked, disabled: isDisabled }}85 onPress={handlePress}86 disabled={isDisabled}87 className={cn(88 "h-6 w-6 rounded-full border-2 justify-center items-center",89 isChecked90 ? "border-primary bg-primary/10"91 : "border-border bg-transparent",92 isDisabled && "opacity-50",93// … truncated
What it pulls in
npm packages
Files it writes
More from nativeui
All 29 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | nativeui | Components | Free | 2 deps | |
| Alertalert | nativeui | Components | Free | 2 deps | |
| Alert Dialogalert-dialog | nativeui | Components | Free | 1 dep | |
| Avataravatar | nativeui | Components | Free | 1 dep | |
| Badgebadge | nativeui | Components | Free | 2 deps | |
| Breadcrumbbreadcrumb | nativeui | Components | Free | 2 deps | |
| Buttonbutton | nativeui | Components | Free | 2 deps | |
| Calendarcalendar | nativeui | Components | Free | 4 deps |
