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

Function move

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

Source from the content-addressed store, hash-verified

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