Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to show the options above the select #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ const Select: React.FC<SelectProps> = ({
primaryColor = DEFAULT_THEME,
formatGroupLabel = null,
formatOptionLabel = null,
classNames
classNames,
scrollableContainer = null
}) => {
const [open, setOpen] = useState<boolean>(menuIsOpen);
const [list, setList] = useState<ListOption>(options);
const [inputValue, setInputValue] = useState<string>("");
const ref = useRef<HTMLDivElement>(null);
const searchBoxRef = useRef<HTMLInputElement>(null);
const [showOptionAboveTheSelect, setShowOptionAboveTheSelect] = useState<boolean>(false);

useEffect(() => {
const formatItem = (item: Option) => {
Expand Down Expand Up @@ -166,6 +168,22 @@ const Select: React.FC<SelectProps> = ({
[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 (
<SelectProvider
otherData={{
Expand Down Expand Up @@ -266,9 +284,10 @@ const Select: React.FC<SelectProps> = ({
{open && !isDisabled && (
<div
className={
classNames?.menu
`${classNames?.menu
? classNames.menu
: "absolute z-10 w-full bg-white shadow-lg border rounded py-1 mt-1.5 text-sm text-gray-700"
: "absolute z-10 w-full bg-white shadow-lg border rounded py-1 mt-1.5 text-sm text-gray-700"}
${showOptionAboveTheSelect ? 'top-auto bottom-[44px]' : ''}`
}
>
{isSearchable && (
Expand Down
1 change: 1 addition & 0 deletions src/components/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export interface SelectProps {
formatGroupLabel?: ((data: GroupOption) => JSX.Element) | null;
formatOptionLabel?: ((data: Option) => JSX.Element) | null;
classNames?: ClassNames;
scrollableContainer?: HTMLElement | null;
}