ARatingForm.tsx 1.45 KB
Newer Older
1
import React, { useState } from "react";
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { RatingType, AnswerType } from "../types";
Lee SeoYeon's avatar
Lee SeoYeon committed
3
4
5

type Props = {
  element: RatingType;
Jiwon Yoon's avatar
Jiwon Yoon committed
6
7
  response: AnswerType;
  handleAnswer: () => void;
Lee SeoYeon's avatar
Lee SeoYeon committed
8
9
};

Jiwon Yoon's avatar
Jiwon Yoon committed
10
export const ARatingForm = ({ element, response, handleAnswer }: Props) => {
11
  const [selectedchoice, setSelectedchoice] = useState("");
Jiwon Yoon's avatar
Jiwon Yoon committed
12
13
  const [answer, setAnswer] = useState("");

14
15
  function buttonClick(event: React.MouseEvent<HTMLButtonElement>) {
    event.preventDefault();
Jiwon Yoon's avatar
Jiwon Yoon committed
16
17
18
19
20
21
22
    const { name } = event.currentTarget;
    response.answers.map((a) => {
      if (a.questionId === element._id) {
        a.answer = name;
      }
    });
    setAnswer(name);
23
    setSelectedchoice(event.currentTarget.name);
Jiwon Yoon's avatar
Jiwon Yoon committed
24
    handleAnswer();
25
  }
Lee SeoYeon's avatar
Lee SeoYeon committed
26
  return (
27
28
    <div className="flex w-full justify-center space-x-12 my-3">
      <label className="mt-3">{element.content.minRateDescription}</label>
Lee SeoYeon's avatar
Lee SeoYeon committed
29
      {element.content.choices.map((choice) => (
30
31
32
33
34
35
36
37
38
39
40
41
42
        <div className="flex gap-4">
          <button
            type="button"
            className="border border-themeColor rounded-full w-12 h-12 text-center hover:bg-slate-300"
            name={choice.text}
            onClick={buttonClick}
            style={{
              backgroundColor:
                selectedchoice === choice.text ? "#58ACFA" : "white",
            }}
          >
            {choice.text}
          </button>
Lee SeoYeon's avatar
Lee SeoYeon committed
43
44
        </div>
      ))}
45
      <label className="mt-3">{element.content.maxRateDescription}</label>
Lee SeoYeon's avatar
Lee SeoYeon committed
46
47
48
    </div>
  );
};