-
Notifications
You must be signed in to change notification settings - Fork 351
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> { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation signature includes redundant
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||
if (typeof itemOrRow === "number") { | ||||||||||||
return mapRowIndexToPath(itemOrRow, flattenedRowGroups); | ||||||||||||
} | ||||||||||||
const r = mapRowIndexToPath(itemOrRow[1], flattenedRowGroups); | ||||||||||||
return { | ||||||||||||
...r, | ||||||||||||
originalIndex: [itemOrRow[0], r.originalIndex], | ||||||||||||
} as RowGroupingMapperResult<Item>; | ||||||||||||
} | ||||||||||||
|
||||||||||||
export interface UseRowGroupingResult { | ||||||||||||
readonly mapper: RowGroupingMapper; | ||||||||||||
|
@@ -34,16 +47,7 @@ export function useRowGrouping(options: RowGroupingOptions | undefined, rows: nu | |||||||||||
getRowGroupingForPath, | ||||||||||||
updateRowGroupingByPath, | ||||||||||||
mapper: React.useCallback<UseRowGroupingResult["mapper"]>( | ||||||||||||
(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] | ||||||||||||
), | ||||||||||||
}; | ||||||||||||
|
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a typo in the variable name. It should be
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
return { | ||||||
path: [row], | ||||||
|
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
RowGroupingMapper
type 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.Copilot uses AI. Check for mistakes.