Skip to content
Open
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
7 changes: 5 additions & 2 deletions packages/react-aria-components/src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2415,11 +2415,14 @@ export const TableLoadMoreItem = createLeafComponent(
{/* Alway render the sentinel. For now onus is on the user for styling when using flex + gap (this would introduce a gap even though it doesn't take room) */}
{/* @ts-ignore - compatibility with React < 19 */}
<TableRowElementType style={{height: 0}} inert={inertValue(true)}>
<TableCellElementType style={{padding: 0, border: 0}}>
<TableCellElementType
colSpan={isVirtualized ? undefined : numColumns}
style={{padding: 0, border: 0}}>
{/* Span the full row so the sentinel stays within the observer root when scrolled horizontally. */}
<div
data-testid="loadMoreSentinel"
ref={sentinelRef}
style={{position: 'relative', height: 1, width: 1}}
style={{position: 'relative', height: 1, width: '100%'}}
/>
</TableCellElementType>
</TableRowElementType>
Expand Down
95 changes: 95 additions & 0 deletions packages/react-aria-components/stories/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,101 @@ export const VirtualizedTableLoaderWidthTestStory: StoryObj<
}
};

const wideColumns = Array.from({length: 12}, (_, i) => ({id: `c${i}`, name: `Column ${i + 1}`}));

const HorizontallyScrolledLoadMoreTable = (args: {
delay: number;
isVirtualized: boolean;
}): JSX.Element => {
let [loadMoreCount, setLoadMoreCount] = useState(0);
let list = useAsyncList<{id: number}>({
async load({cursor}) {
let start = cursor ? Number(cursor) : 0;
if (cursor) {
setLoadMoreCount(count => count + 1);
}
await new Promise(resolve => setTimeout(resolve, args.delay));
return {
items: Array.from({length: 20}, (_, i) => ({id: start + i})),
cursor: start + 20 < 200 ? String(start + 20) : undefined
};
}
});

let table = (
<Table
aria-label="Wide load more table"
style={
args.isVirtualized
? {width: 400, height: 300, overflow: 'auto'}
: {width: 2400, tableLayout: 'fixed'}
}>
<TableHeader columns={wideColumns}>
{column => (
<Column id={column.id} isRowHeader={column.id === 'c0'} width={200}>
{column.name}
</Column>
)}
</TableHeader>
<TableBody>
<Collection items={list.items}>
{item => (
<Row id={item.id} columns={wideColumns}>
{column => <Cell>{`${column.name}, row ${item.id + 1}`}</Cell>}
</Row>
)}
</Collection>
<TableLoadMoreItem
onLoadMore={list.loadMore}
isLoading={list.loadingState === 'loadingMore'}
style={{height: 30}}>
<LoadingSpinner style={{height: 20, position: 'unset'}} />
</TableLoadMoreItem>
</TableBody>
</Table>
);

return (
<div>
<p data-testid="load-more-status">
Pages loaded by onLoadMore: {loadMoreCount}, rows loaded: {list.items.length}
</p>
{args.isVirtualized ? (
<Virtualizer
layout={TableLayout}
layoutOptions={{rowHeight: 25, headingHeight: 25, loaderHeight: 30}}>
{table}
</Virtualizer>
) : (
<div data-testid="scroller" style={{width: 400, height: 300, overflow: 'auto'}}>
{table}
</div>
)}
</div>
);
};

export const HorizontallyScrolledLoadMoreTableStory: StoryObj<
typeof HorizontallyScrolledLoadMoreTable
> = {
render: HorizontallyScrolledLoadMoreTable,
name: 'onLoadMore table, scrolled horizontally',
args: {delay: 200, isVirtualized: false},
parameters: {
description: {
data: 'Scroll all the way to the right, then to the bottom. onLoadMore should still fire and load more rows.'
}
}
};

export const HorizontallyScrolledLoadMoreTableVirtualizedStory: StoryObj<
typeof HorizontallyScrolledLoadMoreTable
> = {
...HorizontallyScrolledLoadMoreTableStory,
name: 'Virtualized onLoadMore table, scrolled horizontally',
args: {delay: 200, isVirtualized: true}
};

