Skip to content

Commit fd27ca6

Browse files
authored
chore(compass-components): expose v-list listRef prop COMPASS-7567 (#6576)
expose listRef and itemIndex
1 parent 22a9af0 commit fd27ca6

File tree

6 files changed

+19
-20
lines changed

6 files changed

+19
-20
lines changed

packages/compass-components/src/components/virtual-list.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export type VirtualListProps<T> = {
4545
* after first render, starts monitoring the actual height but it is still
4646
* super useful to avoid a huge flicker during the initial rendering phase
4747
*/
48-
estimateItemInitialHeight(item: T): number;
48+
estimateItemInitialHeight(item: T, index: number): number;
4949

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

118118
/**
119-
* WARNING: Use only when testing
120-
*
121119
* Mutable Ref object to hold the reference to the VariableSizeList
122120
*/
123-
__TEST_LIST_REF?: VirtualListRef;
121+
listRef?: VirtualListRef;
124122
};
125123

126124
const flexContainerStyles = css({
@@ -147,12 +145,12 @@ export function VirtualList<T>({
147145
itemDataTestId,
148146
initialScrollTop,
149147
scrollableContainerRef,
148+
listRef: _listRef,
150149
__TEST_LIST_WIDTH = 1024,
151150
__TEST_LIST_HEIGHT = 768,
152-
__TEST_LIST_REF,
153151
}: VirtualListProps<T>) {
154152
const listRef = useRef<List | null>(null);
155-
const inUseListRef = __TEST_LIST_REF ?? listRef;
153+
const inUseListRef = _listRef ?? listRef;
156154
const { observer, estimatedItemSize, getItemSize } =
157155
useVirtualListItemObserver({
158156
listRef: inUseListRef,

packages/compass-components/src/hooks/use-virtual-list-item-observer.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { isEqual } from 'lodash';
22
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
33
import { type VariableSizeList } from 'react-window';
4+
import type { VirtualListProps } from '../components/virtual-list';
45

5-
export type VirtualListItemObserverParams<T> = {
6+
export type VirtualListItemObserverParams<T> = Pick<
7+
VirtualListProps<T>,
8+
'items' | 'rowGap' | 'estimateItemInitialHeight'
9+
> & {
610
listRef: React.RefObject<VariableSizeList>;
7-
items: T[];
8-
rowGap?: number;
9-
estimateItemInitialHeight(item: T): number;
1011
};
1112

1213
export type ListItemObserver = {
@@ -103,7 +104,7 @@ export const useVirtualListItemObserver = <T>({
103104
// page changes) in which case we won't have their heights in our state
104105
// hence we fallback to estimating initial document heights.
105106
const itemHeight =
106-
itemsHeights[idx] ?? estimateItemInitialHeight(items[idx]);
107+
itemsHeights[idx] ?? estimateItemInitialHeight(items[idx], idx);
107108
if (rowGap !== undefined && idx !== items.length - 1) {
108109
return itemHeight + rowGap;
109110
}

packages/compass-crud/src/components/virtualized-document-json-view.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ describe('VirtualizedDocumentJsonView', function () {
9090
namespace="x.y"
9191
docs={bigDocuments}
9292
isEditable={true}
93+
listRef={listRef}
9394
__TEST_OVERSCAN_COUNT={0}
9495
__TEST_LIST_HEIGHT={178}
95-
__TEST_LIST_REF={listRef}
9696
/>
9797
);
9898

@@ -183,9 +183,9 @@ describe('VirtualizedDocumentJsonView', function () {
183183
namespace="x.y"
184184
docs={bigDocuments}
185185
isEditable={true}
186+
listRef={listRef}
186187
__TEST_OVERSCAN_COUNT={0}
187188
__TEST_LIST_HEIGHT={178}
188-
__TEST_LIST_REF={listRef}
189189
/>
190190
);
191191

packages/compass-crud/src/components/virtualized-document-json-view.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ export type VirtualizedDocumentJsonViewProps = {
4242
initialScrollTop?: number;
4343
scrollTriggerRef?: React.Ref<HTMLDivElement>;
4444
scrollableContainerRef?: React.Ref<HTMLDivElement>;
45+
listRef?: VirtualListRef;
4546
__TEST_OVERSCAN_COUNT?: number;
4647
__TEST_LIST_HEIGHT?: number;
47-
__TEST_LIST_REF?: VirtualListRef;
4848
} & Pick<
4949
JSONEditorProps,
5050
| 'isTimeSeries'
@@ -72,7 +72,7 @@ const VirtualizedDocumentJsonView: React.FC<
7272
openInsertDocumentDialog,
7373
__TEST_OVERSCAN_COUNT,
7474
__TEST_LIST_HEIGHT,
75-
__TEST_LIST_REF,
75+
listRef,
7676
}) => {
7777
const renderItem: VirtualListItemRenderer<HadronDocument> = useCallback(
7878
(doc, docRef, docIndex) => {
@@ -120,8 +120,8 @@ const VirtualizedDocumentJsonView: React.FC<
120120
listOuterContainerClassName={spacingStyles}
121121
initialScrollTop={initialScrollTop}
122122
scrollableContainerRef={scrollableContainerRef}
123+
listRef={listRef}
123124
__TEST_LIST_HEIGHT={__TEST_LIST_HEIGHT}
124-
__TEST_LIST_REF={__TEST_LIST_REF}
125125
></VirtualList>
126126
);
127127
};

packages/compass-crud/src/components/virtualized-document-list-view.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ describe('VirtualizedDocumentListView', function () {
9595
<VirtualizedDocumentListView
9696
docs={bigDocuments}
9797
isEditable={true}
98+
listRef={listRef}
9899
__TEST_OVERSCAN_COUNT={0}
99100
__TEST_LIST_HEIGHT={178}
100-
__TEST_LIST_REF={listRef}
101101
/>
102102
);
103103

packages/compass-crud/src/components/virtualized-document-list-view.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ type VirtualizedDocumentListViewProps = {
4747
initialScrollTop?: number;
4848
scrollTriggerRef?: React.Ref<HTMLDivElement>;
4949
scrollableContainerRef?: React.Ref<HTMLDivElement>;
50+
listRef?: VirtualListRef;
5051
__TEST_OVERSCAN_COUNT?: number;
5152
__TEST_LIST_HEIGHT?: number;
52-
__TEST_LIST_REF?: VirtualListRef;
5353
} & Pick<
5454
DocumentProps,
5555
| 'isTimeSeries'
@@ -74,9 +74,9 @@ const VirtualizedDocumentListView: React.FC<
7474
replaceDocument,
7575
updateDocument,
7676
openInsertDocumentDialog,
77+
listRef,
7778
__TEST_OVERSCAN_COUNT,
7879
__TEST_LIST_HEIGHT,
79-
__TEST_LIST_REF,
8080
}) => {
8181
const docs = useMemo(() => {
8282
return _docs.map((_doc) => {
@@ -131,9 +131,9 @@ const VirtualizedDocumentListView: React.FC<
131131
listOuterContainerClassName={spacingStyles}
132132
initialScrollTop={initialScrollTop}
133133
scrollableContainerRef={scrollableContainerRef}
134+
listRef={listRef}
134135
overScanCount={__TEST_OVERSCAN_COUNT}
135136
__TEST_LIST_HEIGHT={__TEST_LIST_HEIGHT}
136-
__TEST_LIST_REF={__TEST_LIST_REF}
137137
></VirtualList>
138138
);
139139
};

0 commit comments

Comments
 (0)