MCPcopy Create free account
hub / github.com/adobe/react-spectrum / updateTree

Function updateTree

packages/react-stately/src/data/useTreeData.ts:192–258  ·  view source on GitHub ↗
(
    items: TreeNode<T>[],
    key: Key | null,
    update: (node: TreeNode<T>) => TreeNode<T> | null,
    originalMap: Map<Key, TreeNode<T>>
  )

Source from the content-addressed store, hash-verified

190 }
191
192 function updateTree(
193 items: TreeNode<T>[],
194 key: Key | null,
195 update: (node: TreeNode<T>) => TreeNode<T> | null,
196 originalMap: Map<Key, TreeNode<T>>
197 ) {
198 let node = key == null ? null : originalMap.get(key);
199 if (node == null) {
200 return {items, nodeMap: originalMap};
201 }
202 let map = new Map<Key, TreeNode<T>>(originalMap);
203
204 // Create a new node. If null, then delete the node, otherwise replace.
205 let newNode = update(node);
206 if (newNode == null) {
207 deleteNode(node, map);
208 } else {
209 addNode(newNode, map);
210 }
211
212 // Walk up the tree and update each parent to refer to the new children.
213 while (node && node.parentKey) {
214 let nextParent = map.get(node.parentKey)!;
215 let copy: TreeNode<T> = {
216 key: nextParent.key,
217 parentKey: nextParent.parentKey,
218 value: nextParent.value,
219 children: null
220 };
221
222 let children = nextParent.children;
223 if (newNode == null && children) {
224 children = children.filter(c => c !== node);
225 }
226
227 copy.children =
228 children?.map(child => {
229 if (child === node) {
230 // newNode cannot be null here due to the above filter.
231 return newNode!;
232 }
233
234 return child;
235 }) ?? null;
236
237 map.set(copy.key, copy);
238
239 newNode = copy;
240 node = nextParent;
241 }
242
243 if (newNode == null) {
244 items = items.filter(c => c !== node);
245 }
246
247 return {
248 items: items.map(item => {
249 if (item === node) {

Callers 6

insertFunction · 0.85
removeFunction · 0.85
moveFunction · 0.85
updateFunction · 0.85
postorderFunction · 0.85
moveItemsFunction · 0.85

Calls 5

deleteNodeFunction · 0.85
addNodeFunction · 0.85
updateFunction · 0.70
filterMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected