|
| 1 | +import { useState } from "react"; |
| 2 | +import { Input } from "@/components/ui/input"; |
| 3 | +import { Textarea } from "@/components/ui/textarea"; |
| 4 | +import { Label } from "@/components/ui/label"; |
| 5 | +import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; |
| 6 | +import { Checkbox } from "@/components/ui/checkbox"; |
| 7 | +import { Button } from "@/components/ui/button"; |
| 8 | +import { |
| 9 | + Select, |
| 10 | + SelectContent, |
| 11 | + SelectItem, |
| 12 | + SelectTrigger, |
| 13 | + SelectValue, |
| 14 | +} from "@/components/ui/select"; |
| 15 | +import type { FormQuestion, ApplicationAnswer } from "@/types/application"; |
| 16 | + |
| 17 | +interface FixedFields { |
| 18 | + name: string; |
| 19 | + email: string; |
| 20 | + schoolEmail: string; |
| 21 | + track: string; |
| 22 | +} |
| 23 | + |
| 24 | +interface DynamicFormProps { |
| 25 | + questions: FormQuestion[]; |
| 26 | + fixedFields: FixedFields; |
| 27 | + trackOptions: string[]; |
| 28 | + onSubmit: (answers: ApplicationAnswer[], track: string) => void; |
| 29 | + isPending: boolean; |
| 30 | + submitLabel?: string; |
| 31 | +} |
| 32 | + |
| 33 | +export function DynamicForm({ |
| 34 | + questions, |
| 35 | + fixedFields, |
| 36 | + trackOptions, |
| 37 | + onSubmit, |
| 38 | + isPending, |
| 39 | + submitLabel = "제출", |
| 40 | +}: DynamicFormProps) { |
| 41 | + const [track, setTrack] = useState(fixedFields.track); |
| 42 | + const [answers, setAnswers] = useState<Record<string, string | string[]>>(() => { |
| 43 | + const init: Record<string, string | string[]> = {}; |
| 44 | + for (const q of questions) { |
| 45 | + init[q.id] = q.type === "checkbox" ? [] : ""; |
| 46 | + } |
| 47 | + return init; |
| 48 | + }); |
| 49 | + |
| 50 | + const sorted = [...questions].sort((a, b) => a.order - b.order); |
| 51 | + |
| 52 | + const setAnswer = (questionId: string, value: string | string[]) => { |
| 53 | + setAnswers((prev) => ({ ...prev, [questionId]: value })); |
| 54 | + }; |
| 55 | + |
| 56 | + const toggleCheckbox = (questionId: string, option: string) => { |
| 57 | + setAnswers((prev) => { |
| 58 | + const current = prev[questionId] as string[]; |
| 59 | + const next = current.includes(option) |
| 60 | + ? current.filter((v) => v !== option) |
| 61 | + : [...current, option]; |
| 62 | + return { ...prev, [questionId]: next }; |
| 63 | + }); |
| 64 | + }; |
| 65 | + |
| 66 | + const isValid = () => { |
| 67 | + if (!track) return false; |
| 68 | + for (const q of sorted) { |
| 69 | + if (!q.required) continue; |
| 70 | + const val = answers[q.id]; |
| 71 | + if (Array.isArray(val) ? val.length === 0 : !val) return false; |
| 72 | + } |
| 73 | + return true; |
| 74 | + }; |
| 75 | + |
| 76 | + const handleSubmit = (e: React.FormEvent) => { |
| 77 | + e.preventDefault(); |
| 78 | + const result: ApplicationAnswer[] = sorted.map((q) => ({ |
| 79 | + questionId: q.id, |
| 80 | + value: answers[q.id], |
| 81 | + })); |
| 82 | + onSubmit(result, track); |
| 83 | + }; |
| 84 | + |
| 85 | + return ( |
| 86 | + <form onSubmit={handleSubmit} className="space-y-6"> |
| 87 | + <div className="space-y-4 rounded-lg border p-4"> |
| 88 | + <h3 className="text-sm font-medium text-muted-foreground">기본 정보</h3> |
| 89 | + <div className="space-y-2"> |
| 90 | + <Label>이름</Label> |
| 91 | + <Input value={fixedFields.name} disabled className="bg-muted" /> |
| 92 | + </div> |
| 93 | + <div className="space-y-2"> |
| 94 | + <Label>이메일</Label> |
| 95 | + <Input value={fixedFields.email} disabled className="bg-muted" /> |
| 96 | + </div> |
| 97 | + <div className="space-y-2"> |
| 98 | + <Label>학교 이메일</Label> |
| 99 | + <Input value={fixedFields.schoolEmail} disabled className="bg-muted" /> |
| 100 | + </div> |
| 101 | + <div className="space-y-2"> |
| 102 | + <Label>희망 트랙</Label> |
| 103 | + <Select value={track} onValueChange={(v) => setTrack(v ?? "")}> |
| 104 | + <SelectTrigger> |
| 105 | + <SelectValue placeholder="트랙 선택" /> |
| 106 | + </SelectTrigger> |
| 107 | + <SelectContent> |
| 108 | + {trackOptions.map((t) => ( |
| 109 | + <SelectItem key={t} value={t}>{t}</SelectItem> |
| 110 | + ))} |
| 111 | + </SelectContent> |
| 112 | + </Select> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + |
| 116 | + {sorted.map((q) => ( |
| 117 | + <div key={q.id} className="space-y-2"> |
| 118 | + <Label> |
| 119 | + {q.label} |
| 120 | + {q.required && <span className="ml-1 text-destructive">*</span>} |
| 121 | + </Label> |
| 122 | + |
| 123 | + {q.type === "short_text" && ( |
| 124 | + <Input |
| 125 | + value={answers[q.id] as string} |
| 126 | + onChange={(e) => setAnswer(q.id, e.target.value)} |
| 127 | + /> |
| 128 | + )} |
| 129 | + |
| 130 | + {q.type === "long_text" && ( |
| 131 | + <Textarea |
| 132 | + value={answers[q.id] as string} |
| 133 | + onChange={(e) => setAnswer(q.id, e.target.value)} |
| 134 | + rows={4} |
| 135 | + /> |
| 136 | + )} |
| 137 | + |
| 138 | + {q.type === "multiple_choice" && q.options && ( |
| 139 | + <RadioGroup |
| 140 | + value={answers[q.id] as string} |
| 141 | + onValueChange={(v) => setAnswer(q.id, v)} |
| 142 | + > |
| 143 | + {q.options.map((opt) => ( |
| 144 | + <div key={opt} className="flex items-center space-x-2"> |
| 145 | + <RadioGroupItem value={opt} id={`${q.id}-${opt}`} /> |
| 146 | + <Label htmlFor={`${q.id}-${opt}`} className="cursor-pointer font-normal"> |
| 147 | + {opt} |
| 148 | + </Label> |
| 149 | + </div> |
| 150 | + ))} |
| 151 | + </RadioGroup> |
| 152 | + )} |
| 153 | + |
| 154 | + {q.type === "checkbox" && q.options && ( |
| 155 | + <div className="space-y-2"> |
| 156 | + {q.options.map((opt) => ( |
| 157 | + <label key={opt} className="flex cursor-pointer items-center gap-2"> |
| 158 | + <Checkbox |
| 159 | + checked={(answers[q.id] as string[]).includes(opt)} |
| 160 | + onCheckedChange={() => toggleCheckbox(q.id, opt)} |
| 161 | + /> |
| 162 | + <span className="text-sm">{opt}</span> |
| 163 | + </label> |
| 164 | + ))} |
| 165 | + </div> |
| 166 | + )} |
| 167 | + </div> |
| 168 | + ))} |
| 169 | + |
| 170 | + <Button type="submit" className="w-full" disabled={!isValid() || isPending}> |
| 171 | + {isPending ? "제출 중..." : submitLabel} |
| 172 | + </Button> |
| 173 | + </form> |
| 174 | + ); |
| 175 | +} |
0 commit comments