@react-aria/selection 3.22.0 → 3.23.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/src/utils.ts CHANGED
@@ -10,7 +10,9 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {isAppleDevice} from '@react-aria/utils';
13
+ import {Collection, Key} from '@react-types/shared';
14
+ import {isAppleDevice, useId} from '@react-aria/utils';
15
+ import {RefObject} from 'react';
14
16
 
15
17
  interface Event {
16
18
  altKey: boolean,
@@ -23,3 +25,23 @@ export function isNonContiguousSelectionModifier(e: Event) {
23
25
  // On Windows and Ubuntu, Alt + Space has a system wide meaning.
24
26
  return isAppleDevice() ? e.altKey : e.ctrlKey;
25
27
  }
28
+
29
+ export function getItemElement(collectionRef: RefObject<HTMLElement | null>, key: Key) {
30
+ let selector = `[data-key="${CSS.escape(String(key))}"]`;
31
+ let collection = collectionRef.current?.dataset.collection;
32
+ if (collection) {
33
+ selector = `[data-collection="${CSS.escape(collection)}"]${selector}`;
34
+ }
35
+ return collectionRef.current?.querySelector(selector);
36
+ }
37
+
38
+ const collectionMap = new WeakMap();
39
+ export function useCollectionId(collection: Collection<any>) {
40
+ let id = useId();
41
+ collectionMap.set(collection, id);
42
+ return id;
43
+ }
44
+
45
+ export function getCollectionId(collection: Collection<any>) {
46
+ return collectionMap.get(collection)!;
47
+ }