@react-stately/selection 3.20.9 → 3.21.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 +3 -3
- package/dist/main.js +4 -4
- package/dist/main.js.map +1 -1
- package/dist/module.js +3 -3
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +5 -0
- package/package.json +17 -15
- package/src/index.ts +6 -4
- package/dist/Selection.main.js +0 -31
- package/dist/Selection.main.js.map +0 -1
- package/dist/Selection.mjs +0 -26
- package/dist/Selection.module.js +0 -26
- package/dist/Selection.module.js.map +0 -1
- package/dist/SelectionManager.main.js +0 -323
- package/dist/SelectionManager.main.js.map +0 -1
- package/dist/SelectionManager.mjs +0 -318
- package/dist/SelectionManager.module.js +0 -318
- package/dist/SelectionManager.module.js.map +0 -1
- package/dist/types.d.ts +0 -218
- package/dist/types.d.ts.map +0 -1
- package/dist/useMultipleSelectionState.main.js +0 -101
- package/dist/useMultipleSelectionState.main.js.map +0 -1
- package/dist/useMultipleSelectionState.mjs +0 -96
- package/dist/useMultipleSelectionState.module.js +0 -96
- package/dist/useMultipleSelectionState.module.js.map +0 -1
- package/src/Selection.ts +0 -33
- package/src/SelectionManager.ts +0 -522
- package/src/types.ts +0 -113
- package/src/useMultipleSelectionState.ts +0 -130
|
@@ -1,130 +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 {DisabledBehavior, FocusStrategy, Key, MultipleSelection, SelectionBehavior, SelectionMode} from '@react-types/shared';
|
|
14
|
-
import {MultipleSelectionState} from './types';
|
|
15
|
-
import {Selection} from './Selection';
|
|
16
|
-
import {useControlledState} from '@react-stately/utils';
|
|
17
|
-
import {useEffect, useMemo, useRef, useState} from 'react';
|
|
18
|
-
|
|
19
|
-
function equalSets(setA, setB) {
|
|
20
|
-
if (setA.size !== setB.size) {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
for (let item of setA) {
|
|
25
|
-
if (!setB.has(item)) {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface MultipleSelectionStateProps extends MultipleSelection {
|
|
34
|
-
/** How multiple selection should behave in the collection. */
|
|
35
|
-
selectionBehavior?: SelectionBehavior,
|
|
36
|
-
/** Whether onSelectionChange should fire even if the new set of keys is the same as the last. */
|
|
37
|
-
allowDuplicateSelectionEvents?: boolean,
|
|
38
|
-
/** Whether `disabledKeys` applies to all interactions, or only selection. */
|
|
39
|
-
disabledBehavior?: DisabledBehavior
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Manages state for multiple selection and focus in a collection.
|
|
44
|
-
*/
|
|
45
|
-
export function useMultipleSelectionState(props: MultipleSelectionStateProps): MultipleSelectionState {
|
|
46
|
-
let {
|
|
47
|
-
selectionMode = 'none' as SelectionMode,
|
|
48
|
-
disallowEmptySelection = false,
|
|
49
|
-
allowDuplicateSelectionEvents,
|
|
50
|
-
selectionBehavior: selectionBehaviorProp = 'toggle',
|
|
51
|
-
disabledBehavior = 'all'
|
|
52
|
-
} = props;
|
|
53
|
-
|
|
54
|
-
// We want synchronous updates to `isFocused` and `focusedKey` after their setters are called.
|
|
55
|
-
// But we also need to trigger a react re-render. So, we have both a ref (sync) and state (async).
|
|
56
|
-
let isFocusedRef = useRef(false);
|
|
57
|
-
let [, setFocused] = useState(false);
|
|
58
|
-
let focusedKeyRef = useRef<Key | null>(null);
|
|
59
|
-
let childFocusStrategyRef = useRef<FocusStrategy | null>(null);
|
|
60
|
-
let [, setFocusedKey] = useState<Key | null>(null);
|
|
61
|
-
let selectedKeysProp = useMemo(() => convertSelection(props.selectedKeys), [props.selectedKeys]);
|
|
62
|
-
let defaultSelectedKeys = useMemo(() => convertSelection(props.defaultSelectedKeys, new Selection()), [props.defaultSelectedKeys]);
|
|
63
|
-
let [selectedKeys, setSelectedKeys] = useControlledState(
|
|
64
|
-
selectedKeysProp,
|
|
65
|
-
defaultSelectedKeys!,
|
|
66
|
-
props.onSelectionChange
|
|
67
|
-
);
|
|
68
|
-
let disabledKeysProp = useMemo(() =>
|
|
69
|
-
props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()
|
|
70
|
-
, [props.disabledKeys]);
|
|
71
|
-
let [selectionBehavior, setSelectionBehavior] = useState(selectionBehaviorProp);
|
|
72
|
-
|
|
73
|
-
// If the selectionBehavior prop is set to replace, but the current state is toggle (e.g. due to long press
|
|
74
|
-
// to enter selection mode on touch), and the selection becomes empty, reset the selection behavior.
|
|
75
|
-
if (selectionBehaviorProp === 'replace' && selectionBehavior === 'toggle' && typeof selectedKeys === 'object' && selectedKeys.size === 0) {
|
|
76
|
-
setSelectionBehavior('replace');
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// If the selectionBehavior prop changes, update the state as well.
|
|
80
|
-
let lastSelectionBehavior = useRef(selectionBehaviorProp);
|
|
81
|
-
useEffect(() => {
|
|
82
|
-
if (selectionBehaviorProp !== lastSelectionBehavior.current) {
|
|
83
|
-
setSelectionBehavior(selectionBehaviorProp);
|
|
84
|
-
lastSelectionBehavior.current = selectionBehaviorProp;
|
|
85
|
-
}
|
|
86
|
-
}, [selectionBehaviorProp]);
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
selectionMode,
|
|
90
|
-
disallowEmptySelection,
|
|
91
|
-
selectionBehavior,
|
|
92
|
-
setSelectionBehavior,
|
|
93
|
-
get isFocused() {
|
|
94
|
-
return isFocusedRef.current;
|
|
95
|
-
},
|
|
96
|
-
setFocused(f) {
|
|
97
|
-
isFocusedRef.current = f;
|
|
98
|
-
setFocused(f);
|
|
99
|
-
},
|
|
100
|
-
get focusedKey() {
|
|
101
|
-
return focusedKeyRef.current;
|
|
102
|
-
},
|
|
103
|
-
get childFocusStrategy() {
|
|
104
|
-
return childFocusStrategyRef.current;
|
|
105
|
-
},
|
|
106
|
-
setFocusedKey(k, childFocusStrategy = 'first') {
|
|
107
|
-
focusedKeyRef.current = k;
|
|
108
|
-
childFocusStrategyRef.current = childFocusStrategy;
|
|
109
|
-
setFocusedKey(k);
|
|
110
|
-
},
|
|
111
|
-
selectedKeys,
|
|
112
|
-
setSelectedKeys(keys) {
|
|
113
|
-
if (allowDuplicateSelectionEvents || !equalSets(keys, selectedKeys)) {
|
|
114
|
-
setSelectedKeys(keys);
|
|
115
|
-
}
|
|
116
|
-
},
|
|
117
|
-
disabledKeys: disabledKeysProp,
|
|
118
|
-
disabledBehavior
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function convertSelection(selection: 'all' | Iterable<Key> | null | undefined, defaultValue?: Selection): 'all' | Set<Key> | undefined {
|
|
123
|
-
if (!selection) {
|
|
124
|
-
return defaultValue;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return selection === 'all'
|
|
128
|
-
? 'all'
|
|
129
|
-
: new Selection(selection);
|
|
130
|
-
}
|