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AS,KAAM,gBAAgB,WAAW,UAAU,GAC1J,cAAc,WAAW,WAAW,CAAC;QAEvC,oHAAoH;QACpH,IAAI,eAAe,QAAS,CAAA,aAAa,GAAG,CAAC,gBAAgB,WAAW,OAAO,CAAC,cAAc,OAAO,UAAS,KAAM,gBAAgB,WAAW,UAAU,IACvJ,cAAc,WAAW,WAAW;IAExC;IAEA,OAAO;AACT","sources":["packages/react-stately/src/tabs/useTabListState.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, CollectionBase, CollectionStateBase, Key, Node, SingleSelection} from '@react-types/shared';\nimport {SingleSelectListState, useSingleSelectListState} from '../list/useSingleSelectListState';\nimport {useEffect, useRef} from 'react';\n\nexport interface TabListProps<T> extends CollectionBase<T>, Omit<SingleSelection, 'disallowEmptySelection' | 'selectedKey' | 'defaultSelectedKey' | 'onSelectionChange'> {\n  /**\n   * Whether the TabList is disabled.\n   * Shows that a selection exists, but is not available in that circumstance.\n   */\n  isDisabled?: boolean,\n  /** The currently selected key in the collection (controlled). */\n  selectedKey?: Key,\n  /** The initial selected keys in the collection (uncontrolled). */\n  defaultSelectedKey?: Key,\n  /** Handler that is called when the selection changes. */\n  onSelectionChange?: (key: Key) => void\n}\n\nexport interface TabListStateOptions<T> extends Omit<TabListProps<T>, 'children'>, CollectionStateBase<T> {}\n\nexport interface TabListState<T> extends SingleSelectListState<T> {\n  /** Whether the tab list is disabled. */\n  isDisabled: boolean\n}\n\n/**\n * Provides state management for a Tabs component. Tabs include a TabList which tracks\n * which tab is currently selected and displays the content associated with that Tab in a TabPanel.\n */\nexport function useTabListState<T extends object>(props: TabListStateOptions<T>): TabListState<T> {\n  let state = useSingleSelectListState<T>({\n    ...props,\n    onSelectionChange: props.onSelectionChange ? (key => {\n      if (key != null) {\n        props.onSelectionChange?.(key);\n      }\n    }) : undefined,\n    suppressTextValueWarning: true,\n    defaultSelectedKey: props.defaultSelectedKey ?? findDefaultSelectedKey(props.collection, props.disabledKeys ? new Set(props.disabledKeys) : new Set()) ?? undefined\n  });\n\n  let {\n    selectionManager,\n    collection,\n    selectedKey: currentSelectedKey\n  } = state;\n\n  let lastSelectedKey = useRef(currentSelectedKey);\n  useEffect(() => {\n    // Ensure a tab is always selected (in case no selected key was specified or if selected item was deleted from collection)\n    let selectedKey = currentSelectedKey;\n    if (props.selectedKey == null && (selectionManager.isEmpty || selectedKey == null || !collection.getItem(selectedKey))) {\n      selectedKey = findDefaultSelectedKey(collection, state.disabledKeys);\n      if (selectedKey != null) {\n        // directly set selection because replace/toggle selection won't consider disabled keys\n        selectionManager.setSelectedKeys([selectedKey]);\n      }\n    }\n\n    // If the tablist doesn't have focus and the selected key changes or if there isn't a focused key yet, change focused key to the selected key if it exists.\n    if (selectedKey != null && selectionManager.focusedKey == null || (!selectionManager.isFocused && selectedKey !== lastSelectedKey.current)) {\n      selectionManager.setFocusedKey(selectedKey);\n    }\n    lastSelectedKey.current = selectedKey;\n  });\n\n  return {\n    ...state,\n    isDisabled: props.isDisabled || false\n  };\n}\n\nfunction findDefaultSelectedKey<T>(collection: Collection<Node<T>> | undefined, disabledKeys: Set<Key>) {\n  let selectedKey: Key | null = null;\n  if (collection) {\n    selectedKey = collection.getFirstKey();\n    // loop over tabs until we find one that isn't disabled and select that\n    while (selectedKey != null && (disabledKeys.has(selectedKey) || collection.getItem(selectedKey)?.props?.isDisabled) && selectedKey !== collection.getLastKey()) {\n      selectedKey = collection.getKeyAfter(selectedKey);\n    }\n    // if this check is true, then every item is disabled, it makes more sense to default to the first key than the last\n    if (selectedKey != null && (disabledKeys.has(selectedKey) || collection.getItem(selectedKey)?.props?.isDisabled) && selectedKey === collection.getLastKey()) {\n      selectedKey = collection.getFirstKey();\n    }\n  }\n\n  return selectedKey;\n}\n"],"names":[],"version":3,"file":"useTabListState.mjs.map"}