ARatingForm.tsx 1.47 KB
Newer Older
1
import React, { useState } from "react";
Yoon, Daeki's avatar
Yoon, Daeki committed
2
import { RatingType, AnswerProps } from "../types";
Lee SeoYeon's avatar
Lee SeoYeon committed
3

4
interface Props extends AnswerProps {
Lee SeoYeon's avatar
Lee SeoYeon committed
5
  element: RatingType;
6
}
Lee SeoYeon's avatar
Lee SeoYeon committed
7

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

12
13
  function buttonClick(event: React.MouseEvent<HTMLButtonElement>) {
    event.preventDefault();
Jiwon Yoon's avatar
Jiwon Yoon committed
14
    const { name } = event.currentTarget;
Jiwon Yoon's avatar
Jiwon Yoon committed
15
    answerQuestion.answer = name;
Jiwon Yoon's avatar
Jiwon Yoon committed
16
    setAnswer(name);
17
    setSelectedchoice(event.currentTarget.name);
Jiwon Yoon's avatar
Jiwon Yoon committed
18
19
20
21
22
23
    if (answerQuestion.answer) {
      answerQuestion.requiredCheck = true;
    } else {
      answerQuestion.requiredCheck = false;
    }
    console.log(answerQuestion);
24
  }
Lee SeoYeon's avatar
Lee SeoYeon committed
25
  return (
Jiwon Yoon's avatar
Jiwon Yoon committed
26
    <div className="flex w-full justify-center my-3">
27
      <label className="mt-3">{element.content.minRateDescription}</label>
Lee SeoYeon's avatar
Lee SeoYeon committed
28
      {element.content.choices.map((choice) => (
Jiwon Yoon's avatar
Jiwon Yoon committed
29
        <div className="flex gap-4 mx-1">
30
31
32
33
34
35
36
37
38
39
40
41
          <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
42
43
        </div>
      ))}
44
      <label className="mt-3">{element.content.maxRateDescription}</label>
Lee SeoYeon's avatar
Lee SeoYeon committed
45
46
47
    </div>
  );
};