@react-stately/toggle 3.0.0-nightly-641446f65-240905
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/README.md +3 -0
- package/dist/import.mjs +17 -0
- package/dist/main.js +22 -0
- package/dist/main.js.map +1 -0
- package/dist/module.js +17 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/useToggleState.main.js +39 -0
- package/dist/useToggleState.main.js.map +1 -0
- package/dist/useToggleState.mjs +34 -0
- package/dist/useToggleState.module.js +34 -0
- package/dist/useToggleState.module.js.map +1 -0
- package/package.json +36 -0
- package/src/index.ts +16 -0
- package/src/useToggleState.ts +56 -0
package/README.md
ADDED
package/dist/import.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {useToggleState as $3017fa7ffdddec74$export$8042c6c013fd5226} from "./useToggleState.mjs";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState};
|
|
17
|
+
//# sourceMappingURL=module.js.map
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
var $d84f98b140466b44$exports = require("./useToggleState.main.js");
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function $parcel$export(e, n, v, s) {
|
|
5
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
$parcel$export(module.exports, "useToggleState", () => $d84f98b140466b44$exports.useToggleState);
|
|
9
|
+
/*
|
|
10
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
11
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
13
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
16
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
17
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
18
|
+
* governing permissions and limitations under the License.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +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"}
|
package/dist/module.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {useToggleState as $3017fa7ffdddec74$export$8042c6c013fd5226} from "./useToggleState.module.js";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState};
|
|
17
|
+
//# sourceMappingURL=module.js.map
|
|
@@ -0,0 +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"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ToggleStateOptions } from "@react-types/checkbox";
|
|
2
|
+
export { ToggleStateOptions };
|
|
3
|
+
export interface ToggleState {
|
|
4
|
+
/** Whether the toggle is selected. */
|
|
5
|
+
readonly isSelected: boolean;
|
|
6
|
+
/** Updates selection state. */
|
|
7
|
+
setSelected(isSelected: boolean): void;
|
|
8
|
+
/** Toggle the selection state. */
|
|
9
|
+
toggle(): void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Provides state management for toggle components like checkboxes and switches.
|
|
13
|
+
*/
|
|
14
|
+
export function useToggleState(props?: ToggleStateOptions): ToggleState;
|
|
15
|
+
export type { ToggleProps } from '@react-types/checkbox';
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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;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"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
var $g7qpO$reactstatelyutils = require("@react-stately/utils");
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function $parcel$export(e, n, v, s) {
|
|
5
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
$parcel$export(module.exports, "useToggleState", () => $d84f98b140466b44$export$8042c6c013fd5226);
|
|
9
|
+
/*
|
|
10
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
11
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
13
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
16
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
17
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
18
|
+
* governing permissions and limitations under the License.
|
|
19
|
+
*/
|
|
20
|
+
function $d84f98b140466b44$export$8042c6c013fd5226(props = {}) {
|
|
21
|
+
let { isReadOnly: isReadOnly } = props;
|
|
22
|
+
// have to provide an empty function so useControlledState doesn't throw a fit
|
|
23
|
+
// can't use useControlledState's prop calling because we need the event object from the change
|
|
24
|
+
let [isSelected, setSelected] = (0, $g7qpO$reactstatelyutils.useControlledState)(props.isSelected, props.defaultSelected || false, props.onChange);
|
|
25
|
+
function updateSelected(value) {
|
|
26
|
+
if (!isReadOnly) setSelected(value);
|
|
27
|
+
}
|
|
28
|
+
function toggleState() {
|
|
29
|
+
if (!isReadOnly) setSelected(!isSelected);
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
isSelected: isSelected,
|
|
33
|
+
setSelected: updateSelected,
|
|
34
|
+
toggle: toggleState
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=useToggleState.main.js.map
|
|
@@ -0,0 +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 {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"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {useControlledState as $d8rik$useControlledState} from "@react-stately/utils";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
function $3017fa7ffdddec74$export$8042c6c013fd5226(props = {}) {
|
|
15
|
+
let { isReadOnly: isReadOnly } = props;
|
|
16
|
+
// have to provide an empty function so useControlledState doesn't throw a fit
|
|
17
|
+
// can't use useControlledState's prop calling because we need the event object from the change
|
|
18
|
+
let [isSelected, setSelected] = (0, $d8rik$useControlledState)(props.isSelected, props.defaultSelected || false, props.onChange);
|
|
19
|
+
function updateSelected(value) {
|
|
20
|
+
if (!isReadOnly) setSelected(value);
|
|
21
|
+
}
|
|
22
|
+
function toggleState() {
|
|
23
|
+
if (!isReadOnly) setSelected(!isSelected);
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
isSelected: isSelected,
|
|
27
|
+
setSelected: updateSelected,
|
|
28
|
+
toggle: toggleState
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState};
|
|
34
|
+
//# sourceMappingURL=useToggleState.module.js.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {useControlledState as $d8rik$useControlledState} from "@react-stately/utils";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
function $3017fa7ffdddec74$export$8042c6c013fd5226(props = {}) {
|
|
15
|
+
let { isReadOnly: isReadOnly } = props;
|
|
16
|
+
// have to provide an empty function so useControlledState doesn't throw a fit
|
|
17
|
+
// can't use useControlledState's prop calling because we need the event object from the change
|
|
18
|
+
let [isSelected, setSelected] = (0, $d8rik$useControlledState)(props.isSelected, props.defaultSelected || false, props.onChange);
|
|
19
|
+
function updateSelected(value) {
|
|
20
|
+
if (!isReadOnly) setSelected(value);
|
|
21
|
+
}
|
|
22
|
+
function toggleState() {
|
|
23
|
+
if (!isReadOnly) setSelected(!isSelected);
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
isSelected: isSelected,
|
|
27
|
+
setSelected: updateSelected,
|
|
28
|
+
toggle: toggleState
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
export {$3017fa7ffdddec74$export$8042c6c013fd5226 as useToggleState};
|
|
34
|
+
//# sourceMappingURL=useToggleState.module.js.map
|
|
@@ -0,0 +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 {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
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-stately/toggle",
|
|
3
|
+
"version": "3.0.0-nightly-641446f65-240905",
|
|
4
|
+
"description": "Spectrum UI components in React",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"main": "dist/main.js",
|
|
7
|
+
"module": "dist/module.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
"types": "./dist/types.d.ts",
|
|
10
|
+
"import": "./dist/import.mjs",
|
|
11
|
+
"require": "./dist/main.js"
|
|
12
|
+
},
|
|
13
|
+
"types": "dist/types.d.ts",
|
|
14
|
+
"source": "src/index.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/adobe/react-spectrum"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@react-stately/utils": "^3.0.0-nightly-641446f65-240905",
|
|
26
|
+
"@react-types/checkbox": "^3.0.0-nightly-641446f65-240905",
|
|
27
|
+
"@swc/helpers": "^0.5.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"stableVersion": "3.7.7"
|
|
36
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 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
|
+
export {useToggleState} from './useToggleState';
|
|
14
|
+
|
|
15
|
+
export type {ToggleProps} from '@react-types/checkbox';
|
|
16
|
+
export type {ToggleState, ToggleStateOptions} from './useToggleState';
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 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 {ToggleStateOptions} from '@react-types/checkbox';
|
|
14
|
+
import {useControlledState} from '@react-stately/utils';
|
|
15
|
+
|
|
16
|
+
export type {ToggleStateOptions};
|
|
17
|
+
|
|
18
|
+
export interface ToggleState {
|
|
19
|
+
/** Whether the toggle is selected. */
|
|
20
|
+
readonly isSelected: boolean,
|
|
21
|
+
|
|
22
|
+
/** Updates selection state. */
|
|
23
|
+
setSelected(isSelected: boolean): void,
|
|
24
|
+
|
|
25
|
+
/** Toggle the selection state. */
|
|
26
|
+
toggle(): void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Provides state management for toggle components like checkboxes and switches.
|
|
31
|
+
*/
|
|
32
|
+
export function useToggleState(props: ToggleStateOptions = {}): ToggleState {
|
|
33
|
+
let {isReadOnly} = props;
|
|
34
|
+
|
|
35
|
+
// have to provide an empty function so useControlledState doesn't throw a fit
|
|
36
|
+
// can't use useControlledState's prop calling because we need the event object from the change
|
|
37
|
+
let [isSelected, setSelected] = useControlledState(props.isSelected, props.defaultSelected || false, props.onChange);
|
|
38
|
+
|
|
39
|
+
function updateSelected(value) {
|
|
40
|
+
if (!isReadOnly) {
|
|
41
|
+
setSelected(value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function toggleState() {
|
|
46
|
+
if (!isReadOnly) {
|
|
47
|
+
setSelected(!isSelected);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
isSelected,
|
|
53
|
+
setSelected: updateSelected,
|
|
54
|
+
toggle: toggleState
|
|
55
|
+
};
|
|
56
|
+
}
|