Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Selection Behaviour of ListBox #842

Merged
merged 1 commit into from
Jan 9, 2024
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
42 changes: 41 additions & 1 deletion headless-demo/tests/components/listbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test.describe('To open and close a listBox', () => {
await expect(page.locator("#" + itemId)).toHaveAttribute("data-listbox-selected", "false")
}

const [btn, listBoxItems] = await createLocators(page)
const [btn, listBoxItems] = await createLocators(page)

await btn.focus()
await assertListBoxIsClosed(btn, listBoxItems)
Expand Down Expand Up @@ -225,6 +225,46 @@ test.describe("To select an item from a listBox open the listBoxItems", () => {
await expect(listBoxItems).toHaveAttribute("aria-activedescendant", "starwars-item-3")
});

[
{
name: 'click on item', selectAction: async (item: Locator) => {
await item.click()
}
},
{
name: 'pressing Enter', selectAction: async (item: Locator) => {
await item.hover()
await item.press("Enter")
}
},
{
name: 'presssing Space', selectAction: async (item: Locator) => {
await item.hover()
await item.press("Space")
}
},
].forEach(({name, selectAction}) => {
test(`and select the same item twice by ${name} will close the listBox`, async ({page}) => {
const listBoxItems = page.locator("#starwars-items")
const result = page.locator('#result')
const han = listBoxItems.getByText("Han")

// first selection
await page.getByRole("button", {name: "Luke"}).click()
await expect(listBoxItems).toBeVisible()
await selectAction(han)
await expect(listBoxItems).toBeHidden()
await expect(result).toContainText("Han")

// second selection
await page.getByRole("button", {name: "Han"}).click()
await expect(listBoxItems).toBeVisible()
await selectAction(han)
await expect(listBoxItems).toBeHidden()
await expect(result).toContainText("Han")
})
})

for (const key of ["Enter", "Space"]) {
test(`then press ${key}`, async ({page}) => {
const btn = page.locator("#starwars-button")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,16 @@ class Listbox<T, C : HTMLElement>(tag: Tag<C>, id: String?) : Tag<C> by tag, Ope
val currentIndex = activeIndex.current
val entries = entries.current
when (shortcutOf(event)) {
Keys.ArrowUp -> nextItem(currentIndex, Direction.Previous, entries)
Keys.ArrowDown -> nextItem(currentIndex, Direction.Next, entries)
Keys.Home -> firstItem(entries)
Keys.End -> lastItem(entries)
else -> null
}.also {
if (it != null) {
event.preventDefault()
event.stopImmediatePropagation()
}
Keys.ArrowUp -> nextItem(currentIndex, Direction.Previous, entries)
Keys.ArrowDown -> nextItem(currentIndex, Direction.Next, entries)
Keys.Home -> firstItem(entries)
Keys.End -> lastItem(entries)
else -> null
}.also {
if (it != null) {
event.preventDefault()
event.stopImmediatePropagation()
}
}
} handledBy activeIndex.update

Expand Down Expand Up @@ -247,7 +247,7 @@ class Listbox<T, C : HTMLElement>(tag: Tag<C>, id: String?) : Tag<C> by tag, Ope
it.stopImmediatePropagation()
entries[currentIndex].value
}
}
}.onEach { close() }
)

opened.filter { it }.flatMapLatest {
Expand Down Expand Up @@ -288,7 +288,7 @@ class Listbox<T, C : HTMLElement>(tag: Tag<C>, id: String?) : Tag<C> by tag, Ope
if (it.disabled) null
else it.value
}
})
}.onEach { close() })

value.data.map {} handledBy close
}
Expand Down