Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/@react-aria/collections/src/BaseCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ export class SectionNode<T> extends FilterableNode<T> {
* custom collection behaviors.
*/
export class BaseCollection<T> implements ICollection<Node<T>> {
private keyMap: Map<Key, CollectionNode<T>> = new Map();
private firstKey: Key | null = null;
private lastKey: Key | null = null;
private frozen = false;
private itemCount: number = 0;
protected keyMap: Map<Key, CollectionNode<T>> = new Map();
protected firstKey: Key | null = null;
protected lastKey: Key | null = null;
protected frozen = false;
protected itemCount: number = 0;

get size(): number {
return this.itemCount;
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-aria/collections/src/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class ElementNode<T> extends BaseNode<T> {

get level(): number {
if (this.parentNode instanceof ElementNode) {
return this.parentNode.level + (this.node?.type === 'item' ? 1 : 0);
return this.parentNode.level + (this.parentNode.node?.type === 'item' ? 1 : 0);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here was incorrect: we should increment the level if the parent is an item, not if the child is. This means that loaders and other elements have the same level as their siblings.

}

return 0;
Expand Down
6 changes: 5 additions & 1 deletion packages/@react-aria/gridlist/src/useGridListItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ export function useGridListItem<T>(props: AriaGridListItemOptions, state: ListSt

let isExpanded = hasChildRows ? state.expandedKeys.has(node.key) : undefined;
let setSize = 1;
let index = node.index;
if (node.level >= 0 && node?.parentKey != null) {
let parent = state.collection.getItem(node.parentKey);
if (parent) {
// siblings must exist because our original node exists
let siblings = getDirectChildren(parent, state.collection);
setSize = [...siblings].filter(row => row.type === 'item').length;
if (index > 0 && siblings[0].type !== 'item') {
index -= 1; // subtract one for the parent item's content node
}
}
} else {
setSize = [...state.collection].filter(row => row.level === 0 && row.type === 'item').length;
Expand All @@ -114,7 +118,7 @@ export function useGridListItem<T>(props: AriaGridListItemOptions, state: ListSt
treeGridRowProps = {
'aria-expanded': isExpanded,
'aria-level': node.level + 1,
'aria-posinset': node?.index + 1,
'aria-posinset': index + 1,
'aria-setsize': setSize
};
}
Expand Down
54 changes: 1 addition & 53 deletions packages/@react-aria/selection/src/ListKeyboardDelegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ interface ListKeyboardDelegateOptions<T> {
direction?: Direction,
disabledKeys?: Set<Key>,
disabledBehavior?: DisabledBehavior,
layoutDelegate?: LayoutDelegate,
expandedKeys?: Set<Key>
layoutDelegate?: LayoutDelegate
}

export class ListKeyboardDelegate<T> implements KeyboardDelegate {
Expand All @@ -37,7 +36,6 @@ export class ListKeyboardDelegate<T> implements KeyboardDelegate {
private orientation?: Orientation;
private direction?: Direction;
private layoutDelegate: LayoutDelegate;
private expandedKeys?: Set<Key>;

constructor(collection: Collection<Node<T>>, disabledKeys: Set<Key>, ref: RefObject<HTMLElement | null>, collator?: Intl.Collator, expandedKeys?: Set<Key>);
constructor(options: ListKeyboardDelegateOptions<T>);
Expand All @@ -53,7 +51,6 @@ export class ListKeyboardDelegate<T> implements KeyboardDelegate {
this.direction = opts.direction;
this.layout = opts.layout || 'stack';
this.layoutDelegate = opts.layoutDelegate || new DOMLayoutDelegate(opts.ref);
this.expandedKeys = opts.expandedKeys;
} else {
this.collection = args[0];
this.disabledKeys = args[1];
Expand All @@ -63,7 +60,6 @@ export class ListKeyboardDelegate<T> implements KeyboardDelegate {
this.orientation = 'vertical';
this.disabledBehavior = 'all';
this.layoutDelegate = new DOMLayoutDelegate(this.ref);
this.expandedKeys = args[4];
}

// If this is a vertical stack, remove the left/right methods completely
Expand Down Expand Up @@ -92,50 +88,6 @@ export class ListKeyboardDelegate<T> implements KeyboardDelegate {
return null;
}

// Returns the first key that's visible starting from and inclusive of the provided key
private findNextVisible(key: Key | null): Key | null {
let node = key ? this.collection.getItem(key) : null;
if (!node) {
return null;
}

// If the node's parent is expanded, then we can assume that this is a visible node
if (node.parentKey && this.expandedKeys?.has(node.parentKey)) {
return node.key;
}

// If the node's parent is not expanded, find the top-most non-expanded node since it's possible for them to be nested
let parentNode = node.parentKey ? this.collection.getItem(node.parentKey) : null;
// if the the parent node is not a section, and the parent node is not included in expanded keys
while (parentNode && parentNode.type !== 'section' && node && node.parentKey && this.expandedKeys && !this.expandedKeys.has(parentNode.key)) {
node = this.collection.getItem(node.parentKey);
parentNode = node && node.parentKey ? this.collection.getItem(node.parentKey) : null;
}

return node?.key ?? null;
}

// Returns the first key that's visible and non-disabled starting from and inclusive of the provided key
private findNextNonDisabledVisible(key: Key | null, getNext: (key: Key) => Key | null) {
let nextKey = key;
while (nextKey !== null) {
let visibleKey = this.findNextVisible(nextKey);
// If visibleKey is null, that means there are no visibleKeys (don't feel like this is a real use case though, I would assume that there is always one visible node)
if (visibleKey == null) {
return null;
}

let node = this.collection.getItem(visibleKey);
if (node?.type === 'item' && !this.isDisabled(node)) {
return visibleKey;
}

nextKey = getNext(visibleKey);
}

return null;
}

getNextKey(key: Key): Key | null {
let nextKey: Key | null = key;
nextKey = this.collection.getKeyAfter(nextKey);
Expand Down Expand Up @@ -249,10 +201,6 @@ export class ListKeyboardDelegate<T> implements KeyboardDelegate {

getLastKey(): Key | null {
let key = this.collection.getLastKey();
// we only need to check for visible keys if items can be expanded/collapsed
if (this.expandedKeys) {
return this.findNextNonDisabledVisible(key, key => this.collection.getKeyBefore(key));
}
return this.findNextNonDisabled(key, key => this.collection.getKeyBefore(key));
}

Expand Down
Loading