Checkbox
nativeui/checkboxFreeComponent
A checkbox 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/checkbox.jsonSource
1import { cn } from "@/lib/utils"2import { Ionicons } from "@expo/vector-icons"3import { useColorScheme } from "nativewind"4import * as React from "react"5import { Pressable, Text } from "react-native"67interface CheckboxProps extends Omit<React.ComponentPropsWithoutRef<typeof Pressable>, 'children'> {8 checked?: boolean9 defaultChecked?: boolean10 onCheckedChange?: (checked: boolean) => void11 disabled?: boolean12 id?: string13}1415interface CheckboxLabelProps extends React.ComponentPropsWithoutRef<typeof Text> {16 disabled?: boolean17 htmlFor?: string18}1920const Checkbox = React.forwardRef<21 React.ElementRef<typeof Pressable>,22 CheckboxProps23>(({24 className,25 checked,26 defaultChecked,27 onCheckedChange,28 disabled,29 id,30 ...props31}, ref) => {32 const { colorScheme } = useColorScheme()33 const [innerChecked, setInnerChecked] = React.useState<boolean>(34 checked !== undefined ? checked : defaultChecked || false35 )36 const isChecked = checked !== undefined ? checked : innerChecked3738 const handlePress = React.useCallback(() => {39 if (disabled) return4041 const newValue = !isChecked4243 if (checked === undefined) {44 setInnerChecked(newValue)45 }4647 onCheckedChange?.(newValue)48 }, [checked, isChecked, onCheckedChange, disabled])4950 React.useEffect(() => {51 if (checked !== undefined) {52 setInnerChecked(checked)53 }54 }, [checked])5556 return (57 <Pressable58 ref={ref}59 accessibilityRole="checkbox"60 accessibilityState={{ checked: isChecked, disabled }}61 onPress={handlePress}62 disabled={disabled}63 className={cn(64 "h-6 w-6 rounded-md border-2 justify-center items-center",65 isChecked66 ? "border-primary bg-primary"67 : "border-border bg-transparent",68 disabled && "opacity-50",69 className70 )}71 accessibilityLabel={id}72 {...props}73 >74 {isChecked && (75 <Ionicons76 name="checkmark-sharp"77 size={18}78 className="color-primary-foreground"79 />80 )}81 </Pressable>82 )83})8485const CheckboxLabel = React.forwardRef<86 React.ElementRef<typeof Text>,87 CheckboxLabelProps88>(({ className, disabled, htmlFor, ...props }, ref) => {89 return (90 <Text91 ref={ref}92 className={cn(93 "text-base text-foreground ml-3",94 disabled && "text-muted-foreground",95 className96 )}97 {...props}98 />99 )100})101102Checkbox.displayName = "Checkbox"103CheckboxLabel.displayName = "CheckboxLabel"104105// … 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 |
