|
| 1 | +--- |
| 2 | +title: "QuizGame" |
| 3 | +description: "A multiple choice quiz game." |
| 4 | +author: "@trishaprile" |
| 5 | +version: "1.0.0" |
| 6 | +--- |
| 7 | + |
| 8 | +import { useState } from 'react'; |
| 9 | + |
| 10 | +export const QuizGame = ({ question, options }) => { |
| 11 | + const [selectedIndex, setSelectedIndex] = useState(null); |
| 12 | + const [submitted, setSubmitted] = useState(false); |
| 13 | + |
| 14 | + const handleOptionClick = (index) => { |
| 15 | + if (submitted) return; |
| 16 | + setSelectedIndex(index); |
| 17 | + }; |
| 18 | + |
| 19 | + const handleSubmit = () => { |
| 20 | + if (selectedIndex !== null) { |
| 21 | + setSubmitted(true); |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + const handleReset = () => { |
| 26 | + setSelectedIndex(null); |
| 27 | + setSubmitted(false); |
| 28 | + }; |
| 29 | + |
| 30 | + return ( |
| 31 | + <div className="max-w-md mx-auto p-6 bg-inherit rounded-lg shadow-sm border border-gray-200 dark:border-gray-400"> |
| 32 | + <h3 className="mb-6! mt-0! text-center">{question}</h3> |
| 33 | + <div className="flex flex-col gap-2"> |
| 34 | + {options.map((option, idx) => ( |
| 35 | + <button |
| 36 | + key={idx} |
| 37 | + onClick={() => handleOptionClick(idx)} |
| 38 | + className={` |
| 39 | + text-sm text-left border-0 rounded-md p-[10px] bg-gray-100 text-gray-600 dark:bg-gray-700 dark:text-white |
| 40 | + ${!submitted && 'cursor-pointer hover:bg-gray-200 dark:hover:bg-gray-700'} |
| 41 | + ${!submitted && selectedIndex === idx && 'border! border-blue-300 bg-blue-200! dark:bg-blue-900!'} |
| 42 | + ${submitted && option.isCorrect && 'border! border-green-300 bg-green-100 dark:bg-green-900'} |
| 43 | + ${submitted && selectedIndex === idx && !option.isCorrect && 'border! border-red-300 bg-red-100 dark:bg-red-900'} |
| 44 | + `} |
| 45 | + > |
| 46 | + {option.text} |
| 47 | + </button> |
| 48 | + ))} |
| 49 | + </div> |
| 50 | + <div className="flex justify-end"> |
| 51 | + {!submitted ? ( |
| 52 | + <button |
| 53 | + onClick={handleSubmit} |
| 54 | + disabled={selectedIndex === null} |
| 55 | + className={` |
| 56 | + mt-3 border-none p-3 font-semibold rounded-md cursor-pointer |
| 57 | + ${submitted || selectedIndex === null ? 'bg-gray-300 text-gray-400 cursor-not-allowed!' : 'bg-blue-500 text-white hover:bg-blue-600'} |
| 58 | + `} |
| 59 | + > |
| 60 | + Check Answer |
| 61 | + </button> |
| 62 | + ) : ( |
| 63 | + <button |
| 64 | + onClick={handleReset} |
| 65 | + className="mt-3 border-none py-3 px-7 font-semibold rounded-md cursor-pointer text-white bg-gray-500 hover:bg-gray-400" |
| 66 | + > |
| 67 | + <i className="fa-solid fa-refresh" /> Reset |
| 68 | + </button> |
| 69 | + )} |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + ); |
| 73 | +}; |
| 74 | + |
| 75 | +<QuizGame |
| 76 | + question="What does DOM stand for?" |
| 77 | + options={[ |
| 78 | + { text: 'Document Object Model', isCorrect: true }, |
| 79 | + { text: 'Data Object Model', isCorrect: false }, |
| 80 | + { text: 'Digital Oriented Model', isCorrect: false }, |
| 81 | + { text: 'Document Oriented Markup', isCorrect: false } |
| 82 | + ]} |
| 83 | +/> |
0 commit comments