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AACF","sources":["packages/react-stately/src/steplist/useStepListState.ts"],"sourcesContent":["/*\n * Copyright 2023 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, Key, Node, SingleSelection} from '@react-types/shared';\nimport {SingleSelectListState, useSingleSelectListState} from '../list/useSingleSelectListState';\nimport {useCallback, useEffect, useMemo} from 'react';\nimport {useControlledState} from '../utils/useControlledState';\n\nexport interface StepListProps<T>\n  extends CollectionBase<T>, Omit<SingleSelection, 'onSelectionChange'> {\n  /** The key of the last completed step (controlled). */\n  lastCompletedStep?: Key;\n  /** The key of the initially last completed step (uncontrolled). */\n  defaultLastCompletedStep?: Key;\n  /** Callback for when the last completed step changes. */\n  onLastCompletedStepChange?: (key: Key | null) => void;\n  /** Whether the step list is disabled. Steps will not be focusable or interactive. */\n  isDisabled?: boolean;\n  /** Whether the step list is read only. Steps will be focusable but non-interactive. */\n  isReadOnly?: boolean;\n  /** Handler that is called when the selection changes. */\n  onSelectionChange?: (key: Key) => void;\n}\n\nexport interface StepListState<T> extends SingleSelectListState<T> {\n  readonly lastCompletedStep?: Key;\n  setLastCompletedStep(key: Key): void;\n  isCompleted(key: Key): boolean;\n  isSelectable(key: Key): boolean;\n}\n\nexport function useStepListState<T extends object>(props: StepListProps<T>): StepListState<T> {\n  let state = useSingleSelectListState<T>({\n    ...props,\n    onSelectionChange: props.onSelectionChange\n      ? key => {\n          if (key != null) {\n            props.onSelectionChange?.(key);\n          }\n        }\n      : undefined\n  });\n\n  let [lastCompletedStep, setLastCompletedStep] = useControlledState<Key | null>(\n    props.lastCompletedStep,\n    props.defaultLastCompletedStep ?? null,\n    props.onLastCompletedStepChange\n  );\n  const {setSelectedKey: realSetSelectedKey, selectedKey, collection} = state;\n  const {indexMap, keysLinkedList} = useMemo(() => buildKeysMaps(collection), [collection]);\n  const selectedIdx = selectedKey != null ? indexMap.get(selectedKey) : 0;\n\n  const isCompleted = useCallback(\n    (step: Key | null | undefined) => {\n      if (step === undefined) {\n        return false;\n      }\n\n      return (\n        step !== null &&\n        lastCompletedStep !== null &&\n        indexMap.has(step) &&\n        indexMap.has(lastCompletedStep) &&\n        indexMap.get(step)! <= indexMap.get(lastCompletedStep)!\n      );\n    },\n    [indexMap, lastCompletedStep]\n  );\n\n  const findDefaultSelectedKey = useCallback(\n    (collection: Collection<Node<T>> | null, disabledKeys: Set<Key>) => {\n      let selectedKey: Key | null = null;\n      if (collection && collection.size > 0) {\n        selectedKey = collection.getFirstKey();\n        // loop over keys until we find one that isn't completed or disabled and select that\n        while (\n          selectedKey !== collection.getLastKey() &&\n          selectedKey &&\n          (disabledKeys.has(selectedKey) || isCompleted(selectedKey))\n        ) {\n          selectedKey = collection.getKeyAfter(selectedKey);\n        }\n      }\n\n      return selectedKey;\n    },\n    [isCompleted]\n  );\n\n  useEffect(() => {\n    // Ensure a step is always selected (in case no selected key was specified or if selected item was deleted from collection)\n    let selectedKey: Key | null = state.selectedKey;\n    if (\n      state.selectionManager.isEmpty ||\n      selectedKey == null ||\n      !state.collection.getItem(selectedKey)\n    ) {\n      selectedKey = findDefaultSelectedKey(state.collection, state.disabledKeys);\n      if (selectedKey !== null) {\n        state.selectionManager.replaceSelection(selectedKey);\n      }\n    }\n\n    if (state.selectionManager.focusedKey == null) {\n      state.selectionManager.setFocusedKey(selectedKey);\n    }\n\n    let lcs = (lastCompletedStep !== null ? indexMap.get(lastCompletedStep) : -1) ?? -1;\n    if (\n      selectedIdx !== undefined &&\n      selectedIdx > 0 &&\n      selectedIdx > lcs + 1 &&\n      selectedKey !== null &&\n      keysLinkedList.has(selectedKey)\n    ) {\n      setLastCompletedStep(keysLinkedList.get(selectedKey)!);\n    }\n  });\n\n  function isSelectable(step: Key) {\n    if (props.isDisabled || state.disabledKeys.has(step) || props.isReadOnly) {\n      return false;\n    }\n\n    if (isCompleted(step)) {\n      return true;\n    }\n\n    const prevStep = keysLinkedList.get(step);\n    return isCompleted(prevStep) || step === state.collection.getFirstKey();\n  }\n\n  function setSelectedKey(key: Key) {\n    const prevKey = keysLinkedList.get(key);\n    if (prevKey && !isCompleted(prevKey)) {\n      setLastCompletedStep(prevKey);\n    }\n    realSetSelectedKey(key);\n  }\n\n  return {\n    ...state,\n    setSelectedKey,\n    setLastCompletedStep,\n    isCompleted,\n    isSelectable\n  };\n}\n\nfunction buildKeysMaps<T>(coll: Collection<Node<T>>) {\n  const indexMap = new Map<Key, number>();\n  const keysLinkedList = new Map<Key, Key | undefined>();\n  let i = 0;\n  let prev: Node<T> | undefined = undefined;\n  for (const item of coll) {\n    indexMap.set(item.key, i);\n    keysLinkedList.set(item.key, prev?.key);\n    prev = item;\n    i++;\n  }\n  return {\n    indexMap,\n    keysLinkedList\n  };\n}\n"],"names":[],"version":3,"file":"useStepListState.cjs.map"}