1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Always check if ref is active in virtual-list (#3230)

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-07-01 02:51:11 -04:00 committed by GitHub
parent 1f2692c18f
commit e0a509d462
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -75,23 +75,27 @@ export class VirtualList extends Component<Props, State> {
const { items, rowHeights } = this.props; const { items, rowHeights } = this.props;
if (prevProps.items.length !== items.length || !isEqual(prevProps.rowHeights, rowHeights)) { if (prevProps.items.length !== items.length || !isEqual(prevProps.rowHeights, rowHeights)) {
this.listRef.current.resetAfterIndex(0, false); this.listRef.current?.resetAfterIndex(0, false);
} }
} }
getItemSize = (index: number) => this.props.rowHeights[index]; getItemSize = (index: number) => this.props.rowHeights[index];
scrollToSelectedItem = debounce(() => { scrollToSelectedItem = debounce(() => {
if (!this.props.selectedItemId) return; if (!this.props.selectedItemId) {
return;
}
const { items, selectedItemId } = this.props; const { items, selectedItemId } = this.props;
const index = items.findIndex(item => item.getId() == selectedItemId); const index = items.findIndex(item => item.getId() == selectedItemId);
if (index === -1) return; if (index >= 0) {
this.listRef.current.scrollToItem(index, "start"); this.listRef.current?.scrollToItem(index, "start");
}
}); });
scrollToItem = (index: number, align: Align) => { scrollToItem = (index: number, align: Align) => {
this.listRef.current.scrollToItem(index, align); this.listRef.current?.scrollToItem(index, align);
}; };
render() { render() {