web: fix list crash on shift+click when there's no selection

This commit is contained in:
Abdullah Atta
2023-05-27 16:47:25 +05:00
committed by Abdullah Atta
parent 4eabe79e7e
commit 2da0f164c6
4 changed files with 23 additions and 5 deletions

View File

@@ -256,6 +256,23 @@ test("select notes using Shift+Click upwards", async ({ page }, info) => {
expect(await notesList[0].isFocused()).toBeTruthy();
});
test("using Shift+Click when no notes are selected should not crash the app", async ({
page
}, info) => {
info.setTimeout(60 * 1000);
const { notes } = await populateList(page, 5);
await page.reload();
const note = await notes.findNote({ title: "Test note 3" });
await page.keyboard.down("Shift");
await note?.click();
await page.keyboard.up("Shift");
expect(await notes.isEmpty()).toBeFalsy();
});
test("Ctrl+Click to select/unselect notes", async ({ page }, info) => {
info.setTimeout(60 * 1000);
const { notesList, notes } = await populateList(page, 10);

View File

@@ -114,7 +114,7 @@ function ListContainer(props: ListContainerProps) {
element.focus()
);
},
skip: (index) => items[index].type === "header",
skip: (index) => !items[index] || items[index].type === "header",
open: (index) => {
const item = items[index];
if (!item || !listRef.current) return;

View File

@@ -70,11 +70,11 @@ function ListItem(props) {
const isMenuTarget = target && target === listItemRef.current;
const isSelected = useSelectionStore((store) => {
const inInSelection =
const isInSelection =
store.selectedItems.findIndex((item) => props.item.id === item.id) > -1;
return isFocused
? store.selectedItems.length > 1 && inInSelection
: inInSelection;
? store.selectedItems.length > 1 && isInSelection
: isInSelection;
});
return (

View File

@@ -90,6 +90,7 @@ export function useKeyboardListNavigation(
itemIndex > cursor.current ? itemIndex : cursor.current;
const indices = [];
for (let i = startIndex; i <= endIndex; ++i) {
if (skip && skip(i)) continue;
indices.push(i);
}
bulkSelect(indices);
@@ -99,7 +100,7 @@ export function useKeyboardListNavigation(
select(itemIndex);
}
},
[select, resetSelection, bulkSelect, focusItemAt]
[select, resetSelection, bulkSelect, skip, focusItemAt]
);
const onKeyDown = useCallback(