Fix Always Scrolling to Selected Element

Summary: `forwardedRef` was always triggered, so it forced the container to scroll to the selected element. This diff stores element id so that it scrolls only once.

Reviewed By: mweststrate

Differential Revision: D22235284

fbshipit-source-id: 0eb7aaf2b484d345865db4da66d0f555dc314710
This commit is contained in:
Chaiwat Ekkaewnumchai
2020-06-26 02:16:28 -07:00
committed by Facebook GitHub Bot
parent 534a6186c1
commit 56c9435bd5

View File

@@ -452,6 +452,7 @@ type ElementsState = {
flatKeys: Array<ElementID>; flatKeys: Array<ElementID>;
flatElements: FlatElements; flatElements: FlatElements;
maxDepth: number; maxDepth: number;
scrolledElement: string | null | undefined;
}; };
export type ContextMenuExtension = { export type ContextMenuExtension = {
@@ -470,6 +471,7 @@ export class Elements extends PureComponent<ElementsProps, ElementsState> {
flatElements: [], flatElements: [],
flatKeys: [], flatKeys: [],
maxDepth: 0, maxDepth: 0,
scrolledElement: null,
}; };
} }
@@ -678,11 +680,12 @@ export class Elements extends PureComponent<ElementsProps, ElementsState> {
contextMenuExtensions={contextMenuExtensions || []} contextMenuExtensions={contextMenuExtensions || []}
decorateRow={decorateRow} decorateRow={decorateRow}
forwardedRef={ forwardedRef={
selected == row.key selected == row.key && this.state.scrolledElement !== selected
? (selectedRow) => { ? (selectedRow) => {
if (!selectedRow || !this._outerRef.current) { if (!selectedRow || !this._outerRef.current) {
return; return;
} }
this.setState({scrolledElement: selected});
const outer = this._outerRef.current; const outer = this._outerRef.current;
if (outer.scrollTo) { if (outer.scrollTo) {
outer.scrollTo( outer.scrollTo(