Skip to content

Commit

Permalink
custom arePropsEqual function causing issues
Browse files Browse the repository at this point in the history
sometimes the option doesn't get selected
  • Loading branch information
aaronmcadam committed Aug 5, 2024
1 parent 0fd7146 commit ab0b810
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 54 deletions.
93 changes: 42 additions & 51 deletions src/components/quiz-option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,46 @@ export type QuizOptionProps = {
value: string | boolean;
};

export const QuizOption = React.memo(
function QuizOption({
onClick,
isSelected,
display,
value,
}: QuizOptionProps) {
const type = display === "Yes" || display === "No" ? "boolean" : "image";
export const QuizOption = React.memo(function QuizOption({
onClick,
isSelected,
display,
value,
}: QuizOptionProps) {
const type = display === "Yes" || display === "No" ? "boolean" : "image";

return (
<Button
onClick={onClick}
variant="outline"
className={cn(
"mt-2 h-auto flex flex-col items-center justify-center relative",
{
"ring-2 ring-primary": isSelected,
// TODO: only apply this after clicking the option because otherwise, when navigating back to a question,
// the selected option will blink.
"animate-blink": isSelected,
},
)}
>
{isSelected ? (
<CircleCheck className="h-5 w-5 text-primary absolute top-2 right-2" />
) : null}
{display === "Yes" ? (
<div className="flex items-center h-[154px]">
<Check className="flex-grow text-brand-600 h-16 w-16" />
</div>
) : null}
{display === "No" ? (
<div className="flex items-center h-[154px]">
<X className="text-brand-600 h-16 w-16" />
</div>
) : null}
<span
className={cn({ "h-[154px]": type === "image" })}
dangerouslySetInnerHTML={{ __html: display }}
/>
{value}
</Button>
);
},
(prevProps, nextProps) => {
const arePropsEqual =
prevProps.isSelected === nextProps.isSelected &&
prevProps.display === nextProps.display;

return arePropsEqual;
},
);
return (
<Button
onClick={onClick}
variant="outline"
className={cn(
"mt-2 h-auto flex flex-col items-center justify-center relative",
{
"ring-2 ring-primary": isSelected,
// TODO: only apply this after clicking the option because otherwise, when navigating back to a question,
// the selected option will blink.
"animate-blink": isSelected,
},
)}
>
{isSelected ? (
<CircleCheck className="h-5 w-5 text-primary absolute top-2 right-2" />
) : null}
{display === "Yes" ? (
<div className="flex items-center h-[154px]">
<Check className="flex-grow text-brand-600 h-16 w-16" />
</div>
) : null}
{display === "No" ? (
<div className="flex items-center h-[154px]">
<X className="text-brand-600 h-16 w-16" />
</div>
) : null}
<span
className={cn({ "h-[154px]": type === "image" })}
dangerouslySetInnerHTML={{ __html: display }}
/>
{value}
</Button>
);
});
5 changes: 2 additions & 3 deletions src/components/quiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function Quiz() {
// TODO: Perf: we may want to somehow preload the display HTML because
// we're seeing some flicker when images have to load.
// Hard coding the height of the option helps, but it's not ideal.
// We'd probably need to parse or regex the html to check what element it is.
// We'd probably need to parse or regex the html to check what element it is to preload the image.
React.useEffect(() => {
fetch("/api/questions")
.then((res) => res.json())
Expand All @@ -65,7 +65,6 @@ export function Quiz() {
const currentQuestion = quizState.questions[quizState.currentIndex];

// Update the current question with its response and "complete" status
// and set the next question as "upcoming"
function handleOptionClick(option: Option) {
setQuizState((prevState) => {
const updatedQuestions = prevState.questions.map((q, i) => {
Expand Down Expand Up @@ -117,7 +116,7 @@ export function Quiz() {
result: isRejected ? "rejected" : isAccepted ? "accepted" : null,
};
});
}, 600); // 600ms delay for double blink effect
}, 600); // 600ms delay matches the blink animation duration
}

// If the number of options is a multiple of 3, use 3 columns, otherwise 2
Expand Down

0 comments on commit ab0b810

Please sign in to comment.