Skip to content

Commit eda4593

Browse files
committed
chore(@clayui/autocomplete): LPD-55597 Improve referential stability
1 parent 899e415 commit eda4593

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

packages/clay-autocomplete/src/useInfiniteScrolling.tsx

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import {CollectionState} from '@clayui/core/src/collection/types';
88
import LoadingIndicator from '@clayui/loading-indicator';
99
import {sub, useDebounce} from '@clayui/shared';
1010
import classNames from 'classnames';
11-
import React, {RefObject, useEffect, useRef, useState} from 'react';
11+
import React, {
12+
RefObject,
13+
useCallback,
14+
useEffect,
15+
useRef,
16+
useState,
17+
} from 'react';
1218

1319
import {AutocompleteMessages} from './Autocomplete';
1420

@@ -74,13 +80,13 @@ export function useInfiniteScrolling({
7480
const debouncedIsLoadingMore = useDebounce(isLoadingMore, 200);
7581
const isInfiniteScrollingEnabled = Boolean(externalOnLoadMore);
7682

77-
const onLoadMore = async () => {
83+
const onLoadMore = useCallback(async () => {
7884
setIsLoadingMore(Boolean(externalOnLoadMore));
7985

8086
await externalOnLoadMore?.();
8187

8288
setIsLoadingMore(false);
83-
};
89+
}, [externalOnLoadMore]);
8490

8591
const isLoading = Boolean(loadingState !== undefined && loadingState < 4);
8692

@@ -151,49 +157,58 @@ export function useInfiniteScrolling({
151157
if (active && triggerRef.current && menuRef.current) {
152158
let timeoutId: NodeJS.Timeout | null = null;
153159

154-
const handleIntersect: IntersectionObserverCallback = (entries) => {
155-
if (timeoutId) {
156-
clearTimeout(timeoutId);
157-
}
158-
159-
timeoutId = setTimeout(() => {
160-
const isIntersecting = entries[0]?.isIntersecting ?? false;
161-
const countHasChanged =
162-
currentCount !== lastTriggerCount.current;
163-
164-
if (!isLoading && isIntersecting && countHasChanged) {
165-
isTriggerActive.current = true;
166-
lastTriggerCount.current = currentCount;
167-
168-
onLoadMore().finally(() => {
169-
isTriggerActive.current = false;
170-
});
160+
const observer = new IntersectionObserver(
161+
(entries) => {
162+
if (timeoutId) {
163+
clearTimeout(timeoutId);
171164
}
172-
}, 200);
173-
};
174165

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

180188
observer.observe(triggerRef.current);
181189

182-
return () => observer.disconnect();
190+
return () => {
191+
if (timeoutId) {
192+
clearTimeout(timeoutId);
193+
}
194+
195+
observer.disconnect();
196+
};
183197
}
184198
}, [active, isLoading, onLoadMore]);
185199

186-
const InfiniteScrollingTrigger = () => (
187-
<div
188-
aria-hidden="true"
189-
className={classNames({'mt-2': isInfiniteScrollingEnabled})}
190-
ref={triggerRef}
191-
>
192-
{(isLoadingMore || debouncedIsLoadingMore) && (
193-
<LoadingIndicator className="mb-2" size="sm" />
194-
)}
195-
</div>
196-
);
200+
const InfiniteScrollingTrigger = () =>
201+
isInfiniteScrollingEnabled ? (
202+
<div
203+
aria-hidden="true"
204+
className={classNames({'mt-2': isInfiniteScrollingEnabled})}
205+
ref={triggerRef}
206+
>
207+
{(isLoadingMore || debouncedIsLoadingMore) && (
208+
<LoadingIndicator className="mb-2" size="sm" />
209+
)}
210+
</div>
211+
) : null;
197212

198213
const lastItem = currentCount > 0 ? collection.getLastItem() : null;
199214
const isLastItemActive =
@@ -203,7 +218,7 @@ export function useInfiniteScrolling({
203218
if (isLastItemActive && !isLoading) {
204219
onLoadMore();
205220
}
206-
}, [isLastItemActive, isLoading]);
221+
}, [isLastItemActive, isLoading, onLoadMore]);
207222

208223
return InfiniteScrollingTrigger;
209224
}

0 commit comments

Comments
 (0)