diff --git a/packages/core/src/data-editor/row-grouping-api.ts b/packages/core/src/data-editor/row-grouping-api.ts index 988de9013..035fef94c 100644 --- a/packages/core/src/data-editor/row-grouping-api.ts +++ b/packages/core/src/data-editor/row-grouping-api.ts @@ -1,18 +1,31 @@ import React from "react"; import type { Item } from "../internal/data-grid/data-grid-types.js"; -import { flattenRowGroups, mapRowIndexToPath, type RowGroup, type RowGroupingOptions, type MapResult } from "./row-grouping.js"; +import { flattenRowGroups, mapRowIndexToPath, type FlattenedRowGroup, type RowGroup, type RowGroupingOptions } from "./row-grouping.js"; -export interface RowGroupingMapperResult extends Omit { +export interface RowGroupingMapperResult { path: readonly number[]; - originalIndex: T; isGroupHeader: boolean; groupRows: number; + originalIndex: T; + readonly groupIndex: number; + readonly contentIndex: number; } -export type RowGroupingMapper = { - (itemOrRow: number): RowGroupingMapperResult; - (itemOrRow: Item): RowGroupingMapperResult; -}; +export type RowGroupingMapper = (itemOrRow: number | Item) => RowGroupingMapperResult; + +export function rowGroupingMapper(itemOrRow: number, flattenedRowGroups?: readonly FlattenedRowGroup[] | undefined): RowGroupingMapperResult; +export function rowGroupingMapper(itemOrRow: Item, flattenedRowGroups?: readonly FlattenedRowGroup[] | undefined): RowGroupingMapperResult; +export function rowGroupingMapper(itemOrRow: number | Item, flattenedRowGroups?: readonly FlattenedRowGroup[] | undefined): RowGroupingMapperResult | RowGroupingMapperResult; +export function rowGroupingMapper(itemOrRow: number | Item, flattenedRowGroups?: readonly FlattenedRowGroup[] | undefined): RowGroupingMapperResult | RowGroupingMapperResult { + if (typeof itemOrRow === "number") { + return mapRowIndexToPath(itemOrRow, flattenedRowGroups); + } + const r = mapRowIndexToPath(itemOrRow[1], flattenedRowGroups); + return { + ...r, + originalIndex: [itemOrRow[0], r.originalIndex], + } as RowGroupingMapperResult; +} export interface UseRowGroupingResult { readonly mapper: RowGroupingMapper; @@ -34,16 +47,7 @@ export function useRowGrouping(options: RowGroupingOptions | undefined, rows: nu getRowGroupingForPath, updateRowGroupingByPath, mapper: React.useCallback( - (itemOrRow: number | Item) => { - if (typeof itemOrRow === "number") { - return mapRowIndexToPath(itemOrRow, flattenedRowGroups); - } - const r = mapRowIndexToPath(itemOrRow[1], flattenedRowGroups); - return { - ...r, - originalIndex: [itemOrRow[0], r.originalIndex], - } as any; // FIXME - }, + (itemOrRow: number | Item) => rowGroupingMapper(itemOrRow, flattenedRowGroups), [flattenedRowGroups] ), }; diff --git a/packages/core/src/data-editor/row-grouping.ts b/packages/core/src/data-editor/row-grouping.ts index f226dd93b..d975fb7b5 100644 --- a/packages/core/src/data-editor/row-grouping.ts +++ b/packages/core/src/data-editor/row-grouping.ts @@ -3,6 +3,7 @@ import type { Theme } from "../common/styles.js"; import type { DataEditorProps } from "./data-editor.js"; import type { DataGridProps } from "../internal/data-grid/data-grid.js"; import { whenDefined } from "../common/utils.js"; +import type { RowGroupingMapperResult } from "./row-grouping-api.js"; type Mutable = { -readonly [K in keyof T]: T[K]; @@ -177,17 +178,9 @@ export function flattenRowGroups(rowGrouping: RowGroupingOptions, rows: number): }); } -export interface MapResult { - readonly path: readonly number[]; - readonly isGroupHeader: boolean; - readonly originalIndex: number; - readonly groupIndex: number; - readonly groupRows: number; - readonly contentIndex: number; -} // grid relative index to path and other details -export function mapRowIndexToPath(row: number, flattenedRowGroups?: readonly FlattenedRowGroup[]): MapResult { +export function mapRowIndexToPath(row: number, flattenedRowGroups?: readonly FlattenedRowGroup[]): RowGroupingMapperResult { if (flattenedRowGroups === undefined || flattenRowGroups.length === 0) return { path: [row],