Fixed UI of Dropdown Button

This commit is contained in:
Lucas Oliveira 2023-08-31 11:15:12 -03:00
commit fc0e868515

View file

@ -1,8 +1,13 @@
import { Fragment, useState } from "react"; import { useState } from "react";
import { dropdownButtonPropsType } from "../../types/components";
import IconComponent from "../genericIconComponent"; import IconComponent from "../genericIconComponent";
import { Button } from "../ui/button"; import { Button } from "../ui/button";
import { dropdownButtonPropsType } from "../../types/components"; import {
import { Transition } from "@headlessui/react"; DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "../ui/dropdown-menu";
export default function DropdownButton({ export default function DropdownButton({
firstButtonName, firstButtonName,
@ -12,54 +17,51 @@ export default function DropdownButton({
const [showOptions, setShowOptions] = useState<boolean>(false); const [showOptions, setShowOptions] = useState<boolean>(false);
return ( return (
<div className="align-center relative flex"> <div>
<div> <DropdownMenu open={showOptions}>
<Button <DropdownMenuTrigger asChild>
variant="primary" <Button
className="mr-4 w-full" variant="primary"
onClick={onFirstBtnClick} className="relative pr-10"
> onClick={(event) => {
{firstButtonName} event.stopPropagation();
</Button> event.preventDefault();
</div> onFirstBtnClick();
<div> }}
<button >
className="absolute inset-y-0 right-0 items-center text-muted-foreground" {firstButtonName}
onClick={(event) => { <div
className="absolute right-2 items-center text-muted-foreground"
onClick={(event) => {
event.stopPropagation();
event.preventDefault();
setShowOptions(!showOptions);
}}
>
{!showOptions ? (
<IconComponent
name="ChevronDown"
/>
) : (
<IconComponent name="ChevronUp" />
)}
</div>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent
onPointerDownOutside={(event) => {
event.stopPropagation(); event.stopPropagation();
event.preventDefault();
setShowOptions(!showOptions); setShowOptions(!showOptions);
}} }}
> >
{!showOptions ? (
<IconComponent name="ChevronDown" className="" aria-hidden="true" />
) : (
<IconComponent name="ChevronUp" />
)}
</button>
</div>
<Transition
show={showOptions}
leave="transition ease-in duration-100"
leaveFrom="opacity-100"
leaveTo="opacity-0"
as={Fragment}
enter="transition ease-in duration-100"
enterFrom="opacity-0"
enterTo="opacity-100"
>
<div className="absolute top-10 w-full bg-background pb-0.5 pr-0.5 pl-0.5 rounded-lg shadow-lg ">
{options.map(({ name, onBtnClick }, index) => ( {options.map(({ name, onBtnClick }, index) => (
<Button <DropdownMenuItem onClick={onBtnClick} key={index}>
className="w-full mt-1"
variant="primary"
onClick={onBtnClick}
key={index}
>
{name} {name}
</Button> </DropdownMenuItem>
))} ))}
</div> </DropdownMenuContent>
</Transition> </DropdownMenu>
</div> </div>
); );
} }