From 7f32d6de27a0e9b372d25ef6f86735f27efd3ec9 Mon Sep 17 00:00:00 2001 From: Musix Date: Fri, 1 Dec 2023 16:00:45 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat(#309):=20=ED=85=9C=ED=94=8C?= =?UTF-8?q?=EB=A6=BF=20=ED=8E=98=EC=9D=B4=EC=A7=80=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mypage/template/create/page.tsx | 94 +++++++++++-------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/src/app/(user-menu)/mypage/template/create/page.tsx b/src/app/(user-menu)/mypage/template/create/page.tsx index 069691e2..5b51dde6 100644 --- a/src/app/(user-menu)/mypage/template/create/page.tsx +++ b/src/app/(user-menu)/mypage/template/create/page.tsx @@ -1,8 +1,9 @@ "use client"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { useToast } from "@/components/ui/use-toast"; +import { Command } from "cmdk"; import { createTemplate } from "@/services/template/createTemplate"; import Button, { buttonSize } from "@/components/_common/Button"; import Icon from "@/components/_common/Icon"; @@ -11,7 +12,6 @@ import InputModal from "@/components/_common/Modal/InputModal"; const CreateTemplatePage = () => { const [question, setQuestion] = useState([{ id: 1, value: "" }]); const [content, setContent] = useState(""); - const [count, setCount] = useState(2); const [isModalOpen, setIsModalOpen] = useState(false); const { toast } = useToast(); const router = useRouter(); @@ -38,16 +38,20 @@ const CreateTemplatePage = () => { }; const addQuestion = () => { - const newQuestion = { - id: count, - value: "", - }; - setQuestion((prev) => [...prev, newQuestion]); - setCount(count + 1); + setQuestion((prev) => [ + ...prev, + { id: prev[prev.length - 1].id + 1, value: "" }, + ]); }; const removeQuestion = (id: number) => { - setCount(count - 1); + if (question.length === 1) { + toast({ + description: "질문은 최소 1개 이상이어야 합니다.", + variant: "red", + }); + return; + } setQuestion((prev) => prev.filter((item) => item.id !== id)); }; @@ -78,6 +82,22 @@ const CreateTemplatePage = () => { router.push("/mypage/template"); }; + const handleKeyDown = (event: React.KeyboardEvent) => { + if (event.nativeEvent.isComposing) { + return; + } + if (event.key === "Enter") { + addQuestion(); + } + }; + + useEffect(() => { + const curInput = question.reduce((prev, cur) => { + return cur.id > prev.id ? cur : prev; + }); + document.getElementById(`question-${curInput.id}`)?.focus(); + }, [question.length]); + return (
@@ -93,39 +113,35 @@ const CreateTemplatePage = () => {
- {question.map((item, index) => ( -
( + -
- handleInputChange(event, item.id)} - /> -
{ - if (count === 2) { - toast({ - description: "질문은 최소 1개 이상이어야 합니다.", - variant: "red", - }); - } else { - removeQuestion(item.id); - } - }} - > - +
+ + handleInputChange(event, item.id)} /> + +
removeQuestion(item.id)} + > + +
-
+ ))}