Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
legacy-peer-deps = true
legacy-peer-deps = true
71 changes: 45 additions & 26 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,31 @@ import {
scrollIntoView,
sign
} from './utils';
import type {
CalculatedColumn,
CellClipboardEvent,
CellCopyArgs,
CellKeyboardEvent,
CellKeyDownArgs,
CellMouseEventHandler,
CellNavigationMode,
CellPasteArgs,
CellSelectArgs,
Column,
ColumnOrColumnGroup,
ColumnWidths,
Direction,
FillEvent,
Maybe,
Position,
Renderers,
RowsChangeData,
SelectCellOptions,
SelectHeaderRowEvent,
SelectRowEvent,
SortColumn
import {
type CalculatedColumn,
type CellClipboardEvent,
type CellCopyArgs,
type CellKeyboardEvent,
type CellKeyDownArgs,
type CellMouseEventHandler,
type CellNavigationMode,
type CellPasteArgs,
type CellSelectArgs,
type Column,
type ColumnOrColumnGroup,
type ColumnWidths,
type Direction,
type FillEvent,
type Maybe,
type Position,
type Renderers,
type RowsChangeData,
type SelectCellOptions,
type SelectHeaderRowEvent,
type SelectRowEvent,
type SortColumn,
type VirtualizationOptions,
isVirtualizationOptions
} from './types';
import { defaultRenderCell } from './Cell';
import { renderCheckbox as defaultRenderCheckbox } from './cellRenderers';
Expand Down Expand Up @@ -219,7 +221,9 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
* Toggles and modes
*/
/** @default true */
enableVirtualization?: Maybe<boolean>;
enableVirtualization?: Maybe<boolean | VirtualizationOptions>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enableVirtualization?: Maybe<boolean | VirtualizationOptions>;
enableVirtualization?: Maybe<'row' | 'column' | boolean>;

I think we can keep it simple for now.




/**
* Miscellaneous
Expand Down Expand Up @@ -317,7 +321,22 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
const renderCheckbox =
renderers?.renderCheckbox ?? defaultRenderers?.renderCheckbox ?? defaultRenderCheckbox;
const noRowsFallback = renderers?.noRowsFallback ?? defaultRenderers?.noRowsFallback;
const enableVirtualization = rawEnableVirtualization ?? true;

const enableVirtualization = useMemo(() => {

if(isVirtualizationOptions(rawEnableVirtualization)) {
return rawEnableVirtualization;
}
if(typeof rawEnableVirtualization === 'boolean') {
if(!rawEnableVirtualization) {
return {rows: false, columns: false, rowsOverscanThreshold: 4};
}
}

return {rows: true, columns: true, rowsOverscanThreshold: 4};

}, [rawEnableVirtualization]);

const direction = rawDirection ?? 'ltr';

/**
Expand Down Expand Up @@ -447,7 +466,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
rowHeight,
clientHeight,
scrollTop,
enableVirtualization
enableVirtualization
});

const viewportColumns = useViewportColumns({
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useCalculatedColumns.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo } from 'react';

import { clampColumnWidth, max, min } from '../utils';
import type { CalculatedColumn, CalculatedColumnParent, ColumnOrColumnGroup, Omit } from '../types';
import { isVirtualizationOptions, type CalculatedColumn, type CalculatedColumnParent, type ColumnOrColumnGroup, type Omit, type VirtualizationOptions } from '../types';
import { renderValue } from '../cellRenderers';
import { SELECT_COLUMN_KEY } from '../Columns';
import type { DataGridProps } from '../DataGrid';
Expand Down Expand Up @@ -34,7 +34,7 @@ interface CalculatedColumnsArgs<R, SR> {
viewportWidth: number;
scrollLeft: number;
getColumnWidth: (column: CalculatedColumn<R, SR>) => string | number;
enableVirtualization: boolean;
enableVirtualization: VirtualizationOptions;
}

export function useCalculatedColumns<R, SR>({
Expand Down Expand Up @@ -203,14 +203,14 @@ export function useCalculatedColumns<R, SR>({
return { templateColumns, layoutCssVars, totalFrozenColumnWidth, columnMetrics };
}, [getColumnWidth, columns, lastFrozenColumnIndex]);

const [colOverscanStartIdx, colOverscanEndIdx] = useMemo((): [number, number] => {
if (!enableVirtualization) {
const [colOverscanStartIdx, colOverscanEndIdx] = useMemo((): [number, number] => {
if (enableVirtualization.columns === false) {
return [0, columns.length - 1];
}
// get the viewport's left side and right side positions for non-frozen columns
const viewportLeft = scrollLeft + totalFrozenColumnWidth;
const viewportRight = scrollLeft + viewportWidth;
// get first and last non-frozen column indexes
// get first and last non-frozen column indexes
const lastColIdx = columns.length - 1;
const firstUnfrozenColumnIdx = min(lastFrozenColumnIndex + 1, lastColIdx);

Expand Down
7 changes: 4 additions & 3 deletions src/hooks/useViewportRows.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useMemo } from 'react';

import { floor, max, min } from '../utils';
import type { VirtualizationOptions } from '../types';

interface ViewportRowsArgs<R> {
rows: readonly R[];
rowHeight: number | ((row: R) => number);
clientHeight: number;
scrollTop: number;
enableVirtualization: boolean;
enableVirtualization: VirtualizationOptions
}

export function useViewportRows<R>({
Expand Down Expand Up @@ -107,8 +108,8 @@ export function useViewportRows<R>({
let rowOverscanStartIdx = 0;
let rowOverscanEndIdx = rows.length - 1;

if (enableVirtualization) {
const overscanThreshold = 4;
if (enableVirtualization.rows !== false) {
const overscanThreshold = (enableVirtualization.rows === true || enableVirtualization.rows === undefined) ? 4 : enableVirtualization.rows.overscanThreshold;
const rowVisibleStartIdx = findRowIdx(scrollTop);
const rowVisibleEndIdx = findRowIdx(scrollTop + clientHeight);
rowOverscanStartIdx = max(0, rowVisibleStartIdx - overscanThreshold);
Expand Down
32 changes: 32 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,35 @@ export type ColumnWidths = ReadonlyMap<string, ColumnWidth>;
export type Direction = 'ltr' | 'rtl';

export type ResizedWidth = number | 'max-content';


export interface VirtualizationOptions {
/** Enable row virtualization */
/** @default 4 */
rows?: boolean | {overscanThreshold: number};
columns?: boolean;
}
export function isVirtualizationOptions(obj: unknown): obj is VirtualizationOptions {
if (typeof obj !== 'object' || obj === null) return false;
const o = obj as VirtualizationOptions;

if ('rows' in o && typeof o.rows !== 'undefined') {
if (typeof o.rows !== 'boolean') {
if (
typeof o.rows !== 'object' ||
o.rows === null ||
!('overscanThreshold' in o.rows) ||
typeof (o.rows as any).overscanThreshold !== 'number'
) {
return false;
}
}
}

if ('columns' in o && typeof o.columns !== 'undefined' && typeof o.columns !== 'boolean') {
return false;
}

return true;
}

Loading