Skip to content

Commit 1bbd69c

Browse files
committed
sorta get selected item to scroll into view virtualized
1 parent 6328d20 commit 1bbd69c

File tree

7 files changed

+14
-10
lines changed

7 files changed

+14
-10
lines changed

packages/@react-spectrum/s2/src/TableView.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ export class S2TableLayout<T> extends TableLayout<T> {
194194

195195
protected buildCollection(): LayoutNode[] {
196196
let [header, body] = super.buildCollection();
197+
if (!header) {
198+
return [];
199+
}
197200
let {children, layoutInfo} = body;
198201
// TableLayout's buildCollection always sets the body width to the max width between the header width, but
199202
// we want the body to be sticky and only as wide as the table so it is always in view if loading/empty

packages/@react-stately/layout/src/GridLayout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export class GridLayout<T, O extends GridLayoutOptions = GridLayoutOptions> exte
227227
// Find the closest item within on either side of the point using the gap width.
228228
let key: Key | null = null;
229229
if (this.numColumns === 1) {
230-
let searchRect = new Rect(x, Math.max(0, y - this.gap.height), 1, this.gap.height * 2);
230+
let searchRect = new Rect(x, Math.max(0, y - this.gap.height), 1, Math.max(1, this.gap.height * 2));
231231
let candidates = this.getVisibleLayoutInfos(searchRect);
232232
let minDistance = Infinity;
233233
for (let candidate of candidates) {

packages/@react-stately/layout/src/ListLayout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ export class ListLayout<T, O extends ListLayoutOptions = ListLayoutOptions> exte
506506
y += this.virtualizer!.visibleRect.y;
507507

508508
// Find the closest item within on either side of the point using the gap width.
509-
let searchRect = new Rect(x, Math.max(0, y - this.gap), 1, this.gap * 2);
509+
let searchRect = new Rect(x, Math.max(0, y - this.gap), 1, Math.max(1, this.gap * 2));
510510
let candidates = this.getVisibleLayoutInfos(searchRect);
511511
let key: Key | null = null;
512512
let minDistance = Infinity;

packages/@react-stately/layout/src/TableLayout.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export class TableLayout<T, O extends TableLayoutProps = TableLayoutProps> exten
8585
this.stickyColumnIndices = [];
8686

8787
let collection = this.virtualizer!.collection as TableCollection<T>;
88+
if (collection.head?.key === -1) {
89+
return [];
90+
}
91+
8892
for (let column of collection.columns) {
8993
// The selection cell and any other sticky columns always need to be visible.
9094
// In addition, row headers need to be in the DOM for accessibility labeling.
@@ -543,7 +547,7 @@ export class TableLayout<T, O extends TableLayoutProps = TableLayoutProps> exten
543547
y += this.virtualizer!.visibleRect.y;
544548

545549
// Find the closest item within on either side of the point using the gap width.
546-
let searchRect = new Rect(x, Math.max(0, y - this.gap), 1, this.gap * 2);
550+
let searchRect = new Rect(x, Math.max(0, y - this.gap), 1, Math.max(1, this.gap * 2));
547551
let candidates = this.getVisibleLayoutInfos(searchRect);
548552
let key: Key | null = null;
549553
let minDistance = Infinity;

packages/@react-stately/virtualizer/src/Rect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ export class Rect {
9292
* @param rect - The rectangle to check.
9393
*/
9494
intersects(rect: Rect): boolean {
95-
return this.x <= rect.x + rect.width
95+
return this.area > 0
96+
&& rect.area > 0
97+
&& this.x <= rect.x + rect.width
9698
&& rect.x <= this.x + this.width
9799
&& this.y <= rect.y + rect.height
98100
&& rect.y <= this.y + this.height;

packages/@react-stately/virtualizer/src/Virtualizer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ export class Virtualizer<T extends object, V> {
193193
} else {
194194
rect = this._overscanManager.getOverscannedRect();
195195
}
196-
197-
let layoutInfos = rect.area === 0 ? [] : this.layout.getVisibleLayoutInfos(rect);
196+
let layoutInfos = this.layout.getVisibleLayoutInfos(rect);
198197
let map = new Map;
199198
for (let layoutInfo of layoutInfos) {
200199
map.set(layoutInfo.key, layoutInfo);

packages/react-aria-components/src/Virtualizer.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ function CollectionRoot({collection, persistedKeys, scrollRef, renderDropIndicat
102102
onScrollEnd: state.endScrolling
103103
}, scrollRef!);
104104

105-
if (state.contentSize.area === 0) {
106-
return null;
107-
}
108-
109105
return (
110106
<div {...contentProps}>
111107
<VirtualizerContext.Provider value={state}>

0 commit comments

Comments
 (0)