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

Function move

packages/react-stately/src/data/useTreeData.ts:379–427  ·  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 current = toParentKey;
382 while (current != null) {
383 if (current === key) {
384 throw new Error('Cannot move an item to be a child of itself.');
385 }
386 current = originalMap.get(current)?.parentKey ?? null;
387 }
388
389 let node = originalMap.get(key);
390 if (!node) {
391 return {items, nodeMap: originalMap};
392 }
393
394 let {items: newItems, nodeMap: newMap} = updateTree(items, key, () => null, originalMap);
395
396 const movedNode = {
397 ...node,
398 parentKey: toParentKey
399 };
400
401 // If parentKey is null, insert into the root.
402 if (toParentKey == null) {
403 addNode(movedNode, newMap);
404 return {
405 items: [...newItems.slice(0, index), movedNode, ...newItems.slice(index)],
406 nodeMap: newMap
407 };
408 }
409
410 // Otherwise, update the parent node and its ancestors.
411 return updateTree(
412 newItems,
413 toParentKey,
414 parentNode => ({
415 key: parentNode.key,
416 parentKey: parentNode.parentKey,
417 value: parentNode.value,
418 children: [
419 ...parentNode.children!.slice(0, index),
420 movedNode,
421 ...parentNode.children!.slice(index)
422 ]
423 }),
424 newMap
425 );
426 });
427 },
428 moveBefore(key: Key, keys: Iterable<Key>) {
429 setItems(prevState => {
430 let {items, nodeMap} = prevState;

Callers

nothing calls this directly

Calls 4

setItemsFunction · 0.85
updateTreeFunction · 0.85
addNodeFunction · 0.85
sliceMethod · 0.80

Tested by

no test coverage detected