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}>
One
</Checkbox>
<Checkbox value="two">Two</Checkbox>
</Checkbox.Group>;
};
const Example = () => {
return <Checkbox.Group accessibilityLabel="choose values">
<Checkbox value="one" my={2}>
value 1
</Checkbox>
<Checkbox value="two">value 2</Checkbox>
</Checkbox.Group>;
};
const Example = () => {
return <VStack space={2}>
<Checkbox isDisabled value="one">
Checkbox
</Checkbox>
<Checkbox isDisabled defaultIsChecked value="two">
Checkbox
</Checkbox>
</VStack>;
};
const Example = () => {
return <Checkbox isInvalid value="invalid">
Checkbox
</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 <VStack space={3}>
<Checkbox value="red" colorScheme="red" size="sm" defaultIsChecked>
Checkbox
</Checkbox>
<Checkbox colorScheme="green" size="md" defaultIsChecked value="green">
Checkbox
</Checkbox>
<Checkbox colorScheme="yellow" value="yellow" size="lg" defaultIsChecked>
Checkbox
</Checkbox>
</VStack>;
};
const Example = () => {
const [groupValue, setGroupValue] = React.useState(["Item 1", "Item 3"]);
const getSelectedGroupValue = () => {
if (groupValue.length === 0) return "[]";
let arrayString = groupValue.reduce((accumulator, currentValue) => accumulator + ", " + currentValue);
return "[" + arrayString + "]";
};
return <Box display="flex" justifyContent="space-between" alignItems="center">
<HStack mb={3} alignItems="baseline">
<Heading mt={3}>CheckboxGroup </Heading>
</HStack>
<Checkbox.Group colorScheme="green" defaultValue={groupValue} accessibilityLabel="pick an item" onChange={values => {
setGroupValue(values || []);
}}>
<Checkbox value="Item 1" my={1}>
Item 1
</Checkbox>
<Checkbox value="Item 2" my={1}>
Item 2
</Checkbox>
<Checkbox value="Item 3" my={1}>
Item 3
</Checkbox>
<Checkbox colorScheme="orange" isIndeterminate value="Indeterminate Item" my={1}>
Indeterminate Item
</Checkbox>
</Checkbox.Group>
<VStack mt={3}>
<Box>
<Text fontSize="md">Selected Values: </Text>
<Text fontSize="md" bold>
{getSelectedGroupValue()}
</Text>
</Box>
</VStack>
</Box>;
};
const Example = () => {
const [groupValue, setGroupValue] = React.useState(["Item 1", "Item 3"]);
const getSelectedGroupValue = () => {
if (groupValue.length === 0) return "[]";
let arrayString = groupValue.reduce((accumulator, currentValue) => accumulator + ", " + currentValue);
return "[" + arrayString + "]";
};
return <Container>
<FormControl isRequired isInvalid>
<FormControl.Label>Select Item</FormControl.Label>
<Checkbox.Group colorScheme="green" defaultValue={groupValue} accessibilityLabel="choose multiple items" onChange={values => {
setGroupValue(values || []);
}} alignItems="flex-start">
<Checkbox value="Item 1" my={1}>
Item 1
</Checkbox>
<Checkbox value="Item 2" my={1}>
Item 2
</Checkbox>
<Checkbox value="Item 3" my={1}>
Item 3
</Checkbox>
<Checkbox colorScheme="orange" isIndeterminate value="Indeterminate Item" my={1}>
Indeterminate Item
</Checkbox>
</Checkbox.Group>
<FormControl.HelperText>
We'll keep this between us.
</FormControl.HelperText>
<FormControl.ErrorMessage>Something is wrong.</FormControl.ErrorMessage>
</FormControl>
<VStack mt={4} alignItems="baseline">
<Text fontSize="md">Selected Values: </Text>
<Text fontSize="md" bold>
{getSelectedGroupValue()}
</Text>
</VStack>
</Container>;
};
const Example = () => {
const myRef = React.useRef({});
return <Checkbox value="success" colorScheme="success" icon={<Icon as={MaterialCommunityIcons} name="bullseye" opacity={1} />} ref={myRef} onChange={state => {
if (state) myRef.current.setNativeProps({
backgroundColor: "#00de0050"
});else myRef.current.setNativeProps({
backgroundColor: "#fa000050"
});
}}>
Label
</Checkbox>;
};
Checkbox implements
,
,
,
,
,
,
,
,
,
,
,
,
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. Changes style of unchecked state.
The size (width and height) of the checkbox.
If given, will use this icon instead of the default.
Function called when the state of the checkbox changes.
Type: (isSelected: boolean) => void
Ref to be passed to Icon's wrapper Box
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.
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.