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AU,UAAU,CAAC,IAAI,KAAK,KAAK,MAAM,qBAAqB,EAAE;gBAClE,iBAAiB;gBACjB,aAAa,IAAI;YACnB;QACF;QACA,QAAO,gBAAsC,IAAI;YAC/C,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,KAAK,MAAM,qBAAqB,EAAE;gBAClE,iBAAiB;gBACjB,aAAa,MAAM;YACrB;QACF;mBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAa,KAAqC;IACzD,IAAI,UAAU,WACZ,OAAO;IAET,IAAI,UAAU,MACZ,OAAO,EAAE;IAEX,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C","sources":["packages/react-stately/src/select/useSelectState.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 {\n  CollectionBase,\n  CollectionStateBase,\n  FocusableProps,\n  FocusStrategy,\n  HelpTextProps,\n  InputBase,\n  Key,\n  LabelableProps,\n  Node,\n  Selection,\n  TextInputBase,\n  Validation,\n  ValueBase\n} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '../form/useFormValidationState';\nimport {ListState, useListState} from '../list/useListState';\nimport {OverlayTriggerState, useOverlayTriggerState} from '../overlays/useOverlayTriggerState';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo, useState} from 'react';\n\nexport type SelectionMode = 'single' | 'multiple';\nexport type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];\nexport type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];\ntype ValidationType<M extends SelectionMode> = M extends 'single' ? Key : Key[];\n\nexport interface SelectProps<T, M extends SelectionMode = 'single'>\n  extends\n    CollectionBase<T>,\n    Omit<InputBase, 'isReadOnly'>,\n    ValueBase<ValueType<M>, ChangeValueType<M>>,\n    Validation<ValidationType<M>>,\n    HelpTextProps,\n    LabelableProps,\n    TextInputBase,\n    FocusableProps {\n  /**\n   * Whether single or multiple selection is enabled.\n   *\n   * @default 'single'\n   */\n  selectionMode?: M;\n  /**\n   * The currently selected key in the collection (controlled).\n   *\n   * @deprecated\n   */\n  selectedKey?: Key | null;\n  /**\n   * The initial selected key in the collection (uncontrolled).\n   *\n   * @deprecated\n   */\n  defaultSelectedKey?: Key | null;\n  /**\n   * Handler that is called when the selection changes.\n   *\n   * @deprecated\n   */\n  onSelectionChange?: (key: Key | null) => void;\n  /** Sets the open state of the menu. */\n  isOpen?: boolean;\n  /** Sets the default open state of the menu. */\n  defaultOpen?: boolean;\n  /** Method that is called when the open state of the menu changes. */\n  onOpenChange?: (isOpen: boolean) => void;\n  /**\n   * Whether the Select should close when an item is selected. Defaults to true if selectionMode is\n   * single, false otherwise.\n   */\n  shouldCloseOnSelect?: boolean;\n  /** Whether the select should be allowed to be open when the collection is empty. */\n  allowsEmptyCollection?: boolean;\n}\n\nexport interface SelectStateOptions<T, M extends SelectionMode = 'single'>\n  extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T, M extends SelectionMode = 'single'>\n  extends ListState<T>, OverlayTriggerState, FormValidationState {\n  /**\n   * The key for the first selected item.\n   *\n   * @deprecated\n   */\n  readonly selectedKey: Key | null;\n\n  /**\n   * The default selected key.\n   *\n   * @deprecated\n   */\n  readonly defaultSelectedKey: Key | null;\n\n  /**\n   * Sets the selected key.\n   *\n   * @deprecated\n   */\n  setSelectedKey(key: Key | null): void;\n\n  /** The current select value. */\n  readonly value: ValueType<M>;\n\n  /** The default select value. */\n  readonly defaultValue: ValueType<M>;\n\n  /** Sets the select value. */\n  setValue(value: Key | readonly Key[] | null): void;\n\n  /**\n   * The value of the first selected item.\n   *\n   * @deprecated\n   */\n  readonly selectedItem: Node<T> | null;\n\n  /** The value of the selected items. */\n  readonly selectedItems: Node<T>[];\n\n  /** Whether the select is currently focused. */\n  readonly isFocused: boolean;\n\n  /** Sets whether the select is focused. */\n  setFocused(isFocused: boolean): void;\n\n  /** Controls which item will be auto focused when the menu opens. */\n  readonly focusStrategy: FocusStrategy | null;\n\n  /** Opens the menu. */\n  open(focusStrategy?: FocusStrategy | null): void;\n\n  /** Toggles the menu. */\n  toggle(focusStrategy?: FocusStrategy | null): void;\n}\n\n/**\n * Provides state management for a select component. Handles building a collection\n * of items from props, handles the open state for the popup menu, and manages\n * multiple selection state.\n */\nexport function useSelectState<T, M extends SelectionMode = 'single'>(\n  props: SelectStateOptions<T, M>\n): SelectState<T, M> {\n  let {selectionMode = 'single' as M, shouldCloseOnSelect = selectionMode === 'single'} = props;\n  let triggerState = useOverlayTriggerState(props);\n  let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n  let defaultValue = useMemo(() => {\n    return props.defaultValue !== undefined\n      ? props.defaultValue\n      : ((selectionMode === 'single' ? (props.defaultSelectedKey ?? null) : []) as ValueType<M>);\n  }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);\n  let value = useMemo(() => {\n    return props.value !== undefined\n      ? props.value\n      : ((selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>);\n  }, [props.value, props.selectedKey, selectionMode]);\n  let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(\n    value,\n    defaultValue,\n    props.onChange as any\n  );\n  // Only display the first selected item if in single selection mode but the value is an array.\n  let displayValue =\n    selectionMode === 'single' && Array.isArray(controlledValue)\n      ? controlledValue[0]\n      : controlledValue;\n  let setValue = (value: Key | Key[] | null) => {\n    if (selectionMode === 'single') {\n      let key = Array.isArray(value) ? (value[0] ?? null) : value;\n      setControlledValue(key);\n      if (key !== displayValue) {\n        props.onSelectionChange?.(key);\n      }\n    } else {\n      let keys: Key[] = [];\n      if (Array.isArray(value)) {\n        keys = value;\n      } else if (value != null) {\n        keys = [value];\n      }\n\n      setControlledValue(keys);\n    }\n  };\n\n  let listState = useListState({\n    ...props,\n    selectionMode,\n    disallowEmptySelection: selectionMode === 'single',\n    allowDuplicateSelectionEvents: true,\n    selectedKeys: useMemo(() => convertValue(displayValue), [displayValue]),\n    onSelectionChange: (keys: Selection) => {\n      // impossible, but TS doesn't know that\n      if (keys === 'all') {\n        return;\n      }\n\n      if (selectionMode === 'single') {\n        let key = keys.values().next().value ?? null;\n        setValue(key);\n      } else {\n        setValue([...keys]);\n      }\n      if (shouldCloseOnSelect) {\n        triggerState.close();\n      }\n\n      validationState.commitValidation();\n    }\n  });\n\n  let selectedKey = listState.selectionManager.firstSelectedKey;\n  let selectedItems = useMemo(() => {\n    return [...listState.selectionManager.selectedKeys]\n      .map(key => listState.collection.getItem(key))\n      .filter(item => item != null);\n  }, [listState.selectionManager.selectedKeys, listState.collection]);\n\n  let validationState = useFormValidationState({\n    ...props,\n    value: Array.isArray(displayValue) && displayValue.length === 0 ? null : (displayValue as any)\n  });\n\n  let [isFocused, setFocused] = useState(false);\n  let [initialValue] = useState(displayValue);\n\n  return {\n    ...validationState,\n    ...listState,\n    ...triggerState,\n    value: displayValue as ValueType<M>,\n    defaultValue: defaultValue ?? (initialValue as ValueType<M>),\n    setValue,\n    selectedKey,\n    setSelectedKey: setValue,\n    selectedItem: selectedItems[0] ?? null,\n    selectedItems,\n    defaultSelectedKey:\n      props.defaultSelectedKey ?? (props.selectionMode === 'single' ? (initialValue as Key) : null),\n    focusStrategy,\n    open(focusStrategy: FocusStrategy | null = null) {\n      // Don't open if the collection is empty.\n      if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n        setFocusStrategy(focusStrategy);\n        triggerState.open();\n      }\n    },\n    toggle(focusStrategy: FocusStrategy | null = null) {\n      if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n        setFocusStrategy(focusStrategy);\n        triggerState.toggle();\n      }\n    },\n    isFocused,\n    setFocused\n  };\n}\n\nfunction convertValue(value: Key | Key[] | null | undefined) {\n  if (value === undefined) {\n    return undefined;\n  }\n  if (value === null) {\n    return [];\n  }\n  return Array.isArray(value) ? value : [value];\n}\n"],"names":[],"version":3,"file":"useSelectState.mjs.map"}