diff --git a/src/contexts/GridContext.tsx b/src/contexts/GridContext.tsx index 9e8254e5..fa139f5f 100644 --- a/src/contexts/GridContext.tsx +++ b/src/contexts/GridContext.tsx @@ -39,6 +39,7 @@ export interface GridContextType { autoSizeColumns: (props?: AutoSizeColumnsProps) => AutoSizeColumnsResult; sizeColumnsToFit: () => void; cancelEdit: () => void; + startEditing: ({ rowId, colId, rowPinned }: { rowId: number; colId: string; rowPinned?: "top" | "bottom" }) => void; stopEditing: () => void; updatingCells: ( props: { selectedRows: GridBaseRow[]; field?: string }, @@ -147,6 +148,9 @@ export const GridContext = createContext>({ cancelEdit: () => { console.error("no context provider for cancelEdit"); }, + startEditing: () => { + console.error("no context provider for startEditing"); + }, stopEditing: () => { console.error("no context provider for stopEditing"); }, diff --git a/src/contexts/GridContextProvider.tsx b/src/contexts/GridContextProvider.tsx index 07f4b396..4ba7c504 100644 --- a/src/contexts/GridContextProvider.tsx +++ b/src/contexts/GridContextProvider.tsx @@ -21,7 +21,7 @@ interface GridContextProps { * Also, make sure the provider is created in a separate component, otherwise it won't be found. */ export const GridContextProvider = (props: GridContextProps): ReactElement => { - const { modifyUpdating } = useContext(GridUpdatingContext); + const { modifyUpdating, checkUpdating } = useContext(GridUpdatingContext); const [gridApi, setGridApi] = useState(); const [columnApi, setColumnApi] = useState(); const [gridReady, setGridReady] = useState(false); @@ -377,6 +377,40 @@ export const GridContextProvider = (props: GridCont gridApi.stopEditing(); }, [gridApi]); + const startEditing = useCallback( + async ({ rowId, colId, rowPinned }: { rowId: number; colId: string; rowPinned?: "top" | "bottom" }) => { + if (!gridApi) return; + + const colDef = gridApi.getColumnDef(colId); + if (!colDef) return; + + // Cell already being edited, so don't re-edit until finished + if (checkUpdating([colDef.field ?? ""], rowId)) { + return; + } + + selectRowsById([rowId]); + prePopupOps(); + + const rowIndex = gridApi.getRowNode(`${rowId}`)?.rowIndex; + if (rowIndex != null) { + const focusAndEdit = () => { + gridApi.setFocusedCell(rowIndex, colId, rowPinned); + gridApi.startEditingCell({ + rowIndex, + colKey: colId, + rowPinned, + }); + }; + focusAndEdit(); + if (!gridApi.getEditingCells().length) { + wait(200).then(focusAndEdit); + } + } + }, + [checkUpdating, gridApi, prePopupOps, selectRowsById], + ); + /** * This differs from stopEdit in that it will also invoke cellEditingCompleteCallback */ @@ -618,6 +652,7 @@ export const GridContextProvider = (props: GridCont ensureSelectedRowIsVisible, sizeColumnsToFit, autoSizeColumns, + startEditing, stopEditing, cancelEdit, updatingCells,