@react-stately/list 3.13.4 → 3.14.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.
@@ -1,114 +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 {Collection, Key, Node} from '@react-types/shared';
14
-
15
- export class ListCollection<T> implements Collection<Node<T>> {
16
- private keyMap: Map<Key, Node<T>> = new Map();
17
- private iterable: Iterable<Node<T>>;
18
- private firstKey: Key | null = null;
19
- private lastKey: Key | null = null;
20
- private _size: number;
21
-
22
- constructor(nodes: Iterable<Node<T>>) {
23
- this.iterable = nodes;
24
-
25
- let visit = (node: Node<T>) => {
26
- this.keyMap.set(node.key, node);
27
-
28
- if (node.childNodes && node.type === 'section') {
29
- for (let child of node.childNodes) {
30
- visit(child);
31
- }
32
- }
33
- };
34
-
35
- for (let node of nodes) {
36
- visit(node);
37
- }
38
-
39
- let last: Node<T> | null = null;
40
- let index = 0;
41
- let size = 0;
42
- for (let [key, node] of this.keyMap) {
43
- if (last) {
44
- last.nextKey = key;
45
- node.prevKey = last.key;
46
- } else {
47
- this.firstKey = key;
48
- node.prevKey = undefined;
49
- }
50
-
51
- if (node.type === 'item') {
52
- node.index = index++;
53
- }
54
-
55
- // Only count sections and items when determining size so that
56
- // loaders and separators in RAC/S2 don't influence the emptyState determination
57
- if (node.type === 'section' || node.type === 'item') {
58
- size++;
59
- }
60
-
61
- last = node;
62
-
63
- // Set nextKey as undefined since this might be the last node
64
- // If it isn't the last node, last.nextKey will properly set at start of new loop
65
- last.nextKey = undefined;
66
- }
67
- this._size = size;
68
- this.lastKey = last?.key ?? null;
69
- }
70
-
71
- *[Symbol.iterator](): IterableIterator<Node<T>> {
72
- yield* this.iterable;
73
- }
74
-
75
- get size(): number {
76
- return this._size;
77
- }
78
-
79
- getKeys(): IterableIterator<Key> {
80
- return this.keyMap.keys();
81
- }
82
-
83
- getKeyBefore(key: Key): Key | null {
84
- let node = this.keyMap.get(key);
85
- return node ? node.prevKey ?? null : null;
86
- }
87
-
88
- getKeyAfter(key: Key): Key | null {
89
- let node = this.keyMap.get(key);
90
- return node ? node.nextKey ?? null : null;
91
- }
92
-
93
- getFirstKey(): Key | null {
94
- return this.firstKey;
95
- }
96
-
97
- getLastKey(): Key | null {
98
- return this.lastKey;
99
- }
100
-
101
- getItem(key: Key): Node<T> | null {
102
- return this.keyMap.get(key) ?? null;
103
- }
104
-
105
- at(idx: number): Node<T> | null {
106
- const keys = [...this.getKeys()];
107
- return this.getItem(keys[idx]);
108
- }
109
-
110
- getChildren(key: Key): Iterable<Node<T>> {
111
- let node = this.keyMap.get(key);
112
- return node?.childNodes || [];
113
- }
114
- }
@@ -1,136 +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 {Collection, CollectionStateBase, Key, LayoutDelegate, Node} from '@react-types/shared';
14
- import {ListCollection} from './ListCollection';
15
- import {MultipleSelectionStateProps, SelectionManager, useMultipleSelectionState} from '@react-stately/selection';
16
- import {useCallback, useEffect, useMemo, useRef} from 'react';
17
- import {useCollection} from '@react-stately/collections';
18
-
19
- export interface ListProps<T> extends CollectionStateBase<T>, MultipleSelectionStateProps {
20
- /** Filter function to generate a filtered list of nodes. */
21
- filter?: (nodes: Iterable<Node<T>>) => Iterable<Node<T>>,
22
- /** @private */
23
- suppressTextValueWarning?: boolean,
24
- /**
25
- * A delegate object that provides layout information for items in the collection.
26
- * This can be used to override the behavior of shift selection.
27
- */
28
- layoutDelegate?: LayoutDelegate
29
- }
30
-
31
- export interface ListState<T> {
32
- /** A collection of items in the list. */
33
- collection: Collection<Node<T>>,
34
-
35
- /** A set of items that are disabled. */
36
- disabledKeys: Set<Key>,
37
-
38
- /** A selection manager to read and update multiple selection state. */
39
- selectionManager: SelectionManager
40
- }
41
-
42
- /**
43
- * Provides state management for list-like components. Handles building a collection
44
- * of items from props, and manages multiple selection state.
45
- */
46
- export function useListState<T extends object>(props: ListProps<T>): ListState<T> {
47
- let {filter, layoutDelegate} = props;
48
-
49
- let selectionState = useMultipleSelectionState(props);
50
- let disabledKeys = useMemo(() =>
51
- props.disabledKeys ? new Set(props.disabledKeys) : new Set<Key>()
52
- , [props.disabledKeys]);
53
-
54
- let factory = useCallback(nodes => filter ? new ListCollection(filter(nodes)) : new ListCollection(nodes as Iterable<Node<T>>), [filter]);
55
- let context = useMemo(() => ({suppressTextValueWarning: props.suppressTextValueWarning}), [props.suppressTextValueWarning]);
56
-
57
- let collection = useCollection(props, factory, context);
58
-
59
- let selectionManager = useMemo(() =>
60
- new SelectionManager(collection, selectionState, {layoutDelegate})
61
- , [collection, selectionState, layoutDelegate]
62
- );
63
-
64
- useFocusedKeyReset(collection, selectionManager);
65
-
66
- return {
67
- collection,
68
- disabledKeys,
69
- selectionManager
70
- };
71
- }
72
-
73
- /**
74
- * Filters a collection using the provided filter function and returns a new ListState.
75
- */
76
- export function UNSTABLE_useFilteredListState<T extends object>(state: ListState<T>, filterFn: ((nodeValue: string, node: Node<T>) => boolean) | null | undefined): ListState<T> {
77
- let collection = useMemo(() => filterFn ? state.collection.filter!(filterFn) : state.collection, [state.collection, filterFn]);
78
- let selectionManager = state.selectionManager.withCollection(collection);
79
- useFocusedKeyReset(collection, selectionManager);
80
- return {
81
- collection,
82
- selectionManager,
83
- disabledKeys: state.disabledKeys
84
- };
85
- }
86
-
87
- function useFocusedKeyReset<T>(collection: Collection<Node<T>>, selectionManager: SelectionManager) {
88
- // Reset focused key if that item is deleted from the collection.
89
- const cachedCollection = useRef<Collection<Node<T>> | null>(null);
90
- useEffect(() => {
91
- if (selectionManager.focusedKey != null && !collection.getItem(selectionManager.focusedKey) && cachedCollection.current) {
92
- const startItem = cachedCollection.current.getItem(selectionManager.focusedKey);
93
- const cachedItemNodes = [...cachedCollection.current.getKeys()].map(
94
- key => {
95
- const itemNode = cachedCollection.current!.getItem(key);
96
- return itemNode?.type === 'item' ? itemNode : null;
97
- }
98
- ).filter(node => node !== null);
99
- const itemNodes = [...collection.getKeys()].map(
100
- key => {
101
- const itemNode = collection.getItem(key);
102
- return itemNode?.type === 'item' ? itemNode : null;
103
- }
104
- ).filter(node => node !== null);
105
- const diff: number = (cachedItemNodes?.length ?? 0) - (itemNodes?.length ?? 0);
106
- let index = Math.min(
107
- (
108
- diff > 1 ?
109
- Math.max((startItem?.index ?? 0) - diff + 1, 0) :
110
- startItem?.index ?? 0
111
- ),
112
- (itemNodes?.length ?? 0) - 1);
113
- let newNode: Node<T> | null = null;
114
- let isReverseSearching = false;
115
- while (index >= 0) {
116
- if (!selectionManager.isDisabled(itemNodes[index].key)) {
117
- newNode = itemNodes[index];
118
- break;
119
- }
120
- // Find next, not disabled item.
121
- if (index < itemNodes.length - 1 && !isReverseSearching) {
122
- index++;
123
- // Otherwise, find previous, not disabled item.
124
- } else {
125
- isReverseSearching = true;
126
- if (index > (startItem?.index ?? 0)) {
127
- index = (startItem?.index ?? 0);
128
- }
129
- index--;
130
- }
131
- }
132
- selectionManager.setFocusedKey(newNode ? newNode.key : null);
133
- }
134
- cachedCollection.current = collection;
135
- }, [collection, selectionManager]);
136
- }
@@ -1,78 +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, Key, Node, Selection, SingleSelection} from '@react-types/shared';
14
- import {ListState, useListState} from './useListState';
15
- import {useControlledState} from '@react-stately/utils';
16
- import {useMemo} from 'react';
17
-
18
- export interface SingleSelectListProps<T> extends CollectionStateBase<T>, Omit<SingleSelection, 'disallowEmptySelection'> {
19
- /** Filter function to generate a filtered list of nodes. */
20
- filter?: (nodes: Iterable<Node<T>>) => Iterable<Node<T>>,
21
- /** @private */
22
- suppressTextValueWarning?: boolean
23
- }
24
-
25
- export interface SingleSelectListState<T> extends ListState<T> {
26
- /** The key for the currently selected item. */
27
- readonly selectedKey: Key | null,
28
-
29
- /** Sets the selected key. */
30
- setSelectedKey(key: Key | null): void,
31
-
32
- /** The value of the currently selected item. */
33
- readonly selectedItem: Node<T> | null
34
- }
35
-
36
- /**
37
- * Provides state management for list-like components with single selection.
38
- * Handles building a collection of items from props, and manages selection state.
39
- */
40
- export function useSingleSelectListState<T extends object>(props: SingleSelectListProps<T>): SingleSelectListState<T> {
41
- let [selectedKey, setSelectedKey] = useControlledState(props.selectedKey, props.defaultSelectedKey ?? null, props.onSelectionChange);
42
- let selectedKeys = useMemo(() => selectedKey != null ? [selectedKey] : [], [selectedKey]);
43
- let {collection, disabledKeys, selectionManager} = useListState({
44
- ...props,
45
- selectionMode: 'single',
46
- disallowEmptySelection: true,
47
- allowDuplicateSelectionEvents: true,
48
- selectedKeys,
49
- onSelectionChange: (keys: Selection) => {
50
- // impossible, but TS doesn't know that
51
- if (keys === 'all') {
52
- return;
53
- }
54
- let key = keys.values().next().value ?? null;
55
-
56
- // Always fire onSelectionChange, even if the key is the same
57
- // as the current key (useControlledState does not).
58
- if (key === selectedKey && props.onSelectionChange) {
59
- props.onSelectionChange(key);
60
- }
61
-
62
- setSelectedKey(key);
63
- }
64
- });
65
-
66
- let selectedItem = selectedKey != null
67
- ? collection.getItem(selectedKey)
68
- : null;
69
-
70
- return {
71
- collection,
72
- disabledKeys,
73
- selectionManager,
74
- selectedKey,
75
- setSelectedKey,
76
- selectedItem
77
- };
78
- }