Skip to content

Commit e7331f4

Browse files
committed
chore(ui:virtual-scroll): optimized calculation
1 parent 44c77fb commit e7331f4

1 file changed

Lines changed: 35 additions & 4 deletions

File tree

packages/ui/src/components/_virtual-scroll/VirtualScroll.tsx

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface DVirtualScrollProps<T> extends React.HTMLAttributes<HTMLElement
2020
dPaddingSize?: number;
2121
dList: T[];
2222
dNestedKey?: string;
23+
dNestedAsItem?: boolean;
2324
dCanSelectOption: (option: T) => boolean;
2425
dCompareOption: (a: T, b: T) => boolean;
2526
dItemRender: (item: T, props: DItemRenderProps) => React.ReactNode;
@@ -39,6 +40,7 @@ export function DVirtualScroll<T>(props: DVirtualScrollProps<T>) {
3940
dPaddingSize = 0,
4041
dList,
4142
dNestedKey,
43+
dNestedAsItem = false,
4244
dCanSelectOption,
4345
dCompareOption,
4446
dItemRender,
@@ -109,6 +111,8 @@ export function DVirtualScroll<T>(props: DVirtualScrollProps<T>) {
109111
let skipCount = 0;
110112
let renderCount = 0;
111113
const loop = (arr: T[]) => {
114+
let skipNestedItem = 0;
115+
const size = !dNestedAsItem && dNestedKey ? arr.length - arr.filter((item) => isArray(item[dNestedKey])).length : arr.length;
112116
const list: React.ReactNode[] = [];
113117
if (arr.length === 0) {
114118
if (dEmpty) {
@@ -132,10 +136,24 @@ export function DVirtualScroll<T>(props: DVirtualScrollProps<T>) {
132136
}
133137
const shouldRender = count > startCount;
134138
if (dNestedKey && isArray(arr[index][dNestedKey])) {
139+
if (!dNestedAsItem) {
140+
skipNestedItem += 1;
141+
}
135142
const children = loop(arr[index][dNestedKey] as T[]);
136143
if (shouldRender || children.length > 0) {
137144
renderCount += 1;
138-
list.push(dItemRender(arr[index], { children }));
145+
list.push(
146+
dItemRender(
147+
arr[index],
148+
dNestedAsItem
149+
? {
150+
'aria-setsize': size,
151+
'aria-posinset': index + 1,
152+
children,
153+
}
154+
: { children }
155+
)
156+
);
139157
} else {
140158
skipCount += 1;
141159
}
@@ -144,8 +162,8 @@ export function DVirtualScroll<T>(props: DVirtualScrollProps<T>) {
144162
renderCount += 1;
145163
list.push(
146164
dItemRender(arr[index], {
147-
'aria-setsize': arr.length,
148-
'aria-posinset': index + 1,
165+
'aria-setsize': size,
166+
'aria-posinset': index + 1 - skipNestedItem,
149167
})
150168
);
151169
} else {
@@ -164,7 +182,20 @@ export function DVirtualScroll<T>(props: DVirtualScrollProps<T>) {
164182
{ [dScrollY ? 'height' : 'width']: dItemSize * (flatOptions.length - skipCount - renderCount) },
165183
],
166184
};
167-
}, [dEmpty, dItemRender, dItemSize, dList, dNestedKey, dPaddingSize, dScrollY, dSize, flatOptions.length, listEl]);
185+
}, [
186+
dEmpty,
187+
dItemRender,
188+
dItemSize,
189+
dList,
190+
dNestedAsItem,
191+
dNestedKey,
192+
dPaddingSize,
193+
dScrollY,
194+
dSize,
195+
flatOptions.length,
196+
listEl?.scrollLeft,
197+
listEl?.scrollTop,
198+
]);
168199
const [{ list, fillSize }, _updateList] = useState(() => getStates());
169200
const updateList = useCallback(() => {
170201
_updateList(getStates());

0 commit comments

Comments
 (0)