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E;IACjE,MAAM,mBAAmB,CAAA,GAAA,mBAAK,EAA8B;IAC5D,CAAA,GAAA,sBAAQ,EAAE;QACR,IAAI,iBAAiB,UAAU,IAAI,QAAQ,CAAC,WAAW,OAAO,CAAC,iBAAiB,UAAU,KAAK,iBAAiB,OAAO,EAAE;YACvH,mGAAmG;YACnG,IAAI,MAAM,iBAAiB,OAAO,CAAC,WAAW,CAAC,iBAAiB,UAAU;YAC1E,IAAI,iBAA6B;YACjC,MAAO,OAAO,KAAM;gBAClB,IAAI,OAAO,WAAW,OAAO,CAAC;gBAC9B,IAAI,QAAQ,KAAK,IAAI,KAAK,UAAU,CAAC,iBAAiB,UAAU,CAAC,MAAM;oBACrE,iBAAiB;oBACjB;gBACF;gBAEA,MAAM,iBAAiB,OAAO,CAAC,WAAW,CAAC;YAC7C;YAEA,wCAAwC;YACxC,IAAI,kBAAkB,MAAM;gBAC1B,MAAM,iBAAiB,OAAO,CAAC,YAAY,CAAC,iBAAiB,UAAU;gBACvE,MAAO,OAAO,KAAM;oBAClB,IAAI,OAAO,WAAW,OAAO,CAAC;oBAC9B,IAAI,QAAQ,KAAK,IAAI,KAAK,UAAU,CAAC,iBAAiB,UAAU,CAAC,MAAM;wBACrE,iBAAiB;wBACjB;oBACF;oBAEA,MAAM,iBAAiB,OAAO,CAAC,YAAY,CAAC;gBAC9C;YACF;YAEA,iBAAiB,aAAa,CAAC;QACjC;QACA,iBAAiB,OAAO,GAAG;IAC7B,GAAG;QAAC;QAAY;KAAiB;AACnC","sources":["packages/react-stately/src/list/useListState.ts"],"sourcesContent":["/*\n * Copyright 2020 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 {Collection, CollectionStateBase, Key, LayoutDelegate, Node} from '@react-types/shared';\nimport {ListCollection} from './ListCollection';\nimport {MultipleSelectionStateProps, useMultipleSelectionState} from '../selection/useMultipleSelectionState';\nimport {SelectionManager} from '../selection/SelectionManager';\nimport {useCallback, useEffect, useMemo, useRef} from 'react';\nimport {useCollection} from '../collections/useCollection';\n\nexport interface ListProps<T> extends CollectionStateBase<T>, MultipleSelectionStateProps {\n  /** Filter function to generate a filtered list of nodes. */\n  filter?: (nodes: Iterable<Node<T>>) => Iterable<Node<T>>,\n  /** @private */\n  suppressTextValueWarning?: boolean,\n  /**\n   * A delegate object that provides layout information for items in the collection.\n   * This can be used to override the behavior of shift selection.\n   */\n  layoutDelegate?: LayoutDelegate\n}\n\nexport interface ListState<T> {\n  /** A collection of items in the list. */\n  collection: Collection<Node<T>>,\n\n  /** A set of items that are disabled. */\n  disabledKeys: Set<Key>,\n\n  /** A selection manager to read and update multiple selection state. */\n  selectionManager: SelectionManager\n}\n\n/**\n * Provides state management for list-like components. Handles building a collection\n * of items from props, and manages multiple selection state.\n */\nexport function useListState<T extends object>(props: ListProps<T>): ListState<T>  {\n  let {filter, layoutDelegate} = props;\n\n  let selectionState = useMultipleSelectionState(props);\n  let disabledKeys = useMemo(() =>\n    props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()\n  , [props.disabledKeys]);\n\n  let factory = useCallback(nodes => filter ? new ListCollection(filter(nodes)) : new ListCollection(nodes as Iterable<Node<T>>), [filter]);\n  let context = useMemo(() => ({suppressTextValueWarning: props.suppressTextValueWarning}), [props.suppressTextValueWarning]);\n\n  let collection = useCollection(props, factory, context);\n\n  let selectionManager = useMemo(() =>\n    new SelectionManager(collection, selectionState, {layoutDelegate})\n    , [collection, selectionState, layoutDelegate]\n  );\n\n  useFocusedKeyReset(collection, selectionManager);\n\n  return {\n    collection,\n    disabledKeys,\n    selectionManager\n  };\n}\n\n/**\n * Filters a collection using the provided filter function and returns a new ListState.\n */\nexport function UNSTABLE_useFilteredListState<T extends object>(state: ListState<T>, filterFn: ((nodeValue: string, node: Node<T>) => boolean) | null | undefined): ListState<T> {\n  let collection = useMemo(() => filterFn ? state.collection.filter!(filterFn) : state.collection, [state.collection, filterFn]);\n  let selectionManager = state.selectionManager.withCollection(collection);\n  useFocusedKeyReset(collection, selectionManager);\n  return {\n    collection,\n    selectionManager,\n    disabledKeys: state.disabledKeys\n  };\n}\n\nfunction useFocusedKeyReset<T>(collection: Collection<Node<T>>, selectionManager: SelectionManager) {\n  // Reset focused key if that item is deleted from the collection.\n  const cachedCollection = useRef<Collection<Node<T>> | null>(null);\n  useEffect(() => {\n    if (selectionManager.focusedKey != null && !collection.getItem(selectionManager.focusedKey) && cachedCollection.current) {\n      // Walk forward in the old collection to find the next key that still exists in the new collection.\n      let key = cachedCollection.current.getKeyAfter(selectionManager.focusedKey);\n      let nextFocusedKey: Key | null = null;\n      while (key != null) {\n        let node = collection.getItem(key);\n        if (node && node.type === 'item' && !selectionManager.isDisabled(key)) {\n          nextFocusedKey = key;\n          break;\n        }\n\n        key = cachedCollection.current.getKeyAfter(key);\n      }\n\n      // If no such key exists, walk backward.\n      if (nextFocusedKey == null) {\n        key = cachedCollection.current.getKeyBefore(selectionManager.focusedKey);\n        while (key != null) {\n          let node = collection.getItem(key);\n          if (node && node.type === 'item' && !selectionManager.isDisabled(key)) {\n            nextFocusedKey = key;\n            break;\n          }\n\n          key = cachedCollection.current.getKeyBefore(key);\n        }\n      }\n\n      selectionManager.setFocusedKey(nextFocusedKey);\n    }\n    cachedCollection.current = collection;\n  }, [collection, selectionManager]);\n}\n"],"names":[],"version":3,"file":"useListState.cjs.map"}