🎯 compact question form with better visual hierarchy
Browse files at this commitcda1ab74bd6af6924d62b5907eaffa7fe090cba8Parent: af7500b- refactor
- 🎯 compact question form with better visual hierarchy
1 changed file +140 -138
- src/components/exams/question-form.tsx +140 -138
src/components/exams/question-form.tsxmodified+140 -138
| @@ -126,180 +126,182 @@ export function QuestionForm(props: { | ||
| 126 | 126 | }; |
| 127 | 127 | |
| 128 | 128 | return ( |
| 129 | - <form class="space-y-4" onSubmit={(e) => void submit(e)}> | |
| 130 | - <div class="grid gap-4 sm:grid-cols-[minmax(0,1fr)_15rem] sm:items-start"> | |
| 131 | - <div class="space-y-3"> | |
| 132 | - <div class="space-y-1.5"> | |
| 133 | - <Label for="question-text" class="text-sm font-semibold">{t("questions.text")}</Label> | |
| 134 | - <Textarea | |
| 135 | - id="question-text" | |
| 136 | - ref={textAreaRef} | |
| 137 | - class="min-h-[9rem] resize-none overflow-hidden bg-background text-base" | |
| 138 | - value={text()} | |
| 139 | - maxlength={2000} | |
| 140 | - rows={4} | |
| 141 | - required | |
| 142 | - placeholder={t("questions.text")} | |
| 143 | - onInput={(e) => { | |
| 144 | - setText(e.currentTarget.value); | |
| 145 | - resizeTextArea(); | |
| 146 | - }} | |
| 147 | - /> | |
| 148 | - </div> | |
| 149 | - | |
| 150 | - <div class="rounded-lg border bg-card p-3 shadow-sm"> | |
| 151 | - <Label for="question-image" class="text-xs font-semibold text-muted-foreground">{t("questions.image")}</Label> | |
| 129 | + <form class="space-y-3" onSubmit={(e) => void submit(e)}> | |
| 130 | + <div class="rounded-lg border bg-card p-3.5 shadow-sm"> | |
| 131 | + <div class="space-y-2"> | |
| 132 | + <Label for="question-text" class="text-sm font-semibold">{t("questions.text")}</Label> | |
| 133 | + <Textarea | |
| 134 | + id="question-text" | |
| 135 | + ref={textAreaRef} | |
| 136 | + class="min-h-[7rem] resize-none overflow-hidden bg-background text-base" | |
| 137 | + value={text()} | |
| 138 | + maxlength={2000} | |
| 139 | + rows={3} | |
| 140 | + required | |
| 141 | + placeholder={t("questions.text")} | |
| 142 | + onInput={(e) => { | |
| 143 | + setText(e.currentTarget.value); | |
| 144 | + resizeTextArea(); | |
| 145 | + }} | |
| 146 | + /> | |
| 147 | + <div class="flex items-center gap-3"> | |
| 152 | 148 | <Show when={props.initial?.image}> |
| 153 | - <img | |
| 154 | - src={`/api/exams/${props.initial!.exam}/questions/${props.initial!.id}/image`} | |
| 155 | - alt={t("questions.image")} | |
| 156 | - class="mt-2 h-36 w-full max-w-md rounded-md border bg-muted/20 object-contain" | |
| 157 | - /> | |
| 149 | + <div class="relative shrink-0"> | |
| 150 | + <img | |
| 151 | + src={`/api/exams/${props.initial!.exam}/questions/${props.initial!.id}/image`} | |
| 152 | + alt={t("questions.image")} | |
| 153 | + class="h-14 w-24 rounded border bg-muted/20 object-contain" | |
| 154 | + /> | |
| 155 | + </div> | |
| 158 | 156 | </Show> |
| 159 | - <input | |
| 160 | - id="question-image" | |
| 161 | - type="file" | |
| 162 | - accept="image/png,image/jpeg,image/webp,image/gif" | |
| 163 | - class="sr-only" | |
| 164 | - onChange={(event) => setImage(event.currentTarget.files?.[0] ?? null)} | |
| 165 | - /> | |
| 166 | - <label for="question-image" class="mt-2 flex cursor-pointer items-center gap-2 rounded-md border border-dashed bg-muted/30 px-3 py-2 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted/60 hover:text-foreground"> | |
| 167 | - <IconFileImage class="h-4 w-4" /> | |
| 168 | - <span class="truncate">{image()?.name ?? t("common.createItem", { item: t("questions.image") })}</span> | |
| 169 | - </label> | |
| 170 | - </div> | |
| 171 | - </div> | |
| 172 | - | |
| 173 | - <div class="space-y-3 rounded-lg border bg-card p-3.5 shadow-sm"> | |
| 174 | - <div class="space-y-1.5"> | |
| 175 | - <Label for="question-subject">{t("subjects.subject")}</Label> | |
| 176 | - <Select id="question-subject" class="h-10 py-2" value={subjectId()} required onChange={(e) => setSubjectId(e.currentTarget.value)}> | |
| 177 | - <option value="">{t("subjects.select")}</option> | |
| 178 | - <For each={props.subjects}>{(subject) => <option value={subject.id}>{subject.name}</option>}</For> | |
| 179 | - </Select> | |
| 180 | - </div> | |
| 181 | - | |
| 182 | - <div class="space-y-1.5"> | |
| 183 | - <Label class="text-xs font-semibold uppercase tracking-wider text-muted-foreground">{t("questions.kind")}</Label> | |
| 184 | - <div class="grid grid-cols-2 gap-1 rounded-lg border bg-muted/40 p-1"> | |
| 185 | - <For each={QUESTION_KINDS}> | |
| 186 | - {(k) => ( | |
| 187 | - <button | |
| 188 | - type="button" | |
| 189 | - class={cn( | |
| 190 | - "h-8 rounded-md border text-xs font-semibold transition-colors", | |
| 191 | - kind() === k | |
| 192 | - ? "border-primary bg-primary text-primary-foreground shadow-sm" | |
| 193 | - : "border-transparent text-muted-foreground hover:bg-background/70 hover:text-foreground", | |
| 194 | - )} | |
| 195 | - onClick={() => setKind(k)} | |
| 196 | - > | |
| 197 | - {k === "choice" ? t("questions.kind.choice") : t("questions.kind.text")} | |
| 198 | - </button> | |
| 199 | - )} | |
| 200 | - </For> | |
| 157 | + <div class="flex items-center gap-2"> | |
| 158 | + <input | |
| 159 | + id="question-image" | |
| 160 | + type="file" | |
| 161 | + accept="image/png,image/jpeg,image/webp,image/gif" | |
| 162 | + class="sr-only" | |
| 163 | + onChange={(event) => setImage(event.currentTarget.files?.[0] ?? null)} | |
| 164 | + /> | |
| 165 | + <label for="question-image" class="flex cursor-pointer items-center gap-1.5 rounded-md border border-dashed bg-muted/30 px-2.5 py-1.5 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted/60 hover:text-foreground"> | |
| 166 | + <IconFileImage class="h-3.5 w-3.5" /> | |
| 167 | + <span class="truncate">{image()?.name ?? t("questions.image")}</span> | |
| 168 | + </label> | |
| 201 | 169 | </div> |
| 202 | 170 | </div> |
| 171 | + </div> | |
| 172 | + </div> | |
| 203 | 173 | |
| 204 | - <div class="space-y-1.5"> | |
| 205 | - <Label for="question-points">{t("questions.points")}</Label> | |
| 206 | - <Input | |
| 207 | - id="question-points" | |
| 208 | - type="number" | |
| 209 | - min={1} | |
| 210 | - max={100} | |
| 211 | - value={points()} | |
| 212 | - class="h-10" | |
| 213 | - required | |
| 214 | - onInput={(e) => setPoints(e.currentTarget.value)} | |
| 215 | - /> | |
| 174 | + <div class="flex flex-wrap items-end gap-3 rounded-lg border bg-card px-3.5 py-3 shadow-sm"> | |
| 175 | + <div class="min-w-0 flex-1 basis-[10rem]"> | |
| 176 | + <Label for="question-subject" class="mb-1 block text-xs font-semibold text-muted-foreground">{t("subjects.subject")}</Label> | |
| 177 | + <Select id="question-subject" class="h-9 py-1.5 text-sm" value={subjectId()} required onChange={(e) => setSubjectId(e.currentTarget.value)}> | |
| 178 | + <option value="">{t("subjects.select")}</option> | |
| 179 | + <For each={props.subjects}>{(subject) => <option value={subject.id}>{subject.name}</option>}</For> | |
| 180 | + </Select> | |
| 181 | + </div> | |
| 182 | + <div> | |
| 183 | + <Label class="mb-1 block text-xs font-semibold text-muted-foreground">{t("questions.kind")}</Label> | |
| 184 | + <div class="flex gap-0.5 rounded-md border bg-muted/40 p-0.5"> | |
| 185 | + <For each={QUESTION_KINDS}> | |
| 186 | + {(k) => ( | |
| 187 | + <button | |
| 188 | + type="button" | |
| 189 | + class={cn( | |
| 190 | + "h-7 rounded-sm px-2.5 text-xs font-semibold transition-colors", | |
| 191 | + kind() === k | |
| 192 | + ? "bg-primary text-primary-foreground shadow-xs" | |
| 193 | + : "text-muted-foreground hover:text-foreground", | |
| 194 | + )} | |
| 195 | + onClick={() => setKind(k)} | |
| 196 | + > | |
| 197 | + {k === "choice" ? t("questions.kind.choice") : t("questions.kind.text")} | |
| 198 | + </button> | |
| 199 | + )} | |
| 200 | + </For> | |
| 216 | 201 | </div> |
| 217 | 202 | </div> |
| 203 | + <div class="w-20"> | |
| 204 | + <Label for="question-points" class="mb-1 block text-xs font-semibold text-muted-foreground">{t("questions.points")}</Label> | |
| 205 | + <Input | |
| 206 | + id="question-points" | |
| 207 | + type="number" | |
| 208 | + min={1} | |
| 209 | + max={100} | |
| 210 | + value={points()} | |
| 211 | + class="h-9 text-sm" | |
| 212 | + required | |
| 213 | + onInput={(e) => setPoints(e.currentTarget.value)} | |
| 214 | + /> | |
| 215 | + </div> | |
| 218 | 216 | </div> |
| 219 | 217 | |
| 220 | 218 | <Show when={kind() === "choice"}> |
| 221 | - <div class="space-y-3 rounded-lg border bg-card p-3.5 shadow-sm"> | |
| 222 | - <div class="flex items-center justify-between gap-2 border-b pb-2"> | |
| 223 | - <div> | |
| 224 | - <Label class="text-sm font-semibold">{t("questions.choices")}</Label> | |
| 225 | - <p class="text-xs text-muted-foreground">{t("questions.correctAnswer")}: {String.fromCharCode(65 + correct())}</p> | |
| 226 | - </div> | |
| 227 | - <Button type="button" variant="outline" size="sm" disabled={choices().length >= 10} onClick={addChoice}> | |
| 228 | - <IconPlus class="h-4 w-4" /> | |
| 219 | + <div class="rounded-lg border bg-card p-3.5 shadow-sm"> | |
| 220 | + <div class="mb-3 flex items-center justify-between gap-2 border-b border-border/50 pb-2"> | |
| 221 | + <Label class="text-sm font-semibold">{t("questions.choices")}</Label> | |
| 222 | + <Button type="button" variant="outline" size="sm" class="h-7 gap-1 text-xs" disabled={choices().length >= 10} onClick={addChoice}> | |
| 223 | + <IconPlus class="h-3.5 w-3.5" /> | |
| 229 | 224 | {t("questions.addChoice")} |
| 230 | 225 | </Button> |
| 231 | 226 | </div> |
| 232 | - <div class="grid gap-3 sm:grid-cols-2"> | |
| 227 | + <div class="space-y-2"> | |
| 233 | 228 | <Index each={choices()}> |
| 234 | - {(choice, index) => ( | |
| 235 | - <div class="rounded-md border bg-background p-3 shadow-xs"> | |
| 236 | - <div class="mb-2 flex items-center justify-between gap-2"> | |
| 229 | + {(choice, index) => { | |
| 230 | + const isCorrect = () => correct() === index; | |
| 231 | + return ( | |
| 232 | + <div | |
| 233 | + class={cn( | |
| 234 | + "flex items-start gap-2 rounded-md border p-2 transition-colors", | |
| 235 | + isCorrect() ? "border-emerald-400/60 bg-emerald-50/60" : "border-border bg-background", | |
| 236 | + )} | |
| 237 | + > | |
| 237 | 238 | <button |
| 238 | 239 | type="button" |
| 239 | - class={ | |
| 240 | - correct() === index | |
| 241 | - ? "inline-flex h-8 items-center gap-1.5 rounded-md bg-primary px-2.5 text-xs font-semibold text-primary-foreground shadow-xs" | |
| 242 | - : "inline-flex h-8 items-center gap-1.5 rounded-md border border-input bg-background px-2.5 text-xs font-medium text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground" | |
| 243 | - } | |
| 240 | + class={cn( | |
| 241 | + "mt-0.5 inline-flex h-6 w-6 shrink-0 items-center justify-center rounded text-xs font-bold transition-colors", | |
| 242 | + isCorrect() | |
| 243 | + ? "bg-emerald-600 text-white shadow-xs" | |
| 244 | + : "border bg-background text-muted-foreground hover:border-emerald-400 hover:text-emerald-600", | |
| 245 | + )} | |
| 244 | 246 | onClick={() => setCorrect(index)} |
| 247 | + title={t("questions.correct")} | |
| 245 | 248 | > |
| 246 | - <span class="inline-flex h-4 w-4 items-center justify-center rounded-[3px] border bg-background text-[10px] font-bold text-foreground"> | |
| 247 | - {String.fromCharCode(65 + index)} | |
| 248 | - </span> | |
| 249 | - {correct() === index ? <IconCheck class="h-3.5 w-3.5" /> : t("questions.correct")} | |
| 249 | + {isCorrect() ? <IconCheck class="h-3.5 w-3.5" /> : String.fromCharCode(65 + index)} | |
| 250 | 250 | </button> |
| 251 | + <div class="min-w-0 flex-1 space-y-1.5"> | |
| 252 | + <Input | |
| 253 | + class="h-8 text-sm" | |
| 254 | + value={choice()} | |
| 255 | + maxlength={500} | |
| 256 | + placeholder={t("questions.choicePlaceholder", { index: String.fromCharCode(65 + index) })} | |
| 257 | + onInput={(e) => setChoice(index, e.currentTarget.value)} | |
| 258 | + /> | |
| 259 | + <div class="flex items-center gap-2"> | |
| 260 | + <input | |
| 261 | + id={`choice-image-${index}`} | |
| 262 | + type="file" | |
| 263 | + accept="image/png,image/jpeg,image/webp,image/gif" | |
| 264 | + class="sr-only" | |
| 265 | + onChange={(event) => setChoiceImage(index, event.currentTarget.files?.[0] ?? null)} | |
| 266 | + /> | |
| 267 | + <label for={`choice-image-${index}`} class="flex cursor-pointer items-center gap-1 rounded-md border border-dashed bg-muted/20 px-2 py-0.5 text-[10px] font-medium text-muted-foreground transition-colors hover:bg-muted/60 hover:text-foreground"> | |
| 268 | + <IconFileImage class="h-3 w-3" /> | |
| 269 | + {choiceImages()[index]?.name ?? t("questions.choiceImage")} | |
| 270 | + </label> | |
| 271 | + <Show when={props.initial?.choice_images?.[index]}> | |
| 272 | + <img | |
| 273 | + src={`/api/exams/${props.initial!.exam}/questions/${props.initial!.id}/choices/${index}/image`} | |
| 274 | + alt={t("questions.choiceImage")} | |
| 275 | + class="h-8 w-12 rounded border bg-muted/20 object-contain" | |
| 276 | + /> | |
| 277 | + </Show> | |
| 278 | + </div> | |
| 279 | + </div> | |
| 251 | 280 | <Button |
| 252 | 281 | type="button" |
| 253 | 282 | variant="ghost" |
| 254 | 283 | size="sm" |
| 255 | 284 | disabled={choices().length <= 2} |
| 256 | - class="h-8 w-8 p-0 text-destructive" | |
| 285 | + class="mt-0.5 h-6 w-6 p-0 text-muted-foreground hover:text-destructive" | |
| 257 | 286 | onClick={() => removeChoice(index)} |
| 258 | 287 | > |
| 259 | - <IconTrash class="h-4 w-4" /> | |
| 288 | + <IconTrash class="h-3.5 w-3.5" /> | |
| 260 | 289 | </Button> |
| 261 | 290 | </div> |
| 262 | - <Input | |
| 263 | - class="h-9 text-sm" | |
| 264 | - value={choice()} | |
| 265 | - maxlength={500} | |
| 266 | - placeholder={t("questions.choicePlaceholder", { index: String.fromCharCode(65 + index) })} | |
| 267 | - onInput={(e) => setChoice(index, e.currentTarget.value)} | |
| 268 | - /> | |
| 269 | - <Show when={props.initial?.choice_images?.[index]}> | |
| 270 | - <img | |
| 271 | - src={`/api/exams/${props.initial!.exam}/questions/${props.initial!.id}/choices/${index}/image`} | |
| 272 | - alt={t("questions.choiceImage")} | |
| 273 | - class="mt-2 h-28 w-full max-w-sm rounded-md border bg-muted/20 object-contain" | |
| 274 | - /> | |
| 275 | - </Show> | |
| 276 | - <div class="mt-2"> | |
| 277 | - <input | |
| 278 | - id={`choice-image-${index}`} | |
| 279 | - type="file" | |
| 280 | - accept="image/png,image/jpeg,image/webp,image/gif" | |
| 281 | - class="sr-only" | |
| 282 | - onChange={(event) => setChoiceImage(index, event.currentTarget.files?.[0] ?? null)} | |
| 283 | - /> | |
| 284 | - <label for={`choice-image-${index}`} class="flex cursor-pointer items-center gap-2 rounded-md border border-dashed bg-muted/30 px-2.5 py-1.5 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted/60 hover:text-foreground"> | |
| 285 | - <IconFileImage class="h-3.5 w-3.5" /> | |
| 286 | - <span class="truncate">{choiceImages()[index]?.name ?? t("questions.choiceImage")}</span> | |
| 287 | - </label> | |
| 288 | - </div> | |
| 289 | - </div> | |
| 290 | - )} | |
| 291 | + ); | |
| 292 | + }} | |
| 291 | 293 | </Index> |
| 292 | 294 | </div> |
| 293 | 295 | </div> |
| 294 | 296 | </Show> |
| 295 | 297 | |
| 296 | - {error() && <p class="text-sm text-destructive">{error()}</p>} | |
| 298 | + {error() && <p class="rounded-sm bg-destructive/10 px-3 py-1.5 text-sm text-destructive">{error()}</p>} | |
| 297 | 299 | |
| 298 | - <div class="flex flex-wrap items-center gap-2 pt-2"> | |
| 299 | - <Button type="submit" disabled={pending()}> | |
| 300 | + <div class="flex flex-wrap items-center gap-2 pt-1"> | |
| 301 | + <Button type="submit" class="h-8 text-xs font-semibold" disabled={pending()}> | |
| 300 | 302 | {props.initial ? t("common.update") : t("common.create")} |
| 301 | 303 | </Button> |
| 302 | - <Button type="button" variant="outline" onClick={props.onCancel}> | |
| 304 | + <Button type="button" variant="outline" class="h-8 text-xs font-semibold" onClick={props.onCancel}> | |
| 303 | 305 | {t("common.cancel")} |
| 304 | 306 | </Button> |
| 305 | 307 | </div> |