Skip to content

Commit 33c6eef

Browse files
authored
Merge pull request #17 from Workfront/edit-cell-function
feat: create & pass editCell function to onClick
2 parents bda0bdd + 906c40d commit 33c6eef

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/core/src/data-editor/data-editor.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ type ScrollToFn = (
617617
}
618618
) => void;
619619

620+
type EnterCellEditModeFn = (location: Item, initialValue?: string) => void;
621+
620622
/** @category DataEditor */
621623
export interface DataEditorRef {
622624
/**
@@ -645,6 +647,10 @@ export interface DataEditorRef {
645647
* Scrolls to the desired cell or location in the grid.
646648
*/
647649
scrollTo: ScrollToFn;
650+
/**
651+
* Enters edit mode of the provided cell.
652+
*/
653+
enterCellEditMode: EnterCellEditModeFn;
648654
}
649655

650656
const loadingCell: GridCell = {
@@ -1523,6 +1529,27 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
15231529
[mangledCols, onRowAppended, rowMarkerOffset, rows, scrollTo, setCurrent]
15241530
);
15251531

1532+
const enterCellEditMode: EnterCellEditModeFn = React.useCallback(
1533+
([col, row], initialValue): void => {
1534+
1535+
const cell = getCellContentRef.current([col - rowMarkerOffset, row]);
1536+
const bounds = gridRef.current?.getBounds(col, row);
1537+
1538+
if (cell.allowOverlay && isReadWriteCell(cell) && cell.readonly !== true && bounds) {
1539+
setOverlaySimple({
1540+
target: bounds,
1541+
content: cell,
1542+
initialValue,
1543+
cell: [col, row],
1544+
highlight: true,
1545+
forceEditMode: true,
1546+
});
1547+
}
1548+
1549+
},
1550+
[rowMarkerOffset, setOverlaySimple]
1551+
);
1552+
15261553
const getCustomNewRowTargetColumn = React.useCallback(
15271554
(col: number): number | undefined => {
15281555
const customTargetColumn =
@@ -3357,6 +3384,7 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
33573384
return gridRef.current?.getBounds(col + rowMarkerOffset, row);
33583385
},
33593386
focus: () => gridRef.current?.focus(),
3387+
enterCellEditMode,
33603388
emit: async e => {
33613389
switch (e) {
33623390
case "delete":
@@ -3417,7 +3445,7 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
34173445
},
34183446
scrollTo,
34193447
}),
3420-
[appendRow, onCopy, onKeyDown, onPasteInternal, rowMarkerOffset, scrollTo]
3448+
[appendRow, enterCellEditMode, onCopy, onKeyDown, onPasteInternal, rowMarkerOffset, scrollTo]
34213449
);
34223450

34233451
const [selCol, selRow] = currentCell ?? [];

packages/core/src/data-grid/cells/cell-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
CustomCell,
88
ProvideEditorCallback,
99
BaseGridMouseEventArgs,
10+
Item,
1011
} from "../data-grid-types";
1112

1213
export interface BaseDrawArgs {
@@ -63,6 +64,7 @@ interface BaseCellRenderer<T extends InnerGridCell> {
6364
readonly posY: number;
6465
readonly bounds: Rectangle;
6566
readonly theme: Theme;
67+
readonly location: Item;
6668
readonly preventDefault: () => void;
6769
} & BaseGridMouseEventArgs
6870
) => T | undefined;

0 commit comments

Comments
 (0)