Skip to content
Merged
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
8 changes: 4 additions & 4 deletions specifyweb/frontend/js_src/lib/components/formtable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ export function FormTable<SCHEMA extends AnySchema>({
const headerIsVisible =
resources.length !== 1 || !isExpanded[resources[0].cid];

const scrollerRef = React.useRef<HTMLDivElement | null>(null);
const [scroller, setScroller] = React.useState<HTMLDivElement | null>(null);
const { isFetching, handleScroll } = useInfiniteScroll(
handleFetchMore,
scrollerRef
scroller
);

// FEATURE: add <FormPreferences /> for formTable records when expanded
Expand Down Expand Up @@ -176,7 +176,7 @@ export function FormTable<SCHEMA extends AnySchema>({
}`,
maxHeight: `${maxHeight}px`,
}}
forwardRef={scrollerRef}
forwardRef={setScroller}
onScroll={handleScroll}
>
<div className={headerIsVisible ? 'contents' : 'sr-only'} role="row">
Expand Down Expand Up @@ -474,7 +474,7 @@ export function FormTableCollection({
setRecords(Array.from(collection.models));
handleDelete?.(resource);
}}
onFetchMore={handleFetchMore}
onFetchMore={collection.isComplete() ? undefined : handleFetchMore}
{...props}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ export function QueryResultsTable({
treeRanksLoaded === true;
const canFetchMore = !Array.isArray(results) || results.length !== totalCount;

const scrollRef = React.useRef<HTMLDivElement | null>(null);
const [scroller, setScroller] = React.useState<HTMLDivElement | null>(null);
const { isFetching, handleScroll } = useInfiniteScroll(
canFetchMore ? handleFetchMore : undefined,
scrollRef
scroller
);

return (
Expand Down Expand Up @@ -433,7 +433,7 @@ export function QueryResultsTable({
'--columns': fieldSpecs.length,
} as React.CSSProperties
}
ref={scrollRef}
ref={setScroller}
onScroll={
showResults && (isFetching || !canFetchMore)
? undefined
Expand Down
14 changes: 7 additions & 7 deletions specifyweb/frontend/js_src/lib/components/useInfiniteScroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useBooleanState } from './hooks';
*/
export function useInfiniteScroll(
handleFetch: (() => Promise<void>) | undefined,
scrollerRef: React.RefObject<HTMLElement | null>
scroller: HTMLElement | null
): {
readonly isFetching: boolean;
readonly handleScroll: (event: React.UIEvent<HTMLElement>) => void;
Expand All @@ -23,15 +23,15 @@ export function useInfiniteScroll(
isFetchingRef.current = false;
await new Promise((resolve) => setTimeout(resolve, 0));
// Fetch until there is a scroll bar
if (
scrollerRef.current !== null &&
scrollerRef.current.scrollHeight === scrollerRef.current.clientHeight
)
if (scroller !== null && scroller.scrollHeight === scroller.clientHeight)
doFetch().catch(crash);
handleFetched();
}, [handleFetch, scrollerRef, handleFetching, handleFetched]);
}, [handleFetch, scroller, handleFetching, handleFetched]);

React.useEffect(() => void doFetch(), []);
React.useEffect(
() => (typeof scroller === 'object' ? void doFetch() : undefined),
[scroller]
);

return {
isFetching,
Expand Down