- 
                Notifications
    You must be signed in to change notification settings 
- Fork 366
refactor: improve typing for useRowGrouping #1082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
84902b7
              c8842d6
              6a517ff
              f751e71
              347bcd6
              32af20f
              5b0f420
              6c30726
              818913b
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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<T> extends Omit<MapResult, 'originalIndex'> { | ||||||||||||
| export interface RowGroupingMapperResult<T> { | ||||||||||||
| path: readonly number[]; | ||||||||||||
| originalIndex: T; | ||||||||||||
| isGroupHeader: boolean; | ||||||||||||
| groupRows: number; | ||||||||||||
| originalIndex: T; | ||||||||||||
| readonly groupIndex: number; | ||||||||||||
| readonly contentIndex: number; | ||||||||||||
| } | ||||||||||||
|  | ||||||||||||
| export type RowGroupingMapper = { | ||||||||||||
| (itemOrRow: number): RowGroupingMapperResult<number>; | ||||||||||||
| (itemOrRow: Item): RowGroupingMapperResult<Item>; | ||||||||||||
| }; | ||||||||||||
| export type RowGroupingMapper = (itemOrRow: number | Item) => RowGroupingMapperResult<Item | number>; | ||||||||||||
|  | ||||||||||||
| export function rowGroupingMapper(itemOrRow: number, flattenedRowGroups?: readonly FlattenedRowGroup[] | undefined): RowGroupingMapperResult<number>; | ||||||||||||
| export function rowGroupingMapper(itemOrRow: Item, flattenedRowGroups?: readonly FlattenedRowGroup[] | undefined): RowGroupingMapperResult<Item>; | ||||||||||||
| export function rowGroupingMapper(itemOrRow: number | Item, flattenedRowGroups?: readonly FlattenedRowGroup[] | undefined): RowGroupingMapperResult<number> | RowGroupingMapperResult<Item>; | ||||||||||||
| export function rowGroupingMapper(itemOrRow: number | Item, flattenedRowGroups?: readonly FlattenedRowGroup[] | undefined): RowGroupingMapperResult<number> | RowGroupingMapperResult<Item> { | ||||||||||||
| 
     | ||||||||||||
| export function rowGroupingMapper(itemOrRow: number | Item, flattenedRowGroups?: readonly FlattenedRowGroup[] | undefined): RowGroupingMapperResult<number> | RowGroupingMapperResult<Item> { | |
| export function rowGroupingMapper(itemOrRow: number, flattenedRowGroups?: readonly FlattenedRowGroup[]): RowGroupingMapperResult<number>; | |
| export function rowGroupingMapper(itemOrRow: Item, flattenedRowGroups?: readonly FlattenedRowGroup[]): RowGroupingMapperResult<Item>; | |
| export function rowGroupingMapper(itemOrRow: number | Item, flattenedRowGroups?: readonly FlattenedRowGroup[]): RowGroupingMapperResult<number> | RowGroupingMapperResult<Item>; | |
| export function rowGroupingMapper(itemOrRow: number | Item, flattenedRowGroups?: readonly FlattenedRowGroup[]): RowGroupingMapperResult<number> | RowGroupingMapperResult<Item> { | 
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|  | @@ -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<T> = { | ||||||
| -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<number> { | ||||||
| if (flattenedRowGroups === undefined || flattenRowGroups.length === 0) | ||||||
| 
     | ||||||
| if (flattenedRowGroups === undefined || flattenRowGroups.length === 0) | |
| if (flattenedRowGroups === undefined || flattenedRowGroups.length === 0) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
RowGroupingMappertype loses type safety by returning a union typeRowGroupingMapperResult<Item | number>. This contradicts the purpose of the type overloads inrowGroupingMapper. Consider using function overloads for this type as well to maintain type safety.