WIP fix selectNext

This commit is contained in:
ivk 2025-12-05 17:24:24 +01:00
parent 32fba17cd3
commit 28903238a1

View file

@ -6,7 +6,6 @@ import {
findLast, findLast,
findLastIndex, findLastIndex,
first, first,
getFirstOrThrow,
isEmpty, isEmpty,
last, last,
lastIndex, lastIndex,
@ -60,6 +59,10 @@ type PrivateListState<ItemType> = Omit<ListState<ItemType>, "items" | "activeInd
activeItem: ItemType | null activeItem: ItemType | null
} }
function firstIndex(arr: readonly unknown[]): number {
return isEmpty(arr) ? -1 : 0
}
/** ListModel that does the state upkeep for the List, including loading state, loaded items, selection and filters*/ /** ListModel that does the state upkeep for the List, including loading state, loaded items, selection and filters*/
export class ListModel<ItemType, IdType> { export class ListModel<ItemType, IdType> {
constructor(private readonly config: ListModelConfig<ItemType, IdType>) {} constructor(private readonly config: ListModelConfig<ItemType, IdType>) {}
@ -333,8 +336,6 @@ export class ListModel<ItemType, IdType> {
const oldActiveItem = this.rawState.activeItem const oldActiveItem = this.rawState.activeItem
const oldActiveIndex = oldActiveItem ? this.state.items.indexOf(oldActiveItem) : -1 const oldActiveIndex = oldActiveItem ? this.state.items.indexOf(oldActiveItem) : -1
const firstIndex = (arr: readonly unknown[]) => (isEmpty(arr) ? -1 : 0)
const newActiveIndex: number = const newActiveIndex: number =
oldActiveIndex === -1 oldActiveIndex === -1
? oldActiveItem ? oldActiveItem
@ -405,15 +406,20 @@ export class ListModel<ItemType, IdType> {
} else { } else {
const selectedItems = new Set(this.state.selectedItems) const selectedItems = new Set(this.state.selectedItems)
this.rangeSelectionAnchorItem = this.rangeSelectionAnchorItem ?? first(this.state.items) this.rangeSelectionAnchorItem = this.rangeSelectionAnchorItem ?? first(this.state.items)
if (!this.rangeSelectionAnchorItem) return const anchorItem = this.rangeSelectionAnchorItem
if (!anchorItem) return
const previousActiveIndex = this.state.activeIndex ?? 0 const anchorIndex = this.state.items.findIndex((item) => this.config.isSameId(this.config.getItemId(item), this.config.getItemId(anchorItem)))
const towardsAnchor = this.config.sortCompare(oldActiveItem ?? getFirstOrThrow(this.state.items), this.rangeSelectionAnchorItem) < 0 if (anchorIndex !== -1 && oldActiveIndex !== -1) {
if (towardsAnchor) { const towardsAnchor = oldActiveIndex < anchorIndex
selectedItems.delete(this.state.items[previousActiveIndex]) if (towardsAnchor) {
selectedItems.delete(this.state.items[oldActiveIndex])
} else {
selectedItems.add(newActiveItem)
}
} else { } else {
selectedItems.add(newActiveItem) selectedItems.add(newActiveItem)
} }
this.updateState({ selectedItems, inMultiselect: true, activeItem: newActiveItem }) this.updateState({ selectedItems, inMultiselect: true, activeItem: newActiveItem })
} }
} }