Skip to content

Commit

Permalink
don't disable save when editing rows
Browse files Browse the repository at this point in the history
  • Loading branch information
sc420 committed Jan 20, 2024
1 parent 340489b commit 253f763
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test("should trigger value change when deleting a row", () => {
expect(handleChangeValues).toHaveBeenCalledWith([new Port("b", true)]);
});

test("validation result should change when entering/leaving edit mode", () => {
test("should be valid when editing the row", () => {
const inputPorts: Port[] = [new Port("a", false), new Port("b", true)];
const handleChangeValues = jest.fn();
const handleValidate = jest.fn();
Expand All @@ -64,11 +64,6 @@ test("validation result should change when entering/leaving edit mode", () => {
const editIconButton = screen.getAllByLabelText("Edit");
fireEvent.click(editIconButton[0]);

expect(handleValidate).lastCalledWith(false);

const cancelIconButton = screen.getAllByLabelText("Cancel");
fireEvent.click(cancelIconButton[0]);

expect(handleValidate).lastCalledWith(true);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
GridRowModes,
GridToolbarContainer,
type GridColDef,
type GridEventListener,
type GridRowId,
type GridRowModel,
type GridRowModesModel,
Expand Down Expand Up @@ -100,7 +99,6 @@ const EditOperationInputPortsTab: FunctionComponent<
return inputPorts.map((inputPort) => inputPortToRowModel(inputPort));
}, [inputPortToRowModel, inputPorts]);

const [isEditingRow, setEditingRow] = useState(false);
const [errorMessages, setErrorMessages] = useState<string[]>([]);

const [rows, setRows] =
Expand All @@ -113,44 +111,18 @@ const EditOperationInputPortsTab: FunctionComponent<
});
}, [rows]);

const updateStatesOnStartEdit = useCallback(() => {
setEditingRow(true);
}, []);

const updateStatesOnStopEdit = useCallback(() => {
setEditingRow(false);
}, []);

const handleRowEditStart: GridEventListener<"rowEditStart"> = useCallback(
(params, event) => {
updateStatesOnStartEdit();
},
[updateStatesOnStartEdit],
);

const handleRowEditStop: GridEventListener<"rowEditStop"> = useCallback(
(params, event) => {
updateStatesOnStopEdit();
},
[updateStatesOnStopEdit],
);

const handleEditClick = useCallback(
(id: GridRowId) => () => {
updateStatesOnStartEdit();

setRowModesModel({ ...rowModesModel, [id]: { mode: GridRowModes.Edit } });
},
[rowModesModel, updateStatesOnStartEdit],
[rowModesModel],
);

const handleSaveClick = useCallback(
(id: GridRowId) => () => {
updateStatesOnStopEdit();

setRowModesModel({ ...rowModesModel, [id]: { mode: GridRowModes.View } });
},
[rowModesModel, updateStatesOnStopEdit],
[rowModesModel],
);

const handleDeleteClick = useCallback(
Expand All @@ -162,8 +134,6 @@ const EditOperationInputPortsTab: FunctionComponent<

const handleCancelClick = useCallback(
(id: GridRowId) => () => {
updateStatesOnStopEdit();

setRowModesModel({
...rowModesModel,
[id]: { mode: GridRowModes.View, ignoreModifications: true },
Expand All @@ -175,7 +145,7 @@ const EditOperationInputPortsTab: FunctionComponent<
setRows(rows.filter((row) => row.id !== id));
}
},
[rowModesModel, rows, updateStatesOnStopEdit],
[rowModesModel, rows],
);

const handleRowModesModelChange = useCallback(
Expand Down Expand Up @@ -323,7 +293,7 @@ const EditOperationInputPortsTab: FunctionComponent<

setErrorMessages(errorMessages);

const isValid = !isEditingRow && errorMessages.length === 0;
const isValid = errorMessages.length === 0;

onValidate(isValid);

Expand All @@ -337,7 +307,6 @@ const EditOperationInputPortsTab: FunctionComponent<
getDuplicatePortIdsErrorMessages,
getEmptyRowsErrorMessages,
getInvalidPortIdErrorMessages,
isEditingRow,
onChangeValues,
onValidate,
rowsToInputPorts,
Expand All @@ -353,8 +322,6 @@ const EditOperationInputPortsTab: FunctionComponent<
editMode="row"
rowModesModel={rowModesModel}
onRowModesModelChange={handleRowModesModelChange}
onRowEditStart={handleRowEditStart}
onRowEditStop={handleRowEditStop}
processRowUpdate={processRowUpdate}
slots={{
toolbar: EditToolbar,
Expand All @@ -364,12 +331,11 @@ const EditOperationInputPortsTab: FunctionComponent<
}}
disableColumnMenu
/>
{!isEditingRow &&
errorMessages.map((errorMessage, index) => (
<Alert key={index} severity="error" sx={{ width: "100%" }}>
{errorMessage}
</Alert>
))}
{errorMessages.map((errorMessage, index) => (
<Alert key={index} severity="error" sx={{ width: "100%" }}>
{errorMessage}
</Alert>
))}
</Stack>
</Container>
)
Expand Down

0 comments on commit 253f763

Please sign in to comment.