Skip to content

Commit

Permalink
Fix:#6141:DataTable:Column: onCellEditComplete is fired twice (#6641)
Browse files Browse the repository at this point in the history
* Fix:#6141:DataTable:Column: onCellEditComplete is fired twice

* remove unused import

* feat: use React.useEffect sync editingRowData
  • Loading branch information
kl-nevermore authored May 20, 2024
1 parent 463ba5c commit ecd37b6
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions components/lib/datatable/BodyCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const BodyCell = React.memo((props) => {
const selfClick = React.useRef(false);
const tabindexTimeout = React.useRef(null);
const initFocusTimeout = React.useRef(null);
const editingRowDataStateRef = React.useRef(null);
const { ptm, ptmo, cx } = props.ptCallbacks;

const getColumnProp = (name) => ColumnBase.getCProp(props.column, name);
Expand Down Expand Up @@ -157,43 +158,41 @@ export const BodyCell = React.memo((props) => {
unbindDocumentClickListener();
OverlayService.off('overlay-click', overlayEventListener.current);
overlayEventListener.current = null;
editingRowDataStateRef.current = null;
selfClick.current = false;
}, 1);
};

const switchCellToViewMode = (event, submit) => {
const callbackParams = getCellCallbackParams(event);
const newRowData = { ...editingRowDataStateRef.current };
const newValue = resolveFieldData(newRowData);
const params = { ...callbackParams, newRowData, newValue };
const onCellEditCancel = getColumnProp('onCellEditCancel');
const cellEditValidator = getColumnProp('cellEditValidator');
const onCellEditComplete = getColumnProp('onCellEditComplete');

if (!submit && onCellEditCancel) {
onCellEditCancel(params);
}

setEditingRowDataState((prev) => {
const newRowData = prev;
const newValue = resolveFieldData(newRowData);
const params = { ...callbackParams, newRowData, newValue };
const onCellEditCancel = getColumnProp('onCellEditCancel');
const cellEditValidator = getColumnProp('cellEditValidator');
const onCellEditComplete = getColumnProp('onCellEditComplete');

if (!submit && onCellEditCancel) {
onCellEditCancel(params);
}
let valid = true;

let valid = true;
if ((!submit || cellEditValidateOnClose()) && cellEditValidator) {
valid = cellEditValidator(params);
}

if ((!submit || cellEditValidateOnClose()) && cellEditValidator) {
valid = cellEditValidator(params);
if (valid) {
if (submit && onCellEditComplete) {
onCellEditComplete(params);
}

if (valid) {
if (submit && onCellEditComplete) {
setTimeout(() => onCellEditComplete(params));
}

closeCell(event);
} else {
event.preventDefault();
}
closeCell(event);
} else {
event.preventDefault();
}

return newRowData;
});
setEditingRowDataState(newRowData);
};

const findNextSelectableCell = (cell) => {
Expand Down Expand Up @@ -523,6 +522,12 @@ export const BodyCell = React.memo((props) => {
}
}, [props.editingMeta]);

React.useEffect(() => {
if (editingRowDataState) {
editingRowDataStateRef.current = editingRowDataState;
}
}, [editingRowDataState]);

React.useEffect(() => {
if (props.editMode === 'cell' || props.editMode === 'row') {
const callbackParams = getCellCallbackParams();
Expand Down

0 comments on commit ecd37b6

Please sign in to comment.