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AyC;YACzC,IAAI,UAAU,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 {CollectionBase, CollectionStateBase, FocusableProps, FocusStrategy, HelpTextProps, InputBase, Key, LabelableProps, Node, Selection, TextInputBase, Validation, ValueBase} 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'> extends CollectionBase<T>, Omit<InputBase, 'isReadOnly'>, ValueBase<ValueType<M>, ChangeValueType<M>>, Validation<ValidationType<M>>, HelpTextProps, LabelableProps, TextInputBase, FocusableProps {\n  /**\n   * Whether single or multiple selection is enabled.\n   * @default 'single'\n   */\n  selectionMode?: M,\n  /**\n   * The currently selected key in the collection (controlled).\n   * @deprecated\n   */\n  selectedKey?: Key | null,\n  /**\n   * The initial selected key in the collection (uncontrolled).\n   * @deprecated\n   */\n  defaultSelectedKey?: Key | null,\n  /**\n   * Handler that is called when the selection changes.\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  /** Whether the Select should close when an item is selected. Defaults to true if selectionMode is single, false otherwise. */\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'> extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T, M extends SelectionMode = 'single'> extends ListState<T>, OverlayTriggerState, FormValidationState {\n  /**\n   * The key for the first selected item.\n   * @deprecated\n   */\n  readonly selectedKey: Key | null,\n\n  /**\n   * The default selected key.\n   * @deprecated\n   */\n  readonly defaultSelectedKey: Key | null,\n\n  /**\n   * Sets the selected key.\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   * @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 extends object, M extends SelectionMode = 'single'>(props: SelectStateOptions<T, M>): SelectState<T, M>  {\n  let {\n    selectionMode = 'single' as M,\n    shouldCloseOnSelect = selectionMode === 'single'\n  } = props;\n  let triggerState = useOverlayTriggerState(props);\n  let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n  let defaultValue = useMemo(() => {\n    return props.defaultValue !== undefined ? props.defaultValue : (selectionMode === 'single' ? props.defaultSelectedKey ?? null : []) as ValueType<M>;\n  }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);\n  let value = useMemo(() => {\n    return props.value !== undefined ? props.value : (selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>;\n  }, [props.value, props.selectedKey, selectionMode]);\n  let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(value, defaultValue, props.onChange as any);\n  // Only display the first selected item if in single selection mode but the value is an array.\n  let displayValue = selectionMode === 'single' && Array.isArray(controlledValue) ? controlledValue[0] : 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].map(key => listState.collection.getItem(key)).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: 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.cjs.map"}