Skip to content

Commit

Permalink
chore(compass-components): expose v-list listRef prop COMPASS-7567 (#…
Browse files Browse the repository at this point in the history
…6576)

expose listRef and itemIndex
  • Loading branch information
mabaasit authored Dec 19, 2024
1 parent 22a9af0 commit fd27ca6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
10 changes: 4 additions & 6 deletions packages/compass-components/src/components/virtual-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type VirtualListProps<T> = {
* after first render, starts monitoring the actual height but it is still
* super useful to avoid a huge flicker during the initial rendering phase
*/
estimateItemInitialHeight(item: T): number;
estimateItemInitialHeight(item: T, index: number): number;

/**
* How many items to keep rendered outside of the visible viewport. Keeping
Expand Down Expand Up @@ -116,11 +116,9 @@ export type VirtualListProps<T> = {
__TEST_LIST_HEIGHT?: number;

/**
* WARNING: Use only when testing
*
* Mutable Ref object to hold the reference to the VariableSizeList
*/
__TEST_LIST_REF?: VirtualListRef;
listRef?: VirtualListRef;
};

const flexContainerStyles = css({
Expand All @@ -147,12 +145,12 @@ export function VirtualList<T>({
itemDataTestId,
initialScrollTop,
scrollableContainerRef,
listRef: _listRef,
__TEST_LIST_WIDTH = 1024,
__TEST_LIST_HEIGHT = 768,
__TEST_LIST_REF,
}: VirtualListProps<T>) {
const listRef = useRef<List | null>(null);
const inUseListRef = __TEST_LIST_REF ?? listRef;
const inUseListRef = _listRef ?? listRef;
const { observer, estimatedItemSize, getItemSize } =
useVirtualListItemObserver({
listRef: inUseListRef,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { isEqual } from 'lodash';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { type VariableSizeList } from 'react-window';
import type { VirtualListProps } from '../components/virtual-list';

export type VirtualListItemObserverParams<T> = {
export type VirtualListItemObserverParams<T> = Pick<
VirtualListProps<T>,
'items' | 'rowGap' | 'estimateItemInitialHeight'
> & {
listRef: React.RefObject<VariableSizeList>;
items: T[];
rowGap?: number;
estimateItemInitialHeight(item: T): number;
};

export type ListItemObserver = {
Expand Down Expand Up @@ -103,7 +104,7 @@ export const useVirtualListItemObserver = <T>({
// page changes) in which case we won't have their heights in our state
// hence we fallback to estimating initial document heights.
const itemHeight =
itemsHeights[idx] ?? estimateItemInitialHeight(items[idx]);
itemsHeights[idx] ?? estimateItemInitialHeight(items[idx], idx);
if (rowGap !== undefined && idx !== items.length - 1) {
return itemHeight + rowGap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ describe('VirtualizedDocumentJsonView', function () {
namespace="x.y"
docs={bigDocuments}
isEditable={true}
listRef={listRef}
__TEST_OVERSCAN_COUNT={0}
__TEST_LIST_HEIGHT={178}
__TEST_LIST_REF={listRef}
/>
);

Expand Down Expand Up @@ -183,9 +183,9 @@ describe('VirtualizedDocumentJsonView', function () {
namespace="x.y"
docs={bigDocuments}
isEditable={true}
listRef={listRef}
__TEST_OVERSCAN_COUNT={0}
__TEST_LIST_HEIGHT={178}
__TEST_LIST_REF={listRef}
/>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export type VirtualizedDocumentJsonViewProps = {
initialScrollTop?: number;
scrollTriggerRef?: React.Ref<HTMLDivElement>;
scrollableContainerRef?: React.Ref<HTMLDivElement>;
listRef?: VirtualListRef;
__TEST_OVERSCAN_COUNT?: number;
__TEST_LIST_HEIGHT?: number;
__TEST_LIST_REF?: VirtualListRef;
} & Pick<
JSONEditorProps,
| 'isTimeSeries'
Expand Down Expand Up @@ -72,7 +72,7 @@ const VirtualizedDocumentJsonView: React.FC<
openInsertDocumentDialog,
__TEST_OVERSCAN_COUNT,
__TEST_LIST_HEIGHT,
__TEST_LIST_REF,
listRef,
}) => {
const renderItem: VirtualListItemRenderer<HadronDocument> = useCallback(
(doc, docRef, docIndex) => {
Expand Down Expand Up @@ -120,8 +120,8 @@ const VirtualizedDocumentJsonView: React.FC<
listOuterContainerClassName={spacingStyles}
initialScrollTop={initialScrollTop}
scrollableContainerRef={scrollableContainerRef}
listRef={listRef}
__TEST_LIST_HEIGHT={__TEST_LIST_HEIGHT}
__TEST_LIST_REF={__TEST_LIST_REF}
></VirtualList>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ describe('VirtualizedDocumentListView', function () {
<VirtualizedDocumentListView
docs={bigDocuments}
isEditable={true}
listRef={listRef}
__TEST_OVERSCAN_COUNT={0}
__TEST_LIST_HEIGHT={178}
__TEST_LIST_REF={listRef}
/>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ type VirtualizedDocumentListViewProps = {
initialScrollTop?: number;
scrollTriggerRef?: React.Ref<HTMLDivElement>;
scrollableContainerRef?: React.Ref<HTMLDivElement>;
listRef?: VirtualListRef;
__TEST_OVERSCAN_COUNT?: number;
__TEST_LIST_HEIGHT?: number;
__TEST_LIST_REF?: VirtualListRef;
} & Pick<
DocumentProps,
| 'isTimeSeries'
Expand All @@ -74,9 +74,9 @@ const VirtualizedDocumentListView: React.FC<
replaceDocument,
updateDocument,
openInsertDocumentDialog,
listRef,
__TEST_OVERSCAN_COUNT,
__TEST_LIST_HEIGHT,
__TEST_LIST_REF,
}) => {
const docs = useMemo(() => {
return _docs.map((_doc) => {
Expand Down Expand Up @@ -131,9 +131,9 @@ const VirtualizedDocumentListView: React.FC<
listOuterContainerClassName={spacingStyles}
initialScrollTop={initialScrollTop}
scrollableContainerRef={scrollableContainerRef}
listRef={listRef}
overScanCount={__TEST_OVERSCAN_COUNT}
__TEST_LIST_HEIGHT={__TEST_LIST_HEIGHT}
__TEST_LIST_REF={__TEST_LIST_REF}
></VirtualList>
);
};
Expand Down

0 comments on commit fd27ca6

Please sign in to comment.