diff --git a/src/components/Select.tsx b/src/components/Select.tsx index ceef277..97c2d37 100755 --- a/src/components/Select.tsx +++ b/src/components/Select.tsx @@ -27,13 +27,15 @@ const Select: React.FC = ({ primaryColor = DEFAULT_THEME, formatGroupLabel = null, formatOptionLabel = null, - classNames + classNames, + scrollableContainer = null }) => { const [open, setOpen] = useState(menuIsOpen); const [list, setList] = useState(options); const [inputValue, setInputValue] = useState(""); const ref = useRef(null); const searchBoxRef = useRef(null); + const [showOptionAboveTheSelect, setShowOptionAboveTheSelect] = useState(false); useEffect(() => { const formatItem = (item: Option) => { @@ -166,6 +168,22 @@ const Select: React.FC = ({ [classNames, isDisabled] ); + const handleScroll = useCallback(() => { + if (ref.current) { + const { top } = ref.current.getBoundingClientRect(); + // check if the component is on the bottom half of the page or on the top half + setShowOptionAboveTheSelect(top > window.innerHeight / 2); + } + }, [ref]); + + useEffect(handleScroll, [ref]); + + useEffect(() => { + const _scrollableContainer = scrollableContainer || window.document; + _scrollableContainer.addEventListener('scroll', handleScroll); + return () => _scrollableContainer.removeEventListener('scroll', handleScroll); + }, [scrollableContainer]); + return ( = ({ {open && !isDisabled && (
{isSearchable && ( diff --git a/src/components/type.ts b/src/components/type.ts index 1e5b8dc..8303d15 100644 --- a/src/components/type.ts +++ b/src/components/type.ts @@ -52,4 +52,5 @@ export interface SelectProps { formatGroupLabel?: ((data: GroupOption) => JSX.Element) | null; formatOptionLabel?: ((data: Option) => JSX.Element) | null; classNames?: ClassNames; + scrollableContainer?: HTMLElement | null; }