Skip to content

Commit 8305366

Browse files
adds elect by key
1 parent 17d564b commit 8305366

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/DataGrid.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import type {
5555
FillEvent,
5656
Maybe,
5757
Position,
58+
PositionByKey,
5859
Renderers,
5960
RowsChangeData,
6061
SelectCellOptions,
@@ -111,6 +112,7 @@ export interface DataGridHandle {
111112
element: HTMLDivElement | null;
112113
scrollToCell: (position: PartialPosition) => void;
113114
selectCell: (position: Position, options?: SelectCellOptions) => void;
115+
selectCellByKey: (position: PositionByKey, options?: SelectCellOptions) => void;
114116
}
115117

116118
type SharedDivProps = Pick<
@@ -547,7 +549,8 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
547549
setScrollToPosition({ idx: scrollToIdx, rowIdx: scrollToRowIdx });
548550
}
549551
},
550-
selectCell
552+
selectCell,
553+
selectCellByKey
551554
})
552555
);
553556

@@ -859,6 +862,14 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
859862
}
860863
}
861864

865+
function selectCellByKey(position: PositionByKey, options?: SelectCellOptions): void {
866+
const columnIdx = columns.findIndex((col) => col.key === position.columnKey);
867+
if (columnIdx === -1) {
868+
throw new Error(`Column with key "${position.columnKey}" not found`);
869+
}
870+
selectCell({ idx: columnIdx, rowIdx: position.rowIdx }, options);
871+
}
872+
862873
function selectHeaderCell({ idx, rowIdx }: Position): void {
863874
selectCell({ rowIdx: minRowIdx + rowIdx - 1, idx });
864875
}

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ export interface Position {
120120
readonly rowIdx: number;
121121
}
122122

123+
export interface PositionByKey {
124+
readonly columnKey: string;
125+
readonly rowIdx: number;
126+
}
127+
123128
export interface RenderCellProps<TRow, TSummaryRow = unknown> {
124129
column: CalculatedColumn<TRow, TSummaryRow>;
125130
row: TRow;

0 commit comments

Comments
 (0)