@react-stately/selection 3.0.0-nightly.2843 → 3.0.0-nightly.2848

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.
@@ -0,0 +1,96 @@
1
+ import {Selection as $e40ea825a81a3709$export$52baac22726c72bf} from "./Selection.mjs";
2
+ import {useControlledState as $6tM1y$useControlledState} from "@react-stately/utils";
3
+ import {useRef as $6tM1y$useRef, useState as $6tM1y$useState, useMemo as $6tM1y$useMemo, useEffect as $6tM1y$useEffect} from "react";
4
+
5
+ /*
6
+ * Copyright 2020 Adobe. All rights reserved.
7
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License. You may obtain a copy
9
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software distributed under
12
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
13
+ * OF ANY KIND, either express or implied. See the License for the specific language
14
+ * governing permissions and limitations under the License.
15
+ */
16
+
17
+
18
+ function $7af3f5b51489e0b5$var$equalSets(setA, setB) {
19
+ if (setA.size !== setB.size) return false;
20
+ for (let item of setA){
21
+ if (!setB.has(item)) return false;
22
+ }
23
+ return true;
24
+ }
25
+ function $7af3f5b51489e0b5$export$253fe78d46329472(props) {
26
+ let { selectionMode: selectionMode = "none", disallowEmptySelection: disallowEmptySelection, allowDuplicateSelectionEvents: allowDuplicateSelectionEvents, selectionBehavior: selectionBehaviorProp = "toggle", disabledBehavior: disabledBehavior = "all" } = props;
27
+ // We want synchronous updates to `isFocused` and `focusedKey` after their setters are called.
28
+ // But we also need to trigger a react re-render. So, we have both a ref (sync) and state (async).
29
+ let isFocusedRef = (0, $6tM1y$useRef)(false);
30
+ let [, setFocused] = (0, $6tM1y$useState)(false);
31
+ let focusedKeyRef = (0, $6tM1y$useRef)(null);
32
+ let childFocusStrategyRef = (0, $6tM1y$useRef)(null);
33
+ let [, setFocusedKey] = (0, $6tM1y$useState)(null);
34
+ let selectedKeysProp = (0, $6tM1y$useMemo)(()=>$7af3f5b51489e0b5$var$convertSelection(props.selectedKeys), [
35
+ props.selectedKeys
36
+ ]);
37
+ let defaultSelectedKeys = (0, $6tM1y$useMemo)(()=>$7af3f5b51489e0b5$var$convertSelection(props.defaultSelectedKeys, new (0, $e40ea825a81a3709$export$52baac22726c72bf)()), [
38
+ props.defaultSelectedKeys
39
+ ]);
40
+ let [selectedKeys, setSelectedKeys] = (0, $6tM1y$useControlledState)(selectedKeysProp, defaultSelectedKeys, props.onSelectionChange);
41
+ let disabledKeysProp = (0, $6tM1y$useMemo)(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [
42
+ props.disabledKeys
43
+ ]);
44
+ let [selectionBehavior, setSelectionBehavior] = (0, $6tM1y$useState)(selectionBehaviorProp);
45
+ // If the selectionBehavior prop is set to replace, but the current state is toggle (e.g. due to long press
46
+ // to enter selection mode on touch), and the selection becomes empty, reset the selection behavior.
47
+ if (selectionBehaviorProp === "replace" && selectionBehavior === "toggle" && typeof selectedKeys === "object" && selectedKeys.size === 0) setSelectionBehavior("replace");
48
+ // If the selectionBehavior prop changes, update the state as well.
49
+ let lastSelectionBehavior = (0, $6tM1y$useRef)(selectionBehaviorProp);
50
+ (0, $6tM1y$useEffect)(()=>{
51
+ if (selectionBehaviorProp !== lastSelectionBehavior.current) {
52
+ setSelectionBehavior(selectionBehaviorProp);
53
+ lastSelectionBehavior.current = selectionBehaviorProp;
54
+ }
55
+ }, [
56
+ selectionBehaviorProp
57
+ ]);
58
+ return {
59
+ selectionMode: selectionMode,
60
+ disallowEmptySelection: disallowEmptySelection,
61
+ selectionBehavior: selectionBehavior,
62
+ setSelectionBehavior: setSelectionBehavior,
63
+ get isFocused () {
64
+ return isFocusedRef.current;
65
+ },
66
+ setFocused (f) {
67
+ isFocusedRef.current = f;
68
+ setFocused(f);
69
+ },
70
+ get focusedKey () {
71
+ return focusedKeyRef.current;
72
+ },
73
+ get childFocusStrategy () {
74
+ return childFocusStrategyRef.current;
75
+ },
76
+ setFocusedKey (k, childFocusStrategy = "first") {
77
+ focusedKeyRef.current = k;
78
+ childFocusStrategyRef.current = childFocusStrategy;
79
+ setFocusedKey(k);
80
+ },
81
+ selectedKeys: selectedKeys,
82
+ setSelectedKeys (keys) {
83
+ if (allowDuplicateSelectionEvents || !$7af3f5b51489e0b5$var$equalSets(keys, selectedKeys)) setSelectedKeys(keys);
84
+ },
85
+ disabledKeys: disabledKeysProp,
86
+ disabledBehavior: disabledBehavior
87
+ };
88
+ }
89
+ function $7af3f5b51489e0b5$var$convertSelection(selection, defaultValue) {
90
+ if (!selection) return defaultValue;
91
+ return selection === "all" ? "all" : new (0, $e40ea825a81a3709$export$52baac22726c72bf)(selection);
92
+ }
93
+
94
+
95
+ export {$7af3f5b51489e0b5$export$253fe78d46329472 as useMultipleSelectionState};
96
+ //# sourceMappingURL=useMultipleSelectionState.mjs.map
@@ -0,0 +1,96 @@
1
+ import {Selection as $e40ea825a81a3709$export$52baac22726c72bf} from "./Selection.module.js";
2
+ import {useControlledState as $6tM1y$useControlledState} from "@react-stately/utils";
3
+ import {useRef as $6tM1y$useRef, useState as $6tM1y$useState, useMemo as $6tM1y$useMemo, useEffect as $6tM1y$useEffect} from "react";
4
+
5
+ /*
6
+ * Copyright 2020 Adobe. All rights reserved.
7
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License. You may obtain a copy
9
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software distributed under
12
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
13
+ * OF ANY KIND, either express or implied. See the License for the specific language
14
+ * governing permissions and limitations under the License.
15
+ */
16
+
17
+
18
+ function $7af3f5b51489e0b5$var$equalSets(setA, setB) {
19
+ if (setA.size !== setB.size) return false;
20
+ for (let item of setA){
21
+ if (!setB.has(item)) return false;
22
+ }
23
+ return true;
24
+ }
25
+ function $7af3f5b51489e0b5$export$253fe78d46329472(props) {
26
+ let { selectionMode: selectionMode = "none", disallowEmptySelection: disallowEmptySelection, allowDuplicateSelectionEvents: allowDuplicateSelectionEvents, selectionBehavior: selectionBehaviorProp = "toggle", disabledBehavior: disabledBehavior = "all" } = props;
27
+ // We want synchronous updates to `isFocused` and `focusedKey` after their setters are called.
28
+ // But we also need to trigger a react re-render. So, we have both a ref (sync) and state (async).
29
+ let isFocusedRef = (0, $6tM1y$useRef)(false);
30
+ let [, setFocused] = (0, $6tM1y$useState)(false);
31
+ let focusedKeyRef = (0, $6tM1y$useRef)(null);
32
+ let childFocusStrategyRef = (0, $6tM1y$useRef)(null);
33
+ let [, setFocusedKey] = (0, $6tM1y$useState)(null);
34
+ let selectedKeysProp = (0, $6tM1y$useMemo)(()=>$7af3f5b51489e0b5$var$convertSelection(props.selectedKeys), [
35
+ props.selectedKeys
36
+ ]);
37
+ let defaultSelectedKeys = (0, $6tM1y$useMemo)(()=>$7af3f5b51489e0b5$var$convertSelection(props.defaultSelectedKeys, new (0, $e40ea825a81a3709$export$52baac22726c72bf)()), [
38
+ props.defaultSelectedKeys
39
+ ]);
40
+ let [selectedKeys, setSelectedKeys] = (0, $6tM1y$useControlledState)(selectedKeysProp, defaultSelectedKeys, props.onSelectionChange);
41
+ let disabledKeysProp = (0, $6tM1y$useMemo)(()=>props.disabledKeys ? new Set(props.disabledKeys) : new Set(), [
42
+ props.disabledKeys
43
+ ]);
44
+ let [selectionBehavior, setSelectionBehavior] = (0, $6tM1y$useState)(selectionBehaviorProp);
45
+ // If the selectionBehavior prop is set to replace, but the current state is toggle (e.g. due to long press
46
+ // to enter selection mode on touch), and the selection becomes empty, reset the selection behavior.
47
+ if (selectionBehaviorProp === "replace" && selectionBehavior === "toggle" && typeof selectedKeys === "object" && selectedKeys.size === 0) setSelectionBehavior("replace");
48
+ // If the selectionBehavior prop changes, update the state as well.
49
+ let lastSelectionBehavior = (0, $6tM1y$useRef)(selectionBehaviorProp);
50
+ (0, $6tM1y$useEffect)(()=>{
51
+ if (selectionBehaviorProp !== lastSelectionBehavior.current) {
52
+ setSelectionBehavior(selectionBehaviorProp);
53
+ lastSelectionBehavior.current = selectionBehaviorProp;
54
+ }
55
+ }, [
56
+ selectionBehaviorProp
57
+ ]);
58
+ return {
59
+ selectionMode: selectionMode,
60
+ disallowEmptySelection: disallowEmptySelection,
61
+ selectionBehavior: selectionBehavior,
62
+ setSelectionBehavior: setSelectionBehavior,
63
+ get isFocused () {
64
+ return isFocusedRef.current;
65
+ },
66
+ setFocused (f) {
67
+ isFocusedRef.current = f;
68
+ setFocused(f);
69
+ },
70
+ get focusedKey () {
71
+ return focusedKeyRef.current;
72
+ },
73
+ get childFocusStrategy () {
74
+ return childFocusStrategyRef.current;
75
+ },
76
+ setFocusedKey (k, childFocusStrategy = "first") {
77
+ focusedKeyRef.current = k;
78
+ childFocusStrategyRef.current = childFocusStrategy;
79
+ setFocusedKey(k);
80
+ },
81
+ selectedKeys: selectedKeys,
82
+ setSelectedKeys (keys) {
83
+ if (allowDuplicateSelectionEvents || !$7af3f5b51489e0b5$var$equalSets(keys, selectedKeys)) setSelectedKeys(keys);
84
+ },
85
+ disabledKeys: disabledKeysProp,
86
+ disabledBehavior: disabledBehavior
87
+ };
88
+ }
89
+ function $7af3f5b51489e0b5$var$convertSelection(selection, defaultValue) {
90
+ if (!selection) return defaultValue;
91
+ return selection === "all" ? "all" : new (0, $e40ea825a81a3709$export$52baac22726c72bf)(selection);
92
+ }
93
+
94
+
95
+ export {$7af3f5b51489e0b5$export$253fe78d46329472 as useMultipleSelectionState};
96
+ //# sourceMappingURL=useMultipleSelectionState.module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;AAAA;;;;;;;;;;CAUC;;;AAQD,SAAS,gCAAU,IAAI,EAAE,IAAI;IAC3B,IAAI,KAAK,IAAI,KAAK,KAAK,IAAI,EACzB,OAAO;IAGT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AAE;IACnC,CAAA,GAAA,gBAAQ,EAAE;QACR,IAAI,0BAA0B,sBAAsB,OAAO,EAAE;YAC3D,qBAAqB;YACrB,sBAAsB,OAAO,GAAG;QAClC;IACF,GAAG;QAAC;KAAsB;IAE1B,OAAO;uBACL;gCACA;2BACA;8BACA;QACA,IAAI,aAAY;YACd,OAAO,aAAa,OAAO;QAC7B;QACA,YAAW,CAAC;YACV,aAAa,OAAO,GAAG;YACvB,WAAW;QACb;QACA,IAAI,cAAa;YACf,OAAO,cAAc,OAAO;QAC9B;QACA,IAAI,sBAAqB;YACvB,OAAO,sBAAsB,OAAO;QACtC;QACA,eAAc,CAAC,EAAE,qBAAqB,OAAO;YAC3C,cAAc,OAAO,GAAG;YACxB,sBAAsB,OAAO,GAAG;YAChC,cAAc;QAChB;sBACA;QACA,iBAAgB,IAAI;YAClB,IAAI,iCAAiC,CAAC,gCAAU,MAAM,eACpD,gBAAgB;QAEpB;QACA,cAAc;0BACd;IACF;AACF;AAEA,SAAS,uCAAiB,SAAgC,EAAE,YAAwB;IAClF,IAAI,CAAC,WACH,OAAO;IAGT,OAAO,cAAc,QACjB,QACA,IAAI,CAAA,GAAA,yCAAQ,EAAE;AACpB","sources":["packages/@react-stately/selection/src/useMultipleSelectionState.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 {DisabledBehavior, Key, MultipleSelection, SelectionBehavior, SelectionMode} from '@react-types/shared';\nimport {MultipleSelectionState} from './types';\nimport {Selection} from './Selection';\nimport {useControlledState} from '@react-stately/utils';\nimport {useEffect, useMemo, useRef, useState} from 'react';\n\nfunction equalSets(setA, setB) {\n if (setA.size !== setB.size) {\n return false;\n }\n\n for (let item of setA) {\n if (!setB.has(item)) {\n return false;\n }\n }\n\n return true;\n}\n\nexport interface MultipleSelectionStateProps extends MultipleSelection {\n /** How multiple selection should behave in the collection. */\n selectionBehavior?: SelectionBehavior,\n /** Whether onSelectionChange should fire even if the new set of keys is the same as the last. */\n allowDuplicateSelectionEvents?: boolean,\n /** Whether `disabledKeys` applies to all interactions, or only selection. */\n disabledBehavior?: DisabledBehavior\n}\n\n/**\n * Manages state for multiple selection and focus in a collection.\n */\nexport function useMultipleSelectionState(props: MultipleSelectionStateProps): MultipleSelectionState {\n let {\n selectionMode = 'none' as SelectionMode,\n disallowEmptySelection,\n allowDuplicateSelectionEvents,\n selectionBehavior: selectionBehaviorProp = 'toggle',\n disabledBehavior = 'all'\n } = props;\n\n // We want synchronous updates to `isFocused` and `focusedKey` after their setters are called.\n // But we also need to trigger a react re-render. So, we have both a ref (sync) and state (async).\n let isFocusedRef = useRef(false);\n let [, setFocused] = useState(false);\n let focusedKeyRef = useRef(null);\n let childFocusStrategyRef = useRef(null);\n let [, setFocusedKey] = useState(null);\n let selectedKeysProp = useMemo(() => convertSelection(props.selectedKeys), [props.selectedKeys]);\n let defaultSelectedKeys = useMemo(() => convertSelection(props.defaultSelectedKeys, new Selection()), [props.defaultSelectedKeys]);\n let [selectedKeys, setSelectedKeys] = useControlledState(\n selectedKeysProp,\n defaultSelectedKeys,\n props.onSelectionChange\n );\n let disabledKeysProp = useMemo(() =>\n props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()\n , [props.disabledKeys]);\n let [selectionBehavior, setSelectionBehavior] = useState(selectionBehaviorProp);\n\n // If the selectionBehavior prop is set to replace, but the current state is toggle (e.g. due to long press\n // to enter selection mode on touch), and the selection becomes empty, reset the selection behavior.\n if (selectionBehaviorProp === 'replace' && selectionBehavior === 'toggle' && typeof selectedKeys === 'object' && selectedKeys.size === 0) {\n setSelectionBehavior('replace');\n }\n\n // If the selectionBehavior prop changes, update the state as well.\n let lastSelectionBehavior = useRef(selectionBehaviorProp);\n useEffect(() => {\n if (selectionBehaviorProp !== lastSelectionBehavior.current) {\n setSelectionBehavior(selectionBehaviorProp);\n lastSelectionBehavior.current = selectionBehaviorProp;\n }\n }, [selectionBehaviorProp]);\n\n return {\n selectionMode,\n disallowEmptySelection,\n selectionBehavior,\n setSelectionBehavior,\n get isFocused() {\n return isFocusedRef.current;\n },\n setFocused(f) {\n isFocusedRef.current = f;\n setFocused(f);\n },\n get focusedKey() {\n return focusedKeyRef.current;\n },\n get childFocusStrategy() {\n return childFocusStrategyRef.current;\n },\n setFocusedKey(k, childFocusStrategy = 'first') {\n focusedKeyRef.current = k;\n childFocusStrategyRef.current = childFocusStrategy;\n setFocusedKey(k);\n },\n selectedKeys,\n setSelectedKeys(keys) {\n if (allowDuplicateSelectionEvents || !equalSets(keys, selectedKeys)) {\n setSelectedKeys(keys);\n }\n },\n disabledKeys: disabledKeysProp,\n disabledBehavior\n };\n}\n\nfunction convertSelection(selection: 'all' | Iterable<Key>, defaultValue?: Selection): 'all' | Set<Key> {\n if (!selection) {\n return defaultValue;\n }\n\n return selection === 'all'\n ? 'all'\n : new Selection(selection);\n}\n"],"names":[],"version":3,"file":"useMultipleSelectionState.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/selection",
3
- "version": "3.0.0-nightly.2843+81162ea39",
3
+ "version": "3.0.0-nightly.2848+72775d272",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -22,9 +22,9 @@
22
22
  "url": "https://github.com/adobe/react-spectrum"
23
23
  },
24
24
  "dependencies": {
25
- "@react-stately/collections": "3.0.0-nightly.2843+81162ea39",
26
- "@react-stately/utils": "3.0.0-nightly.2843+81162ea39",
27
- "@react-types/shared": "3.0.0-nightly.2843+81162ea39",
25
+ "@react-stately/collections": "3.0.0-nightly.2848+72775d272",
26
+ "@react-stately/utils": "3.0.0-nightly.2848+72775d272",
27
+ "@react-types/shared": "3.0.0-nightly.2848+72775d272",
28
28
  "@swc/helpers": "^0.5.0"
29
29
  },
30
30
  "peerDependencies": {
@@ -33,5 +33,5 @@
33
33
  "publishConfig": {
34
34
  "access": "public"
35
35
  },
36
- "gitHead": "81162ea392926d06ec3d25c228e6f9b6b5479d59"
36
+ "gitHead": "72775d272be04cfa32d26ec821bc68513d92a72b"
37
37
  }