interface Launch {
id: number;
mission_name: string;
Expand Down
84 changes: 84 additions & 0 deletions packages/react-aria-components/test/Table.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2026 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import {Cell, Column, Row, Table, TableBody, TableHeader, TableLoadMoreItem} from '../src/Table';
import {Collection} from 'react-aria/Collection';
import {describe, expect, it, vi} from 'vitest';
import React from 'react';
import {render} from 'vitest-browser-react';
import {TableLayout} from '../src/TableLayout';
import {Virtualizer} from '../src/Virtualizer';

let columns = Array.from({length: 12}, (_, i) => ({id: `c${i}`, name: `Column ${i}`}));
let rows = Array.from({length: 50}, (_, i) => ({id: i}));

function WideTable({onLoadMore, style}: {onLoadMore: () => void; style?: React.CSSProperties}) {
return (
<Table aria-label="Wide table" style={style}>
<TableHeader columns={columns}>
{column => (
<Column id={column.id} isRowHeader={column.id === 'c0'} width={200}>
{column.name}
</Column>
)}
</TableHeader>
<TableBody>
<Collection items={rows}>
{row => (
<Row id={row.id} columns={columns}>
{column => <Cell>{`${column.name}, row ${row.id + 1}`}</Cell>}
</Row>
)}
</Collection>
<TableLoadMoreItem onLoadMore={onLoadMore} />
</TableBody>
</Table>
);
}

async function scrollRightThenDown(scroller: HTMLElement, onLoadMore: () => void) {
scroller.scrollLeft = scroller.scrollWidth;
// Make sure the table is scrolled by more than one container width.
expect(scroller.scrollLeft).toBeGreaterThan(2 * scroller.clientWidth);
await new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)));
expect(onLoadMore).not.toHaveBeenCalled();

scroller.scrollTop = scroller.scrollHeight;
await vi.waitFor(() => expect(onLoadMore).toHaveBeenCalled(), {timeout: 2000});
}

describe('Table load more', () => {
it('fires onLoadMore when a virtualized table is scrolled to the bottom after scrolling far to the right', async () => {
let onLoadMore = vi.fn();
let {container} = await render(
<Virtualizer layout={TableLayout} layoutOptions={{rowHeight: 32, headingHeight: 32}}>
<WideTable onLoadMore={onLoadMore} style={{width: 400, height: 300, overflow: 'auto'}} />
</Virtualizer>
);

await scrollRightThenDown(container.querySelector('[role=grid]') as HTMLElement, onLoadMore);
});

it('fires onLoadMore when a non-virtualized table is scrolled to the bottom after scrolling far to the right', async () => {
let onLoadMore = vi.fn();
let {container} = await render(
<div data-testid="scroller" style={{width: 400, height: 300, overflow: 'auto'}}>
<WideTable onLoadMore={onLoadMore} style={{width: 2400, tableLayout: 'fixed'}} />
</div>
);

await scrollRightThenDown(
container.querySelector('[data-testid=scroller]') as HTMLElement,
onLoadMore
);
});
});
7 changes: 7 additions & 0 deletions packages/react-aria-components/test/Table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2881,6 +2881,13 @@ describe('Table', () => {
expect(tree.getByTestId('loadMoreSentinel')).toBeInTheDocument();
});

it('should span the sentinel across every column so it can be observed when scrolled horizontally', async () => {
let tree = render(<LoadMoreTable items={items} />);
let sentinel = tree.getByTestId('loadMoreSentinel');
expect(sentinel.closest('td')).toHaveAttribute('colspan', '3');
expect(sentinel).toHaveStyle({width: '100%'});
});

it('should properly render the renderEmptyState if table is empty', async () => {
let tree = render(<LoadMoreTable items={[]} />);
let tableTester = testUtilUser.createTester('Table', {root: tree.getByRole('grid')});
Expand Down