From aab06cfe8f961e543292878fc5f444caadda967a Mon Sep 17 00:00:00 2001 From: Petyo Ivanov Date: Tue, 16 Aug 2022 09:49:48 +0300 Subject: [PATCH] fix: correct typing for itemSize Closes #724 Reverts #688 - the PR was incorrect, and I misled the contributor. --- src/components.tsx | 5 +++-- src/hooks/useChangedChildSizes.ts | 4 ++-- src/interfaces.ts | 3 +++ src/sizeSystem.ts | 4 +--- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/components.tsx b/src/components.tsx index b9c74c9db..37590a7e0 100644 --- a/src/components.tsx +++ b/src/components.tsx @@ -21,6 +21,7 @@ import { GridRootProps, FlatIndexLocationWithAlign, FlatScrollIntoViewLocation, + SizeFunction, } from './interfaces' import { List } from './List' import { Grid } from './Grid' @@ -121,7 +122,7 @@ export interface VirtuosoProps extends ListRootProps { * * The default implementation reads `el.getBoundingClientRect().height` and `el.getBoundingClientRect().width`. */ - itemSize?: (el: HTMLElement, dimension: 'height' | 'width') => number + itemSize?: SizeFunction /** * Can be used to improve performance if the rendered items are of known size. @@ -349,7 +350,7 @@ export interface TableVirtuosoProps extends Omit, 'com * * The default implementation reads `el.getBoundingClientRect().height` and `el.getBoundingClientRect().width`. */ - itemSize?: (el: HTMLElement, dimension: 'height' | 'width') => number + itemSize?: SizeFunction /** * Can be used to improve performance if the rendered items are of known size. diff --git a/src/hooks/useChangedChildSizes.ts b/src/hooks/useChangedChildSizes.ts index c57ce3309..9afcdd08a 100644 --- a/src/hooks/useChangedChildSizes.ts +++ b/src/hooks/useChangedChildSizes.ts @@ -1,7 +1,7 @@ import { Log, LogLevel } from '../loggerSystem' -import { SizeFunction, SizeRange } from '../sizeSystem' +import { SizeRange } from '../sizeSystem' import { useSizeWithElRef } from './useSize' -import { ScrollContainerState } from '../interfaces' +import { SizeFunction, ScrollContainerState } from '../interfaces' export default function useChangedListContentsSizes( callback: (ranges: SizeRange[]) => void, itemSize: SizeFunction, diff --git a/src/interfaces.ts b/src/interfaces.ts index 2f05b7d8c..7b02b30da 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -327,3 +327,6 @@ export interface ScrollContainerState { scrollTop: number viewportHeight: number } + +/** Calculates the height of `el`, which will be the `Item` element in the DOM. */ +export type SizeFunction = (el: HTMLElement, field: 'offsetHeight' | 'offsetWidth') => number diff --git a/src/sizeSystem.ts b/src/sizeSystem.ts index d2e805541..5ac63b1a4 100644 --- a/src/sizeSystem.ts +++ b/src/sizeSystem.ts @@ -4,6 +4,7 @@ import * as arrayBinarySearch from './utils/binaryArraySearch' import { correctItemSize } from './utils/correctItemSize' import { loggerSystem, Log, LogLevel } from './loggerSystem' import { recalcSystem } from './recalcSystem' +import { SizeFunction } from './interfaces' export interface SizeRange { startIndex: number @@ -273,9 +274,6 @@ const SIZE_MAP = { offsetWidth: 'width', } as const -/** Calculates the height of `el`, which will be the `Item` element in the DOM. */ -export type SizeFunction = (el: HTMLElement, field: 'offsetHeight' | 'offsetWidth') => number - export const sizeSystem = u.system( ([{ log }, { recalcInProgress }]) => { const sizeRanges = u.stream()