@react-stately/toggle 3.7.7 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/import.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import {useToggleState as $3017fa7ffdddec74$export$8042c6c013fd5226} from "./useToggleState.mjs";
2
+ import {useToggleGroupState as $ad999b7f496648d1$export$8deac67f4addf270} from "./useToggleGroupState.mjs";
2
3
 
3
4
  /*
4
5
  * Copyright 2020 Adobe. All rights reserved.
@@ -13,5 +14,6 @@ import {useToggleState as $3017fa7ffdddec74$export$8042c6c013fd5226} from "./use
13
14
  */
14
15
 
15
16
 
16
- export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState};
17
+
18
+ export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState, $ad999b7f496648d1$export$8deac67f4addf270 as useToggleGroupState};
17
19
  //# sourceMappingURL=module.js.map
package/dist/main.js CHANGED
@@ -1,4 +1,5 @@
1
1
  var $d84f98b140466b44$exports = require("./useToggleState.main.js");
2
+ var $40e5a2b1abaa0aa5$exports = require("./useToggleGroupState.main.js");
2
3
 
3
4
 
4
5
  function $parcel$export(e, n, v, s) {
@@ -6,6 +7,7 @@ function $parcel$export(e, n, v, s) {
6
7
  }
7
8
 
8
9
  $parcel$export(module.exports, "useToggleState", () => $d84f98b140466b44$exports.useToggleState);
10
+ $parcel$export(module.exports, "useToggleGroupState", () => $40e5a2b1abaa0aa5$exports.useToggleGroupState);
9
11
  /*
10
12
  * Copyright 2020 Adobe. All rights reserved.
11
13
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
@@ -19,4 +21,5 @@ $parcel$export(module.exports, "useToggleState", () => $d84f98b140466b44$exports
19
21
  */
20
22
 
21
23
 
24
+
22
25
  //# sourceMappingURL=main.js.map
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/toggle/src/index.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\nexport {useToggleState} from './useToggleState';\n\nexport type {ToggleProps} from '@react-types/checkbox';\nexport type {ToggleState, ToggleStateOptions} from './useToggleState';\n"],"names":[],"version":3,"file":"main.js.map"}
1
+ {"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/toggle/src/index.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\nexport {useToggleState} from './useToggleState';\nexport {useToggleGroupState} from './useToggleGroupState';\n\nexport type {ToggleProps} from '@react-types/checkbox';\nexport type {ToggleState, ToggleStateOptions} from './useToggleState';\nexport type {ToggleGroupProps, ToggleGroupState} from './useToggleGroupState';\n"],"names":[],"version":3,"file":"main.js.map"}
package/dist/module.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import {useToggleState as $3017fa7ffdddec74$export$8042c6c013fd5226} from "./useToggleState.module.js";
2
+ import {useToggleGroupState as $ad999b7f496648d1$export$8deac67f4addf270} from "./useToggleGroupState.module.js";
2
3
 
3
4
  /*
4
5
  * Copyright 2020 Adobe. All rights reserved.
@@ -13,5 +14,6 @@ import {useToggleState as $3017fa7ffdddec74$export$8042c6c013fd5226} from "./use
13
14
  */
14
15
 
15
16
 
16
- export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState};
17
+
18
+ export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState, $ad999b7f496648d1$export$8deac67f4addf270 as useToggleGroupState};
17
19
  //# sourceMappingURL=module.js.map
