{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AAsBM,SAAS,0CAAoC,KAA+B;IACjF,IAAI,YAAC,QAAQ,SAAE,KAAK,WAAE,OAAO,iBAAE,aAAa,gBAAE,eAAe,EAAE,EAAC,GAAG;IAEnE,0DAA0D;IAC1D,uDAAuD;IACvD,IAAI,QAAQ,CAAA,GAAA,oBAAM,EAAE,IAAM,IAAI,WAAW;IACzC,OAAO,CAAA,GAAA,oBAAM,EAAE;QACb,IAAI,SAAS,OAAO,aAAa,YAAY;YAC3C,IAAI,MAAsB,EAAE;YAC5B,KAAK,IAAI,QAAQ,MAAO;gBACtB,IAAI,WAAW,MAAM,GAAG,CAAC;gBACzB,IAAI,CAAC,UAAU;oBACb,WAAW,SAAS;oBACpB,aAAa;oBACb,IAAI,MAAM,SAAS,KAAK,CAAC,EAAE,IAAI,KAAK,GAAG,IAAI,KAAK,EAAE;oBAElD,IAAI,OAAO,MACT,MAAM,IAAI,MAAM;oBAGlB,IAAI,WAAW,QAAQ,SAAS,KAAK,CAAC,EAAE,IAAI,MAC1C,MAAM,UAAU,MAAM;oBAExB,wDAAwD;oBACxD,WAAW,CAAA,GAAA,yBAAW,EACpB,UACA,gBAAgB;6BAAC;wBAAK,IAAI;wBAAK,OAAO;oBAAI,IAAI;6BAAC;oBAAG;oBAEpD,MAAM,GAAG,CAAC,MAAM;gBAClB;gBACA,IAAI,IAAI,CAAC;YACX;YACA,OAAO;QACT,OAAO,IAAI,OAAO,aAAa,YAC7B,OAAO;IAEX,GAAG;QAAC;QAAU;QAAO;QAAO;QAAS;KAAc;AACrD","sources":["packages/react-aria/src/collections/useCachedChildren.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {cloneElement, ReactElement, ReactNode, useMemo} from 'react';\nimport {Key} from '@react-types/shared';\n\nexport interface CachedChildrenOptions<T> {\n  /** Item objects in the collection. */\n  items?: Iterable<T>,\n  /** The contents of the collection. */\n  children?: ReactNode | ((item: T) => ReactNode),\n  /** Values that should invalidate the item cache when using dynamic collections. */\n  dependencies?: ReadonlyArray<any>,\n  /** A scope to prepend to all child item ids to ensure they are unique. */\n  idScope?: Key,\n  /** Whether to add `id` and `value` props to all child items. */\n  addIdAndValue?: boolean\n}\n\n/**\n * Maps over a list of items and renders React elements for them. Each rendered item is\n * cached based on object identity, and React keys are generated from the `key` or `id` property.\n */\nexport function useCachedChildren<T extends object>(props: CachedChildrenOptions<T>): ReactNode {\n  let {children, items, idScope, addIdAndValue, dependencies = []} = props;\n\n  // Invalidate the cache whenever the parent value changes.\n  // eslint-disable-next-line react-hooks/exhaustive-deps\n  let cache = useMemo(() => new WeakMap(), dependencies);\n  return useMemo(() => {\n    if (items && typeof children === 'function') {\n      let res: ReactElement[] = [];\n      for (let item of items) {\n        let rendered = cache.get(item);\n        if (!rendered) {\n          rendered = children(item);\n          // @ts-ignore\n          let key = rendered.props.id ?? item.key ?? item.id;\n\n          if (key == null) {\n            throw new Error('Could not determine key for item');\n          }\n\n          if (idScope != null && rendered.props.id == null) {\n            key = idScope + ':' + key;\n          }\n          // Note: only works if wrapped Item passes through id...\n          rendered = cloneElement(\n            rendered,\n            addIdAndValue ? {key, id: key, value: item} : {key}\n          );\n          cache.set(item, rendered);\n        }\n        res.push(rendered);\n      }\n      return res;\n    } else if (typeof children !== 'function') {\n      return children;\n    }\n  }, [children, items, cache, idScope, addIdAndValue]);\n}\n"],"names":[],"version":3,"file":"useCachedChildren.cjs.map"}