From f1a85ee0462cb7af62c4aac6105fc9f979ebb778 Mon Sep 17 00:00:00 2001 From: Franco Springveldt Date: Wed, 14 Jun 2023 14:20:45 +1200 Subject: [PATCH] SURVEY-17202 adding the ability to place a cell in editing mode --- src/contexts/GridContext.tsx | 4 +++ src/contexts/GridContextProvider.tsx | 37 +++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) 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..ccfde415 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()) { + wait(100).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,