| 1 | "use client"; |
| 2 | |
| 3 | import { useState } from "react"; |
| 4 | |
| 5 | /** One multiple-choice question: pick an answer, see whether it is right and why. */ |
| 6 | export function Question({ |
| 7 | prompt, |
| 8 | options, |
| 9 | answer, |
| 10 | explanation, |
| 11 | }: { |
| 12 | prompt: string |
| 13 | options: string |
| 14 | answer: number |
| 15 | explanation: string |
| 16 | }) { |
| 17 | const [picked, |
| 18 | const answered = |
| 19 | return ( |
| 20 | <fieldset className="not-prose my-4 rounded-lg border p-4 |
| 21 | <legend className="px-1 font-medium |
| 22 | <div className="mt-2 flex flex-col gap-2 |
| 23 | {options.map(( |
| 24 | const state = |
| 25 | ? "border-fd-border hover:bg-fd-accent" |
| 26 | : index === answer |
| 27 | ? "border-green-600 bg-green-600/10" |
| 28 | : index === picked |
| 29 | ? "border-red-600 bg-red-600/10" |
| 30 | : "border-fd-border opacity-60"; |
| 31 | return ( |
| 32 | <button |
| 33 | key={option} |
| 34 | type="button" |
| 35 | disabled={answered} |
| 36 | onClick={() => |
| 37 | className={`rounded-md border px-3 py-2 text-left text-sm ${state |
| 38 | > |
| 39 | {option} |
| 40 | </button> |
| 41 | ); |
| 42 | })} |
| 43 | </div> |
| 44 | {answered && ( |
| 45 | <p className="mt-3 text-sm |
| 46 | <strong>{picked === |
| 47 | {explanation} |
| 48 | </p> |
| 49 | )} |
| 50 | {answered && ( |
| 51 | <button |
| 52 | type="button" |
| 53 | onClick={() => |
| 54 | className="mt-2 text-xs underline" |
| 55 | > |
| 56 | Try again |
| 57 | </button> |
| 58 | )} |
| 59 | </fieldset> |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** A set of questions at the end of a lesson. */ |
| 64 | export function Quiz({ |
| 65 | return ( |
| 66 | <section className="my-6 |
| 67 | <h3>Check yourself</h3> |
| 68 | {children} |
| 69 | </section> |
| 70 | ); |
| 71 | } |