From 4e73876daf515ff006db54cb9fb3ff5d7a0bf1be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Paj=C4=85k?= Date: Fri, 10 Jan 2025 13:25:21 +0100 Subject: [PATCH] refactor: fix appearance of dropdown questions (#355) * refactor: fix appearance of dropdown questions * fix: dropdown window clipping --------- Co-authored-by: Jakub Wilk --- apps/web/app/components/ui/dropdown-menu.tsx | 6 +- .../components/QuestionSelector.tsx | 88 ++++++++----------- 2 files changed, 37 insertions(+), 57 deletions(-) diff --git a/apps/web/app/components/ui/dropdown-menu.tsx b/apps/web/app/components/ui/dropdown-menu.tsx index 1e5853141..82933c5a1 100644 --- a/apps/web/app/components/ui/dropdown-menu.tsx +++ b/apps/web/app/components/ui/dropdown-menu.tsx @@ -78,11 +78,7 @@ const DropdownMenuItem = React.forwardRef< >(({ className, inset, ...props }, ref) => ( )); diff --git a/apps/web/app/modules/Admin/EditCourse/CourseLessons/NewLesson/QuizLessonForm/components/QuestionSelector.tsx b/apps/web/app/modules/Admin/EditCourse/CourseLessons/NewLesson/QuizLessonForm/components/QuestionSelector.tsx index 567f39f1a..e1b900980 100644 --- a/apps/web/app/modules/Admin/EditCourse/CourseLessons/NewLesson/QuizLessonForm/components/QuestionSelector.tsx +++ b/apps/web/app/modules/Admin/EditCourse/CourseLessons/NewLesson/QuizLessonForm/components/QuestionSelector.tsx @@ -1,9 +1,15 @@ -import { useCallback, useEffect, useRef, useState } from "react"; +import { useCallback, useState } from "react"; import { Icon } from "~/components/Icon"; import { Button } from "~/components/ui/button"; -import { Card } from "~/components/ui/card"; -import { cn } from "~/lib/utils"; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuLabel, + DropdownMenuSeparator, + DropdownMenuTrigger, +} from "~/components/ui/dropdown-menu"; import { QuestionIcons, QuestionType } from "../QuizLessonForm.types"; @@ -13,11 +19,6 @@ type QuestionSelectorProps = { const QuestionSelector = ({ addQuestion }: QuestionSelectorProps) => { const [showOptions, setShowOptions] = useState(false); - const [openUpwards, setOpenUpwards] = useState(false); - const buttonRef = useRef(null); - const cardRef = useRef(null); - - const toggleOptions = () => setShowOptions(!showOptions); const onTypeChoose = useCallback( (type: QuestionType) => { @@ -60,55 +61,38 @@ const QuestionSelector = ({ addQuestion }: QuestionSelectorProps) => { { type: QuestionType.SCALE_1_5, label: "Scale 1 to 5", icon: QuestionIcons.Scale_1_5 }, ]; - useEffect(() => { - if (showOptions && buttonRef.current) { - const buttonRect = buttonRef.current.getBoundingClientRect(); - const windowHeight = window.innerHeight; - const spaceBelow = windowHeight - buttonRect.bottom; - const spaceAbove = buttonRect.top; - - setOpenUpwards(spaceAbove > spaceBelow); - } - }, [showOptions]); return ( -
- - - {showOptions && ( - -

- Select question type: -

+ setShowOptions(open)}> + + + + + + Select question type: + + +
{questionTypes.map(({ type, label, icon }) => { return ( - + + + ); })} - - )} -
+
+ + ); };