Skip to content

Commit bf4c0bd

Browse files
committed
chore(@clayui/autocomplete): LPD-55597 Revert infinite scrolling trigger
1 parent fc78a95 commit bf4c0bd

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

packages/clay-autocomplete/src/useInfiniteScrolling.tsx

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {AnnouncerAPI} from '@clayui/core';
77
import {CollectionState} from '@clayui/core/src/collection/types';
88
import LoadingIndicator from '@clayui/loading-indicator';
99
import {sub, useDebounce} from '@clayui/shared';
10+
import classNames from 'classnames';
1011
import React, {
1112
RefObject,
1213
useCallback,
@@ -153,36 +154,39 @@ export function useInfiniteScrolling({
153154
const lastTriggerCount = useRef<number | null>(null);
154155

155156
useEffect(() => {
156-
if (active && triggerRef.current && menuRef.current) {
157+
if (
158+
isInfiniteScrollingEnabled &&
159+
active &&
160+
triggerRef.current &&
161+
menuRef.current
162+
) {
157163
let timeoutId: NodeJS.Timeout | null = null;
158164

159-
const observer = new IntersectionObserver(
160-
(entries) => {
161-
if (timeoutId) {
162-
clearTimeout(timeoutId);
165+
const onIntersection: IntersectionObserverCallback = (entries) => {
166+
if (timeoutId) {
167+
clearTimeout(timeoutId);
168+
}
169+
170+
timeoutId = setTimeout(() => {
171+
const isIntersecting = entries[0]?.isIntersecting ?? false;
172+
const countHasChanged =
173+
currentCount !== lastTriggerCount.current;
174+
175+
if (!isLoading && isIntersecting && countHasChanged) {
176+
isTriggerActive.current = true;
177+
lastTriggerCount.current = currentCount;
178+
179+
onLoadMore().finally(() => {
180+
isTriggerActive.current = false;
181+
});
163182
}
183+
}, 200);
184+
};
164185

165-
timeoutId = setTimeout(() => {
166-
const isIntersecting =
167-
entries[0]?.isIntersecting ?? false;
168-
const countHasChanged =
169-
currentCount !== lastTriggerCount.current;
170-
171-
if (!isLoading && isIntersecting && countHasChanged) {
172-
isTriggerActive.current = true;
173-
lastTriggerCount.current = currentCount;
174-
175-
onLoadMore().finally(() => {
176-
isTriggerActive.current = false;
177-
});
178-
}
179-
}, 200);
180-
},
181-
{
182-
root: menuRef.current,
183-
threshold: 1.0,
184-
}
185-
);
186+
const observer = new IntersectionObserver(onIntersection, {
187+
root: menuRef.current,
188+
threshold: 1,
189+
});
186190

187191
observer.observe(triggerRef.current);
188192

@@ -196,14 +200,17 @@ export function useInfiniteScrolling({
196200
}
197201
}, [active, isLoading, onLoadMore]);
198202

199-
const InfiniteScrollingTrigger = () =>
200-
isInfiniteScrollingEnabled ? (
201-
<div aria-hidden="true" className="mt-2" ref={triggerRef}>
202-
{(isLoadingMore || debouncedIsLoadingMore) && (
203-
<LoadingIndicator className="mb-2" size="sm" />
204-
)}
205-
</div>
206-
) : null;
203+
const InfiniteScrollingTrigger = () => (
204+
<div
205+
aria-hidden="true"
206+
className={classNames({'mt-2': isInfiniteScrollingEnabled})}
207+
ref={triggerRef}
208+
>
209+
{(isLoadingMore || debouncedIsLoadingMore) && (
210+
<LoadingIndicator className="mb-2" size="sm" />
211+
)}
212+
</div>
213+
);
207214

208215
const lastItem = currentCount > 0 ? collection.getLastItem() : null;
209216
const isLastItemActive =

0 commit comments

Comments
 (0)