Compare commits

...

2 Commits

Author SHA1 Message Date
Aaryan Khandelwal
21e24acb40 chore: disable scroll when dropdown is opened 2023-08-21 16:38:50 +05:30
Anmol Singh Bhatia
81710e97cc chore: dynamic postion dropdown 2023-08-21 16:31:59 +05:30

View File

@ -1,7 +1,9 @@
import React, { useState } from "react"; import React, { useState, useEffect, useRef } from "react";
// headless ui // headless ui
import { Combobox, Transition } from "@headlessui/react"; import { Combobox, Transition } from "@headlessui/react";
// hooks
import useOutsideClickDetector from "hooks/use-outside-click-detector";
// icons // icons
import { ChevronDownIcon } from "@heroicons/react/20/solid"; import { ChevronDownIcon } from "@heroicons/react/20/solid";
import { CheckIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline"; import { CheckIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline";
@ -47,6 +49,11 @@ export const CustomSearchSelect = ({
verticalPosition = "bottom", verticalPosition = "bottom",
width = "auto", width = "auto",
}: CustomSearchSelectProps) => { }: CustomSearchSelectProps) => {
const [isOpen, setIsOpen] = useState(false);
const dropdownBtn = useRef<any>(null);
const dropdownOptions = useRef<any>(null);
const [query, setQuery] = useState(""); const [query, setQuery] = useState("");
const filteredOptions = const filteredOptions =
@ -62,21 +69,51 @@ export const CustomSearchSelect = ({
if (multiple) props.multiple = true; if (multiple) props.multiple = true;
const handleOnOpen = () => {
const dropdownOptionsRef = dropdownOptions.current;
const dropdownBtnRef = dropdownBtn.current;
if (!dropdownOptionsRef || !dropdownBtnRef) return;
const dropdownWidth = dropdownOptionsRef.clientWidth;
const dropdownHeight = dropdownOptionsRef.clientHeight;
const dropdownBtnX = dropdownBtnRef.getBoundingClientRect().x;
const dropdownBtnY = dropdownBtnRef.getBoundingClientRect().y;
const dropdownBtnHeight = dropdownBtnRef.clientHeight;
let top = dropdownBtnY + dropdownBtnHeight;
if (dropdownBtnY + dropdownHeight > window.innerHeight)
top = dropdownBtnY - dropdownHeight - 10;
let left = dropdownBtnX;
if (dropdownBtnX + dropdownWidth > window.innerWidth) left = dropdownBtnX - dropdownWidth;
dropdownOptionsRef.style.top = `${Math.round(top)}px`;
dropdownOptionsRef.style.left = `${Math.round(left)}px`;
};
useOutsideClickDetector(dropdownOptions, () => {
if (isOpen) setIsOpen(false);
});
return ( return (
<Combobox <Combobox as="div" className={`relative flex-shrink-0 text-left ${className}`} {...props}>
as="div"
className={`${selfPositioned ? "" : "relative"} flex-shrink-0 text-left ${className}`}
{...props}
>
{({ open }: { open: boolean }) => { {({ open }: { open: boolean }) => {
if (open && onOpen) onOpen(); if (open) {
handleOnOpen();
if (onOpen) onOpen();
}
return ( return (
<> <>
{customButton ? ( {customButton ? (
<Combobox.Button as="div">{customButton}</Combobox.Button> <Combobox.Button as="div" ref={dropdownBtn}>
{customButton}
</Combobox.Button>
) : ( ) : (
<Combobox.Button <Combobox.Button
ref={dropdownBtn}
type="button" type="button"
className={`flex items-center justify-between gap-1 w-full rounded-md shadow-sm border border-custom-border-300 duration-300 focus:outline-none ${ className={`flex items-center justify-between gap-1 w-full rounded-md shadow-sm border border-custom-border-300 duration-300 focus:outline-none ${
input ? "px-3 py-2 text-sm" : "px-2.5 py-1 text-xs" input ? "px-3 py-2 text-sm" : "px-2.5 py-1 text-xs"
@ -102,10 +139,10 @@ export const CustomSearchSelect = ({
leaveFrom="opacity-100 translate-y-0" leaveFrom="opacity-100 translate-y-0"
leaveTo="opacity-0 translate-y-1" leaveTo="opacity-0 translate-y-1"
> >
<div className="fixed z-20 top-0 left-0 h-full w-full cursor-auto">
<Combobox.Options <Combobox.Options
className={`absolute z-10 min-w-[10rem] border border-custom-border-300 p-2 rounded-md bg-custom-background-90 text-xs shadow-lg focus:outline-none ${ ref={dropdownOptions}
position === "left" ? "left-0 origin-top-left" : "right-0 origin-top-right" className={`fixed z-10 min-w-[10rem] border border-custom-border-300 p-2 rounded-md bg-custom-background-90 text-xs shadow-lg focus:outline-none ${
} ${verticalPosition === "top" ? "bottom-full mb-1" : "mt-1"} ${
width === "auto" ? "min-w-[8rem] whitespace-nowrap" : width width === "auto" ? "min-w-[8rem] whitespace-nowrap" : width
} ${optionsClassName}`} } ${optionsClassName}`}
> >
@ -154,7 +191,9 @@ export const CustomSearchSelect = ({
}`} }`}
> >
<CheckIcon <CheckIcon
className={`h-3 w-3 ${selected ? "opacity-100" : "opacity-0"}`} className={`h-3 w-3 ${
selected ? "opacity-100" : "opacity-0"
}`}
/> />
</div> </div>
) : ( ) : (
@ -177,6 +216,7 @@ export const CustomSearchSelect = ({
</div> </div>
{footerOption} {footerOption}
</Combobox.Options> </Combobox.Options>
</div>
</Transition> </Transition>
</> </>
); );