Skip to content

Commit 6d0bd8b

Browse files
Fix form
1 parent ca3797b commit 6d0bd8b

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

components/QuizExamFormUF.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Props = {
2424
images?: { url: string; alt: string }[];
2525
};
2626

27-
const QuizExamFormUF: FC<Props> = ({
27+
const QuizExamForm: FC<Props> = ({
2828
isLoading,
2929
handleNextQuestion,
3030
handleSkipQuestion,
@@ -53,13 +53,13 @@ const QuizExamFormUF: FC<Props> = ({
5353
? options?.filter((el: any) => el.isAnswer === true).length
5454
: 0;
5555

56-
const { control, handleSubmit, setValue, watch } = useForm({
56+
const { control, handleSubmit, setValue, watch, register } = useForm({
5757
defaultValues: {
5858
options: [{ checked: false, text: "Option 1", isAnswer: false }],
5959
},
6060
});
6161

62-
const { fields, append, remove } = useFieldArray({
62+
const { fields, replace } = useFieldArray({
6363
control,
6464
name: "options",
6565
});
@@ -129,12 +129,8 @@ const QuizExamFormUF: FC<Props> = ({
129129
};
130130

131131
const isQuestionAnswered = () => {
132-
for (const answer of fields) {
133-
if (answer.checked === true) {
134-
return true;
135-
}
136-
}
137-
return false;
132+
const options = watch("options");
133+
return options.some((option) => option.checked);
138134
};
139135

140136
const isOptionChecked = (optionText: string): boolean | undefined => {
@@ -155,7 +151,7 @@ const QuizExamFormUF: FC<Props> = ({
155151
text: option.text,
156152
isAnswer: option.isAnswer,
157153
}));
158-
append(opt || []);
154+
replace(opt || []);
159155
}, [options]);
160156

161157
const saveAnswers = async (skip = false) => {
@@ -240,7 +236,8 @@ const QuizExamFormUF: FC<Props> = ({
240236
<input
241237
{...field}
242238
type="checkbox"
243-
id={`options.${index}`}
239+
id={`options.${currentQuestionIndex}.${index}`}
240+
className={`peer hidden [&:checked_+_label_svg_path]:block `}
244241
disabled={showCorrectAnswer}
245242
checked={field.value}
246243
onChange={(e) => {
@@ -259,7 +256,7 @@ const QuizExamFormUF: FC<Props> = ({
259256
)}
260257
/>
261258
<label
262-
htmlFor={`options.${index}`}
259+
htmlFor={`options.${currentQuestionIndex}.${index}`}
263260
className={`m-[1px] flex cursor-pointer items-center rounded-lg border hover:bg-slate-600 p-4 text-xs sm:text-sm font-medium shadow-sm ${
264261
showCorrectAnswer && option.isAnswer
265262
? option.checked
@@ -382,4 +379,4 @@ const QuizExamFormUF: FC<Props> = ({
382379
);
383380
};
384381

385-
export default QuizExamFormUF;
382+
export default QuizExamForm;

0 commit comments

Comments
 (0)