@react-stately/select 3.9.2 → 3.10.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,4 @@
1
- import {useSelectState as $2bc3a590c5373a4e$export$5159ec8b34d4ec12} from "./useSelectState.mjs";
1
+ import {useSelectState as $2bc56025f8f4a612$re_export$useSelectState} from "react-stately/useSelectState";
2
2
 
3
3
  /*
4
4
  * Copyright 2020 Adobe. All rights reserved.
@@ -13,5 +13,5 @@ import {useSelectState as $2bc3a590c5373a4e$export$5159ec8b34d4ec12} from "./use
13
13
  */
14
14
 
15
15
 
16
- export {$2bc3a590c5373a4e$export$5159ec8b34d4ec12 as useSelectState};
16
+ export {$2bc56025f8f4a612$re_export$useSelectState as useSelectState};
17
17
  //# sourceMappingURL=module.js.map
package/dist/main.js CHANGED
@@ -1,11 +1,11 @@
1
- var $80ebb60e77198879$exports = require("./useSelectState.main.js");
1
+ var $gInje$reactstatelyuseSelectState = require("react-stately/useSelectState");
2
2
 
3
3
 
4
4
  function $parcel$export(e, n, v, s) {
5
5
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
6
6
  }
7
7
 
8
- $parcel$export(module.exports, "useSelectState", () => $80ebb60e77198879$exports.useSelectState);
8
+ $parcel$export(module.exports, "useSelectState", function () { return $gInje$reactstatelyuseSelectState.useSelectState; });
9
9
  /*
10
10
  * Copyright 2020 Adobe. All rights reserved.
11
11
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/select/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 {useSelectState} from './useSelectState';\n\nexport type {SelectProps} from '@react-types/select';\nexport type {SelectState, SelectStateOptions} from './useSelectState';\n"],"names":[],"version":3,"file":"main.js.map"}
1
+ {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/select/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 {useSelectState} from 'react-stately/useSelectState';\n\nexport type {SelectProps, SelectState, SelectStateOptions, SelectionMode, ValueType, ChangeValueType} from 'react-stately/useSelectState';\n"],"names":[],"version":3,"file":"main.js.map"}
package/dist/module.js CHANGED
@@ -1,4 +1,4 @@
1
- import {useSelectState as $2bc3a590c5373a4e$export$5159ec8b34d4ec12} from "./useSelectState.module.js";
1
+ import {useSelectState as $2bc56025f8f4a612$re_export$useSelectState} from "react-stately/useSelectState";
2
2
 
3
3
  /*
4
4
  * Copyright 2020 Adobe. All rights reserved.
@@ -13,5 +13,5 @@ import {useSelectState as $2bc3a590c5373a4e$export$5159ec8b34d4ec12} from "./use
13
13
  */
14
14
 
15
15
 
16
- export {$2bc3a590c5373a4e$export$5159ec8b34d4ec12 as useSelectState};
16
+ export {$2bc56025f8f4a612$re_export$useSelectState as useSelectState};
17
17
  //# sourceMappingURL=module.js.map
@@ -1 +1 @@
1
- {"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/select/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 {useSelectState} from './useSelectState';\n\nexport type {SelectProps} from '@react-types/select';\nexport type {SelectState, SelectStateOptions} from './useSelectState';\n"],"names":[],"version":3,"file":"module.js.map"}
1
+ {"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/select/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 {useSelectState} from 'react-stately/useSelectState';\n\nexport type {SelectProps, SelectState, SelectStateOptions, SelectionMode, ValueType, ChangeValueType} from 'react-stately/useSelectState';\n"],"names":[],"version":3,"file":"module.js.map"}
@@ -0,0 +1,2 @@
1
+ export { useSelectState } from 'react-stately/useSelectState';
2
+ export type { SelectProps, SelectState, SelectStateOptions, SelectionMode, ValueType, ChangeValueType } from 'react-stately/useSelectState';
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@react-stately/select",
3
- "version": "3.9.2",
3
+ "version": "3.10.0",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/module.js",
8
8
  "exports": {
9
- "source": "./src/index.ts",
10
- "types": [
11
- "./dist/types.d.ts",
12
- "./src/index.ts"
13
- ],
14
- "import": "./dist/import.mjs",
15
- "require": "./dist/main.js"
9
+ ".": {
10
+ "source": "./src/index.ts",
11
+ "types": "./dist/types/src/index.d.ts",
12
+ "import": "./dist/import.mjs",
13
+ "require": "./dist/main.js"
14
+ },
15
+ "./package.json": "./package.json"
16
16
  },
17
- "types": "dist/types.d.ts",
17
+ "types": "dist/types/src/index.d.ts",
18
18
  "source": "src/index.ts",
19
19
  "files": [
20
20
  "dist",
@@ -26,19 +26,18 @@
26
26
  "url": "https://github.com/adobe/react-spectrum"
27
27
  },
28
28
  "dependencies": {
29
- "@react-stately/form": "^3.2.4",
30
- "@react-stately/list": "^3.13.4",
31
- "@react-stately/overlays": "^3.6.23",
32
- "@react-stately/utils": "^3.11.0",
33
- "@react-types/select": "^3.12.2",
34
- "@react-types/shared": "^3.33.1",
35
- "@swc/helpers": "^0.5.0"
29
+ "@swc/helpers": "^0.5.0",
30
+ "react-stately": "3.46.0"
36
31
  },
37
32
  "peerDependencies": {
38
- "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
33
+ "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1",
34
+ "react-dom": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
39
35
  },
40
36
  "publishConfig": {
41
37
  "access": "public"
42
38
  },
43
- "gitHead": "8df187370053aa35f553cb388ad670f65e1ab371"
39
+ "targets": {
40
+ "types": false
41
+ },
42
+ "gitHead": "a6999bdf494a2e9c0381a5881908328bdd22ddae"
44
43
  }
package/src/index.ts CHANGED
@@ -10,7 +10,6 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export {useSelectState} from './useSelectState';
13
+ export {useSelectState} from 'react-stately/useSelectState';
14
14
 
