The Checkbox component is used in forms when a user needs to select multiple values from several options.
const Example = () => {
return <HStack space={6}>
<Checkbox value="test" accessibilityLabel="This is a dummy checkbox" />
<Checkbox value="test" accessibilityLabel="This is a dummy checkbox" defaultIsChecked />
</HStack>;
};
const Example = () => {
const [groupValues, setGroupValues] = React.useState([]);
return <Checkbox.Group onChange={setGroupValues} value={groupValues} accessibilityLabel="choose numbers">
<Checkbox value="one" my={2}>
UX Research
</Checkbox>
<Checkbox value="two">Software Development</Checkbox>
</Checkbox.Group>;
};
const Example = () => {
return <Checkbox.Group accessibilityLabel="choose values">
<Checkbox value="one" my={2}>
UX Research
</Checkbox>
<Checkbox value="two">Software Development</Checkbox>
</Checkbox.Group>;
};
const Example = () => {
return <VStack space={2}>
<Checkbox isDisabled value="one">
UX Research
</Checkbox>
<Checkbox isDisabled defaultIsChecked value="two">
Software Development
</Checkbox>
</VStack>;
};
const Example = () => {
return <Checkbox isInvalid value="invalid">
Software Development
</Checkbox>;
};
const Example = () => {
return <VStack space={3} alignItems="flex-start">
<Checkbox value="danger" colorScheme="danger" defaultIsChecked>
Danger
</Checkbox>
<Checkbox value="info" colorScheme="info" defaultIsChecked>
Info
</Checkbox>
<Checkbox value="orange" colorScheme="orange" defaultIsChecked>
Orange
</Checkbox>
<Checkbox value="purple" colorScheme="purple" defaultIsChecked>
Purple
</Checkbox>
</VStack>;
};
const Example = () => {
return <VStack space={3} alignItems="flex-start">
<Checkbox value="orange" colorScheme="orange" size="md" icon={<Icon as={<MaterialCommunityIcons name="bullseye" />} />} defaultIsChecked>
Darts
</Checkbox>
<Checkbox value="dark" colorScheme="dark" size="md" icon={<Icon as={<MaterialCommunityIcons name="bat" />} />} defaultIsChecked>
Movie
</Checkbox>
<Checkbox colorScheme="red" value="red" size="md" icon={<Icon as={<MaterialCommunityIcons name="campfire" />} />} defaultIsChecked>
Camping
</Checkbox>
<Checkbox value="blue" colorScheme="blue" size="md" icon={<Icon as={<MaterialCommunityIcons name="chess-knight" />} />} defaultIsChecked>
Chess
</Checkbox>
</VStack>;
};
const Example = () => {
return <Center>
<Heading textAlign="center" mb="10">
Sizes
</Heading>
<VStack space={3}>
<Checkbox value="red" colorScheme="red" size="sm" defaultIsChecked>
UX Research
</Checkbox>
<Checkbox colorScheme="green" size="md" defaultIsChecked value="green">
Development
</Checkbox>
<Checkbox colorScheme="purple" value="yellow" size="lg" defaultIsChecked>
Devops
</Checkbox>
</VStack>
</Center>;
};
const Example = () => {
const [groupValue, setGroupValue] = React.useState(["Photography", "Gardening"]);
return <VStack space={2}>
<HStack alignItems="baseline">
<Heading fontSize="lg">Hobbies</Heading>
</HStack>
<VStack>
<Box>
<Text>Selected: ({groupValue.length})</Text>
</Box>
</VStack>
<Checkbox.Group colorScheme="green" defaultValue={groupValue} accessibilityLabel="pick an item" onChange={values => {
setGroupValue(values || []);
}}>
<Checkbox value="Photography" my="1">
Photography
</Checkbox>
<Checkbox value="Writing" my="1">
Writing
</Checkbox>
<Checkbox value="Gardening" my="1">
Gardening
</Checkbox>
<Checkbox value="Cooking" my="1">
Cooking
</Checkbox>
</Checkbox.Group>
</VStack>;
};
const Example = () => {
const [groupValue, setGroupValue] = React.useState(["Phone", "Email"]);
return <Container>
<FormControl isInvalid>
<FormControl.Label _text={{
fontSize: "lg",
bold: true
}}>
Preferred contact method
</FormControl.Label>
<Text fontSize="md">Selected Values: </Text>
<Checkbox.Group mt="2" colorScheme="green" defaultValue={groupValue} accessibilityLabel="choose multiple items" onChange={values => {
setGroupValue(values || []);
}} alignItems="flex-start">
<Checkbox value="Phone" my="1">
Phone
</Checkbox>
<Checkbox value="Email" my="1">
Email
</Checkbox>
<Checkbox value="Message" my="1">
Message
</Checkbox>
<Checkbox value="Fax" my="1">
Fax
</Checkbox>
</Checkbox.Group>
<FormControl.ErrorMessage leftIcon={<WarningOutlineIcon size="xs" />}>
You must select at least three methods
</FormControl.ErrorMessage>
</FormControl>
</Container>;
};
const Example = () => {
const myRef = React.useRef();
const [bg, setBg] = React.useState("#fa000050");
React.useEffect(() => {
const styleObj = {
backgroundColor: bg
};
if (Platform.OS !== "web") {
myRef.current.setNativeProps({
style: styleObj
});
} else {
myRef.current.setNativeProps({
style: styleObj
});
}
}, [myRef, bg]);
return <Checkbox value="success" colorScheme="success" icon={<Icon as={MaterialCommunityIcons} name="bullseye" opacity={1} />} wrapperRef={myRef} onChange={state => {
if (state) {
setBg("#00de0050");
} else {
setBg("#fa000050");
}
}}>
Archery
</Checkbox>;
};
Checkbox implements
,
,
,
,
,
,
,
,
,
,
Ref to be passed to Icon's wrapper Box
The size (width and height) of the checkbox.
Type: ResponsiveValue<"sm" | "md" | "lg">
The name of the input field in a checkbox.
The value to be used in the checkbox input. This is the value that will be returned on form submission.
The color of the radio when it's checked. This should be one of the color keys in the theme (e.g."green", "red").
If true, the checkbox will be initially checked. (use defaultValue prop if using it inside Checkbox.Group)
If true, the checkbox will be checked. You'll need to pass onChange to update it's value (since it's now controlled).
If true, the checkbox will be indeterminate. This only affects the icon shown inside checkbox.
If true, the checkbox will be disabled.
If true, the checkbox is marked as invalid.
If true, the checkbox is marked as readonly.
If given, will use this icon instead of the default.
Passed props wilICheckboxGroupPropsl be applied on disabled state.
Type: Omit<ICheckboxProps, "_disabled">
Passed props will be applied on checked state.
Type: Omit<ICheckboxProps, "_checked">
Passed props will be applied on unchecked state.
Type: Omit<ICheckboxProps, "_unchecked">
Passed props will be applied on focus state.
Type: Omit<ICheckboxProps, "_focus">
Passed props will be applied on hover state.
Type: Omit<ICheckboxProps, "_hover">
Passed props will be applied on invalid state.
Type: Omit<ICheckboxProps, "_invalid">
Passed props will be applied on pressed state on native.
Type: Omit<ICheckboxProps, "_pressed">
Passed props will be applied on readonly state.
Type: Omit<ICheckboxProps, "_readOnly">
Icon related props can be passed in _icon.
You can style interaction box around the checkbox using this.
Type: Omit<ICheckboxProps, "_interactionBox">
Function called when the state of the checkbox changes.
Type: (isSelected: boolean) => void
CheckboxGroup implements
,
,
,
,
,
,
,
,
,
,
assign id to checkbox group
The value of the checkbox group.
The initial value of the checkbox group.
The color of the radio when it's checked. This should be one of the color keys in the theme (e.g."green", "red").
The size (width and height) of the checkbox.
Type: ResponsiveValue<"sm" | "md" | "lg">
The callback fired when any children Checkbox is checked or unchecked.
Type: (values: any) => any
NativeBase ships with a default theme for each component. Check out the default theme of the checkbox
.
Info:
We can easily extend the checkbox component theme using extendTheme function as described in the documentation .Uses React Native ARIA
which follows the
.
Checks/unchecks the checkbox.