Radio Group
A set of checkable buttons where only one can be checked at a time.
Installation
bash npx @moe/cli add radio-group Install the following dependency:
npx expo install @rn-primitives/radio-groupCopy/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<
React.ElementRef<typeof RadioGroupPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Root>
>(({ className, ...props }, ref) => {
return (
<RadioGroupPrimitive.Root
className={cn("gap-2 web:grid", className)}
{...props}
ref={ref}
/>
);
});
RadioGroup.displayName = RadioGroupPrimitive.Root.displayName;
const RadioGroupItem = React.forwardRef<
React.ElementRef<typeof RadioGroupPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Item>
>(({ 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="h-2 w-2 native:h-2.5 native:w-2.5 rounded-full bg-primary" />
</RadioGroupPrimitive.Indicator>
</RadioGroupPrimitive.Item>
);
});
RadioGroupItem.displayName = RadioGroupPrimitive.Item.displayName;
export { RadioGroup, RadioGroupItem };Usage
import { RadioGroup, RadioGroupItem } from "@moe/registry/ui/radio-group";
import { Label } from "@moe/registry/ui/label";
import { View } from "react-native";
export function RadioGroupDemo() {
return (
<RadioGroup defaultValue="option-one">
<View className="flex-row items-center gap-2">
<RadioGroupItem value="option-one" id="option-one" />
<Label htmlFor="option-one">Option One</Label>
</View>
<View className="flex-row items-center gap-2">
<RadioGroupItem value="option-two" id="option-two" />
<Label htmlFor="option-two">Option Two</Label>
</View>
</RadioGroup>
);
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | - | The controlled value |
onValueChange | (value: string) => void | - | Event handler called when value changes |
defaultValue | string | - | The default value |