@@ -1 +1 @@
1
- {"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/toggle/src/index.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\nexport {useToggleState} from './useToggleState';\n\nexport type {ToggleProps} from '@react-types/checkbox';\nexport type {ToggleState, ToggleStateOptions} from './useToggleState';\n"],"names":[],"version":3,"file":"module.js.map"}
1
+ {"mappings":";;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/toggle/src/index.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\nexport {useToggleState} from './useToggleState';\nexport {useToggleGroupState} from './useToggleGroupState';\n\nexport type {ToggleProps} from '@react-types/checkbox';\nexport type {ToggleState, ToggleStateOptions} from './useToggleState';\nexport type {ToggleGroupProps, ToggleGroupState} from './useToggleGroupState';\n"],"names":[],"version":3,"file":"module.js.map"}
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { ToggleProps } from "@react-types/checkbox";
2
- export interface ToggleStateOptions extends Omit<ToggleProps, 'children'> {
3
- }
1
+ import { ToggleStateOptions } from "@react-types/checkbox";
2
+ import { Key } from "@react-types/shared";
3
+ export { ToggleStateOptions };
4
4
  export interface ToggleState {
5
5
  /** Whether the toggle is selected. */
6
6
  readonly isSelected: boolean;
@@ -13,6 +13,39 @@ export interface ToggleState {
13
13
  * Provides state management for toggle components like checkboxes and switches.
14
14
  */
15
15
  export function useToggleState(props?: ToggleStateOptions): ToggleState;
16
+ export interface ToggleGroupProps {
17
+ /** Whether single or multiple selection is enabled. */
18
+ selectionMode?: 'single' | 'multiple';
19
+ /** Whether the collection allows empty selection. */
20
+ disallowEmptySelection?: boolean;
21
+ /** The currently selected keys in the collection (controlled). */
22
+ selectedKeys?: Iterable<Key>;
23
+ /** The initial selected keys in the collection (uncontrolled). */
24
+ defaultSelectedKeys?: Iterable<Key>;
25
+ /** Handler that is called when the selection changes. */
26
+ onSelectionChange?: (keys: Set<Key>) => void;
27
+ /** Whether all items are disabled. */
28
+ isDisabled?: boolean;
29
+ }
30
+ export interface ToggleGroupState {
31
+ /** Whether single or multiple selection is enabled. */
32
+ readonly selectionMode: 'single' | 'multiple';
33
+ /** Whether all items are disabled. */
34
+ readonly isDisabled: boolean;
35
+ /** A set of keys for items that are selected. */
36
+ readonly selectedKeys: Set<Key>;
37
+ /** Toggles the selected state for an item by its key. */
38
+ toggleKey(key: Key): void;
39
+ /** Sets whether the given key is selected. */
40
+ setSelected(key: Key, isSelected: boolean): void;
41
+ /** Replaces the set of selected keys. */
42
+ setSelectedKeys(keys: Set<Key>): void;
43
+ }
44
+ /**
45
+ * Manages state for a group of toggles.
46
+ * It supports both single and multiple selected items.
47
+ */
48
+ export function useToggleGroupState(props: ToggleGroupProps): ToggleGroupState;
16
49
  export type { ToggleProps } from '@react-types/checkbox';
17
50
 
18
51
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"mappings":";AAeA,mCAAoC,SAAQ,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC;CAAG;AAE5E;IACE,sCAAsC;IACtC,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B,+BAA+B;IAC/B,WAAW,CAAC,UAAU,EAAE,OAAO,GAAG,IAAI,CAAC;IAEvC,kCAAkC;IAClC,MAAM,IAAI,IAAI,CAAA;CACf;AAED;;GAEG;AACH,+BAA+B,KAAK,GAAE,kBAAuB,GAAG,WAAW,CAwB1E;ACzCD,YAAY,EAAC,WAAW,EAAC,MAAM,uBAAuB,CAAC","sources":["packages/@react-stately/toggle/src/packages/@react-stately/toggle/src/useToggleState.ts","packages/@react-stately/toggle/src/packages/@react-stately/toggle/src/index.ts","packages/@react-stately/toggle/src/index.ts"],"sourcesContent":[null,null,"/*\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\nexport {useToggleState} from './useToggleState';\n\nexport type {ToggleProps} from '@react-types/checkbox';\nexport type {ToggleState, ToggleStateOptions} from './useToggleState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
1
+ {"mappings":";;AAeA,OAAY,EAAC,kBAAkB,EAAC,CAAC;AAEjC;IACE,sCAAsC;IACtC,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B,+BAA+B;IAC/B,WAAW,CAAC,UAAU,EAAE,OAAO,GAAG,IAAI,CAAC;IAEvC,kCAAkC;IAClC,MAAM,IAAI,IAAI,CAAA;CACf;AAED;;GAEG;AACH,+BAA+B,KAAK,GAAE,kBAAuB,GAAG,WAAW,CAwB1E;ACvCD;IACE,uDAAuD;IACvD,aAAa,CAAC,EAAE,QAAQ,GAAG,UAAU,CAAC;IACtC,qDAAqD;IACrD,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC,kEAAkE;IAClE,YAAY,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC7B,kEAAkE;IAClE,mBAAmB,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC;IACpC,yDAAyD;IACzD,iBAAiB,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC;IAC7C,sCAAsC;IACtC,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB;AAED;IACE,uDAAuD;IACvD,QAAQ,CAAC,aAAa,EAAE,QAAQ,GAAG,UAAU,CAAC;IAE9C,sCAAsC;IACtC,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B,iDAAiD;IACjD,QAAQ,CAAC,YAAY,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IAEhC,yDAAyD;IACzD,SAAS,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC;IAE1B,8CAA8C;IAC9C,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,UAAU,EAAE,OAAO,GAAG,IAAI,CAAC;IAEjD,yCAAyC;IACzC,eAAe,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAAA;CACtC;AAED;;;GAGG;AACH,oCAAoC,KAAK,EAAE,gBAAgB,GAAG,gBAAgB,CAkC7E;AC1ED,YAAY,EAAC,WAAW,EAAC,MAAM,uBAAuB,CAAC","sources":["packages/@react-stately/toggle/src/packages/@react-stately/toggle/src/useToggleState.ts","packages/@react-stately/toggle/src/packages/@react-stately/toggle/src/useToggleGroupState.ts","packages/@react-stately/toggle/src/packages/@react-stately/toggle/src/index.ts","packages/@react-stately/toggle/src/index.ts"],"sourcesContent":[null,null,null,"/*\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\nexport {useToggleState} from './useToggleState';\nexport {useToggleGroupState} from './useToggleGroupState';\n\nexport type {ToggleProps} from '@react-types/checkbox';\nexport type {ToggleState, ToggleStateOptions} from './useToggleState';\nexport type {ToggleGroupProps, ToggleGroupState} from './useToggleGroupState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
@@ -0,0 +1,52 @@
1
+ var $fmWqE$reactstatelyutils = require("@react-stately/utils");
2
+ var $fmWqE$react = require("react");
3
+
4
+
5
+ function $parcel$export(e, n, v, s) {
6
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
7
+ }
8
+
9
+ $parcel$export(module.exports, "useToggleGroupState", () => $40e5a2b1abaa0aa5$export$8deac67f4addf270);
10
+ /*
11
+ * Copyright 2024 Adobe. All rights reserved.
12
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License. You may obtain a copy
14
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software distributed under
17
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
18
+ * OF ANY KIND, either express or implied. See the License for the specific language
19
+ * governing permissions and limitations under the License.
20
+ */
21
+
22
+ function $40e5a2b1abaa0aa5$export$8deac67f4addf270(props) {
23
+ let { selectionMode: selectionMode = 'single', disallowEmptySelection: disallowEmptySelection, isDisabled: isDisabled = false } = props;
24
+ let [selectedKeys, setSelectedKeys] = (0, $fmWqE$reactstatelyutils.useControlledState)((0, $fmWqE$react.useMemo)(()=>props.selectedKeys ? new Set(props.selectedKeys) : undefined, [
25
+ props.selectedKeys
26
+ ]), (0, $fmWqE$react.useMemo)(()=>props.defaultSelectedKeys ? new Set(props.defaultSelectedKeys) : new Set(), [
27
+ props.defaultSelectedKeys
28
+ ]), props.onSelectionChange);
29
+ return {
30
+ selectionMode: selectionMode,
31
+ isDisabled: isDisabled,
32
+ selectedKeys: selectedKeys,
33
+ setSelectedKeys: setSelectedKeys,
34
+ toggleKey (key) {
35
+ let keys;
36
+ if (selectionMode === 'multiple') {
37
+ keys = new Set(selectedKeys);
38
+ if (keys.has(key) && (!disallowEmptySelection || keys.size > 1)) keys.delete(key);
39
+ else keys.add(key);
40
+ } else keys = new Set(selectedKeys.has(key) && !disallowEmptySelection ? [] : [
41
+ key
42
+ ]);
43
+ setSelectedKeys(keys);
44
+ },
45
+ setSelected (key, isSelected) {
46
+ if (isSelected !== selectedKeys.has(key)) this.toggleKey(key);
47
+ }
48
+ };
49
+ }
50
+
51
+
52
+ //# sourceMappingURL=useToggleGroupState.main.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;;AAAA;;;;;;;;;;CAUC;;AA6CM,SAAS,0CAAoB,KAAuB;IACzD,IAAI,iBAAC,gBAAgB,kCAAU,sBAAsB,cAAE,aAAa,OAAM,GAAG;IAC7E,IAAI,CAAC,cAAc,gBAAgB,GAAG,CAAA,GAAA,2CAAiB,EACrD,CAAA,GAAA,oBAAM,EAAE,IAAM,MAAM,YAAY,GAAG,IAAI,IAAI,MAAM,YAAY,IAAI,WAAW;QAAC,MAAM,YAAY;KAAC,GAChG,CAAA,GAAA,oBAAM,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/toggle/src/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 '@react-stately/utils';\nimport {useMemo} from 'react';\n\nexport interface ToggleGroupProps {\n /** Whether single or multiple selection is enabled. */\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.main.js.map"}
@@ -0,0 +1,47 @@
1
+ import {useControlledState as $69RjW$useControlledState} from "@react-stately/utils";
2
+ import {useMemo as $69RjW$useMemo} from "react";
3
+
4
+ /*
5
+ * Copyright 2024 Adobe. All rights reserved.
6
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License. You may obtain a copy
8
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software distributed under
11
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
+ * OF ANY KIND, either express or implied. See the License for the specific language
13
+ * governing permissions and limitations under the License.
14
+ */
15
+
16
+ function $ad999b7f496648d1$export$8deac67f4addf270(props) {
17
+ let { selectionMode: selectionMode = 'single', disallowEmptySelection: disallowEmptySelection, isDisabled: isDisabled = false } = props;
18
+ let [selectedKeys, setSelectedKeys] = (0, $69RjW$useControlledState)((0, $69RjW$useMemo)(()=>props.selectedKeys ? new Set(props.selectedKeys) : undefined, [
19
+ props.selectedKeys
20
+ ]), (0, $69RjW$useMemo)(()=>props.defaultSelectedKeys ? new Set(props.defaultSelectedKeys) : new Set(), [
21
+ props.defaultSelectedKeys
22
+ ]), props.onSelectionChange);
23
+ return {
24
+ selectionMode: selectionMode,
25
+ isDisabled: isDisabled,
26
+ selectedKeys: selectedKeys,
27
+ setSelectedKeys: setSelectedKeys,
28
+ toggleKey (key) {
29
+ let keys;
30
+ if (selectionMode === 'multiple') {
31
+ keys = new Set(selectedKeys);
32
+ if (keys.has(key) && (!disallowEmptySelection || keys.size > 1)) keys.delete(key);
33
+ else keys.add(key);
34
+ } else keys = new Set(selectedKeys.has(key) && !disallowEmptySelection ? [] : [
35
+ key
36
+ ]);
37
+ setSelectedKeys(keys);
38
+ },
39
+ setSelected (key, isSelected) {
40
+ if (isSelected !== selectedKeys.has(key)) this.toggleKey(key);
41
+ }
42
+ };
43
+ }
44
+
45
+
46
+ export {$ad999b7f496648d1$export$8deac67f4addf270 as useToggleGroupState};
47
+ //# sourceMappingURL=useToggleGroupState.module.js.map
@@ -0,0 +1,47 @@
1
+ import {useControlledState as $69RjW$useControlledState} from "@react-stately/utils";
2
+ import {useMemo as $69RjW$useMemo} from "react";
3
+
4
+ /*
5
+ * Copyright 2024 Adobe. All rights reserved.
6
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License. You may obtain a copy
8
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software distributed under
11
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
+ * OF ANY KIND, either express or implied. See the License for the specific language
13
+ * governing permissions and limitations under the License.
14
+ */
15
+
16
+ function $ad999b7f496648d1$export$8deac67f4addf270(props) {
17
+ let { selectionMode: selectionMode = 'single', disallowEmptySelection: disallowEmptySelection, isDisabled: isDisabled = false } = props;
18
+ let [selectedKeys, setSelectedKeys] = (0, $69RjW$useControlledState)((0, $69RjW$useMemo)(()=>props.selectedKeys ? new Set(props.selectedKeys) : undefined, [
19
+ props.selectedKeys
20
+ ]), (0, $69RjW$useMemo)(()=>props.defaultSelectedKeys ? new Set(props.defaultSelectedKeys) : new Set(), [
21
+ props.defaultSelectedKeys
22
+ ]), props.onSelectionChange);
23
+ return {
24
+ selectionMode: selectionMode,
25
+ isDisabled: isDisabled,
26
+ selectedKeys: selectedKeys,
27
+ setSelectedKeys: setSelectedKeys,
28
+ toggleKey (key) {
29
+ let keys;
30
+ if (selectionMode === 'multiple') {
31
+ keys = new Set(selectedKeys);
32
+ if (keys.has(key) && (!disallowEmptySelection || keys.size > 1)) keys.delete(key);
33
+ else keys.add(key);
34
+ } else keys = new Set(selectedKeys.has(key) && !disallowEmptySelection ? [] : [
35
+ key
36
+ ]);
37
+ setSelectedKeys(keys);
38
+ },
39
+ setSelected (key, isSelected) {
40
+ if (isSelected !== selectedKeys.has(key)) this.toggleKey(key);
41
+ }
42
+ };
43
+ }
44
+
45
+
46
+ export {$ad999b7f496648d1$export$8deac67f4addf270 as useToggleGroupState};
47
+ //# sourceMappingURL=useToggleGroupState.module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;AAAA;;;;;;;;;;CAUC;;AA6CM,SAAS,0CAAoB,KAAuB;IACzD,IAAI,iBAAC,gBAAgB,kCAAU,sBAAsB,cAAE,aAAa,OAAM,GAAG;IAC7E,IAAI,CAAC,cAAc,gBAAgB,GAAG,CAAA,GAAA,yBAAiB,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/toggle/src/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 '@react-stately/utils';\nimport {useMemo} from 'react';\n\nexport interface ToggleGroupProps {\n /** Whether single or multiple selection is enabled. */\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.module.js.map"}
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AAqBM,SAAS,0CAAe,QAA4B,CAAC,CAAC;IAC3D,IAAI,cAAC,UAAU,EAAC,GAAG;IAEnB,8EAA8E;IAC9E,+FAA+F;IAC/F,IAAI,CAAC,YAAY,YAAY,GAAG,CAAA,GAAA,2CAAiB,EAAE,MAAM,UAAU,EAAE,MAAM,eAAe,IAAI,OAAO,MAAM,QAAQ;IAEnH,SAAS,eAAe,KAAK;QAC3B,IAAI,CAAC,YACH,YAAY;IAEhB;IAEA,SAAS;QACP,IAAI,CAAC,YACH,YAAY,CAAC;IAEjB;IAEA,OAAO;oBACL;QACA,aAAa;QACb,QAAQ;IACV;AACF","sources":["packages/@react-stately/toggle/src/useToggleState.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 {ToggleProps} from '@react-types/checkbox';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface ToggleStateOptions extends Omit<ToggleProps, 'children'> {}\n\nexport interface ToggleState {\n /** Whether the toggle is selected. */\n readonly isSelected: boolean,\n\n /** Updates selection state. */\n setSelected(isSelected: boolean): void,\n\n /** Toggle the selection state. */\n toggle(): void\n}\n\n/**\n * Provides state management for toggle components like checkboxes and switches.\n */\nexport function useToggleState(props: ToggleStateOptions = {}): ToggleState {\n let {isReadOnly} = props;\n\n // have to provide an empty function so useControlledState doesn't throw a fit\n // can't use useControlledState's prop calling because we need the event object from the change\n let [isSelected, setSelected] = useControlledState(props.isSelected, props.defaultSelected || false, props.onChange);\n\n function updateSelected(value) {\n if (!isReadOnly) {\n setSelected(value);\n }\n }\n\n function toggleState() {\n if (!isReadOnly) {\n setSelected(!isSelected);\n }\n }\n\n return {\n isSelected,\n setSelected: updateSelected,\n toggle: toggleState\n };\n}\n"],"names":[],"version":3,"file":"useToggleState.main.js.map"}
1
+ {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AAqBM,SAAS,0CAAe,QAA4B,CAAC,CAAC;IAC3D,IAAI,cAAC,UAAU,EAAC,GAAG;IAEnB,8EAA8E;IAC9E,+FAA+F;IAC/F,IAAI,CAAC,YAAY,YAAY,GAAG,CAAA,GAAA,2CAAiB,EAAE,MAAM,UAAU,EAAE,MAAM,eAAe,IAAI,OAAO,MAAM,QAAQ;IAEnH,SAAS,eAAe,KAAK;QAC3B,IAAI,CAAC,YACH,YAAY;IAEhB;IAEA,SAAS;QACP,IAAI,CAAC,YACH,YAAY,CAAC;IAEjB;IAEA,OAAO;oBACL;QACA,aAAa;QACb,QAAQ;IACV;AACF","sources":["packages/@react-stately/toggle/src/useToggleState.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 {ToggleStateOptions} from '@react-types/checkbox';\nimport {useControlledState} from '@react-stately/utils';\n\nexport type {ToggleStateOptions};\n\nexport interface ToggleState {\n /** Whether the toggle is selected. */\n readonly isSelected: boolean,\n\n /** Updates selection state. */\n setSelected(isSelected: boolean): void,\n\n /** Toggle the selection state. */\n toggle(): void\n}\n\n/**\n * Provides state management for toggle components like checkboxes and switches.\n */\nexport function useToggleState(props: ToggleStateOptions = {}): ToggleState {\n let {isReadOnly} = props;\n\n // have to provide an empty function so useControlledState doesn't throw a fit\n // can't use useControlledState's prop calling because we need the event object from the change\n let [isSelected, setSelected] = useControlledState(props.isSelected, props.defaultSelected || false, props.onChange);\n\n function updateSelected(value) {\n if (!isReadOnly) {\n setSelected(value);\n }\n }\n\n function toggleState() {\n if (!isReadOnly) {\n setSelected(!isSelected);\n }\n }\n\n return {\n isSelected,\n setSelected: updateSelected,\n toggle: toggleState\n };\n}\n"],"names":[],"version":3,"file":"useToggleState.main.js.map"}
@@ -1 +1 @@
1
- {"mappings":";;AAAA;;;;;;;;;;CAUC;AAqBM,SAAS,0CAAe,QAA4B,CAAC,CAAC;IAC3D,IAAI,cAAC,UAAU,EAAC,GAAG;IAEnB,8EAA8E;IAC9E,+FAA+F;IAC/F,IAAI,CAAC,YAAY,YAAY,GAAG,CAAA,GAAA,yBAAiB,EAAE,MAAM,UAAU,EAAE,MAAM,eAAe,IAAI,OAAO,MAAM,QAAQ;IAEnH,SAAS,eAAe,KAAK;QAC3B,IAAI,CAAC,YACH,YAAY;IAEhB;IAEA,SAAS;QACP,IAAI,CAAC,YACH,YAAY,CAAC;IAEjB;IAEA,OAAO;oBACL;QACA,aAAa;QACb,QAAQ;IACV;AACF","sources":["packages/@react-stately/toggle/src/useToggleState.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 {ToggleProps} from '@react-types/checkbox';\nimport {useControlledState} from '@react-stately/utils';\n\nexport interface ToggleStateOptions extends Omit<ToggleProps, 'children'> {}\n\nexport interface ToggleState {\n /** Whether the toggle is selected. */\n readonly isSelected: boolean,\n\n /** Updates selection state. */\n setSelected(isSelected: boolean): void,\n\n /** Toggle the selection state. */\n toggle(): void\n}\n\n/**\n * Provides state management for toggle components like checkboxes and switches.\n */\nexport function useToggleState(props: ToggleStateOptions = {}): ToggleState {\n let {isReadOnly} = props;\n\n // have to provide an empty function so useControlledState doesn't throw a fit\n // can't use useControlledState's prop calling because we need the event object from the change\n let [isSelected, setSelected] = useControlledState(props.isSelected, props.defaultSelected || false, props.onChange);\n\n function updateSelected(value) {\n if (!isReadOnly) {\n setSelected(value);\n }\n }\n\n function toggleState() {\n if (!isReadOnly) {\n setSelected(!isSelected);\n }\n }\n\n return {\n isSelected,\n setSelected: updateSelected,\n toggle: toggleState\n };\n}\n"],"names":[],"version":3,"file":"useToggleState.module.js.map"}
1
+ {"mappings":";;AAAA;;;;;;;;;;CAUC;AAqBM,SAAS,0CAAe,QAA4B,CAAC,CAAC;IAC3D,IAAI,cAAC,UAAU,EAAC,GAAG;IAEnB,8EAA8E;IAC9E,+FAA+F;IAC/F,IAAI,CAAC,YAAY,YAAY,GAAG,CAAA,GAAA,yBAAiB,EAAE,MAAM,UAAU,EAAE,MAAM,eAAe,IAAI,OAAO,MAAM,QAAQ;IAEnH,SAAS,eAAe,KAAK;QAC3B,IAAI,CAAC,YACH,YAAY;IAEhB;IAEA,SAAS;QACP,IAAI,CAAC,YACH,YAAY,CAAC;IAEjB;IAEA,OAAO;oBACL;QACA,aAAa;QACb,QAAQ;IACV;AACF","sources":["packages/@react-stately/toggle/src/useToggleState.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 {ToggleStateOptions} from '@react-types/checkbox';\nimport {useControlledState} from '@react-stately/utils';\n\nexport type {ToggleStateOptions};\n\nexport interface ToggleState {\n /** Whether the toggle is selected. */\n readonly isSelected: boolean,\n\n /** Updates selection state. */\n setSelected(isSelected: boolean): void,\n\n /** Toggle the selection state. */\n toggle(): void\n}\n\n/**\n * Provides state management for toggle components like checkboxes and switches.\n */\nexport function useToggleState(props: ToggleStateOptions = {}): ToggleState {\n let {isReadOnly} = props;\n\n // have to provide an empty function so useControlledState doesn't throw a fit\n // can't use useControlledState's prop calling because we need the event object from the change\n let [isSelected, setSelected] = useControlledState(props.isSelected, props.defaultSelected || false, props.onChange);\n\n function updateSelected(value) {\n if (!isReadOnly) {\n setSelected(value);\n }\n }\n\n function toggleState() {\n if (!isReadOnly) {\n setSelected(!isSelected);\n }\n }\n\n return {\n isSelected,\n setSelected: updateSelected,\n toggle: toggleState\n };\n}\n"],"names":[],"version":3,"file":"useToggleState.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/toggle",
3
- "version": "3.7.7",
3
+ "version": "3.8.0",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -22,15 +22,16 @@
22
22
  "url": "https://github.com/adobe/react-spectrum"
23
23
  },
24
24
  "dependencies": {
25
- "@react-stately/utils": "^3.10.3",
26
- "@react-types/checkbox": "^3.8.3",
25
+ "@react-stately/utils": "^3.10.5",
26
+ "@react-types/checkbox": "^3.9.0",
27
+ "@react-types/shared": "^3.26.0",
27
28
  "@swc/helpers": "^0.5.0"
28
29
  },
29
30
  "peerDependencies": {
30
- "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
31
+ "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
31
32
  },
32
33
  "publishConfig": {
33
34
  "access": "public"
34
35
  },
35
- "gitHead": "faf0e18467231422cb7a06eb92a74d04e271f1e8"
36
+ "gitHead": "71f0ef23053f9e03ee7e97df736e8b083e006849"
36
37
  }
package/src/index.ts CHANGED
@@ -11,6 +11,8 @@
11
11
  */
12
12
 
13
13
  export {useToggleState} from './useToggleState';
14
+ export {useToggleGroupState} from './useToggleGroupState';
14
15
 
15
16
  export type {ToggleProps} from '@react-types/checkbox';
16
17
  export type {ToggleState, ToggleStateOptions} from './useToggleState';
18
+ export type {ToggleGroupProps, ToggleGroupState} from './useToggleGroupState';
@@ -0,0 +1,90 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {Key} from '@react-types/shared';
14
+ import {useControlledState} from '@react-stately/utils';
15
+ import {useMemo} from 'react';
16
+
17
+ export interface ToggleGroupProps {
18
+ /** Whether single or multiple selection is enabled. */
19
+ selectionMode?: 'single' | 'multiple',
20
+ /** Whether the collection allows empty selection. */
21
+ disallowEmptySelection?: boolean,
22
+ /** The currently selected keys in the collection (controlled). */
23
+ selectedKeys?: Iterable<Key>,
24
+ /** The initial selected keys in the collection (uncontrolled). */
25
+ defaultSelectedKeys?: Iterable<Key>,
26
+ /** Handler that is called when the selection changes. */
27
+ onSelectionChange?: (keys: Set<Key>) => void,
28
+ /** Whether all items are disabled. */
29
+ isDisabled?: boolean
30
+ }
31
+
32
+ export interface ToggleGroupState {
33
+ /** Whether single or multiple selection is enabled. */
34
+ readonly selectionMode: 'single' | 'multiple',
35
+
36
+ /** Whether all items are disabled. */
37
+ readonly isDisabled: boolean,
38
+
39
+ /** A set of keys for items that are selected. */
40
+ readonly selectedKeys: Set<Key>,
41
+
42
+ /** Toggles the selected state for an item by its key. */
43
+ toggleKey(key: Key): void,
44
+
45
+ /** Sets whether the given key is selected. */
46
+ setSelected(key: Key, isSelected: boolean): void,
47
+
48
+ /** Replaces the set of selected keys. */
49
+ setSelectedKeys(keys: Set<Key>): void
50
+ }
51
+
52
+ /**
53
+ * Manages state for a group of toggles.
54
+ * It supports both single and multiple selected items.
55
+ */
56
+ export function useToggleGroupState(props: ToggleGroupProps): ToggleGroupState {
57
+ let {selectionMode = 'single', disallowEmptySelection, isDisabled = false} = props;
58
+ let [selectedKeys, setSelectedKeys] = useControlledState(
59
+ useMemo(() => props.selectedKeys ? new Set(props.selectedKeys) : undefined, [props.selectedKeys]),
60
+ useMemo(() => props.defaultSelectedKeys ? new Set(props.defaultSelectedKeys) : new Set(), [props.defaultSelectedKeys]),
61
+ props.onSelectionChange
62
+ );
63
+
64
+ return {
65
+ selectionMode,
66
+ isDisabled,
67
+ selectedKeys,
68
+ setSelectedKeys,
69
+ toggleKey(key) {
70
+ let keys: Set<Key>;
71
+ if (selectionMode === 'multiple') {
72
+ keys = new Set(selectedKeys);
73
+ if (keys.has(key) && (!disallowEmptySelection || keys.size > 1)) {
74
+ keys.delete(key);
75
+ } else {
76
+ keys.add(key);
77
+ }
78
+ } else {
79
+ keys = new Set(selectedKeys.has(key) && !disallowEmptySelection ? [] : [key]);
80
+ }
81
+
82
+ setSelectedKeys(keys);
83
+ },
84
+ setSelected(key, isSelected) {
85
+ if (isSelected !== selectedKeys.has(key)) {
86
+ this.toggleKey(key);
87
+ }
88
+ }
89
+ };
90
+ }
@@ -10,10 +10,10 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {ToggleProps} from '@react-types/checkbox';
13
+ import {ToggleStateOptions} from '@react-types/checkbox';
14
14
  import {useControlledState} from '@react-stately/utils';
15
15
 
16
- export interface ToggleStateOptions extends Omit<ToggleProps, 'children'> {}
16
+ export type {ToggleStateOptions};
17
17
 
18
18
  export interface ToggleState {
19
19
  /** Whether the toggle is selected. */