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

Function insert

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

buildTreeFunction · 0.85
updateTreeFunction · 0.85
sliceMethod · 0.80

Tested by

no test coverage detected