@atlaskit/tree
Version:
A React Component for displaying expandable and sortable tree hierarchies
25 lines (24 loc) • 592 B
JavaScript
/*
Changes the tree data structure with minimal reference changes.
*/
export const mutateTree = (tree, itemId, mutation) => {
const itemToChange = tree.items[itemId];
if (!itemToChange) {
// Item not found
return tree;
}
// Returning a clone of the tree structure and overwriting the field coming in mutation
return {
// rootId should not change
rootId: tree.rootId,
items: {
// copy all old items
...tree.items,
// overwriting only the item being changed
[itemId]: {
...itemToChange,
...mutation
}
}
};
};