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

Function getItemCount

packages/react-stately/src/collections/getItemCount.ts:18–39  ·  view source on GitHub ↗
(collection: Collection<Node<T>>)

Source from the content-addressed store, hash-verified

16const cache = new WeakMap<Iterable<unknown>, number>();
17
18export function getItemCount<T>(collection: Collection<Node<T>>): number {
19 let count = cache.get(collection);
20 if (count != null) {
21 return count;
22 }
23
24 // TS isn't smart enough to know we've ensured count is a number, so use a new variable
25 let counter = 0;
26 let countItems = (items: Iterable<Node<T>>) => {
27 for (let item of items) {
28 if (item.type === 'section') {
29 countItems(getChildNodes(item, collection));
30 } else if (item.type === 'item') {
31 counter++;
32 }
33 }
34 };
35
36 countItems(collection);
37 cache.set(collection, counter);
38 return counter;
39}

Callers 3

useComboBoxFunction · 0.90
useMenuItemFunction · 0.90
useOptionFunction · 0.90

Calls 2

countItemsFunction · 0.85
setMethod · 0.45

Tested by

no test coverage detected