{"ast":null,"code":"import _toConsumableArray from \"@babel/runtime/helpers/toConsumableArray\";\nimport _slicedToArray from \"@babel/runtime/helpers/slicedToArray\";\nimport { useMenuTriggerState } from \"@react-stately/menu\";\nimport { useEffect, useMemo, useRef, useState } from \"react\";\nimport { useControlledState } from \"@react-stately/utils\";\nimport { ListCollection, useSingleSelectListState } from \"@react-stately/list\";\nimport _babelRuntimeHelpersEsmExtends from \"@babel/runtime/helpers/esm/extends\";\nexport function useComboBoxState(props) {\n  var _props$defaultInputVa, _props$items, _ref, _props$selectedKey;\n  var defaultFilter = props.defaultFilter,\n    _props$menuTrigger = props.menuTrigger,\n    menuTrigger = _props$menuTrigger === void 0 ? 'input' : _props$menuTrigger,\n    _props$allowsEmptyCol = props.allowsEmptyCollection,\n    allowsEmptyCollection = _props$allowsEmptyCol === void 0 ? false : _props$allowsEmptyCol,\n    allowsCustomValue = props.allowsCustomValue,\n    _props$shouldCloseOnB = props.shouldCloseOnBlur,\n    shouldCloseOnBlur = _props$shouldCloseOnB === void 0 ? true : _props$shouldCloseOnB;\n  var _useState = useState(false),\n    _useState2 = _slicedToArray(_useState, 2),\n    isFocused = _useState2[0],\n    setFocusedState = _useState2[1];\n  var _useControlledState = useControlledState(props.inputValue, (_props$defaultInputVa = props.defaultInputValue) != null ? _props$defaultInputVa : '', props.onInputChange),\n    _useControlledState2 = _slicedToArray(_useControlledState, 2),\n    inputValue = _useControlledState2[0],\n    setInputValue = _useControlledState2[1];\n  var onSelectionChange = function onSelectionChange(key) {\n    if (props.onSelectionChange) {\n      props.onSelectionChange(key);\n    }\n    if (props.isOpen === undefined || props.selectedKey === undefined) {\n      if (key === selectedKey) {\n        resetInputValue();\n        triggerState.close();\n      }\n    }\n  };\n  var _useSingleSelectListS = useSingleSelectListState(_babelRuntimeHelpersEsmExtends({}, props, {\n      onSelectionChange: onSelectionChange,\n      items: (_props$items = props.items) != null ? _props$items : props.defaultItems\n    })),\n    collection = _useSingleSelectListS.collection,\n    selectionManager = _useSingleSelectListS.selectionManager,\n    selectedKey = _useSingleSelectListS.selectedKey,\n    setSelectedKey = _useSingleSelectListS.setSelectedKey,\n    selectedItem = _useSingleSelectListS.selectedItem,\n    disabledKeys = _useSingleSelectListS.disabledKeys;\n  var filteredCollection = useMemo(function () {\n    return props.items != null || !defaultFilter ? collection : $b1b6afed698df4428b7ed319af4b90$var$filterCollection(collection, inputValue, defaultFilter);\n  }, [collection, inputValue, defaultFilter, props.items]);\n  var triggerState = useMenuTriggerState(props);\n  var open = function open(focusStrategy) {\n    if (allowsEmptyCollection || filteredCollection.size > 0) {\n      triggerState.open(focusStrategy);\n    }\n  };\n  var toggle = function toggle(focusStrategy) {\n    if (!(allowsEmptyCollection || filteredCollection.size > 0) && !triggerState.isOpen) {\n      return;\n    }\n    triggerState.toggle(focusStrategy);\n  };\n  var lastValue = useRef(inputValue);\n  var resetInputValue = function resetInputValue() {\n    var _collection$getItem$t, _collection$getItem;\n    var itemText = (_collection$getItem$t = (_collection$getItem = collection.getItem(selectedKey)) == null ? void 0 : _collection$getItem.textValue) != null ? _collection$getItem$t : '';\n    lastValue.current = itemText;\n    setInputValue(itemText);\n  };\n  var isInitialRender = useRef(true);\n  var lastSelectedKey = useRef((_ref = (_props$selectedKey = props.selectedKey) != null ? _props$selectedKey : props.defaultSelectedKey) != null ? _ref : null);\n  useEffect(function () {\n    if (isFocused && filteredCollection.size > 0 && !triggerState.isOpen && inputValue !== lastValue.current && menuTrigger !== 'manual' && (props.isOpen === undefined || props.inputValue === undefined)) {\n      open();\n    }\n    if (!allowsEmptyCollection && triggerState.isOpen && filteredCollection.size === 0 && (props.isOpen === undefined || props.items === undefined)) {\n      triggerState.close();\n    }\n    if (selectedKey != null && selectedKey !== lastSelectedKey.current && (props.isOpen === undefined || props.selectedKey === undefined)) {\n      triggerState.close();\n    }\n    if (inputValue !== lastValue.current) {\n      selectionManager.setFocusedKey(null);\n      if (inputValue === '' && (props.inputValue === undefined || props.selectedKey === undefined)) {\n        setSelectedKey(null);\n      }\n    }\n    if (isInitialRender.current && props.inputValue === undefined && props.defaultInputValue === undefined) {\n      resetInputValue();\n    }\n    if (selectedKey !== lastSelectedKey.current && (props.inputValue === undefined || props.selectedKey === undefined)) {\n      resetInputValue();\n    } else {\n      lastValue.current = inputValue;\n    }\n    isInitialRender.current = false;\n    lastSelectedKey.current = selectedKey;\n  });\n  useEffect(function () {\n    if (!triggerState.isOpen) {\n      selectionManager.setFocusedKey(null);\n    }\n  }, [triggerState.isOpen, selectionManager]);\n  var commitCustomValue = function commitCustomValue() {\n    var shouldClose = false;\n    lastSelectedKey.current = null;\n    setSelectedKey(null);\n    if (selectedKey === null && props.onSelectionChange) {\n      props.onSelectionChange(null);\n    }\n    shouldClose = props.isOpen == null || props.selectedKey === undefined;\n    if (shouldClose) {\n      triggerState.close();\n    }\n  };\n  var commit = function commit() {\n    if (triggerState.isOpen && selectionManager.focusedKey != null) {\n      if (selectedKey === selectionManager.focusedKey) {\n        resetInputValue();\n        triggerState.close();\n      } else {\n        setSelectedKey(selectionManager.focusedKey);\n      }\n    } else if (allowsCustomValue) {\n      commitCustomValue();\n    }\n  };\n  var setFocused = function setFocused(isFocused) {\n    if (isFocused) {\n      if (menuTrigger === 'focus') {\n        open();\n      }\n    } else if (shouldCloseOnBlur) {\n      var _collection$getItem$t2, _collection$getItem2;\n      var itemText = (_collection$getItem$t2 = (_collection$getItem2 = collection.getItem(selectedKey)) == null ? void 0 : _collection$getItem2.textValue) != null ? _collection$getItem$t2 : '';\n      if (allowsCustomValue && inputValue !== itemText) {\n        commitCustomValue();\n      } else {\n        resetInputValue();\n        triggerState.close();\n      }\n    }\n    setFocusedState(isFocused);\n  };\n  return _babelRuntimeHelpersEsmExtends({}, triggerState, {\n    toggle: toggle,\n    open: open,\n    selectionManager: selectionManager,\n    selectedKey: selectedKey,\n    setSelectedKey: setSelectedKey,\n    disabledKeys: disabledKeys,\n    isFocused: isFocused,\n    setFocused: setFocused,\n    selectedItem: selectedItem,\n    collection: filteredCollection,\n    inputValue: inputValue,\n    setInputValue: setInputValue,\n    commit: commit\n  });\n}\nfunction $b1b6afed698df4428b7ed319af4b90$var$filterCollection(collection, inputValue, filter) {\n  return new ListCollection($b1b6afed698df4428b7ed319af4b90$var$filterNodes(collection, inputValue, filter));\n}\nfunction $b1b6afed698df4428b7ed319af4b90$var$filterNodes(nodes, inputValue, filter) {\n  var filteredNode = [];\n  for (var node of nodes) {\n    if (node.type === 'section' && node.hasChildNodes) {\n      var filtered = $b1b6afed698df4428b7ed319af4b90$var$filterNodes(node.childNodes, inputValue, filter);\n      if (_toConsumableArray(filtered).length > 0) {\n        filteredNode.push(_babelRuntimeHelpersEsmExtends({}, node, {\n          childNodes: filtered\n        }));\n      }\n    } else if (node.type !== 'section' && filter(node.textValue, inputValue)) {\n      filteredNode.push(node);\n    }\n  }\n  return filteredNode;\n}","map":{"version":3,"sources":["packages/@react-stately/combobox/src/useComboBoxState.ts"],"names":["useComboBoxState","props","defaultFilter","menuTrigger","allowsEmptyCollection","allowsCustomValue","shouldCloseOnBlur","isFocused","setFocusedState","useState","inputValue","setInputValue","useControlledState","defaultInputValue","onInputChange","onSelectionChange","key","isOpen","undefined","selectedKey","resetInputValue","triggerState","close","collection","selectionManager","setSelectedKey","selectedItem","disabledKeys","useSingleSelectListState","items","defaultItems","filteredCollection","useMemo","filterCollection","useMenuTriggerState","open","focusStrategy","size","toggle","lastValue","useRef","itemText","getItem","textValue","current","isInitialRender","lastSelectedKey","defaultSelectedKey","useEffect","setFocusedKey","commitCustomValue","shouldClose","commit","focusedKey","setFocused","filter","ListCollection","filterNodes","nodes","filteredNode","node","type","hasChildNodes","filtered","childNodes","length","push"],"mappings":";;;;;;;OA4CO,SAASA,gBAAT,CAA4CC,KAA5C,EAA4F;EAAA,IAAA,qBAAA,EAAA,YAAA,EAAA,IAAA,EAAA,kBAAA;EACjG,IACEC,aADE,GAMAD,KANJ,CACEC,aADE;IAAA,kBAAA,GAMAD,KANJ,CAEEE,WAAW;IAA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ACI,UAAN,EAAkBtD,UAAlB,EAA8B6C,MAA9B,CAA1B;MACA,IAAI,kBAAA,CAAIQ,QAAJ,EAAcE,MAAd,GAAuB,CAA3B,EAA8B;QAC5BN,YAAY,CAACO,IAAbP,CAAAA,8BAAAA,CAAAA,CAAAA,CAAAA,EAAsBC,IAAtBD,EAAAA;UAA4BK,UAAU,EAAED;QAAxCJ,CAAAA,CAAAA,CAAAA;MACD;IACF,CALD,MAKO,IAAIC,IAAI,CAACC,IAALD,KAAc,SAAdA,IAA2BL,MAAM,CAACK,IAAI,CAACjB,SAAN,EAAiBjC,UAAjB,CAArC,EAAmE;MACxEiD,YAAY,CAACO,IAAbP,CAAkBC,IAAlBD,CAAAA;IACD;EACF;EACD,OAAOA,YAAP;AACD","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, FocusStrategy, Node} from '@react-types/shared';\nimport {ComboBoxProps} from '@react-types/combobox';\nimport {ListCollection, useSingleSelectListState} from '@react-stately/list';\nimport {SelectState} from '@react-stately/select';\nimport {useControlledState} from '@react-stately/utils';\nimport {useEffect, useMemo, useRef, useState} from 'react';\nimport {useMenuTriggerState} from '@react-stately/menu';\n\nexport interface ComboBoxState<T> extends SelectState<T> {\n  /** The current value of the combo box input. */\n  inputValue: string,\n  /** Sets the value of the combo box input. */\n  setInputValue(value: string): void,\n  /** Selects the currently focused item and updates the input value. */\n  commit(): void\n}\n\ntype FilterFn = (textValue: string, inputValue: string) => boolean;\ninterface ComboBoxStateProps<T> extends ComboBoxProps<T> {\n  /** The filter function used to determine if a option should be included in the combo box list. */\n  defaultFilter?: FilterFn,\n  /** Whether the combo box allows the menu to be open when the collection is empty. */\n  allowsEmptyCollection?: boolean,\n  /** Whether the combo box menu should close on blur. */\n  shouldCloseOnBlur?: boolean\n}\n\n/**\n * Provides state management for a combo box component. Handles building a collection\n * of items from props and manages the option selection state of the combo box. In addition, it tracks the input value,\n * focus state, and other properties of the combo box.\n */\nexport function useComboBoxState<T extends object>(props: ComboBoxStateProps<T>): ComboBoxState<T> {\n  let {\n    defaultFilter,\n    menuTrigger = 'input',\n    allowsEmptyCollection = false,\n    allowsCustomValue,\n    shouldCloseOnBlur = true\n  } = props;\n\n  let [isFocused, setFocusedState] = useState(false);\n  let [inputValue, setInputValue] = useControlledState(\n    props.inputValue,\n    props.defaultInputValue ?? '',\n    props.onInputChange\n  );\n\n  let onSelectionChange = (key) => {\n    if (props.onSelectionChange) {\n      props.onSelectionChange(key);\n    }\n\n    // If open state or selectedKey is uncontrolled and key is the same, reset the inputValue and close the menu\n    // (scenario: user clicks on already selected option)\n    if (props.isOpen === undefined || props.selectedKey === undefined) {\n      if (key === selectedKey) {\n        resetInputValue();\n        triggerState.close();\n      }\n    }\n  };\n\n  let {collection, selectionManager, selectedKey, setSelectedKey, selectedItem, disabledKeys} = useSingleSelectListState({\n    ...props,\n    onSelectionChange,\n    items: props.items ?? props.defaultItems\n  });\n\n  let filteredCollection = useMemo(() => (\n    // No default filter if items are controlled.\n    props.items != null || !defaultFilter\n      ? collection\n      : filterCollection(collection, inputValue, defaultFilter)\n  ), [collection, inputValue, defaultFilter, props.items]);\n\n  let triggerState = useMenuTriggerState(props);\n  let open = (focusStrategy?: FocusStrategy) => {\n    // Prevent open operations from triggering if there is nothing to display\n    if (allowsEmptyCollection || filteredCollection.size > 0) {\n      triggerState.open(focusStrategy);\n    }\n  };\n\n  let toggle = (focusStrategy?: FocusStrategy) => {\n    // If the menu is closed and there is nothing to display, early return so toggle isn't called to prevent extraneous onOpenChange\n    if (!(allowsEmptyCollection || filteredCollection.size > 0) && !triggerState.isOpen) {\n      return;\n    }\n\n    triggerState.toggle(focusStrategy);\n  };\n\n  let lastValue = useRef(inputValue);\n  let resetInputValue = () => {\n    let itemText = collection.getItem(selectedKey)?.textValue ?? '';\n    lastValue.current = itemText;\n    setInputValue(itemText);\n  };\n\n  let isInitialRender = useRef(true);\n  let lastSelectedKey = useRef(props.selectedKey ?? props.defaultSelectedKey ?? null);\n  useEffect(() => {\n    // If open state or inputValue is uncontrolled, open and close automatically when the input value changes,\n    // the input is if focused, and there are items in the collection.\n    if (\n      isFocused &&\n      filteredCollection.size > 0 &&\n      !triggerState.isOpen &&\n      inputValue !== lastValue.current &&\n      menuTrigger !== 'manual' &&\n      (props.isOpen === undefined || props.inputValue === undefined)\n    ) {\n      open();\n    }\n\n    // Close the menu if the collection is empty and either open state or items are uncontrolled.\n    if (\n      !allowsEmptyCollection &&\n      triggerState.isOpen &&\n      filteredCollection.size === 0 &&\n      (props.isOpen === undefined || props.items === undefined)\n    ) {\n      triggerState.close();\n    }\n\n    // Close when an item is selected, if open state or selectedKey is uncontrolled.\n    if (\n      selectedKey != null &&\n      selectedKey !== lastSelectedKey.current &&\n      (props.isOpen === undefined || props.selectedKey === undefined)\n    ) {\n      triggerState.close();\n    }\n\n    // Clear focused key when input value changes.\n    if (inputValue !== lastValue.current) {\n      selectionManager.setFocusedKey(null);\n\n      // Set selectedKey to null when the user clears the input.\n      // If controlled, this is the application developer's responsibility.\n      if (inputValue === '' && (props.inputValue === undefined || props.selectedKey === undefined)) {\n        setSelectedKey(null);\n      }\n    }\n\n    // If it is the intial render and inputValue isn't controlled nor has an intial value, set input to match current selected key if any\n    if (isInitialRender.current && (props.inputValue === undefined && props.defaultInputValue === undefined)) {\n      resetInputValue();\n    }\n\n    // If the selectedKey changed, update the input value.\n    // Do nothing if both inputValue and selectedKey are controlled.\n    // In this case, it's the user's responsibility to update inputValue in onSelectionChange. In addition, we preserve the defaultInputValue\n    // on initial render, even if it doesn't match the selected item's text.\n    if (\n      selectedKey !== lastSelectedKey.current &&\n      (props.inputValue === undefined || props.selectedKey === undefined)\n    ) {\n      resetInputValue();\n    } else {\n      lastValue.current = inputValue;\n    }\n\n    isInitialRender.current = false;\n    lastSelectedKey.current = selectedKey;\n  });\n\n  useEffect(() => {\n    // Reset focused key when the menu closes\n    if (!triggerState.isOpen) {\n      selectionManager.setFocusedKey(null);\n    }\n  }, [triggerState.isOpen, selectionManager]);\n\n  let commitCustomValue = () => {\n    let shouldClose = false;\n    lastSelectedKey.current = null;\n    setSelectedKey(null);\n\n    // If previous key was already null, need to manually call onSelectionChange since it won't be triggered by a setSelectedKey call\n    // This allows the application to control whether or not to close the menu on custom value commit\n    if (selectedKey === null && props.onSelectionChange) {\n      props.onSelectionChange(null);\n    }\n\n    // Should close menu ourselves if component open state or selected key is uncontrolled and therefore won't be closed by a user defined event handler\n    shouldClose = props.isOpen == null || props.selectedKey === undefined;\n\n\n    // Close if no other event will be fired. Otherwise, allow the\n    // application to control this based on that event.\n    if (shouldClose) {\n      triggerState.close();\n    }\n  };\n\n  let commit = () => {\n    if (triggerState.isOpen && selectionManager.focusedKey != null) {\n      // Reset inputValue and close menu here if the selected key is already the focused key. Otherwise\n      // fire onSelectionChange to allow the application to control the closing.\n      if (selectedKey === selectionManager.focusedKey) {\n        resetInputValue();\n        triggerState.close();\n      } else {\n        setSelectedKey(selectionManager.focusedKey);\n      }\n    } else if (allowsCustomValue) {\n      commitCustomValue();\n    }\n  };\n\n  let setFocused = (isFocused: boolean) => {\n    if (isFocused) {\n      if (menuTrigger === 'focus') {\n        open();\n      }\n    } else if (shouldCloseOnBlur) {\n      let itemText = collection.getItem(selectedKey)?.textValue ?? '';\n      if (allowsCustomValue && inputValue !== itemText) {\n        commitCustomValue();\n      } else {\n        resetInputValue();\n        // Close menu if blurring away from the combobox\n        // Specifically handles case where user clicks away from the field\n        triggerState.close();\n      }\n    }\n\n    setFocusedState(isFocused);\n  };\n\n  return {\n    ...triggerState,\n    toggle,\n    open,\n    selectionManager,\n    selectedKey,\n    setSelectedKey,\n    disabledKeys,\n    isFocused,\n    setFocused,\n    selectedItem,\n    collection: filteredCollection,\n    inputValue,\n    setInputValue,\n    commit\n  };\n}\n\nfunction filterCollection<T extends object>(collection: Collection<Node<T>>, inputValue: string, filter: FilterFn): Collection<Node<T>> {\n  return new ListCollection(filterNodes(collection, inputValue, filter));\n}\n\nfunction filterNodes<T>(nodes: Iterable<Node<T>>, inputValue: string, filter: FilterFn): Iterable<Node<T>> {\n  let filteredNode = [];\n  for (let node of nodes) {\n    if (node.type === 'section' && node.hasChildNodes) {\n      let filtered = filterNodes(node.childNodes, inputValue, filter);\n      if ([...filtered].length > 0) {\n        filteredNode.push({...node, childNodes: filtered});\n      }\n    } else if (node.type !== 'section' && filter(node.textValue, inputValue)) {\n      filteredNode.push(node);\n    }\n  }\n  return filteredNode;\n}\n"]},"metadata":{},"sourceType":"module"}