{"mappings":";;;AAAA;;;;;;;;;;CAUC;;AAgDM,SAAS,0CAAoB,KAAuB;IACzD,IAAI,iBAAC,gBAAgB,kCAAU,sBAAsB,cAAE,aAAa,OAAM,GAAG;IAC7E,IAAI,CAAC,cAAc,gBAAgB,GAAG,CAAA,GAAA,yCAAiB,EACrD,CAAA,GAAA,cAAM,EAAE,IAAM,MAAM,YAAY,GAAG,IAAI,IAAI,MAAM,YAAY,IAAI,WAAW;QAAC,MAAM,YAAY;KAAC,GAChG,CAAA,GAAA,cAAM,EAAE,IAAM,MAAM,mBAAmB,GAAG,IAAI,IAAI,MAAM,mBAAmB,IAAI,IAAI,OAAO;QAAC,MAAM,mBAAmB;KAAC,GACrH,MAAM,iBAAiB;IAGzB,OAAO;uBACL;oBACA;sBACA;yBACA;QACA,WAAU,GAAG;YACX,IAAI;YACJ,IAAI,kBAAkB,YAAY;gBAChC,OAAO,IAAI,IAAI;gBACf,IAAI,KAAK,GAAG,CAAC,QAAS,CAAA,CAAC,0BAA0B,KAAK,IAAI,GAAG,CAAA,GAC3D,KAAK,MAAM,CAAC;qBAEZ,KAAK,GAAG,CAAC;YAEb,OACE,OAAO,IAAI,IAAI,aAAa,GAAG,CAAC,QAAQ,CAAC,yBAAyB,EAAE,GAAG;gBAAC;aAAI;YAG9E,gBAAgB;QAClB;QACA,aAAY,GAAG,EAAE,UAAU;YACzB,IAAI,eAAe,aAAa,GAAG,CAAC,MAClC,IAAI,CAAC,SAAS,CAAC;QAEnB;IACF;AACF","sources":["packages/react-stately/src/toggle/useToggleGroupState.ts"],"sourcesContent":["/*\n * Copyright 2024 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 {Key} from '@react-types/shared';\nimport {useControlledState} from '../utils/useControlledState';\nimport {useMemo} from 'react';\n\nexport interface ToggleGroupProps {\n  /**\n   * Whether single or multiple selection is enabled.\n   * @default 'single'\n   */\n  selectionMode?: 'single' | 'multiple',\n  /** Whether the collection allows empty selection. */\n  disallowEmptySelection?: boolean,\n  /** The currently selected keys in the collection (controlled). */\n  selectedKeys?: Iterable<Key>,\n  /** The initial selected keys in the collection (uncontrolled). */\n  defaultSelectedKeys?: Iterable<Key>,\n  /** Handler that is called when the selection changes. */\n  onSelectionChange?: (keys: Set<Key>) => void,\n  /** Whether all items are disabled. */\n  isDisabled?: boolean\n}\n\nexport interface ToggleGroupState {\n  /** Whether single or multiple selection is enabled. */\n  readonly selectionMode: 'single' | 'multiple',\n\n  /** Whether all items are disabled. */\n  readonly isDisabled: boolean,\n  \n  /** A set of keys for items that are selected. */\n  readonly selectedKeys: Set<Key>,\n\n  /** Toggles the selected state for an item by its key. */\n  toggleKey(key: Key): void,\n\n  /** Sets whether the given key is selected. */\n  setSelected(key: Key, isSelected: boolean): void,\n\n  /** Replaces the set of selected keys. */\n  setSelectedKeys(keys: Set<Key>): void\n}\n\n/**\n * Manages state for a group of toggles.\n * It supports both single and multiple selected items.\n */\nexport function useToggleGroupState(props: ToggleGroupProps): ToggleGroupState {\n  let {selectionMode = 'single', disallowEmptySelection, isDisabled = false} = props;\n  let [selectedKeys, setSelectedKeys] = useControlledState(\n    useMemo(() => props.selectedKeys ? new Set(props.selectedKeys) : undefined, [props.selectedKeys]),\n    useMemo(() => props.defaultSelectedKeys ? new Set(props.defaultSelectedKeys) : new Set(), [props.defaultSelectedKeys]),\n    props.onSelectionChange\n  );\n\n  return {\n    selectionMode,\n    isDisabled,\n    selectedKeys,\n    setSelectedKeys,\n    toggleKey(key) {\n      let keys: Set<Key>;\n      if (selectionMode === 'multiple') {\n        keys = new Set(selectedKeys);\n        if (keys.has(key) && (!disallowEmptySelection || keys.size > 1)) {\n          keys.delete(key);\n        } else {\n          keys.add(key);\n        }\n      } else {\n        keys = new Set(selectedKeys.has(key) && !disallowEmptySelection ? [] : [key]);\n      }\n  \n      setSelectedKeys(keys);\n    },\n    setSelected(key, isSelected) {\n      if (isSelected !== selectedKeys.has(key)) {\n        this.toggleKey(key);\n      }\n    }\n  };\n}\n"],"names":[],"version":3,"file":"useToggleGroupState.mjs.map"}