Skip to content

Instantly share code, notes, and snippets.

@riceboyler
Created October 27, 2021 16:54
Show Gist options
  • Save riceboyler/7cbf1e353e30dba21d180037a8a86378 to your computer and use it in GitHub Desktop.
Save riceboyler/7cbf1e353e30dba21d180037a8a86378 to your computer and use it in GitHub Desktop.
import { useRadio, UseRadioProps, Box } from '@chakra-ui/react';
import React from 'react';
import { QuestionResponse } from '../types';
interface Props extends UseRadioProps {
children: React.ReactNode;
question: QuestionResponse;
}
const RadioButton = (props: Props) => {
const { getInputProps, getCheckboxProps } = useRadio(props);
const input = getInputProps();
const checkbox = getCheckboxProps();
const matchesDefaultValue = props.value === props.question.suggested_response;
return (
<Box as="label">
<input
{...input}
/>
<Box
{...checkbox}
cursor="pointer"
borderWidth="1px"
borderRadius="md"
boxShadow="md"
bgColor={matchesDefaultValue ? 'green.50' : 'transparent'}
_checked={{
bg: matchesDefaultValue ? "blue.600" : "red.600",
color: "white",
borderColor: matchesDefaultValue ? "blue.600" : "red.600",
}}
_focus={{
boxShadow: "outline",
}}
px={5}
py={3}
>
{props.children}
</Box>
</Box>
);
};
export default RadioButton;
import { HStack, useRadioGroup } from '@chakra-ui/react';
import React from 'react';
import { QuestionResponse } from '../types';
import RadioButton from './RadioButton';
interface Props {
question: QuestionResponse;
value: string;
onChange: (e: string) => void;
}
const RadioCard = ({ question, value, onChange }: Props) => {
const options = question.excludeNA ? ['Yes', 'No'] : ['Yes', 'No', 'N/A'];
const { getRootProps, getRadioProps } = useRadioGroup({
name: `question-${question.question_id}`,
defaultValue: question.actual_response ?? question.suggested_response?.toString(),
value,
onChange: onChange
});
const group = getRootProps();
return (
<HStack
{...group}
w="200px"
>
{options.map((value) => {
const radio = getRadioProps({ value });
return (
<RadioButton
key={value}
question={question}
{...radio}
>
{value}
</RadioButton>
);
})}
</HStack>
);
};
export default RadioCard;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment