-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathListKeyboardDelegate.ts
279 lines (236 loc) · 9.28 KB
/
ListKeyboardDelegate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {Collection, Direction, DisabledBehavior, Key, KeyboardDelegate, LayoutDelegate, Node, Orientation, Rect, RefObject} from '@react-types/shared';
import {DOMLayoutDelegate} from './DOMLayoutDelegate';
import {isScrollable} from '@react-aria/utils';
interface ListKeyboardDelegateOptions<T> {
collection: Collection<Node<T>>,
ref: RefObject<HTMLElement | null>,
collator?: Intl.Collator,
layout?: 'stack' | 'grid',
orientation?: Orientation,
direction?: Direction,
disabledKeys?: Set<Key>,
disabledBehavior?: DisabledBehavior,
layoutDelegate?: LayoutDelegate
}
export class ListKeyboardDelegate<T> implements KeyboardDelegate {
private collection: Collection<Node<T>>;
private disabledKeys: Set<Key>;
private disabledBehavior: DisabledBehavior;
private ref: RefObject<HTMLElement | null>;
private collator: Intl.Collator | undefined;
private layout: 'stack' | 'grid';
private orientation?: Orientation;
private direction?: Direction;
private layoutDelegate: LayoutDelegate;
constructor(collection: Collection<Node<T>>, disabledKeys: Set<Key>, ref: RefObject<HTMLElement | null>, collator?: Intl.Collator);
constructor(options: ListKeyboardDelegateOptions<T>);
constructor(...args: any[]) {
if (args.length === 1) {
let opts = args[0] as ListKeyboardDelegateOptions<T>;
this.collection = opts.collection;
this.ref = opts.ref;
this.collator = opts.collator;
this.disabledKeys = opts.disabledKeys || new Set();
this.disabledBehavior = opts.disabledBehavior || 'all';
this.orientation = opts.orientation || 'vertical';
this.direction = opts.direction;
this.layout = opts.layout || 'stack';
this.layoutDelegate = opts.layoutDelegate || new DOMLayoutDelegate(opts.ref);
} else {
this.collection = args[0];
this.disabledKeys = args[1];
this.ref = args[2];
this.collator = args[3];
this.layout = 'stack';
this.orientation = 'vertical';
this.disabledBehavior = 'all';
this.layoutDelegate = new DOMLayoutDelegate(this.ref);
}
// If this is a vertical stack, remove the left/right methods completely
// so they aren't called by useDroppableCollection.
if (this.layout === 'stack' && this.orientation === 'vertical') {
this.getKeyLeftOf = undefined;
this.getKeyRightOf = undefined;
}
}
private isDisabled(item: Node<unknown>) {
return this.disabledBehavior === 'all' && (item.props?.isDisabled || this.disabledKeys.has(item.key));
}
private findNextNonDisabled(key: Key, getNext: (key: Key) => Key | null): Key | null {
while (key != null) {
let item = this.collection.getItem(key);
if (item?.type === 'item' && !this.isDisabled(item)) {
return key;
}
key = getNext(key);
}
return null;
}
getNextKey(key: Key) {
key = this.collection.getKeyAfter(key);
return this.findNextNonDisabled(key, key => this.collection.getKeyAfter(key));
}
getPreviousKey(key: Key) {
key = this.collection.getKeyBefore(key);
return this.findNextNonDisabled(key, key => this.collection.getKeyBefore(key));
}
private findKey(
key: Key,
nextKey: (key: Key) => Key,
shouldSkip: (prevRect: Rect, itemRect: Rect) => boolean
) {
let itemRect = this.layoutDelegate.getItemRect(key);
if (!itemRect) {
return null;
}
// Find the item above or below in the same column.
let prevRect = itemRect;
do {
key = nextKey(key);
itemRect = this.layoutDelegate.getItemRect(key);
} while (itemRect && shouldSkip(prevRect, itemRect));
return key;
}
private isSameRow(prevRect: Rect, itemRect: Rect) {
return prevRect.y === itemRect.y || prevRect.x !== itemRect.x;
}
private isSameColumn(prevRect: Rect, itemRect: Rect) {
return prevRect.x === itemRect.x || prevRect.y !== itemRect.y;
}
getKeyBelow(key: Key) {
if (this.layout === 'grid' && this.orientation === 'vertical') {
return this.findKey(key, (key) => this.getNextKey(key), this.isSameRow);
} else {
return this.getNextKey(key);
}
}
getKeyAbove(key: Key) {
if (this.layout === 'grid' && this.orientation === 'vertical') {
return this.findKey(key, (key) => this.getPreviousKey(key), this.isSameRow);
} else {
return this.getPreviousKey(key);
}
}
private getNextColumn(key: Key, right: boolean) {
return right ? this.getPreviousKey(key) : this.getNextKey(key);
}
getKeyRightOf(key: Key) {
// This is a temporary solution for CardView until we refactor useSelectableCollection.
// https://github.com/orgs/adobe/projects/19/views/32?pane=issue&itemId=77825042
let layoutDelegateMethod = this.direction === 'ltr' ? 'getKeyRightOf' : 'getKeyLeftOf';
if (this.layoutDelegate[layoutDelegateMethod]) {
key = this.layoutDelegate[layoutDelegateMethod](key);
return this.findNextNonDisabled(key, key => this.layoutDelegate[layoutDelegateMethod](key));
}
if (this.layout === 'grid') {
if (this.orientation === 'vertical') {
return this.getNextColumn(key, this.direction === 'rtl');
} else {
return this.findKey(key, (key) => this.getNextColumn(key, this.direction === 'rtl'), this.isSameColumn);
}
} else if (this.orientation === 'horizontal') {
return this.getNextColumn(key, this.direction === 'rtl');
}
return null;
}
getKeyLeftOf(key: Key) {
let layoutDelegateMethod = this.direction === 'ltr' ? 'getKeyLeftOf' : 'getKeyRightOf';
if (this.layoutDelegate[layoutDelegateMethod]) {
key = this.layoutDelegate[layoutDelegateMethod](key);
return this.findNextNonDisabled(key, key => this.layoutDelegate[layoutDelegateMethod](key));
}
if (this.layout === 'grid') {
if (this.orientation === 'vertical') {
return this.getNextColumn(key, this.direction === 'ltr');
} else {
return this.findKey(key, (key) => this.getNextColumn(key, this.direction === 'ltr'), this.isSameColumn);
}
} else if (this.orientation === 'horizontal') {
return this.getNextColumn(key, this.direction === 'ltr');
}
return null;
}
getFirstKey() {
let key = this.collection.getFirstKey();
return this.findNextNonDisabled(key, key => this.collection.getKeyAfter(key));
}
getLastKey() {
let key = this.collection.getLastKey();
return this.findNextNonDisabled(key, key => this.collection.getKeyBefore(key));
}
getKeyPageAbove(key: Key) {
let menu = this.ref.current;
let itemRect = this.layoutDelegate.getItemRect(key);
if (!itemRect) {
return null;
}
if (!isScrollable(menu)) {
return this.getFirstKey();
}
if (this.orientation === 'horizontal') {
let pageX = Math.max(0, itemRect.x + itemRect.width - this.layoutDelegate.getVisibleRect().width);
while (itemRect && itemRect.x > pageX) {
key = this.getKeyAbove(key);
itemRect = key == null ? null : this.layoutDelegate.getItemRect(key);
}
} else {
let pageY = Math.max(0, itemRect.y + itemRect.height - this.layoutDelegate.getVisibleRect().height);
while (itemRect && itemRect.y > pageY) {
key = this.getKeyAbove(key);
itemRect = key == null ? null : this.layoutDelegate.getItemRect(key);
}
}
return key ?? this.getFirstKey();
}
getKeyPageBelow(key: Key) {
let menu = this.ref.current;
let itemRect = this.layoutDelegate.getItemRect(key);
if (!itemRect) {
return null;
}
if (!isScrollable(menu)) {
return this.getLastKey();
}
if (this.orientation === 'horizontal') {
let pageX = Math.min(this.layoutDelegate.getContentSize().width, itemRect.y - itemRect.width + this.layoutDelegate.getVisibleRect().width);
while (itemRect && itemRect.x < pageX) {
key = this.getKeyBelow(key);
itemRect = key == null ? null : this.layoutDelegate.getItemRect(key);
}
} else {
let pageY = Math.min(this.layoutDelegate.getContentSize().height, itemRect.y - itemRect.height + this.layoutDelegate.getVisibleRect().height);
while (itemRect && itemRect.y < pageY) {
key = this.getKeyBelow(key);
itemRect = key == null ? null : this.layoutDelegate.getItemRect(key);
}
}
return key ?? this.getLastKey();
}
getKeyForSearch(search: string, fromKey?: Key) {
if (!this.collator) {
return null;
}
let collection = this.collection;
let key = fromKey || this.getFirstKey();
while (key != null) {
let item = collection.getItem(key);
let substring = item.textValue.slice(0, search.length);
if (item.textValue && this.collator.compare(substring, search) === 0) {
return key;
}
key = this.getNextKey(key);
}
return null;
}
}