Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Tree copy by doing a shallow copy of the value #5081

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions components/lib/tree/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,32 @@ export const Tree = React.memo(
dragState.current = null;
};

/**
* Deep copy a value. If the value has a data property, it will be shallow copied.
* Values that are not plain objects or arrays are returned as-is.
*/
const cloneValue = (value) => {
if (Array.isArray(value)) {
return value.map(cloneValue);
} else if (!!value && Object.getPrototypeOf(value) === Object.prototype) {
const result = {};

// Leave data property alone and clone children
for (let key in value) {
if (key !== 'data') {
result[key] = cloneValue(value[key]);
} else {
result[key] = value[key];
}
}

return result;
} else return value;
};

const onDrop = (event) => {
if (validateDropNode(dragState.current.path, event.path)) {
let value = JSON.parse(JSON.stringify(props.value));
const value = cloneValue(props.value);
let dragPaths = dragState.current.path.split('-');

dragPaths.pop();
Expand Down Expand Up @@ -89,7 +112,7 @@ export const Tree = React.memo(

const onDropPoint = (event) => {
if (validateDropPoint(event)) {
let value = JSON.parse(JSON.stringify(props.value));
const value = cloneValue(props.value);
let dragPaths = dragState.current.path.split('-');

dragPaths.pop();
Expand Down
Loading