Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@react-stately/toggle

Package Overview
Dependencies
Maintainers
2
Versions
1318
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-stately/toggle - npm Package Compare versions

Comparing version
3.0.0-nightly-a22a0aed3-251013
to
3.0.0-nightly-a2b69fbc3-260507
+4
dist/types/src/index.d.ts
export { useToggleState } from 'react-stately/useToggleState';
export { useToggleGroupState } from 'react-stately/useToggleGroupState';
export type { ToggleProps, ToggleState, ToggleStateOptions } from 'react-stately/useToggleState';
export type { ToggleGroupProps, ToggleGroupState } from 'react-stately/useToggleGroupState';
+3
-3

@@ -1,3 +0,3 @@

import {useToggleState as $3017fa7ffdddec74$export$8042c6c013fd5226} from "./useToggleState.mjs";
import {useToggleGroupState as $ad999b7f496648d1$export$8deac67f4addf270} from "./useToggleGroupState.mjs";
import {useToggleState as $0123a9b9f7391d3f$re_export$useToggleState} from "react-stately/useToggleState";
import {useToggleGroupState as $0123a9b9f7391d3f$re_export$useToggleGroupState} from "react-stately/useToggleGroupState";

@@ -18,3 +18,3 @@ /*

export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState, $ad999b7f496648d1$export$8deac67f4addf270 as useToggleGroupState};
export {$0123a9b9f7391d3f$re_export$useToggleState as useToggleState, $0123a9b9f7391d3f$re_export$useToggleGroupState as useToggleGroupState};
//# sourceMappingURL=module.js.map

@@ -1,3 +0,3 @@

var $d84f98b140466b44$exports = require("./useToggleState.main.js");
var $40e5a2b1abaa0aa5$exports = require("./useToggleGroupState.main.js");
var $i46gm$reactstatelyuseToggleState = require("react-stately/useToggleState");
var $i46gm$reactstatelyuseToggleGroupState = require("react-stately/useToggleGroupState");

@@ -9,4 +9,4 @@

$parcel$export(module.exports, "useToggleState", () => $d84f98b140466b44$exports.useToggleState);
$parcel$export(module.exports, "useToggleGroupState", () => $40e5a2b1abaa0aa5$exports.useToggleGroupState);
$parcel$export(module.exports, "useToggleState", function () { return $i46gm$reactstatelyuseToggleState.useToggleState; });
$parcel$export(module.exports, "useToggleGroupState", function () { return $i46gm$reactstatelyuseToggleGroupState.useToggleGroupState; });
/*

@@ -13,0 +13,0 @@ * Copyright 2020 Adobe. All rights reserved.

@@ -1,1 +0,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"}
{"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 'react-stately/useToggleState';\n\nexport {useToggleGroupState} from 'react-stately/useToggleGroupState';\nexport type {ToggleProps, ToggleState, ToggleStateOptions} from 'react-stately/useToggleState';\nexport type {ToggleGroupProps, ToggleGroupState} from 'react-stately/useToggleGroupState';\n"],"names":[],"version":3,"file":"main.js.map"}

@@ -1,3 +0,3 @@

import {useToggleState as $3017fa7ffdddec74$export$8042c6c013fd5226} from "./useToggleState.module.js";
import {useToggleGroupState as $ad999b7f496648d1$export$8deac67f4addf270} from "./useToggleGroupState.module.js";
import {useToggleState as $0123a9b9f7391d3f$re_export$useToggleState} from "react-stately/useToggleState";
import {useToggleGroupState as $0123a9b9f7391d3f$re_export$useToggleGroupState} from "react-stately/useToggleGroupState";

@@ -18,3 +18,3 @@ /*

export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState, $ad999b7f496648d1$export$8deac67f4addf270 as useToggleGroupState};
export {$0123a9b9f7391d3f$re_export$useToggleState as useToggleState, $0123a9b9f7391d3f$re_export$useToggleGroupState as useToggleGroupState};
//# sourceMappingURL=module.js.map

@@ -1,1 +0,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"}
{"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 'react-stately/useToggleState';\n\nexport {useToggleGroupState} from 'react-stately/useToggleGroupState';\nexport type {ToggleProps, ToggleState, ToggleStateOptions} from 'react-stately/useToggleState';\nexport type {ToggleGroupProps, ToggleGroupState} from 'react-stately/useToggleGroupState';\n"],"names":[],"version":3,"file":"module.js.map"}
{
"name": "@react-stately/toggle",
"version": "3.0.0-nightly-a22a0aed3-251013",
"version": "3.0.0-nightly-a2b69fbc3-260507",
"description": "Spectrum UI components in React",

@@ -9,11 +9,11 @@ "license": "Apache-2.0",

"exports": {
"source": "./src/index.ts",
"types": [
"./dist/types.d.ts",
"./src/index.ts"
],
"import": "./dist/import.mjs",
"require": "./dist/main.js"
".": {
"source": "./src/index.ts",
"types": "./dist/types/src/index.d.ts",
"import": "./dist/import.mjs",
"require": "./dist/main.js"
},
"./package.json": "./package.json"
},
"types": "dist/types.d.ts",
"types": "dist/types/src/index.d.ts",
"source": "src/index.ts",

@@ -30,13 +30,19 @@ "files": [

"dependencies": {
"@react-stately/utils": "3.0.0-nightly-a22a0aed3-251013",
"@react-types/checkbox": "3.0.0-nightly-a22a0aed3-251013",
"@react-types/shared": "3.0.0-nightly-a22a0aed3-251013",
"@swc/helpers": "^0.5.0"
"@swc/helpers": "^0.5.0",
"react-stately": "3.0.0-nightly-a2b69fbc3-260507"
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1",
"react-dom": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
},
"publishConfig": {
"access": "public"
},
"targets": {
"types": false
},
"devDependencies": {
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1",
"react-dom": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
}
}

@@ -13,7 +13,6 @@ /*

export {useToggleState} from './useToggleState';
export {useToggleGroupState} from './useToggleGroupState';
export {useToggleState} from 'react-stately/useToggleState';
export type {ToggleProps} from '@react-types/checkbox';
export type {ToggleState, ToggleStateOptions} from './useToggleState';
export type {ToggleGroupProps, ToggleGroupState} from './useToggleGroupState';
export {useToggleGroupState} from 'react-stately/useToggleGroupState';
export type {ToggleProps, ToggleState, ToggleStateOptions} from 'react-stately/useToggleState';
export type {ToggleGroupProps, ToggleGroupState} from 'react-stately/useToggleGroupState';
import { ToggleStateOptions } from "@react-types/checkbox";
import { Key } from "@react-types/shared";
export { ToggleStateOptions };
export interface ToggleState {
/** Whether the toggle is selected. */
readonly isSelected: boolean;
/** Whether the toggle is selected by default. */
readonly defaultSelected: boolean;
/** Updates selection state. */
setSelected(isSelected: boolean): void;
/** Toggle the selection state. */
toggle(): void;
}
/**
* Provides state management for toggle components like checkboxes and switches.
*/
export function useToggleState(props?: ToggleStateOptions): ToggleState;
export interface ToggleGroupProps {
/**
* Whether single or multiple selection is enabled.
* @default 'single'
*/
selectionMode?: 'single' | 'multiple';
/** Whether the collection allows empty selection. */
disallowEmptySelection?: boolean;
/** The currently selected keys in the collection (controlled). */
selectedKeys?: Iterable<Key>;
/** The initial selected keys in the collection (uncontrolled). */
defaultSelectedKeys?: Iterable<Key>;
/** Handler that is called when the selection changes. */
onSelectionChange?: (keys: Set<Key>) => void;
/** Whether all items are disabled. */
isDisabled?: boolean;
}
export interface ToggleGroupState {
/** Whether single or multiple selection is enabled. */
readonly selectionMode: 'single' | 'multiple';
/** Whether all items are disabled. */
readonly isDisabled: boolean;
/** A set of keys for items that are selected. */
readonly selectedKeys: Set<Key>;
/** Toggles the selected state for an item by its key. */
toggleKey(key: Key): void;
/** Sets whether the given key is selected. */
setSelected(key: Key, isSelected: boolean): void;
/** Replaces the set of selected keys. */
setSelectedKeys(keys: Set<Key>): void;
}
/**
* Manages state for a group of toggles.
* It supports both single and multiple selected items.
*/
export function useToggleGroupState(props: ToggleGroupProps): ToggleGroupState;
export type { ToggleProps } from '@react-types/checkbox';
//# sourceMappingURL=types.d.ts.map
{"mappings":";;AAgBA,OAAY,EAAC,kBAAkB,EAAC,CAAC;AAEjC;IACE,sCAAsC;IACtC,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B,iDAAiD;IACjD,QAAQ,CAAC,eAAe,EAAE,OAAO,CAAC;IAElC,+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,CA0B1E;AC7CD;IACE;;;OAGG;IACH,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;AC7ED,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"}
var $fmWqE$reactstatelyutils = require("@react-stately/utils");
var $fmWqE$react = require("react");
function $parcel$export(e, n, v, s) {
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
}
$parcel$export(module.exports, "useToggleGroupState", () => $40e5a2b1abaa0aa5$export$8deac67f4addf270);
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $40e5a2b1abaa0aa5$export$8deac67f4addf270(props) {
let { selectionMode: selectionMode = 'single', disallowEmptySelection: disallowEmptySelection, isDisabled: isDisabled = false } = props;
let [selectedKeys, setSelectedKeys] = (0, $fmWqE$reactstatelyutils.useControlledState)((0, $fmWqE$react.useMemo)(()=>props.selectedKeys ? new Set(props.selectedKeys) : undefined, [
props.selectedKeys
]), (0, $fmWqE$react.useMemo)(()=>props.defaultSelectedKeys ? new Set(props.defaultSelectedKeys) : new Set(), [
props.defaultSelectedKeys
]), props.onSelectionChange);
return {
selectionMode: selectionMode,
isDisabled: isDisabled,
selectedKeys: selectedKeys,
setSelectedKeys: setSelectedKeys,
toggleKey (key) {
let keys;
if (selectionMode === 'multiple') {
keys = new Set(selectedKeys);
if (keys.has(key) && (!disallowEmptySelection || keys.size > 1)) keys.delete(key);
else keys.add(key);
} else keys = new Set(selectedKeys.has(key) && !disallowEmptySelection ? [] : [
key
]);
setSelectedKeys(keys);
},
setSelected (key, isSelected) {
if (isSelected !== selectedKeys.has(key)) this.toggleKey(key);
}
};
}
//# sourceMappingURL=useToggleGroupState.main.js.map
{"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,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 /**\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.main.js.map"}
import {useControlledState as $69RjW$useControlledState} from "@react-stately/utils";
import {useMemo as $69RjW$useMemo} from "react";
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $ad999b7f496648d1$export$8deac67f4addf270(props) {
let { selectionMode: selectionMode = 'single', disallowEmptySelection: disallowEmptySelection, isDisabled: isDisabled = false } = props;
let [selectedKeys, setSelectedKeys] = (0, $69RjW$useControlledState)((0, $69RjW$useMemo)(()=>props.selectedKeys ? new Set(props.selectedKeys) : undefined, [
props.selectedKeys
]), (0, $69RjW$useMemo)(()=>props.defaultSelectedKeys ? new Set(props.defaultSelectedKeys) : new Set(), [
props.defaultSelectedKeys
]), props.onSelectionChange);
return {
selectionMode: selectionMode,
isDisabled: isDisabled,
selectedKeys: selectedKeys,
setSelectedKeys: setSelectedKeys,
toggleKey (key) {
let keys;
if (selectionMode === 'multiple') {
keys = new Set(selectedKeys);
if (keys.has(key) && (!disallowEmptySelection || keys.size > 1)) keys.delete(key);
else keys.add(key);
} else keys = new Set(selectedKeys.has(key) && !disallowEmptySelection ? [] : [
key
]);
setSelectedKeys(keys);
},
setSelected (key, isSelected) {
if (isSelected !== selectedKeys.has(key)) this.toggleKey(key);
}
};
}
export {$ad999b7f496648d1$export$8deac67f4addf270 as useToggleGroupState};
//# sourceMappingURL=useToggleGroupState.module.js.map
import {useControlledState as $69RjW$useControlledState} from "@react-stately/utils";
import {useMemo as $69RjW$useMemo} from "react";
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $ad999b7f496648d1$export$8deac67f4addf270(props) {
let { selectionMode: selectionMode = 'single', disallowEmptySelection: disallowEmptySelection, isDisabled: isDisabled = false } = props;
let [selectedKeys, setSelectedKeys] = (0, $69RjW$useControlledState)((0, $69RjW$useMemo)(()=>props.selectedKeys ? new Set(props.selectedKeys) : undefined, [
props.selectedKeys
]), (0, $69RjW$useMemo)(()=>props.defaultSelectedKeys ? new Set(props.defaultSelectedKeys) : new Set(), [
props.defaultSelectedKeys
]), props.onSelectionChange);
return {
selectionMode: selectionMode,
isDisabled: isDisabled,
selectedKeys: selectedKeys,
setSelectedKeys: setSelectedKeys,
toggleKey (key) {
let keys;
if (selectionMode === 'multiple') {
keys = new Set(selectedKeys);
if (keys.has(key) && (!disallowEmptySelection || keys.size > 1)) keys.delete(key);
else keys.add(key);
} else keys = new Set(selectedKeys.has(key) && !disallowEmptySelection ? [] : [
key
]);
setSelectedKeys(keys);
},
setSelected (key, isSelected) {
if (isSelected !== selectedKeys.has(key)) this.toggleKey(key);
}
};
}
export {$ad999b7f496648d1$export$8deac67f4addf270 as useToggleGroupState};
//# sourceMappingURL=useToggleGroupState.module.js.map
{"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,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 /**\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.module.js.map"}
var $g7qpO$reactstatelyutils = require("@react-stately/utils");
var $g7qpO$react = require("react");
function $parcel$export(e, n, v, s) {
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
}
$parcel$export(module.exports, "useToggleState", () => $d84f98b140466b44$export$8042c6c013fd5226);
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $d84f98b140466b44$export$8042c6c013fd5226(props = {}) {
let { isReadOnly: isReadOnly } = props;
// have to provide an empty function so useControlledState doesn't throw a fit
// can't use useControlledState's prop calling because we need the event object from the change
let [isSelected, setSelected] = (0, $g7qpO$reactstatelyutils.useControlledState)(props.isSelected, props.defaultSelected || false, props.onChange);
let [initialValue] = (0, $g7qpO$react.useState)(isSelected);
function updateSelected(value) {
if (!isReadOnly) setSelected(value);
}
function toggleState() {
if (!isReadOnly) setSelected(!isSelected);
}
var _props_defaultSelected;
return {
isSelected: isSelected,
defaultSelected: (_props_defaultSelected = props.defaultSelected) !== null && _props_defaultSelected !== void 0 ? _props_defaultSelected : initialValue,
setSelected: updateSelected,
toggle: toggleState
};
}
//# sourceMappingURL=useToggleState.main.js.map
{"mappings":";;;;;;;;;AAAA;;;;;;;;;;CAUC;;AAyBM,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;IACnH,IAAI,CAAC,aAAa,GAAG,CAAA,GAAA,qBAAO,EAAE;IAE9B,SAAS,eAAe,KAAK;QAC3B,IAAI,CAAC,YACH,YAAY;IAEhB;IAEA,SAAS;QACP,IAAI,CAAC,YACH,YAAY,CAAC;IAEjB;QAImB;IAFnB,OAAO;oBACL;QACA,iBAAiB,CAAA,yBAAA,MAAM,eAAe,cAArB,oCAAA,yBAAyB;QAC1C,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';\nimport {useState} from 'react';\n\nexport type {ToggleStateOptions};\n\nexport interface ToggleState {\n /** Whether the toggle is selected. */\n readonly isSelected: boolean,\n\n /** Whether the toggle is selected by default. */\n readonly defaultSelected: 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 let [initialValue] = useState(isSelected);\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 defaultSelected: props.defaultSelected ?? initialValue,\n setSelected: updateSelected,\n toggle: toggleState\n };\n}\n"],"names":[],"version":3,"file":"useToggleState.main.js.map"}
import {useControlledState as $d8rik$useControlledState} from "@react-stately/utils";
import {useState as $d8rik$useState} from "react";
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $3017fa7ffdddec74$export$8042c6c013fd5226(props = {}) {
let { isReadOnly: isReadOnly } = props;
// have to provide an empty function so useControlledState doesn't throw a fit
// can't use useControlledState's prop calling because we need the event object from the change
let [isSelected, setSelected] = (0, $d8rik$useControlledState)(props.isSelected, props.defaultSelected || false, props.onChange);
let [initialValue] = (0, $d8rik$useState)(isSelected);
function updateSelected(value) {
if (!isReadOnly) setSelected(value);
}
function toggleState() {
if (!isReadOnly) setSelected(!isSelected);
}
var _props_defaultSelected;
return {
isSelected: isSelected,
defaultSelected: (_props_defaultSelected = props.defaultSelected) !== null && _props_defaultSelected !== void 0 ? _props_defaultSelected : initialValue,
setSelected: updateSelected,
toggle: toggleState
};
}
export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState};
//# sourceMappingURL=useToggleState.module.js.map
import {useControlledState as $d8rik$useControlledState} from "@react-stately/utils";
import {useState as $d8rik$useState} from "react";
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
function $3017fa7ffdddec74$export$8042c6c013fd5226(props = {}) {
let { isReadOnly: isReadOnly } = props;
// have to provide an empty function so useControlledState doesn't throw a fit
// can't use useControlledState's prop calling because we need the event object from the change
let [isSelected, setSelected] = (0, $d8rik$useControlledState)(props.isSelected, props.defaultSelected || false, props.onChange);
let [initialValue] = (0, $d8rik$useState)(isSelected);
function updateSelected(value) {
if (!isReadOnly) setSelected(value);
}
function toggleState() {
if (!isReadOnly) setSelected(!isSelected);
}
var _props_defaultSelected;
return {
isSelected: isSelected,
defaultSelected: (_props_defaultSelected = props.defaultSelected) !== null && _props_defaultSelected !== void 0 ? _props_defaultSelected : initialValue,
setSelected: updateSelected,
toggle: toggleState
};
}
export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState};
//# sourceMappingURL=useToggleState.module.js.map
{"mappings":";;;AAAA;;;;;;;;;;CAUC;;AAyBM,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;IACnH,IAAI,CAAC,aAAa,GAAG,CAAA,GAAA,eAAO,EAAE;IAE9B,SAAS,eAAe,KAAK;QAC3B,IAAI,CAAC,YACH,YAAY;IAEhB;IAEA,SAAS;QACP,IAAI,CAAC,YACH,YAAY,CAAC;IAEjB;QAImB;IAFnB,OAAO;oBACL;QACA,iBAAiB,CAAA,yBAAA,MAAM,eAAe,cAArB,oCAAA,yBAAyB;QAC1C,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';\nimport {useState} from 'react';\n\nexport type {ToggleStateOptions};\n\nexport interface ToggleState {\n /** Whether the toggle is selected. */\n readonly isSelected: boolean,\n\n /** Whether the toggle is selected by default. */\n readonly defaultSelected: 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 let [initialValue] = useState(isSelected);\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 defaultSelected: props.defaultSelected ?? initialValue,\n setSelected: updateSelected,\n toggle: toggleState\n };\n}\n"],"names":[],"version":3,"file":"useToggleState.module.js.map"}
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {Key} from '@react-types/shared';
import {useControlledState} from '@react-stately/utils';
import {useMemo} from 'react';
export interface ToggleGroupProps {
/**
* Whether single or multiple selection is enabled.
* @default 'single'
*/
selectionMode?: 'single' | 'multiple',
/** Whether the collection allows empty selection. */
disallowEmptySelection?: boolean,
/** The currently selected keys in the collection (controlled). */
selectedKeys?: Iterable<Key>,
/** The initial selected keys in the collection (uncontrolled). */
defaultSelectedKeys?: Iterable<Key>,
/** Handler that is called when the selection changes. */
onSelectionChange?: (keys: Set<Key>) => void,
/** Whether all items are disabled. */
isDisabled?: boolean
}
export interface ToggleGroupState {
/** Whether single or multiple selection is enabled. */
readonly selectionMode: 'single' | 'multiple',
/** Whether all items are disabled. */
readonly isDisabled: boolean,
/** A set of keys for items that are selected. */
readonly selectedKeys: Set<Key>,
/** Toggles the selected state for an item by its key. */
toggleKey(key: Key): void,
/** Sets whether the given key is selected. */
setSelected(key: Key, isSelected: boolean): void,
/** Replaces the set of selected keys. */
setSelectedKeys(keys: Set<Key>): void
}
/**
* Manages state for a group of toggles.
* It supports both single and multiple selected items.
*/
export function useToggleGroupState(props: ToggleGroupProps): ToggleGroupState {
let {selectionMode = 'single', disallowEmptySelection, isDisabled = false} = props;
let [selectedKeys, setSelectedKeys] = useControlledState(
useMemo(() => props.selectedKeys ? new Set(props.selectedKeys) : undefined, [props.selectedKeys]),
useMemo(() => props.defaultSelectedKeys ? new Set(props.defaultSelectedKeys) : new Set(), [props.defaultSelectedKeys]),
props.onSelectionChange
);
return {
selectionMode,
isDisabled,
selectedKeys,
setSelectedKeys,
toggleKey(key) {
let keys: Set<Key>;
if (selectionMode === 'multiple') {
keys = new Set(selectedKeys);
if (keys.has(key) && (!disallowEmptySelection || keys.size > 1)) {
keys.delete(key);
} else {
keys.add(key);
}
} else {
keys = new Set(selectedKeys.has(key) && !disallowEmptySelection ? [] : [key]);
}
setSelectedKeys(keys);
},
setSelected(key, isSelected) {
if (isSelected !== selectedKeys.has(key)) {
this.toggleKey(key);
}
}
};
}
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {ToggleStateOptions} from '@react-types/checkbox';
import {useControlledState} from '@react-stately/utils';
import {useState} from 'react';
export type {ToggleStateOptions};
export interface ToggleState {
/** Whether the toggle is selected. */
readonly isSelected: boolean,
/** Whether the toggle is selected by default. */
readonly defaultSelected: boolean,
/** Updates selection state. */
setSelected(isSelected: boolean): void,
/** Toggle the selection state. */
toggle(): void
}
/**
* Provides state management for toggle components like checkboxes and switches.
*/
export function useToggleState(props: ToggleStateOptions = {}): ToggleState {
let {isReadOnly} = props;
// have to provide an empty function so useControlledState doesn't throw a fit
// can't use useControlledState's prop calling because we need the event object from the change
let [isSelected, setSelected] = useControlledState(props.isSelected, props.defaultSelected || false, props.onChange);
let [initialValue] = useState(isSelected);
function updateSelected(value) {
if (!isReadOnly) {
setSelected(value);
}
}
function toggleState() {
if (!isReadOnly) {
setSelected(!isSelected);
}
}
return {
isSelected,
defaultSelected: props.defaultSelected ?? initialValue,
setSelected: updateSelected,
toggle: toggleState
};
}