@@ -7,8 +7,13 @@ import {AnnouncerAPI} from '@clayui/core';
77import { CollectionState } from '@clayui/core/src/collection/types' ;
88import LoadingIndicator from '@clayui/loading-indicator' ;
99import { sub , useDebounce } from '@clayui/shared' ;
10- import classNames from 'classnames' ;
11- import React , { RefObject , useEffect , useRef , useState } from 'react' ;
10+ import React , {
11+ RefObject ,
12+ useCallback ,
13+ useEffect ,
14+ useRef ,
15+ useState ,
16+ } from 'react' ;
1217
1318import { AutocompleteMessages } from './Autocomplete' ;
1419
@@ -74,13 +79,13 @@ export function useInfiniteScrolling({
7479 const debouncedIsLoadingMore = useDebounce ( isLoadingMore , 200 ) ;
7580 const isInfiniteScrollingEnabled = Boolean ( externalOnLoadMore ) ;
7681
77- const onLoadMore = async ( ) => {
82+ const onLoadMore = useCallback ( async ( ) => {
7883 setIsLoadingMore ( Boolean ( externalOnLoadMore ) ) ;
7984
8085 await externalOnLoadMore ?.( ) ;
8186
8287 setIsLoadingMore ( false ) ;
83- } ;
88+ } , [ externalOnLoadMore ] ) ;
8489
8590 const isLoading = Boolean ( loadingState !== undefined && loadingState < 4 ) ;
8691
@@ -151,49 +156,54 @@ export function useInfiniteScrolling({
151156 if ( active && triggerRef . current && menuRef . current ) {
152157 let timeoutId : NodeJS . Timeout | null = null ;
153158
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- } ) ;
159+ const observer = new IntersectionObserver (
160+ ( entries ) => {
161+ if ( timeoutId ) {
162+ clearTimeout ( timeoutId ) ;
171163 }
172- } , 200 ) ;
173- } ;
174164
175- const observer = new IntersectionObserver ( handleIntersect , {
176- root : menuRef . current ,
177- threshold : 1.0 ,
178- } ) ;
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+ ) ;
179186
180187 observer . observe ( triggerRef . current ) ;
181188
182- return ( ) => observer . disconnect ( ) ;
189+ return ( ) => {
190+ if ( timeoutId ) {
191+ clearTimeout ( timeoutId ) ;
192+ }
193+
194+ observer . disconnect ( ) ;
195+ } ;
183196 }
184197 } , [ active , isLoading , onLoadMore ] ) ;
185198
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- ) ;
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 ;
197207
198208 const lastItem = currentCount > 0 ? collection . getLastItem ( ) : null ;
199209 const isLastItemActive =
@@ -203,7 +213,7 @@ export function useInfiniteScrolling({
203213 if ( isLastItemActive && ! isLoading ) {
204214 onLoadMore ( ) ;
205215 }
206- } , [ isLastItemActive , isLoading ] ) ;
216+ } , [ isLastItemActive , isLoading , onLoadMore ] ) ;
207217
208218 return InfiniteScrollingTrigger ;
209219}
0 commit comments