Skip to content

Commit

Permalink
SURVEY-17202 adding the ability to place a cell in editing mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fspringveldt committed Jun 15, 2023
1 parent 0bf87d4 commit 227a629
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
24 changes: 7 additions & 17 deletions src/components/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export const Grid = ({
const {
gridReady,
setApis,
prePopupOps,
ensureRowVisible,
getFirstRowId,
selectRowsById,
Expand All @@ -98,7 +97,8 @@ export const Grid = ({
setOnCellEditingComplete,
getColDef,
} = useContext(GridContext);
const { checkUpdating, updatedDep, updatingCols } = useContext(GridUpdatingContext);
const { updatedDep, updatingCols } = useContext(GridUpdatingContext);
const gridContext = useContext(GridContext);

const gridDivRef = useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -387,23 +387,13 @@ export const Grid = ({
*/
const startCellEditing = useCallback(
(event: CellEvent) => {
prePopupOps();
if (!event.node.isSelected()) {
event.node.setSelected(true, true);
}
// Cell already being edited, so don't re-edit until finished
if (checkUpdating([event.colDef.field ?? ""], event.data.id)) {
return;
}

if (event.rowIndex !== null) {
event.api.startEditingCell({
rowIndex: event.rowIndex,
colKey: event.column.getColId(),
event.data.id &&
gridContext.startCellEditing({
rowId: event.data.id,
colId: event.column.getColId(),
});
}
},
[checkUpdating, prePopupOps],
[gridContext],
);

/**
Expand Down
4 changes: 4 additions & 0 deletions src/contexts/GridContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface GridContextType<RowType extends GridBaseRow> {
autoSizeColumns: (props?: AutoSizeColumnsProps) => AutoSizeColumnsResult;
sizeColumnsToFit: () => void;
cancelEdit: () => void;
startCellEditing: ({ rowId, colId }: { rowId: number; colId: string }) => void;
stopEditing: () => void;
updatingCells: (
props: { selectedRows: GridBaseRow[]; field?: string },
Expand Down Expand Up @@ -147,6 +148,9 @@ export const GridContext = createContext<GridContextType<any>>({
cancelEdit: () => {
console.error("no context provider for cancelEdit");
},
startCellEditing: () => {
console.error("no context provider for startCellEditing");
},
stopEditing: () => {
console.error("no context provider for stopEditing");
},
Expand Down
31 changes: 30 additions & 1 deletion src/contexts/GridContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <RowType extends GridBaseRow>(props: GridContextProps): ReactElement => {
const { modifyUpdating } = useContext(GridUpdatingContext);
const { modifyUpdating, checkUpdating } = useContext(GridUpdatingContext);
const [gridApi, setGridApi] = useState<GridApi>();
const [columnApi, setColumnApi] = useState<ColumnApi>();
const [gridReady, setGridReady] = useState(false);
Expand Down Expand Up @@ -377,6 +377,34 @@ export const GridContextProvider = <RowType extends GridBaseRow>(props: GridCont
gridApi.stopEditing();
}, [gridApi]);

const startCellEditing = useCallback(
async ({ rowId, colId }: { rowId: number; colId: string }) => {
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;
}

prePopupOps();

const rowIndex = gridApi.getRowNode(`${rowId}`)?.rowIndex;
if (rowIndex != null) {
const focusAndEdit = () => {
gridApi.startEditingCell({
rowIndex,
colKey: colId,
});
};
defer(focusAndEdit);
}
},
[checkUpdating, gridApi, prePopupOps],
);

/**
* This differs from stopEdit in that it will also invoke cellEditingCompleteCallback
*/
Expand Down Expand Up @@ -618,6 +646,7 @@ export const GridContextProvider = <RowType extends GridBaseRow>(props: GridCont
ensureSelectedRowIsVisible,
sizeColumnsToFit,
autoSizeColumns,
startCellEditing,
stopEditing,
cancelEdit,
updatingCells,
Expand Down

0 comments on commit 227a629

Please sign in to comment.