MCPcopy Index your code
hub / github.com/adobe/react-spectrum / insert

Function insert

packages/react-stately/src/data/useTreeData.ts:283–312  ·  view source on GitHub ↗
(parentKey: Key | null, index: number, ...values: T[])

Source from the content-addressed store, hash-verified

281 return nodeMap.get(key);
282 },
283 insert(parentKey: Key | null, index: number, ...values: T[]) {
284 setItems(({items, nodeMap: originalMap}) => {
285 let {items: newNodes, nodeMap: newMap} = buildTree(values, new Map(originalMap), parentKey);
286
287 // If parentKey is null, insert into the root.
288 if (parentKey == null) {
289 return {
290 items: [...items.slice(0, index), ...newNodes, ...items.slice(index)],
291 nodeMap: newMap
292 };
293 }
294
295 // Otherwise, update the parent node and its ancestors.
296 return updateTree(
297 items,
298 parentKey,
299 parentNode => ({
300 key: parentNode.key,
301 parentKey: parentNode.parentKey,
302 value: parentNode.value,
303 children: [
304 ...parentNode.children!.slice(0, index),
305 ...newNodes,
306 ...parentNode.children!.slice(index)
307 ]
308 }),
309 newMap
310 );
311 });
312 },
313 insertBefore(key: Key, ...values: T[]): void {
314 let node = nodeMap.get(key);
315 if (!node) {

Callers

nothing calls this directly

Calls 3

buildTreeFunction · 0.85
updateTreeFunction · 0.85
sliceMethod · 0.80

Tested by

no test coverage detected