15
- export type {SelectProps} from '@react-types/select';
16
- export type {SelectState, SelectStateOptions} from './useSelectState';
15
+ export type {SelectProps, SelectState, SelectStateOptions, SelectionMode, ValueType, ChangeValueType} from 'react-stately/useSelectState';
package/dist/types.d.ts DELETED
@@ -1,56 +0,0 @@
1
- import { CollectionStateBase, FocusStrategy, Key, Node } from "@react-types/shared";
2
- import { FormValidationState } from "@react-stately/form";
3
- import { ListState } from "@react-stately/list";
4
- import { OverlayTriggerState } from "@react-stately/overlays";
5
- import { SelectionMode, SelectProps, ValueType } from "@react-types/select";
6
- export interface SelectStateOptions<T, M extends SelectionMode = 'single'> extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {
7
- }
8
- export interface SelectState<T, M extends SelectionMode = 'single'> extends ListState<T>, OverlayTriggerState, FormValidationState {
9
- /**
10
- * The key for the first selected item.
11
- * @deprecated
12
- */
13
- readonly selectedKey: Key | null;
14
- /**
15
- * The default selected key.
16
- * @deprecated
17
- */
18
- readonly defaultSelectedKey: Key | null;
19
- /**
20
- * Sets the selected key.
21
- * @deprecated
22
- */
23
- setSelectedKey(key: Key | null): void;
24
- /** The current select value. */
25
- readonly value: ValueType<M>;
26
- /** The default select value. */
27
- readonly defaultValue: ValueType<M>;
28
- /** Sets the select value. */
29
- setValue(value: Key | readonly Key[] | null): void;
30
- /**
31
- * The value of the first selected item.
32
- * @deprecated
33
- */
34
- readonly selectedItem: Node<T> | null;
35
- /** The value of the selected items. */
36
- readonly selectedItems: Node<T>[];
37
- /** Whether the select is currently focused. */
38
- readonly isFocused: boolean;
39
- /** Sets whether the select is focused. */
40
- setFocused(isFocused: boolean): void;
41
- /** Controls which item will be auto focused when the menu opens. */
42
- readonly focusStrategy: FocusStrategy | null;
43
- /** Opens the menu. */
44
- open(focusStrategy?: FocusStrategy | null): void;
45
- /** Toggles the menu. */
46
- toggle(focusStrategy?: FocusStrategy | null): void;
47
- }
48
- /**
49
- * Provides state management for a select component. Handles building a collection
50
- * of items from props, handles the open state for the popup menu, and manages
51
- * multiple selection state.
52
- */
53
- export function useSelectState<T extends object, M extends SelectionMode = 'single'>(props: SelectStateOptions<T, M>): SelectState<T, M>;
54
- export type { SelectProps } from '@react-types/select';
55
-
56
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"mappings":";;;;;AAoBA,oCAAoC,CAAC,EAAE,CAAC,SAAS,aAAa,GAAG,QAAQ,CAAE,SAAQ,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,EAAE,UAAU,CAAC,EAAE,oBAAoB,CAAC,CAAC;CAAG;AAEjJ,6BAA6B,CAAC,EAAE,CAAC,SAAS,aAAa,GAAG,QAAQ,CAAE,SAAQ,UAAU,CAAC,CAAC,EAAE,mBAAmB,EAAE,mBAAmB;IAChI;;;OAGG;IACH,QAAQ,CAAC,WAAW,EAAE,GAAG,GAAG,IAAI,CAAC;IAEjC;;;OAGG;IACH,QAAQ,CAAC,kBAAkB,EAAE,GAAG,GAAG,IAAI,CAAC;IAExC;;;OAGG;IACH,cAAc,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;IAEtC,gCAAgC;IAChC,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC;IAE7B,gCAAgC;IAChC,QAAQ,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC,CAAC;IAEpC,6BAA6B;IAC7B,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,SAAS,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;IAEnD;;;OAGG;IACH,QAAQ,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;IAEtC,uCAAuC;IACvC,QAAQ,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC,EAAE,CAAC;IAElC,+CAA+C;IAC/C,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B,0CAA0C;IAC1C,UAAU,CAAC,SAAS,EAAE,OAAO,GAAG,IAAI,CAAC;IAErC,oEAAoE;IACpE,QAAQ,CAAC,aAAa,EAAE,aAAa,GAAG,IAAI,CAAC;IAE7C,sBAAsB;IACtB,IAAI,CAAC,aAAa,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,IAAI,CAAC;IAEjD,wBAAwB;IACxB,MAAM,CAAC,aAAa,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,IAAI,CAAA;CACnD;AAED;;;;GAIG;AACH,+BAA+B,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,aAAa,GAAG,QAAQ,EAAE,KAAK,EAAE,mBAAmB,CAAC,EAAE,CAAC,CAAC,GAAG,YAAY,CAAC,EAAE,CAAC,CAAC,CAkGvI;ACpKD,YAAY,EAAC,WAAW,EAAC,MAAM,qBAAqB,CAAC","sources":["packages/@react-stately/select/src/packages/@react-stately/select/src/useSelectState.ts","packages/@react-stately/select/src/packages/@react-stately/select/src/index.ts","packages/@react-stately/select/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 {useSelectState} from './useSelectState';\n\nexport type {SelectProps} from '@react-types/select';\nexport type {SelectState, SelectStateOptions} from './useSelectState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
@@ -1,143 +0,0 @@
1
- var $5KJtg$reactstatelyform = require("@react-stately/form");
2
- var $5KJtg$reactstatelylist = require("@react-stately/list");
3
- var $5KJtg$reactstatelyoverlays = require("@react-stately/overlays");
4
- var $5KJtg$reactstatelyutils = require("@react-stately/utils");
5
- var $5KJtg$react = require("react");
6
-
7
-
8
- function $parcel$export(e, n, v, s) {
9
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
10
- }
11
-
12
- $parcel$export(module.exports, "useSelectState", () => $80ebb60e77198879$export$5159ec8b34d4ec12);
13
- /*
14
- * Copyright 2020 Adobe. All rights reserved.
15
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
16
- * you may not use this file except in compliance with the License. You may obtain a copy
17
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
18
- *
19
- * Unless required by applicable law or agreed to in writing, software distributed under
20
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
21
- * OF ANY KIND, either express or implied. See the License for the specific language
22
- * governing permissions and limitations under the License.
23
- */
24
-
25
-
26
-
27
-
28
- function $80ebb60e77198879$export$5159ec8b34d4ec12(props) {
29
- let { selectionMode: selectionMode = 'single' } = props;
30
- let triggerState = (0, $5KJtg$reactstatelyoverlays.useOverlayTriggerState)(props);
31
- let [focusStrategy, setFocusStrategy] = (0, $5KJtg$react.useState)(null);
32
- let defaultValue = (0, $5KJtg$react.useMemo)(()=>{
33
- var _props_defaultSelectedKey;
34
- return props.defaultValue !== undefined ? props.defaultValue : selectionMode === 'single' ? (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : null : [];
35
- }, [
36
- props.defaultValue,
37
- props.defaultSelectedKey,
38
- selectionMode
39
- ]);
40
- let value = (0, $5KJtg$react.useMemo)(()=>{
41
- return props.value !== undefined ? props.value : selectionMode === 'single' ? props.selectedKey : undefined;
42
- }, [
43
- props.value,
44
- props.selectedKey,
45
- selectionMode
46
- ]);
47
- let [controlledValue, setControlledValue] = (0, $5KJtg$reactstatelyutils.useControlledState)(value, defaultValue, props.onChange);
48
- // Only display the first selected item if in single selection mode but the value is an array.
49
- let displayValue = selectionMode === 'single' && Array.isArray(controlledValue) ? controlledValue[0] : controlledValue;
50
- let setValue = (value)=>{
51
- if (selectionMode === 'single') {
52
- var _props_onSelectionChange;
53
- var _value_;
54
- let key = Array.isArray(value) ? (_value_ = value[0]) !== null && _value_ !== void 0 ? _value_ : null : value;
55
- setControlledValue(key);
56
- if (key !== displayValue) (_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, key);
57
- } else {
58
- let keys = [];
59
- if (Array.isArray(value)) keys = value;
60
- else if (value != null) keys = [
61
- value
62
- ];
63
- setControlledValue(keys);
64
- }
65
- };
66
- let listState = (0, $5KJtg$reactstatelylist.useListState)({
67
- ...props,
68
- selectionMode: selectionMode,
69
- disallowEmptySelection: selectionMode === 'single',
70
- allowDuplicateSelectionEvents: true,
71
- selectedKeys: (0, $5KJtg$react.useMemo)(()=>$80ebb60e77198879$var$convertValue(displayValue), [
72
- displayValue
73
- ]),
74
- onSelectionChange: (keys)=>{
75
- // impossible, but TS doesn't know that
76
- if (keys === 'all') return;
77
- if (selectionMode === 'single') {
78
- var _keys_values_next_value;
79
- let key = (_keys_values_next_value = keys.values().next().value) !== null && _keys_values_next_value !== void 0 ? _keys_values_next_value : null;
80
- setValue(key);
81
- triggerState.close();
82
- } else setValue([
83
- ...keys
84
- ]);
85
- validationState.commitValidation();
86
- }
87
- });
88
- let selectedKey = listState.selectionManager.firstSelectedKey;
89
- let selectedItems = (0, $5KJtg$react.useMemo)(()=>{
90
- return [
91
- ...listState.selectionManager.selectedKeys
92
- ].map((key)=>listState.collection.getItem(key)).filter((item)=>item != null);
93
- }, [
94
- listState.selectionManager.selectedKeys,
95
- listState.collection
96
- ]);
97
- let validationState = (0, $5KJtg$reactstatelyform.useFormValidationState)({
98
- ...props,
99
- value: Array.isArray(displayValue) && displayValue.length === 0 ? null : displayValue
100
- });
101
- let [isFocused, setFocused] = (0, $5KJtg$react.useState)(false);
102
- let [initialValue] = (0, $5KJtg$react.useState)(displayValue);
103
- var _selectedItems_, _props_defaultSelectedKey;
104
- return {
105
- ...validationState,
106
- ...listState,
107
- ...triggerState,
108
- value: displayValue,
109
- defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : initialValue,
110
- setValue: setValue,
111
- selectedKey: selectedKey,
112
- setSelectedKey: setValue,
113
- selectedItem: (_selectedItems_ = selectedItems[0]) !== null && _selectedItems_ !== void 0 ? _selectedItems_ : null,
114
- selectedItems: selectedItems,
115
- defaultSelectedKey: (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : props.selectionMode === 'single' ? initialValue : null,
116
- focusStrategy: focusStrategy,
117
- open (focusStrategy = null) {
118
- // Don't open if the collection is empty.
119
- if (listState.collection.size !== 0 || props.allowsEmptyCollection) {
120
- setFocusStrategy(focusStrategy);
121
- triggerState.open();
122
- }
123
- },
124
- toggle (focusStrategy = null) {
125
- if (listState.collection.size !== 0 || props.allowsEmptyCollection) {
126
- setFocusStrategy(focusStrategy);
127
- triggerState.toggle();
128
- }
129
- },
130
- isFocused: isFocused,
131
- setFocused: setFocused
132
- };
133
- }
134
- function $80ebb60e77198879$var$convertValue(value) {
135
- if (value === undefined) return undefined;
136
- if (value === null) return [];
137
- return Array.isArray(value) ? value : [
138
- value
139
- ];
140
- }
141
-
142
-
143
- //# sourceMappingURL=useSelectState.main.js.map
@@ -1 +0,0 @@
1
- {"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;AAsEM,SAAS,0CAAqE,KAA+B;IAClH,IAAI,iBAAC,gBAAgB,UAAc,GAAG;IACtC,IAAI,eAAe,CAAA,GAAA,kDAAqB,EAAE;IAC1C,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,qBAAO,EAAwB;IACvE,IAAI,eAAe,CAAA,GAAA,oBAAM,EAAE;YACoE;QAA7F,OAAO,MAAM,YAAY,KAAK,YAAY,MAAM,YAAY,GAAI,kBAAkB,WAAW,CAAA,4BAAA,MAAM,kBAAkB,cAAxB,uCAAA,4BAA4B,OAAO,EAAE;IACpI,GAAG;QAAC,MAAM,YAAY;QAAE,MAAM,kBAA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IACF;AACF;AAEA,SAAS,mCAAa,KAAqC;IACzD,IAAI,UAAU,WACZ,OAAO;IAET,IAAI,UAAU,MACZ,OAAO,EAAE;IAEX,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C","sources":["packages/@react-stately/select/src/useSelectState.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 {CollectionStateBase, FocusStrategy, Key, Node, Selection} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '@react-stately/form';\nimport {ListState, useListState} from '@react-stately/list';\nimport {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';\nimport {SelectionMode, SelectProps, ValueType} from '@react-types/select';\nimport {useControlledState} from '@react-stately/utils';\nimport {useMemo, useState} from 'react';\n\nexport interface SelectStateOptions<T, M extends SelectionMode = 'single'> extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T, M extends SelectionMode = 'single'> extends ListState<T>, OverlayTriggerState, FormValidationState {\n /**\n * The key for the first selected item.\n * @deprecated\n */\n readonly selectedKey: Key | null,\n\n /**\n * The default selected key.\n * @deprecated\n */\n readonly defaultSelectedKey: Key | null,\n\n /**\n * Sets the selected key.\n * @deprecated\n */\n setSelectedKey(key: Key | null): void,\n\n /** The current select value. */\n readonly value: ValueType<M>,\n\n /** The default select value. */\n readonly defaultValue: ValueType<M>,\n\n /** Sets the select value. */\n setValue(value: Key | readonly Key[] | null): void,\n\n /**\n * The value of the first selected item.\n * @deprecated\n */\n readonly selectedItem: Node<T> | null,\n\n /** The value of the selected items. */\n readonly selectedItems: Node<T>[],\n\n /** Whether the select is currently focused. */\n readonly isFocused: boolean,\n\n /** Sets whether the select is focused. */\n setFocused(isFocused: boolean): void,\n\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy | null,\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void,\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void\n}\n\n/**\n * Provides state management for a select component. Handles building a collection\n * of items from props, handles the open state for the popup menu, and manages\n * multiple selection state.\n */\nexport function useSelectState<T extends object, M extends SelectionMode = 'single'>(props: SelectStateOptions<T, M>): SelectState<T, M> {\n let {selectionMode = 'single' as M} = props;\n let triggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n let defaultValue = useMemo(() => {\n return props.defaultValue !== undefined ? props.defaultValue : (selectionMode === 'single' ? props.defaultSelectedKey ?? null : []) as ValueType<M>;\n }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);\n let value = useMemo(() => {\n return props.value !== undefined ? props.value : (selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>;\n }, [props.value, props.selectedKey, selectionMode]);\n let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(value, defaultValue, props.onChange as any);\n // Only display the first selected item if in single selection mode but the value is an array.\n let displayValue = selectionMode === 'single' && Array.isArray(controlledValue) ? controlledValue[0] : controlledValue;\n let setValue = (value: Key | Key[] | null) => {\n if (selectionMode === 'single') {\n let key = Array.isArray(value) ? value[0] ?? null : value;\n setControlledValue(key);\n if (key !== displayValue) {\n props.onSelectionChange?.(key);\n }\n } else {\n let keys: Key[] = [];\n if (Array.isArray(value)) {\n keys = value;\n } else if (value != null) {\n keys = [value];\n }\n\n setControlledValue(keys);\n }\n };\n\n let listState = useListState({\n ...props,\n selectionMode,\n disallowEmptySelection: selectionMode === 'single',\n allowDuplicateSelectionEvents: true,\n selectedKeys: useMemo(() => convertValue(displayValue), [displayValue]),\n onSelectionChange: (keys: Selection) => {\n // impossible, but TS doesn't know that\n if (keys === 'all') {\n return;\n }\n\n if (selectionMode === 'single') {\n let key = keys.values().next().value ?? null;\n setValue(key);\n triggerState.close();\n } else {\n setValue([...keys]);\n }\n\n validationState.commitValidation();\n }\n });\n\n let selectedKey = listState.selectionManager.firstSelectedKey;\n let selectedItems = useMemo(() => {\n return [...listState.selectionManager.selectedKeys].map(key => listState.collection.getItem(key)).filter(item => item != null);\n }, [listState.selectionManager.selectedKeys, listState.collection]);\n\n let validationState = useFormValidationState({\n ...props,\n value: Array.isArray(displayValue) && displayValue.length === 0 ? null : displayValue as any\n });\n\n let [isFocused, setFocused] = useState(false);\n let [initialValue] = useState(displayValue);\n\n return {\n ...validationState,\n ...listState,\n ...triggerState,\n value: displayValue as ValueType<M>,\n defaultValue: defaultValue ?? initialValue as ValueType<M>,\n setValue,\n selectedKey,\n setSelectedKey: setValue,\n selectedItem: selectedItems[0] ?? null,\n selectedItems,\n defaultSelectedKey: props.defaultSelectedKey ?? (props.selectionMode === 'single' ? initialValue as Key : null),\n focusStrategy,\n open(focusStrategy: FocusStrategy | null = null) {\n // Don't open if the collection is empty.\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.toggle();\n }\n },\n isFocused,\n setFocused\n };\n}\n\nfunction convertValue(value: Key | Key[] | null | undefined) {\n if (value === undefined) {\n return undefined;\n }\n if (value === null) {\n return [];\n }\n return Array.isArray(value) ? value : [value];\n}\n"],"names":[],"version":3,"file":"useSelectState.main.js.map"}
@@ -1,138 +0,0 @@
1
- import {useFormValidationState as $6FfNf$useFormValidationState} from "@react-stately/form";
2
- import {useListState as $6FfNf$useListState} from "@react-stately/list";
3
- import {useOverlayTriggerState as $6FfNf$useOverlayTriggerState} from "@react-stately/overlays";
4
- import {useControlledState as $6FfNf$useControlledState} from "@react-stately/utils";
5
- import {useState as $6FfNf$useState, useMemo as $6FfNf$useMemo} from "react";
6
-
7
- /*
8
- * Copyright 2020 Adobe. All rights reserved.
9
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
10
- * you may not use this file except in compliance with the License. You may obtain a copy
11
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software distributed under
14
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
15
- * OF ANY KIND, either express or implied. See the License for the specific language
16
- * governing permissions and limitations under the License.
17
- */
18
-
19
-
20
-
21
-
22
- function $2bc3a590c5373a4e$export$5159ec8b34d4ec12(props) {
23
- let { selectionMode: selectionMode = 'single' } = props;
24
- let triggerState = (0, $6FfNf$useOverlayTriggerState)(props);
25
- let [focusStrategy, setFocusStrategy] = (0, $6FfNf$useState)(null);
26
- let defaultValue = (0, $6FfNf$useMemo)(()=>{
27
- var _props_defaultSelectedKey;
28
- return props.defaultValue !== undefined ? props.defaultValue : selectionMode === 'single' ? (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : null : [];
29
- }, [
30
- props.defaultValue,
31
- props.defaultSelectedKey,
32
- selectionMode
33
- ]);
34
- let value = (0, $6FfNf$useMemo)(()=>{
35
- return props.value !== undefined ? props.value : selectionMode === 'single' ? props.selectedKey : undefined;
36
- }, [
37
- props.value,
38
- props.selectedKey,
39
- selectionMode
40
- ]);
41
- let [controlledValue, setControlledValue] = (0, $6FfNf$useControlledState)(value, defaultValue, props.onChange);
42
- // Only display the first selected item if in single selection mode but the value is an array.
43
- let displayValue = selectionMode === 'single' && Array.isArray(controlledValue) ? controlledValue[0] : controlledValue;
44
- let setValue = (value)=>{
45
- if (selectionMode === 'single') {
46
- var _props_onSelectionChange;
47
- var _value_;
48
- let key = Array.isArray(value) ? (_value_ = value[0]) !== null && _value_ !== void 0 ? _value_ : null : value;
49
- setControlledValue(key);
50
- if (key !== displayValue) (_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, key);
51
- } else {
52
- let keys = [];
53
- if (Array.isArray(value)) keys = value;
54
- else if (value != null) keys = [
55
- value
56
- ];
57
- setControlledValue(keys);
58
- }
59
- };
60
- let listState = (0, $6FfNf$useListState)({
61
- ...props,
62
- selectionMode: selectionMode,
63
- disallowEmptySelection: selectionMode === 'single',
64
- allowDuplicateSelectionEvents: true,
65
- selectedKeys: (0, $6FfNf$useMemo)(()=>$2bc3a590c5373a4e$var$convertValue(displayValue), [
66
- displayValue
67
- ]),
68
- onSelectionChange: (keys)=>{
69
- // impossible, but TS doesn't know that
70
- if (keys === 'all') return;
71
- if (selectionMode === 'single') {
72
- var _keys_values_next_value;
73
- let key = (_keys_values_next_value = keys.values().next().value) !== null && _keys_values_next_value !== void 0 ? _keys_values_next_value : null;
74
- setValue(key);
75
- triggerState.close();
76
- } else setValue([
77
- ...keys
78
- ]);
79
- validationState.commitValidation();
80
- }
81
- });
82
- let selectedKey = listState.selectionManager.firstSelectedKey;
83
- let selectedItems = (0, $6FfNf$useMemo)(()=>{
84
- return [
85
- ...listState.selectionManager.selectedKeys
86
- ].map((key)=>listState.collection.getItem(key)).filter((item)=>item != null);
87
- }, [
88
- listState.selectionManager.selectedKeys,
89
- listState.collection
90
- ]);
91
- let validationState = (0, $6FfNf$useFormValidationState)({
92
- ...props,
93
- value: Array.isArray(displayValue) && displayValue.length === 0 ? null : displayValue
94
- });
95
- let [isFocused, setFocused] = (0, $6FfNf$useState)(false);
96
- let [initialValue] = (0, $6FfNf$useState)(displayValue);
97
- var _selectedItems_, _props_defaultSelectedKey;
98
- return {
99
- ...validationState,
100
- ...listState,
101
- ...triggerState,
102
- value: displayValue,
103
- defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : initialValue,
104
- setValue: setValue,
105
- selectedKey: selectedKey,
106
- setSelectedKey: setValue,
107
- selectedItem: (_selectedItems_ = selectedItems[0]) !== null && _selectedItems_ !== void 0 ? _selectedItems_ : null,
108
- selectedItems: selectedItems,
109
- defaultSelectedKey: (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : props.selectionMode === 'single' ? initialValue : null,
110
- focusStrategy: focusStrategy,
111
- open (focusStrategy = null) {
112
- // Don't open if the collection is empty.
113
- if (listState.collection.size !== 0 || props.allowsEmptyCollection) {
114
- setFocusStrategy(focusStrategy);
115
- triggerState.open();
116
- }
117
- },
118
- toggle (focusStrategy = null) {
119
- if (listState.collection.size !== 0 || props.allowsEmptyCollection) {
120
- setFocusStrategy(focusStrategy);
121
- triggerState.toggle();
122
- }
123
- },
124
- isFocused: isFocused,
125
- setFocused: setFocused
126
- };
127
- }
128
- function $2bc3a590c5373a4e$var$convertValue(value) {
129
- if (value === undefined) return undefined;
130
- if (value === null) return [];
131
- return Array.isArray(value) ? value : [
132
- value
133
- ];
134
- }
135
-
136
-
137
- export {$2bc3a590c5373a4e$export$5159ec8b34d4ec12 as useSelectState};
138
- //# sourceMappingURL=useSelectState.module.js.map
@@ -1,138 +0,0 @@
1
- import {useFormValidationState as $6FfNf$useFormValidationState} from "@react-stately/form";
2
- import {useListState as $6FfNf$useListState} from "@react-stately/list";
3
- import {useOverlayTriggerState as $6FfNf$useOverlayTriggerState} from "@react-stately/overlays";
4
- import {useControlledState as $6FfNf$useControlledState} from "@react-stately/utils";
5
- import {useState as $6FfNf$useState, useMemo as $6FfNf$useMemo} from "react";
6
-
7
- /*
8
- * Copyright 2020 Adobe. All rights reserved.
9
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
10
- * you may not use this file except in compliance with the License. You may obtain a copy
11
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software distributed under
14
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
15
- * OF ANY KIND, either express or implied. See the License for the specific language
16
- * governing permissions and limitations under the License.
17
- */
18
-
19
-
20
-
21
-
22
- function $2bc3a590c5373a4e$export$5159ec8b34d4ec12(props) {
23
- let { selectionMode: selectionMode = 'single' } = props;
24
- let triggerState = (0, $6FfNf$useOverlayTriggerState)(props);
25
- let [focusStrategy, setFocusStrategy] = (0, $6FfNf$useState)(null);
26
- let defaultValue = (0, $6FfNf$useMemo)(()=>{
27
- var _props_defaultSelectedKey;
28
- return props.defaultValue !== undefined ? props.defaultValue : selectionMode === 'single' ? (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : null : [];
29
- }, [
30
- props.defaultValue,
31
- props.defaultSelectedKey,
32
- selectionMode
33
- ]);
34
- let value = (0, $6FfNf$useMemo)(()=>{
35
- return props.value !== undefined ? props.value : selectionMode === 'single' ? props.selectedKey : undefined;
36
- }, [
37
- props.value,
38
- props.selectedKey,
39
- selectionMode
40
- ]);
41
- let [controlledValue, setControlledValue] = (0, $6FfNf$useControlledState)(value, defaultValue, props.onChange);
42
- // Only display the first selected item if in single selection mode but the value is an array.
43
- let displayValue = selectionMode === 'single' && Array.isArray(controlledValue) ? controlledValue[0] : controlledValue;
44
- let setValue = (value)=>{
45
- if (selectionMode === 'single') {
46
- var _props_onSelectionChange;
47
- var _value_;
48
- let key = Array.isArray(value) ? (_value_ = value[0]) !== null && _value_ !== void 0 ? _value_ : null : value;
49
- setControlledValue(key);
50
- if (key !== displayValue) (_props_onSelectionChange = props.onSelectionChange) === null || _props_onSelectionChange === void 0 ? void 0 : _props_onSelectionChange.call(props, key);
51
- } else {
52
- let keys = [];
53
- if (Array.isArray(value)) keys = value;
54
- else if (value != null) keys = [
55
- value
56
- ];
57
- setControlledValue(keys);
58
- }
59
- };
60
- let listState = (0, $6FfNf$useListState)({
61
- ...props,
62
- selectionMode: selectionMode,
63
- disallowEmptySelection: selectionMode === 'single',
64
- allowDuplicateSelectionEvents: true,
65
- selectedKeys: (0, $6FfNf$useMemo)(()=>$2bc3a590c5373a4e$var$convertValue(displayValue), [
66
- displayValue
67
- ]),
68
- onSelectionChange: (keys)=>{
69
- // impossible, but TS doesn't know that
70
- if (keys === 'all') return;
71
- if (selectionMode === 'single') {
72
- var _keys_values_next_value;
73
- let key = (_keys_values_next_value = keys.values().next().value) !== null && _keys_values_next_value !== void 0 ? _keys_values_next_value : null;
74
- setValue(key);
75
- triggerState.close();
76
- } else setValue([
77
- ...keys
78
- ]);
79
- validationState.commitValidation();
80
- }
81
- });
82
- let selectedKey = listState.selectionManager.firstSelectedKey;
83
- let selectedItems = (0, $6FfNf$useMemo)(()=>{
84
- return [
85
- ...listState.selectionManager.selectedKeys
86
- ].map((key)=>listState.collection.getItem(key)).filter((item)=>item != null);
87
- }, [
88
- listState.selectionManager.selectedKeys,
89
- listState.collection
90
- ]);
91
- let validationState = (0, $6FfNf$useFormValidationState)({
92
- ...props,
93
- value: Array.isArray(displayValue) && displayValue.length === 0 ? null : displayValue
94
- });
95
- let [isFocused, setFocused] = (0, $6FfNf$useState)(false);
96
- let [initialValue] = (0, $6FfNf$useState)(displayValue);
97
- var _selectedItems_, _props_defaultSelectedKey;
98
- return {
99
- ...validationState,
100
- ...listState,
101
- ...triggerState,
102
- value: displayValue,
103
- defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : initialValue,
104
- setValue: setValue,
105
- selectedKey: selectedKey,
106
- setSelectedKey: setValue,
107
- selectedItem: (_selectedItems_ = selectedItems[0]) !== null && _selectedItems_ !== void 0 ? _selectedItems_ : null,
108
- selectedItems: selectedItems,
109
- defaultSelectedKey: (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : props.selectionMode === 'single' ? initialValue : null,
110
- focusStrategy: focusStrategy,
111
- open (focusStrategy = null) {
112
- // Don't open if the collection is empty.
113
- if (listState.collection.size !== 0 || props.allowsEmptyCollection) {
114
- setFocusStrategy(focusStrategy);
115
- triggerState.open();
116
- }
117
- },
118
- toggle (focusStrategy = null) {
119
- if (listState.collection.size !== 0 || props.allowsEmptyCollection) {
120
- setFocusStrategy(focusStrategy);
121
- triggerState.toggle();
122
- }
123
- },
124
- isFocused: isFocused,
125
- setFocused: setFocused
126
- };
127
- }
128
- function $2bc3a590c5373a4e$var$convertValue(value) {
129
- if (value === undefined) return undefined;
130
- if (value === null) return [];
131
- return Array.isArray(value) ? value : [
132
- value
133
- ];
134
- }
135
-
136
-
137
- export {$2bc3a590c5373a4e$export$5159ec8b34d4ec12 as useSelectState};
138
- //# sourceMappingURL=useSelectState.module.js.map
@@ -1 +0,0 @@
1
- {"mappings":";;;;;;AAAA;;;;;;;;;;CAUC;;;;;AAsEM,SAAS,0CAAqE,KAA+B;IAClH,IAAI,iBAAC,gBAAgB,UAAc,GAAG;IACtC,IAAI,eAAe,CAAA,GAAA,6BAAqB,EAAE;IAC1C,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,eAAO,EAAwB;IACvE,IAAI,eAAe,CAAA,GAAA,cAAM,EAAE;YACoE;QAA7F,OAAO,MAAM,YAAY,KAAK,YAAY,MAAM,YAAY,GAAI,kBAAkB,WAAW,CAAA,4BAAA,MAAM,kBAAkB,cAAxB,uCAAA,4BAA4B,OAAO,EAAE;IACpI,GAAG;QAAC,MAAM,YAAY;QAAE,MAAM,kBAAkB;QAAE;KAAc;IAChE,IAAI,QAAQ,CAAA,GAAA,cAAM,EAAE;QAClB,OAAO,MAAM,KAAK,KAAK,YAAY,MAAM,KAAK,GAAI,kBAAkB,WAAW,MAAM,WAAW,GAAG;IACrG,GAAG;QAAC,MAAM,KAAK;QAAE,MAAM,WAAW;QAAE;KAAc;IAClD,IAAI,CAAC,iBAAiB,mBAAmB,GAAG,CAAA,GAAA,yBAAiB,EAA+B,OAAO,cAAc,MAAM,QAAQ;IAC/H,8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AY;QAAE,UAAU,UAAU;KAAC;IAElE,IAAI,kBAAkB,CAAA,GAAA,6BAAqB,EAAE;QAC3C,GAAG,KAAK;QACR,OAAO,MAAM,OAAO,CAAC,iBAAiB,aAAa,MAAM,KAAK,IAAI,OAAO;IAC3E;IAEA,IAAI,CAAC,WAAW,WAAW,GAAG,CAAA,GAAA,eAAO,EAAE;IACvC,IAAI,CAAC,aAAa,GAAG,CAAA,GAAA,eAAO,EAAE;QAWd,iBAEM;IAXtB,OAAO;QACL,GAAG,eAAe;QAClB,GAAG,SAAS;QACZ,GAAG,YAAY;QACf,OAAO;QACP,cAAc,yBAAA,0BAAA,eAAgB;kBAC9B;qBACA;QACA,gBAAgB;QAChB,cAAc,CAAA,kBAAA,aAAa,CAAC,EAAE,cAAhB,6BAAA,kBAAoB;uBAClC;QACA,oBAAoB,CAAA,4BAAA,MAAM,kBAAkB,cAAxB,uCAAA,4BAA6B,MAAM,aAAa,KAAK,WAAW,eAAsB;uBAC1G;QACA,MAAK,gBAAsC,IAAI;YAC7C,yCAAyC;YACzC,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,KAAK,MAAM,qBAAqB,EAAE;gBAClE,iBAAiB;gBACjB,aAAa,IAAI;YACnB;QACF;QACA,QAAO,gBAAsC,IAAI;YAC/C,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,KAAK,MAAM,qBAAqB,EAAE;gBAClE,iBAAiB;gBACjB,aAAa,MAAM;YACrB;QACF;mBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAa,KAAqC;IACzD,IAAI,UAAU,WACZ,OAAO;IAET,IAAI,UAAU,MACZ,OAAO,EAAE;IAEX,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C","sources":["packages/@react-stately/select/src/useSelectState.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 {CollectionStateBase, FocusStrategy, Key, Node, Selection} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '@react-stately/form';\nimport {ListState, useListState} from '@react-stately/list';\nimport {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';\nimport {SelectionMode, SelectProps, ValueType} from '@react-types/select';\nimport {useControlledState} from '@react-stately/utils';\nimport {useMemo, useState} from 'react';\n\nexport interface SelectStateOptions<T, M extends SelectionMode = 'single'> extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T, M extends SelectionMode = 'single'> extends ListState<T>, OverlayTriggerState, FormValidationState {\n /**\n * The key for the first selected item.\n * @deprecated\n */\n readonly selectedKey: Key | null,\n\n /**\n * The default selected key.\n * @deprecated\n */\n readonly defaultSelectedKey: Key | null,\n\n /**\n * Sets the selected key.\n * @deprecated\n */\n setSelectedKey(key: Key | null): void,\n\n /** The current select value. */\n readonly value: ValueType<M>,\n\n /** The default select value. */\n readonly defaultValue: ValueType<M>,\n\n /** Sets the select value. */\n setValue(value: Key | readonly Key[] | null): void,\n\n /**\n * The value of the first selected item.\n * @deprecated\n */\n readonly selectedItem: Node<T> | null,\n\n /** The value of the selected items. */\n readonly selectedItems: Node<T>[],\n\n /** Whether the select is currently focused. */\n readonly isFocused: boolean,\n\n /** Sets whether the select is focused. */\n setFocused(isFocused: boolean): void,\n\n /** Controls which item will be auto focused when the menu opens. */\n readonly focusStrategy: FocusStrategy | null,\n\n /** Opens the menu. */\n open(focusStrategy?: FocusStrategy | null): void,\n\n /** Toggles the menu. */\n toggle(focusStrategy?: FocusStrategy | null): void\n}\n\n/**\n * Provides state management for a select component. Handles building a collection\n * of items from props, handles the open state for the popup menu, and manages\n * multiple selection state.\n */\nexport function useSelectState<T extends object, M extends SelectionMode = 'single'>(props: SelectStateOptions<T, M>): SelectState<T, M> {\n let {selectionMode = 'single' as M} = props;\n let triggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n let defaultValue = useMemo(() => {\n return props.defaultValue !== undefined ? props.defaultValue : (selectionMode === 'single' ? props.defaultSelectedKey ?? null : []) as ValueType<M>;\n }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);\n let value = useMemo(() => {\n return props.value !== undefined ? props.value : (selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>;\n }, [props.value, props.selectedKey, selectionMode]);\n let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(value, defaultValue, props.onChange as any);\n // Only display the first selected item if in single selection mode but the value is an array.\n let displayValue = selectionMode === 'single' && Array.isArray(controlledValue) ? controlledValue[0] : controlledValue;\n let setValue = (value: Key | Key[] | null) => {\n if (selectionMode === 'single') {\n let key = Array.isArray(value) ? value[0] ?? null : value;\n setControlledValue(key);\n if (key !== displayValue) {\n props.onSelectionChange?.(key);\n }\n } else {\n let keys: Key[] = [];\n if (Array.isArray(value)) {\n keys = value;\n } else if (value != null) {\n keys = [value];\n }\n\n setControlledValue(keys);\n }\n };\n\n let listState = useListState({\n ...props,\n selectionMode,\n disallowEmptySelection: selectionMode === 'single',\n allowDuplicateSelectionEvents: true,\n selectedKeys: useMemo(() => convertValue(displayValue), [displayValue]),\n onSelectionChange: (keys: Selection) => {\n // impossible, but TS doesn't know that\n if (keys === 'all') {\n return;\n }\n\n if (selectionMode === 'single') {\n let key = keys.values().next().value ?? null;\n setValue(key);\n triggerState.close();\n } else {\n setValue([...keys]);\n }\n\n validationState.commitValidation();\n }\n });\n\n let selectedKey = listState.selectionManager.firstSelectedKey;\n let selectedItems = useMemo(() => {\n return [...listState.selectionManager.selectedKeys].map(key => listState.collection.getItem(key)).filter(item => item != null);\n }, [listState.selectionManager.selectedKeys, listState.collection]);\n\n let validationState = useFormValidationState({\n ...props,\n value: Array.isArray(displayValue) && displayValue.length === 0 ? null : displayValue as any\n });\n\n let [isFocused, setFocused] = useState(false);\n let [initialValue] = useState(displayValue);\n\n return {\n ...validationState,\n ...listState,\n ...triggerState,\n value: displayValue as ValueType<M>,\n defaultValue: defaultValue ?? initialValue as ValueType<M>,\n setValue,\n selectedKey,\n setSelectedKey: setValue,\n selectedItem: selectedItems[0] ?? null,\n selectedItems,\n defaultSelectedKey: props.defaultSelectedKey ?? (props.selectionMode === 'single' ? initialValue as Key : null),\n focusStrategy,\n open(focusStrategy: FocusStrategy | null = null) {\n // Don't open if the collection is empty.\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0 || props.allowsEmptyCollection) {\n setFocusStrategy(focusStrategy);\n triggerState.toggle();\n }\n },\n isFocused,\n setFocused\n };\n}\n\nfunction convertValue(value: Key | Key[] | null | undefined) {\n if (value === undefined) {\n return undefined;\n }\n if (value === null) {\n return [];\n }\n return Array.isArray(value) ? value : [value];\n}\n"],"names":[],"version":3,"file":"useSelectState.module.js.map"}
@@ -1,189 +0,0 @@
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 {CollectionStateBase, FocusStrategy, Key, Node, Selection} from '@react-types/shared';
14
- import {FormValidationState, useFormValidationState} from '@react-stately/form';
15
- import {ListState, useListState} from '@react-stately/list';
16
- import {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';
17
- import {SelectionMode, SelectProps, ValueType} from '@react-types/select';
18
- import {useControlledState} from '@react-stately/utils';
19
- import {useMemo, useState} from 'react';
20
-
21
- export interface SelectStateOptions<T, M extends SelectionMode = 'single'> extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}
22
-
23
- export interface SelectState<T, M extends SelectionMode = 'single'> extends ListState<T>, OverlayTriggerState, FormValidationState {
24
- /**
25
- * The key for the first selected item.
26
- * @deprecated
27
- */
28
- readonly selectedKey: Key | null,
29
-
30
- /**
31
- * The default selected key.
32
- * @deprecated
33
- */
34
- readonly defaultSelectedKey: Key | null,
35
-
36
- /**
37
- * Sets the selected key.
38
- * @deprecated
39
- */
40
- setSelectedKey(key: Key | null): void,
41
-
42
- /** The current select value. */
43
- readonly value: ValueType<M>,
44
-
45
- /** The default select value. */
46
- readonly defaultValue: ValueType<M>,
47
-
48
- /** Sets the select value. */
49
- setValue(value: Key | readonly Key[] | null): void,
50
-
51
- /**
52
- * The value of the first selected item.
53
- * @deprecated
54
- */
55
- readonly selectedItem: Node<T> | null,
56
-
57
- /** The value of the selected items. */
58
- readonly selectedItems: Node<T>[],
59
-
60
- /** Whether the select is currently focused. */
61
- readonly isFocused: boolean,
62
-
63
- /** Sets whether the select is focused. */
64
- setFocused(isFocused: boolean): void,
65
-
66
- /** Controls which item will be auto focused when the menu opens. */
67
- readonly focusStrategy: FocusStrategy | null,
68
-
69
- /** Opens the menu. */
70
- open(focusStrategy?: FocusStrategy | null): void,
71
-
72
- /** Toggles the menu. */
73
- toggle(focusStrategy?: FocusStrategy | null): void
74
- }
75
-
76
- /**
77
- * Provides state management for a select component. Handles building a collection
78
- * of items from props, handles the open state for the popup menu, and manages
79
- * multiple selection state.
80
- */
81
- export function useSelectState<T extends object, M extends SelectionMode = 'single'>(props: SelectStateOptions<T, M>): SelectState<T, M> {
82
- let {selectionMode = 'single' as M} = props;
83
- let triggerState = useOverlayTriggerState(props);
84
- let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);
85
- let defaultValue = useMemo(() => {
86
- return props.defaultValue !== undefined ? props.defaultValue : (selectionMode === 'single' ? props.defaultSelectedKey ?? null : []) as ValueType<M>;
87
- }, [props.defaultValue, props.defaultSelectedKey, selectionMode]);
88
- let value = useMemo(() => {
89
- return props.value !== undefined ? props.value : (selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>;
90
- }, [props.value, props.selectedKey, selectionMode]);
91
- let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(value, defaultValue, props.onChange as any);
92
- // Only display the first selected item if in single selection mode but the value is an array.
93
- let displayValue = selectionMode === 'single' && Array.isArray(controlledValue) ? controlledValue[0] : controlledValue;
94
- let setValue = (value: Key | Key[] | null) => {
95
- if (selectionMode === 'single') {
96
- let key = Array.isArray(value) ? value[0] ?? null : value;
97
- setControlledValue(key);
98
- if (key !== displayValue) {
99
- props.onSelectionChange?.(key);
100
- }
101
- } else {
102
- let keys: Key[] = [];
103
- if (Array.isArray(value)) {
104
- keys = value;
105
- } else if (value != null) {
106
- keys = [value];
107
- }
108
-
109
- setControlledValue(keys);
110
- }
111
- };
112
-
113
- let listState = useListState({
114
- ...props,
115
- selectionMode,
116
- disallowEmptySelection: selectionMode === 'single',
117
- allowDuplicateSelectionEvents: true,
118
- selectedKeys: useMemo(() => convertValue(displayValue), [displayValue]),
119
- onSelectionChange: (keys: Selection) => {
120
- // impossible, but TS doesn't know that
121
- if (keys === 'all') {
122
- return;
123
- }
124
-
125
- if (selectionMode === 'single') {
126
- let key = keys.values().next().value ?? null;
127
- setValue(key);
128
- triggerState.close();
129
- } else {
130
- setValue([...keys]);
131
- }
132
-
133
- validationState.commitValidation();
134
- }
135
- });
136
-
137
- let selectedKey = listState.selectionManager.firstSelectedKey;
138
- let selectedItems = useMemo(() => {
139
- return [...listState.selectionManager.selectedKeys].map(key => listState.collection.getItem(key)).filter(item => item != null);
140
- }, [listState.selectionManager.selectedKeys, listState.collection]);
141
-
142
- let validationState = useFormValidationState({
143
- ...props,
144
- value: Array.isArray(displayValue) && displayValue.length === 0 ? null : displayValue as any
145
- });
146
-
147
- let [isFocused, setFocused] = useState(false);
148
- let [initialValue] = useState(displayValue);
149
-
150
- return {
151
- ...validationState,
152
- ...listState,
153
- ...triggerState,
154
- value: displayValue as ValueType<M>,
155
- defaultValue: defaultValue ?? initialValue as ValueType<M>,
156
- setValue,
157
- selectedKey,
158
- setSelectedKey: setValue,
159
- selectedItem: selectedItems[0] ?? null,
160
- selectedItems,
161
- defaultSelectedKey: props.defaultSelectedKey ?? (props.selectionMode === 'single' ? initialValue as Key : null),
162
- focusStrategy,
163
- open(focusStrategy: FocusStrategy | null = null) {
164
- // Don't open if the collection is empty.
165
- if (listState.collection.size !== 0 || props.allowsEmptyCollection) {
166
- setFocusStrategy(focusStrategy);
167
- triggerState.open();
168
- }
169
- },
170
- toggle(focusStrategy: FocusStrategy | null = null) {
171
- if (listState.collection.size !== 0 || props.allowsEmptyCollection) {
172
- setFocusStrategy(focusStrategy);
173
- triggerState.toggle();
174
- }
175
- },
176
- isFocused,
177
- setFocused
178
- };
179
- }
180
-
181
- function convertValue(value: Key | Key[] | null | undefined) {
182
- if (value === undefined) {
183
- return undefined;
184
- }
185
- if (value === null) {
186
- return [];
187
- }
188
- return Array.isArray(value) ? value : [value];
189
- }