Skip to content

Commit

Permalink
PR4814: fix deeply nested replace on row edit
Browse files Browse the repository at this point in the history
  • Loading branch information
bu3alwa committed Aug 25, 2023
1 parent dbaf012 commit 2c2551f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 2 additions & 3 deletions components/lib/datatable/BodyCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,14 @@ export const BodyCell = React.memo((props) => {
const editorCallback = (val) => {
let editingRowData = { ...editingRowDataState };

editingRowData[field] = val;

ObjectUtils.mutateFieldData(field, val, editingRowData);
setEditingRowDataState(editingRowData);

// update editing meta for complete methods on row mode
const currentData = getEditingRowData();

if (currentData) {
currentData[field] = val;
ObjectUtils.mutateFieldData(field, val, currentData);
}
};

Expand Down
21 changes: 21 additions & 0 deletions components/lib/utils/ObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,25 @@ export default class ObjectUtils {

return [];
}

static mutateFieldData(prop, value, obj) {
// Make sure types are correct
if (typeof obj === 'object' && typeof prop === 'string') {
const _prop = prop.split('.');

// Exit if for some reason the array is empty
if (_prop.length < 1) return;

// If key does not exist create and empty object
if (!obj[_prop[0]]) {
obj[_prop[0]] = {};
}

if (_prop.length === 1) {
return (obj[_prop[0]] = value);
}

this.mutateFieldData(_prop.slice(1).join('.'), value, obj[_prop[0]]);
}
}
}
1 change: 1 addition & 0 deletions components/lib/utils/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export declare class ObjectUtils {
static findLast(value: any[], callback: () => any): any;
static findLastIndex(value: any[], callback: () => any): number;
static sort(value1: any, value2: any, order: number, locale: string | string[]): number;
static mutateFieldData(prop: string, value: any, obj: object): void;
}

/**
Expand Down

0 comments on commit 2c2551f

Please sign in to comment.