Skip to content

Commit

Permalink
refactor: extract a component for the quiz option
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronmcadam committed Aug 5, 2024
1 parent 90b00b7 commit 639d4e4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
37 changes: 37 additions & 0 deletions src/components/quiz-option.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from "react";
import { cn } from "@/lib/utils";
import { Button } from "./ui/button";
import { CircleCheck } from "lucide-react";

export type QuizOptionProps = {
onClick: () => void;
isSelected: boolean;
display: string;
value: string | boolean;
};

export const QuizOption = React.memo(function QuizOption({
onClick,
isSelected,
display,
value,
}: QuizOptionProps) {
return (
<Button
onClick={onClick}
variant="outline"
className={cn("mt-2 h-auto flex flex-col 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}
<span dangerouslySetInnerHTML={{ __html: display }} />
{value}
</Button>
);
});
23 changes: 6 additions & 17 deletions src/components/quiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import * as React from "react";
import { cn } from "@/lib/utils";
import { Button } from "./ui/button";
import { CircleCheck } from "lucide-react";
import { QuizNavigation } from "./quiz-navigation";
import { QuizResult } from "./quiz-result";
import { QuizOption } from "./quiz-option";

type Option = {
display: string;
Expand Down Expand Up @@ -157,23 +156,13 @@ export function Quiz() {
const isOptionSelected = currentQuestion.response === o;

return (
<Button
<QuizOption
key={i}
onClick={() => handleOptionClick(o)}
variant="outline"
className={cn("mt-2 h-auto flex flex-col relative", {
"ring-2 ring-primary": isOptionSelected,
// TODO: only apply this after clicking the option because otherwise, when navigating back to a question,
// the selected option will blink.
"animate-blink": isOptionSelected,
})}
>
{isOptionSelected ? (
<CircleCheck className="h-5 w-5 text-primary absolute top-2 right-2" />
) : null}
<span dangerouslySetInnerHTML={{ __html: o.display }} />
{o.value}
</Button>
isSelected={isOptionSelected}
display={o.display}
value={o.value}
/>
);
})}
</div>
Expand Down

0 comments on commit 639d4e4

Please sign in to comment.