@react-stately/select 3.7.1 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.d.ts CHANGED
@@ -1,13 +1,39 @@
1
- import { CollectionStateBase, FocusStrategy, Key } from "@react-types/shared";
1
+ import { CollectionStateBase, FocusStrategy, Key, Node } from "@react-types/shared";
2
2
  import { FormValidationState } from "@react-stately/form";
3
+ import { ListState } from "@react-stately/list";
3
4
  import { OverlayTriggerState } from "@react-stately/overlays";
4
- import { SelectProps } from "@react-types/select";
5
- import { SingleSelectListState } from "@react-stately/list";
6
- export interface SelectStateOptions<T> extends Omit<SelectProps<T>, 'children'>, CollectionStateBase<T> {
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
7
  }
8
- export interface SelectState<T> extends SingleSelectListState<T>, OverlayTriggerState, FormValidationState {
9
- /** The default selected key. */
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
+ */
10
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 | 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>[];
11
37
  /** Whether the select is currently focused. */
12
38
  readonly isFocused: boolean;
13
39
  /** Sets whether the select is focused. */
@@ -24,7 +50,7 @@ export interface SelectState<T> extends SingleSelectListState<T>, OverlayTrigger
24
50
  * of items from props, handles the open state for the popup menu, and manages
25
51
  * multiple selection state.
26
52
  */
27
- export function useSelectState<T extends object>(props: SelectStateOptions<T>): SelectState<T>;
53
+ export function useSelectState<T extends object, M extends SelectionMode = 'single'>(props: SelectStateOptions<T, M>): SelectState<T, M>;
28
54
  export type { SelectProps } from '@react-types/select';
29
55
 
30
56
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"mappings":";;;;;AAmBA,oCAAoC,CAAC,CAAE,SAAQ,IAAI,CAAC,YAAY,CAAC,CAAC,EAAE,UAAU,CAAC,EAAE,oBAAoB,CAAC,CAAC;CAAG;AAE1G,6BAA6B,CAAC,CAAE,SAAQ,sBAAsB,CAAC,CAAC,EAAE,mBAAmB,EAAE,mBAAmB;IACxG,gCAAgC;IAChC,QAAQ,CAAC,kBAAkB,EAAE,GAAG,GAAG,IAAI,CAAC;IAExC,+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,KAAK,EAAE,mBAAmB,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CA6C7F;AC7ED,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
+ {"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,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;IAE1C;;;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,6 +1,7 @@
1
1
  var $5KJtg$reactstatelyform = require("@react-stately/form");
2
- var $5KJtg$reactstatelyoverlays = require("@react-stately/overlays");
3
2
  var $5KJtg$reactstatelylist = require("@react-stately/list");
3
+ var $5KJtg$reactstatelyoverlays = require("@react-stately/overlays");
4
+ var $5KJtg$reactstatelyutils = require("@react-stately/utils");
4
5
  var $5KJtg$react = require("react");
5
6
 
6
7
 
@@ -23,29 +24,95 @@ $parcel$export(module.exports, "useSelectState", () => $80ebb60e77198879$export$
23
24
 
24
25
 
25
26
 
27
+
26
28
  function $80ebb60e77198879$export$5159ec8b34d4ec12(props) {
29
+ let { selectionMode: selectionMode = 'single' } = props;
27
30
  let triggerState = (0, $5KJtg$reactstatelyoverlays.useOverlayTriggerState)(props);
28
31
  let [focusStrategy, setFocusStrategy] = (0, $5KJtg$react.useState)(null);
29
- let listState = (0, $5KJtg$reactstatelylist.useSingleSelectListState)({
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)({
30
67
  ...props,
31
- onSelectionChange: (key)=>{
32
- if (props.onSelectionChange != null) props.onSelectionChange(key);
33
- triggerState.close();
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
+ ]);
34
85
  validationState.commitValidation();
35
86
  }
36
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
+ ]);
37
97
  let validationState = (0, $5KJtg$reactstatelyform.useFormValidationState)({
38
98
  ...props,
39
- value: listState.selectedKey
99
+ value: Array.isArray(displayValue) && displayValue.length === 0 ? null : displayValue
40
100
  });
41
101
  let [isFocused, setFocused] = (0, $5KJtg$react.useState)(false);
42
- let [initialSelectedKey] = (0, $5KJtg$react.useState)(listState.selectedKey);
43
- var _props_defaultSelectedKey;
102
+ let [initialValue] = (0, $5KJtg$react.useState)(displayValue);
103
+ var _selectedItems_, _props_defaultSelectedKey;
44
104
  return {
45
105
  ...validationState,
46
106
  ...listState,
47
107
  ...triggerState,
48
- defaultSelectedKey: (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : initialSelectedKey,
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,
49
116
  focusStrategy: focusStrategy,
50
117
  open (focusStrategy = null) {
51
118
  // Don't open if the collection is empty.
@@ -64,6 +131,13 @@ function $80ebb60e77198879$export$5159ec8b34d4ec12(props) {
64
131
  setFocused: setFocused
65
132
  };
66
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
+ }
67
141
 
68
142
 
69
143
  //# sourceMappingURL=useSelectState.main.js.map
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;AAoCM,SAAS,0CAAiC,KAA4B;IAC3E,IAAI,eAAe,CAAA,GAAA,kDAAqB,EAAE;IAC1C,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,qBAAO,EAAwB;IACvE,IAAI,YAAY,CAAA,GAAA,gDAAuB,EAAE;QACvC,GAAG,KAAK;QACR,mBAAmB,CAAC;YAClB,IAAI,MAAM,iBAAiB,IAAI,MAC7B,MAAM,iBAAiB,CAAC;YAG1B,aAAa,KAAK;YAClB,gBAAgB,gBAAgB;QAClC;IACF;IAEA,IAAI,kBAAkB,CAAA,GAAA,8CAAqB,EAAE;QAC3C,GAAG,KAAK;QACR,OAAO,UAAU,WAAW;IAC9B;IAEA,IAAI,CAAC,WAAW,WAAW,GAAG,CAAA,GAAA,qBAAO,EAAE;IACvC,IAAI,CAAC,mBAAmB,GAAG,CAAA,GAAA,qBAAO,EAAE,UAAU,WAAW;QAMnC;IAJtB,OAAO;QACL,GAAG,eAAe;QAClB,GAAG,SAAS;QACZ,GAAG,YAAY;QACf,oBAAoB,CAAA,4BAAA,MAAM,kBAAkB,cAAxB,uCAAA,4BAA4B;uBAChD;QACA,MAAK,gBAAsC,IAAI;YAC7C,yCAAyC;YACzC,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,GAAG;gBACnC,iBAAiB;gBACjB,aAAa,IAAI;YACnB;QACF;QACA,QAAO,gBAAsC,IAAI;YAC/C,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,GAAG;gBACnC,iBAAiB;gBACjB,aAAa,MAAM;YACrB;QACF;mBACA;oBACA;IACF;AACF","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} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '@react-stately/form';\nimport {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';\nimport {SelectProps} from '@react-types/select';\nimport {SingleSelectListState, useSingleSelectListState} from '@react-stately/list';\nimport {useState} from 'react';\n\nexport interface SelectStateOptions<T> extends Omit<SelectProps<T>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T> extends SingleSelectListState<T>, OverlayTriggerState, FormValidationState {\n /** The default selected key. */\n readonly defaultSelectedKey: Key | null,\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>(props: SelectStateOptions<T>): SelectState<T> {\n let triggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n let listState = useSingleSelectListState({\n ...props,\n onSelectionChange: (key) => {\n if (props.onSelectionChange != null) {\n props.onSelectionChange(key);\n }\n\n triggerState.close();\n validationState.commitValidation();\n }\n });\n\n let validationState = useFormValidationState({\n ...props,\n value: listState.selectedKey\n });\n\n let [isFocused, setFocused] = useState(false);\n let [initialSelectedKey] = useState(listState.selectedKey);\n\n return {\n ...validationState,\n ...listState,\n ...triggerState,\n defaultSelectedKey: props.defaultSelectedKey ?? initialSelectedKey,\n focusStrategy,\n open(focusStrategy: FocusStrategy | null = null) {\n // Don't open if the collection is empty.\n if (listState.collection.size !== 0) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0) {\n setFocusStrategy(focusStrategy);\n triggerState.toggle();\n }\n },\n isFocused,\n setFocused\n };\n}\n"],"names":[],"version":3,"file":"useSelectState.main.js.map"}
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,kBAAkB;QAAE;KAAc;IAChE,IAAI,QAAQ,CAAA,GAAA,oBAAM,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,2CAAiB,EAAsB,OAAO,cAAc,MAAM,QAAQ;IACtH,8FAA8F;IAC9F,IAAI,eAAe,kBAAkB,YAAY,MAAM,OAAO,CAAC,mBAAmB,eAAe,CAAC,EAAE,GAAG;IACvG,IAAI,WAAW,CAAC;QACd,IAAI,kBAAkB,UAAU;gBAI5B;gBAH+B;YAAjC,IAAI,MAAM,MAAM,OAAO,CAAC,SAAS,CAAA,UAAA,KAAK,CAAC,EAAE,cAAR,qBAAA,UAAY,OAAO;YACpD,mBAAmB;YACnB,IAAI,QAAQ,eACV,2BAAA,MAAM,iBAAiB,cAAvB,+CAAA,8BAAA,OAA0B;QAE9B,OAAO;YACL,IAAI,OAAc,EAAE;YACpB,IAAI,MAAM,OAAO,CAAC,QAChB,OAAO;iBACF,IAAI,SAAS,MAClB,OAAO;gBAAC;aAAM;YAGhB,mBAAmB;QACrB;IACF;IAEA,IAAI,YAAY,CAAA,GAAA,oCAAW,EAAE;QAC3B,GAAG,KAAK;uBACR;QACA,wBAAwB,kBAAkB;QAC1C,+BAA+B;QAC/B,cAAc,CAAA,GAAA,oBAAM,EAAE,IAAM,mCAAa,eAAe;YAAC;SAAa;QACtE,mBAAmB,CAAC;YAClB,uCAAuC;YACvC,IAAI,SAAS,OACX;YAGF,IAAI,kBAAkB,UAAU;oBACpB;gBAAV,IAAI,MAAM,CAAA,0BAAA,KAAK,MAAM,GAAG,IAAI,GAAG,KAAK,cAA1B,qCAAA,0BAA8B;gBACxC,SAAS;gBACT,aAAa,KAAK;YACpB,OACE,SAAS;mBAAI;aAAK;YAGpB,gBAAgB,gBAAgB;QAClC;IACF;IAEA,IAAI,cAAc,UAAU,gBAAgB,CAAC,gBAAgB;IAC7D,IAAI,gBAAgB,CAAA,GAAA,oBAAM,EAAE;QAC1B,OAAO;eAAI,UAAU,gBAAgB,CAAC,YAAY;SAAC,CAAC,GAAG,CAAC,CAAA,MAAO,UAAU,UAAU,CAAC,OAAO,CAAC,MAAM,MAAM,CAAC,CAAA,OAAQ,QAAQ;IAC3H,GAAG;QAAC,UAAU,gBAAgB,CAAC,YAAY;QAAE,UAAU,UAAU;KAAC;IAElE,IAAI,kBAAkB,CAAA,GAAA,8CAAqB,EAAE;QAC3C,GAAG,KAAK;QACR,OAAO,MAAM,OAAO,CAAC,iBAAiB,aAAa,MAAM,KAAK,IAAI,OAAO;IAC3E;IAEA,IAAI,CAAC,WAAW,WAAW,GAAG,CAAA,GAAA,qBAAO,EAAE;IACvC,IAAI,CAAC,aAAa,GAAG,CAAA,GAAA,qBAAO,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,GAAG;gBACnC,iBAAiB;gBACjB,aAAa,IAAI;YACnB;QACF;QACA,QAAO,gBAAsC,IAAI;YAC/C,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,GAAG;gBACnC,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 | 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 | 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) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0) {\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,7 +1,8 @@
1
1
  import {useFormValidationState as $6FfNf$useFormValidationState} from "@react-stately/form";
2
+ import {useListState as $6FfNf$useListState} from "@react-stately/list";
2
3
  import {useOverlayTriggerState as $6FfNf$useOverlayTriggerState} from "@react-stately/overlays";
3
- import {useSingleSelectListState as $6FfNf$useSingleSelectListState} from "@react-stately/list";
4
- import {useState as $6FfNf$useState} from "react";
4
+ import {useControlledState as $6FfNf$useControlledState} from "@react-stately/utils";
5
+ import {useState as $6FfNf$useState, useMemo as $6FfNf$useMemo} from "react";
5
6
 
6
7
  /*
7
8
  * Copyright 2020 Adobe. All rights reserved.
@@ -17,29 +18,95 @@ import {useState as $6FfNf$useState} from "react";
17
18
 
18
19
 
19
20
 
21
+
20
22
  function $2bc3a590c5373a4e$export$5159ec8b34d4ec12(props) {
23
+ let { selectionMode: selectionMode = 'single' } = props;
21
24
  let triggerState = (0, $6FfNf$useOverlayTriggerState)(props);
22
25
  let [focusStrategy, setFocusStrategy] = (0, $6FfNf$useState)(null);
23
- let listState = (0, $6FfNf$useSingleSelectListState)({
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)({
24
61
  ...props,
25
- onSelectionChange: (key)=>{
26
- if (props.onSelectionChange != null) props.onSelectionChange(key);
27
- triggerState.close();
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
+ ]);
28
79
  validationState.commitValidation();
29
80
  }
30
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
+ ]);
31
91
  let validationState = (0, $6FfNf$useFormValidationState)({
32
92
  ...props,
33
- value: listState.selectedKey
93
+ value: Array.isArray(displayValue) && displayValue.length === 0 ? null : displayValue
34
94
  });
35
95
  let [isFocused, setFocused] = (0, $6FfNf$useState)(false);
36
- let [initialSelectedKey] = (0, $6FfNf$useState)(listState.selectedKey);
37
- var _props_defaultSelectedKey;
96
+ let [initialValue] = (0, $6FfNf$useState)(displayValue);
97
+ var _selectedItems_, _props_defaultSelectedKey;
38
98
  return {
39
99
  ...validationState,
40
100
  ...listState,
41
101
  ...triggerState,
42
- defaultSelectedKey: (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : initialSelectedKey,
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,
43
110
  focusStrategy: focusStrategy,
44
111
  open (focusStrategy = null) {
45
112
  // Don't open if the collection is empty.
@@ -58,6 +125,13 @@ function $2bc3a590c5373a4e$export$5159ec8b34d4ec12(props) {
58
125
  setFocused: setFocused
59
126
  };
60
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
+ }
61
135
 
62
136
 
63
137
  export {$2bc3a590c5373a4e$export$5159ec8b34d4ec12 as useSelectState};
@@ -1,7 +1,8 @@
1
1
  import {useFormValidationState as $6FfNf$useFormValidationState} from "@react-stately/form";
2
+ import {useListState as $6FfNf$useListState} from "@react-stately/list";
2
3
  import {useOverlayTriggerState as $6FfNf$useOverlayTriggerState} from "@react-stately/overlays";
3
- import {useSingleSelectListState as $6FfNf$useSingleSelectListState} from "@react-stately/list";
4
- import {useState as $6FfNf$useState} from "react";
4
+ import {useControlledState as $6FfNf$useControlledState} from "@react-stately/utils";
5
+ import {useState as $6FfNf$useState, useMemo as $6FfNf$useMemo} from "react";
5
6
 
6
7
  /*
7
8
  * Copyright 2020 Adobe. All rights reserved.
@@ -17,29 +18,95 @@ import {useState as $6FfNf$useState} from "react";
17
18
 
18
19
 
19
20
 
21
+
20
22
  function $2bc3a590c5373a4e$export$5159ec8b34d4ec12(props) {
23
+ let { selectionMode: selectionMode = 'single' } = props;
21
24
  let triggerState = (0, $6FfNf$useOverlayTriggerState)(props);
22
25
  let [focusStrategy, setFocusStrategy] = (0, $6FfNf$useState)(null);
23
- let listState = (0, $6FfNf$useSingleSelectListState)({
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)({
24
61
  ...props,
25
- onSelectionChange: (key)=>{
26
- if (props.onSelectionChange != null) props.onSelectionChange(key);
27
- triggerState.close();
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
+ ]);
28
79
  validationState.commitValidation();
29
80
  }
30
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
+ ]);
31
91
  let validationState = (0, $6FfNf$useFormValidationState)({
32
92
  ...props,
33
- value: listState.selectedKey
93
+ value: Array.isArray(displayValue) && displayValue.length === 0 ? null : displayValue
34
94
  });
35
95
  let [isFocused, setFocused] = (0, $6FfNf$useState)(false);
36
- let [initialSelectedKey] = (0, $6FfNf$useState)(listState.selectedKey);
37
- var _props_defaultSelectedKey;
96
+ let [initialValue] = (0, $6FfNf$useState)(displayValue);
97
+ var _selectedItems_, _props_defaultSelectedKey;
38
98
  return {
39
99
  ...validationState,
40
100
  ...listState,
41
101
  ...triggerState,
42
- defaultSelectedKey: (_props_defaultSelectedKey = props.defaultSelectedKey) !== null && _props_defaultSelectedKey !== void 0 ? _props_defaultSelectedKey : initialSelectedKey,
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,
43
110
  focusStrategy: focusStrategy,
44
111
  open (focusStrategy = null) {
45
112
  // Don't open if the collection is empty.
@@ -58,6 +125,13 @@ function $2bc3a590c5373a4e$export$5159ec8b34d4ec12(props) {
58
125
  setFocused: setFocused
59
126
  };
60
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
+ }
61
135
 
62
136
 
63
137
  export {$2bc3a590c5373a4e$export$5159ec8b34d4ec12 as useSelectState};
@@ -1 +1 @@
1
- {"mappings":";;;;;AAAA;;;;;;;;;;CAUC;;;;AAoCM,SAAS,0CAAiC,KAA4B;IAC3E,IAAI,eAAe,CAAA,GAAA,6BAAqB,EAAE;IAC1C,IAAI,CAAC,eAAe,iBAAiB,GAAG,CAAA,GAAA,eAAO,EAAwB;IACvE,IAAI,YAAY,CAAA,GAAA,+BAAuB,EAAE;QACvC,GAAG,KAAK;QACR,mBAAmB,CAAC;YAClB,IAAI,MAAM,iBAAiB,IAAI,MAC7B,MAAM,iBAAiB,CAAC;YAG1B,aAAa,KAAK;YAClB,gBAAgB,gBAAgB;QAClC;IACF;IAEA,IAAI,kBAAkB,CAAA,GAAA,6BAAqB,EAAE;QAC3C,GAAG,KAAK;QACR,OAAO,UAAU,WAAW;IAC9B;IAEA,IAAI,CAAC,WAAW,WAAW,GAAG,CAAA,GAAA,eAAO,EAAE;IACvC,IAAI,CAAC,mBAAmB,GAAG,CAAA,GAAA,eAAO,EAAE,UAAU,WAAW;QAMnC;IAJtB,OAAO;QACL,GAAG,eAAe;QAClB,GAAG,SAAS;QACZ,GAAG,YAAY;QACf,oBAAoB,CAAA,4BAAA,MAAM,kBAAkB,cAAxB,uCAAA,4BAA4B;uBAChD;QACA,MAAK,gBAAsC,IAAI;YAC7C,yCAAyC;YACzC,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,GAAG;gBACnC,iBAAiB;gBACjB,aAAa,IAAI;YACnB;QACF;QACA,QAAO,gBAAsC,IAAI;YAC/C,IAAI,UAAU,UAAU,CAAC,IAAI,KAAK,GAAG;gBACnC,iBAAiB;gBACjB,aAAa,MAAM;YACrB;QACF;mBACA;oBACA;IACF;AACF","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} from '@react-types/shared';\nimport {FormValidationState, useFormValidationState} from '@react-stately/form';\nimport {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';\nimport {SelectProps} from '@react-types/select';\nimport {SingleSelectListState, useSingleSelectListState} from '@react-stately/list';\nimport {useState} from 'react';\n\nexport interface SelectStateOptions<T> extends Omit<SelectProps<T>, 'children'>, CollectionStateBase<T> {}\n\nexport interface SelectState<T> extends SingleSelectListState<T>, OverlayTriggerState, FormValidationState {\n /** The default selected key. */\n readonly defaultSelectedKey: Key | null,\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>(props: SelectStateOptions<T>): SelectState<T> {\n let triggerState = useOverlayTriggerState(props);\n let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);\n let listState = useSingleSelectListState({\n ...props,\n onSelectionChange: (key) => {\n if (props.onSelectionChange != null) {\n props.onSelectionChange(key);\n }\n\n triggerState.close();\n validationState.commitValidation();\n }\n });\n\n let validationState = useFormValidationState({\n ...props,\n value: listState.selectedKey\n });\n\n let [isFocused, setFocused] = useState(false);\n let [initialSelectedKey] = useState(listState.selectedKey);\n\n return {\n ...validationState,\n ...listState,\n ...triggerState,\n defaultSelectedKey: props.defaultSelectedKey ?? initialSelectedKey,\n focusStrategy,\n open(focusStrategy: FocusStrategy | null = null) {\n // Don't open if the collection is empty.\n if (listState.collection.size !== 0) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0) {\n setFocusStrategy(focusStrategy);\n triggerState.toggle();\n }\n },\n isFocused,\n setFocused\n };\n}\n"],"names":[],"version":3,"file":"useSelectState.module.js.map"}
1
+ {"mappings":"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","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 | 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 | 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) {\n setFocusStrategy(focusStrategy);\n triggerState.open();\n }\n },\n toggle(focusStrategy: FocusStrategy | null = null) {\n if (listState.collection.size !== 0) {\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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/select",
3
- "version": "3.7.1",
3
+ "version": "3.8.0",
4
4
  "description": "Spectrum UI components in React",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -26,11 +26,12 @@
26
26
  "url": "https://github.com/adobe/react-spectrum"
27
27
  },
28
28
  "dependencies": {
29
- "@react-stately/form": "^3.2.1",
30
- "@react-stately/list": "^3.13.0",
31
- "@react-stately/overlays": "^3.6.19",
32
- "@react-types/select": "^3.10.1",
33
- "@react-types/shared": "^3.32.0",
29
+ "@react-stately/form": "^3.2.2",
30
+ "@react-stately/list": "^3.13.1",
31
+ "@react-stately/overlays": "^3.6.20",
32
+ "@react-stately/utils": "^3.10.8",
33
+ "@react-types/select": "^3.11.0",
34
+ "@react-types/shared": "^3.32.1",
34
35
  "@swc/helpers": "^0.5.0"
35
36
  },
36
37
  "peerDependencies": {
@@ -39,5 +40,5 @@
39
40
  "publishConfig": {
40
41
  "access": "public"
41
42
  },
42
- "gitHead": "2c58ed3ddd9be9100af9b1d0cfd137fcdc95ba2d"
43
+ "gitHead": "0bda51183baa23306342af32a82012ea0fe0f2dc"
43
44
  }
@@ -10,19 +10,53 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {CollectionStateBase, FocusStrategy, Key} from '@react-types/shared';
13
+ import {CollectionStateBase, FocusStrategy, Key, Node, Selection} from '@react-types/shared';
14
14
  import {FormValidationState, useFormValidationState} from '@react-stately/form';
15
+ import {ListState, useListState} from '@react-stately/list';
15
16
  import {OverlayTriggerState, useOverlayTriggerState} from '@react-stately/overlays';
16
- import {SelectProps} from '@react-types/select';
17
- import {SingleSelectListState, useSingleSelectListState} from '@react-stately/list';
18
- import {useState} from 'react';
17
+ import {SelectionMode, SelectProps, ValueType} from '@react-types/select';
18
+ import {useControlledState} from '@react-stately/utils';
19
+ import {useMemo, useState} from 'react';
19
20
 
20
- export interface SelectStateOptions<T> extends Omit<SelectProps<T>, 'children'>, CollectionStateBase<T> {}
21
+ export interface SelectStateOptions<T, M extends SelectionMode = 'single'> extends Omit<SelectProps<T, M>, 'children'>, CollectionStateBase<T> {}
21
22
 
22
- export interface SelectState<T> extends SingleSelectListState<T>, OverlayTriggerState, FormValidationState {
23
- /** The default selected key. */
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
+ */
24
34
  readonly defaultSelectedKey: Key | null,
25
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 | 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
+
26
60
  /** Whether the select is currently focused. */
27
61
  readonly isFocused: boolean,
28
62
 
@@ -44,34 +78,87 @@ export interface SelectState<T> extends SingleSelectListState<T>, OverlayTrigger
44
78
  * of items from props, handles the open state for the popup menu, and manages
45
79
  * multiple selection state.
46
80
  */
47
- export function useSelectState<T extends object>(props: SelectStateOptions<T>): SelectState<T> {
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;
48
83
  let triggerState = useOverlayTriggerState(props);
49
84
  let [focusStrategy, setFocusStrategy] = useState<FocusStrategy | null>(null);
50
- let listState = useSingleSelectListState({
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 | 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({
51
114
  ...props,
52
- onSelectionChange: (key) => {
53
- if (props.onSelectionChange != null) {
54
- props.onSelectionChange(key);
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]);
55
131
  }
56
132
 
57
- triggerState.close();
58
133
  validationState.commitValidation();
59
134
  }
60
135
  });
61
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
+
62
142
  let validationState = useFormValidationState({
63
143
  ...props,
64
- value: listState.selectedKey
144
+ value: Array.isArray(displayValue) && displayValue.length === 0 ? null : displayValue as any
65
145
  });
66
146
 
67
147
  let [isFocused, setFocused] = useState(false);
68
- let [initialSelectedKey] = useState(listState.selectedKey);
148
+ let [initialValue] = useState(displayValue);
69
149
 
70
150
  return {
71
151
  ...validationState,
72
152
  ...listState,
73
153
  ...triggerState,
74
- defaultSelectedKey: props.defaultSelectedKey ?? initialSelectedKey,
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),
75
162
  focusStrategy,
76
163
  open(focusStrategy: FocusStrategy | null = null) {
77
164
  // Don't open if the collection is empty.
@@ -90,3 +177,13 @@ export function useSelectState<T extends object>(props: SelectStateOptions<T>):
90
177
  setFocused
91
178
  };
92
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
+ }