Skip to content

Commit 7cd244a

Browse files
authored
new(component): QuizGame (#32)
* feat: quiz game * cleanup * border colors * update img * add reset button
1 parent 81ee70c commit 7cd244a

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

components/QuizGame/QuizGame.mdx

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
/>

components/QuizGame/quiz-game.png

73.9 KB
Loading

components/QuizGame/readme.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# QuizGame
2+
3+
## Overview
4+
5+
`<QuizGame />` can be used to display a question with multiple-choice answers, let the user select an option, and then check if the answer is correct.
6+
7+
## Usage
8+
9+
```mdx
10+
<QuizGame
11+
question="What does DOM stand for?"
12+
options={[
13+
{ text: 'Document Object Model', isCorrect: true },
14+
{ text: 'Data Object Model', isCorrect: false },
15+
{ text: 'Digital Oriented Model', isCorrect: false },
16+
{ text: 'Document Oriented Markup', isCorrect: false },
17+
]}
18+
/>
19+
```
20+
21+
## Props
22+
23+
| Prop | Type | Description |
24+
| ---------- | ------ | ----------------------------------------------------------------------------------------- |
25+
| `question` | string | The question displayed at the top of the quiz. |
26+
| `options` | array | An array of answer options. Each object must have `{ text: string, isCorrect: boolean }`. |

0 commit comments

Comments
 (0)