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

Sticky items for search/select are added by topItemCount prop. When s… #333

Merged
merged 3 commits into from
Nov 12, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from 'react';
import cl from 'clsx';
import { useTranslation } from 'react-i18next';
import { useDebounce } from '@uidotdev/usehooks';
import { Virtuoso } from 'react-virtuoso';
import { Virtuoso, VirtuosoHandle } from 'react-virtuoso';

import classes from './VariableBoxContent.module.scss';
import { Checkbox, MixedCheckbox } from '../../Checkbox/Checkbox';
Expand Down Expand Up @@ -209,6 +209,9 @@ export function VariableBoxContent({
<Search
onChange={(value: string) => {
setSearch(value);
if(value === '') {
setScrollingDown(false);
}
}}
variant="inVariableBox"
showLabel={false}
Expand Down Expand Up @@ -264,6 +267,38 @@ export function VariableBoxContent({
}
};

//How many items should be sticky
const stickyTopValueCount = hasSevenOrMoreValues
? 2
: hasTwoOrMoreValues
? 1
: 0;

const virtuosoRef = useRef<VirtuosoHandle>(null);
const [lastScrollPosition, setLastScrollPosition] = useState(0);
const [scrollingDown, setScrollingDown] = useState(false);

const handleVirtuosoScroll = () => {
if (virtuosoRef.current) {
virtuosoRef.current.getState((state) => {
const scrollTop = state.scrollTop;
const isIntentionalScrollUp = scrollTop < lastScrollPosition - 2;
const isIntentionalScrollDown = scrollTop > lastScrollPosition + 2;

if (isIntentionalScrollDown && !scrollingDown) {
setScrollingDown(true);
} else if (isIntentionalScrollUp && scrollingDown) {
setScrollingDown(false);
}
setLastScrollPosition(scrollTop);
});
}
};

// To override element styling added by Virtuoso when scrolling down
/* eslint-disable-next-line */
const TopItemListEmptyFragment = () => <></>;

return (
<div className={cl(classes['variablebox-content'])}>
<div className={cl(classes['variablebox-content-main'])}>
Expand Down Expand Up @@ -328,6 +363,9 @@ export function VariableBoxContent({
enter: (velocity) => Math.abs(velocity) > 1000,
exit: (velocity) => Math.abs(velocity) < 30,
}}
topItemCount={stickyTopValueCount}
ref={virtuosoRef}
onScroll={handleVirtuosoScroll}
components={{
ScrollSeekPlaceholder: ({ height }) => (
<Skeleton
Expand All @@ -336,6 +374,9 @@ export function VariableBoxContent({
width={50 + Math.ceil(Math.random() * 15) + '%'}
/>
),
TopItemList: (scrollingDown && search === '')
? TopItemListEmptyFragment
: undefined,
}}
/>
)}
Expand Down