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

Function move

packages/react-stately/src/data/useTreeData.ts:379–419  ·  view source on GitHub ↗
(key: Key, toParentKey: Key | null, index: number)

Source from the content-addressed store, hash-verified

377 this.remove(...selectedKeys);
378 },
379 move(key: Key, toParentKey: Key | null, index: number) {
380 setItems(({items, nodeMap: originalMap}) => {
381 let node = originalMap.get(key);
382 if (!node) {
383 return {items, nodeMap: originalMap};
384 }
385
386 let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);
387
388 const movedNode = {
389 ...node,
390 parentKey: toParentKey
391 };
392
393 // If parentKey is null, insert into the root.
394 if (toParentKey == null) {
395 addNode(movedNode, newMap);
396 return {
397 items: [...newItems.slice(0, index), movedNode, ...newItems.slice(index)],
398 nodeMap: newMap
399 };
400 }
401
402 // Otherwise, update the parent node and its ancestors.
403 return updateTree(
404 newItems,
405 toParentKey,
406 parentNode => ({
407 key: parentNode.key,
408 parentKey: parentNode.parentKey,
409 value: parentNode.value,
410 children: [
411 ...parentNode.children!.slice(0, index),
412 movedNode,
413 ...parentNode.children!.slice(index)
414 ]
415 }),
416 newMap
417 );
418 });
419 },
420 moveBefore(key: Key, keys: Iterable<Key>) {
421 setItems(prevState => {
422 let {items, nodeMap} = prevState;

Callers

nothing calls this directly

Calls 3

updateTreeFunction · 0.85
addNodeFunction · 0.85
sliceMethod · 0.80

Tested by

no test coverage detected