import React from
"react"
;
import {
ChakraProvider,
CSSReset,
Box,
Stack,
Radio,
RadioGroup,
HStack,
useRadio,
useRadioGroup,
Heading,
} from
"@chakra-ui/react"
;
function
RadioCard(props) {
const { getInputProps, getRadioProps } = useRadio(props);
const input = getInputProps();
const checkbox = getRadioProps();
return
(
<Box as=
"label"
>
<input {...input} />
<Box
{...checkbox}
cursor=
"pointer"
borderWidth=
"1px"
borderRadius=
"md"
boxShadow=
"md"
_checked={{
bg:
"teal.600"
,
color:
"white"
,
borderColor:
"teal.600"
,
}}
_focus={{
boxShadow:
"outline"
,
}}
px={5}
py={3}
>
{props.children}
</Box>
</Box>
);
}
function
App() {
const options = [
"courses"
,
"articles"
,
"programming-languages"
];
const { getRootProps: radioGroupRootProps, getRadioProps: radioProps } =
useRadioGroup({
name:
"example-radio-group"
,
defaultValue:
"courses"
,
onChange: console.log,
});
const radioGroup = radioGroupRootProps();
return
(
<ChakraProvider>
<CSSReset />
<Box p={4}>
<Heading as=
"h1"
color=
"green.500"
>
GeeksforGeeks
</Heading>
<Heading as=
"h3"
mb={4}>
Chakra UI Form Radio
</Heading>
<Stack spacing={4}>
<RadioGroup {...radioGroup}>
<Stack direction=
"row"
>
{options.map((value) => (
<Radio key={value} value={value}>
{value ===
"courses"
?
"Courses"
: value ===
"articles"
?
"Articles"
:
"Programming Languages"
}
</Radio>
))}
</Stack>
</RadioGroup>
<RadioGroup defaultValue=
"programming-languages"
>
<Stack spacing={5} direction=
"row"
>
<Radio colorScheme=
"red"
value=
"courses"
>
Courses - Custom Color
</Radio>
<Radio colorScheme=
"green"
value=
"articles"
>
Articles - Custom Color
</Radio>
<Radio
colorScheme=
"blue"
value=
"programming-languages"
>
Programming Languages - Custom Color
</Radio>
</Stack>
</RadioGroup>
<Stack>
<Radio
size=
"sm"
name=
"size"
colorScheme=
"red"
value=
"courses"
>
Courses - Small
</Radio>
<Radio
size=
"md"
name=
"size"
colorScheme=
"green"
value=
"articles"
>
Articles - Medium
</Radio>
<Radio
size=
"lg"
name=
"size"
colorScheme=
"orange"
defaultChecked
value=
"programming-languages"
>
Programming Languages - Large
</Radio>
</Stack>
<RadioGroup defaultValue=
"courses"
>
<Stack>
<Radio value=
"courses"
isDisabled>
Courses - Disabled
</Radio>
<Radio value=
"articles"
>Articles - Enabled</Radio>
<Radio value=
"programming-languages"
>
Programming Languages - Enabled
</Radio>
</Stack>
</RadioGroup>
<RadioGroup defaultValue=
"courses"
>
<Stack spacing={4} direction=
"row"
>
<Radio value=
"courses"
isDisabled>
Courses - Horizontal
</Radio>
<Radio value=
"articles"
>
Articles - Horizontal
</Radio>
<Radio value=
"programming-languages"
>
Programming Languages - Horizontal
</Radio>
</Stack>
</RadioGroup>
<Radio isInvalid>Invalid Radio - Custom</Radio>
<HStack {...radioGroup}>
{options.map((value) => (
<RadioCard key={value} {...radioProps({ value })}>
{value ===
"courses"
?
"Courses"
: value ===
"articles"
?
"Articles"
:
"Programming Languages"
}{
" "
}
- Custom Radio
</RadioCard>
))}
</HStack>
</Stack>
</Box>
</ChakraProvider>
);
}
export
default
App;