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(combobox): handle loop property correctly #950

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/yellow-coins-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zag-js/combobox": patch
---

Fix a case where item highlight was looping even though loop property was false
2 changes: 1 addition & 1 deletion .xstate/combobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const fetchMachine = createMachine({
actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
}, {
target: "interacting",
actions: ["highlightNextItem", "invokeOnOpen"]
segunadebayo marked this conversation as resolved.
Show resolved Hide resolved
actions: ["highlightFirstItem", "invokeOnOpen"]
}],
"INPUT.ARROW_DOWN+ALT": {
target: "interacting",
Expand Down
47 changes: 45 additions & 2 deletions e2e/combobox.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,35 @@ test.describe("combobox", () => {
await expect(page.locator(content)).toBeHidden()
})

test("[keyboard] on arrow down, open and highlight first enabled option", async ({ page }) => {
test("[keyboard / loop] on arrow down, open and highlight first enabled option", async ({ page }) => {
await page.focus(input)
await page.keyboard.press("ArrowDown")
const option = page.locator(options).first()
await expect(page.locator(content)).toBeVisible()
await expectToBeHighlighted(option)
})

test("[keyboard] on arrow up, open and highlight last enabled option", async ({ page }) => {
test("[keyboard / no-loop] on arrow down, open and highlight first enabled option", async ({ page }) => {
await controls(page).bool("loop", false)

await page.focus(input)
await page.keyboard.press("ArrowDown")
const option = page.locator(options).first()
await expect(page.locator(content)).toBeVisible()
await expectToBeHighlighted(option)
})

test("[keyboard / loop] on arrow up, open and highlight last enabled option", async ({ page }) => {
await page.focus(input)
await page.keyboard.press("ArrowUp")
const option = page.locator(options).last()
await expect(page.locator(content)).toBeVisible()
await expectToBeHighlighted(option)
})

test("[keyboard / no-loop] on arrow up, open and highlight last enabled option", async ({ page }) => {
await controls(page).bool("loop", false)

await page.focus(input)
await page.keyboard.press("ArrowUp")
const option = page.locator(options).last()
Expand Down Expand Up @@ -137,13 +157,36 @@ test.describe("combobox", () => {
await expectToBeHighlighted(option_els.first())
})

test("[keyboard / arrowdown / no-loop]", async ({ page }) => {
await controls(page).bool("loop", false)

await page.type(input, "mal")

const option_els = page.locator(options)

await repeat(4, () => page.keyboard.press("ArrowDown"))

await expectToBeHighlighted(option_els.last())
await page.keyboard.press("ArrowDown")
await expectToBeHighlighted(option_els.last())
})

test("[keyboard / arrowup / loop]", async ({ page }) => {
await page.type(input, "mal")
const option_els = page.locator(options)
await page.keyboard.press("ArrowUp")
await expectToBeHighlighted(option_els.last())
})

test("[keyboard / arrowup / no-loop]", async ({ page }) => {
await controls(page).bool("loop", false)

await page.type(input, "mal")
const option_els = page.locator(options)
await page.keyboard.press("ArrowUp")
await expectToBeHighlighted(option_els.first())
})

test("[pointer / open-on-click]", async ({ page }) => {
await controls(page).bool("openOnClick")
await page.click(input, { force: true })
Expand Down
6 changes: 3 additions & 3 deletions packages/machines/combobox/src/combobox.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function machine<T extends CollectionItem>(userContext: UserDefinedContex
},
{
target: "interacting",
actions: ["highlightNextItem", "invokeOnOpen"],
actions: ["highlightFirstItem", "invokeOnOpen"],
},
],
"INPUT.ARROW_DOWN+ALT": {
Expand Down Expand Up @@ -558,11 +558,11 @@ export function machine<T extends CollectionItem>(userContext: UserDefinedContex
set.highlightedItem(ctx, value)
},
highlightNextItem(ctx) {
const value = ctx.collection.next(ctx.highlightedValue) ?? ctx.collection.first()
const value = ctx.collection.next(ctx.highlightedValue) ?? (ctx.loop ? ctx.collection.first() : null)
set.highlightedItem(ctx, value)
},
highlightPrevItem(ctx) {
const value = ctx.collection.prev(ctx.highlightedValue) ?? ctx.collection.last()
const value = ctx.collection.prev(ctx.highlightedValue) ?? (ctx.loop ? ctx.collection.last() : null)
set.highlightedItem(ctx, value)
},
highlightFirstSelectedItem(ctx) {
Expand Down
2 changes: 1 addition & 1 deletion shared/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const comboboxData = [
{ label: "Albania", code: "AL" },
{ label: "Algeria", code: "DZ" },
{ label: "American Samoa", code: "AS" },
{ label: "AndorrA", code: "AD" },
{ label: "Andorra", code: "AD" },
{ label: "Angola", code: "AO" },
// { label: "Angola", code: "AO" },
{ label: "Anguilla", code: "AI" },
Expand Down
3 changes: 1 addition & 2 deletions website/components/machines/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const comboboxData = [
{ label: "Albania", code: "AL" },
{ label: "Algeria", code: "DZ" },
{ label: "American Samoa", code: "AS" },
{ label: "AndorrA", code: "AD" },
{ label: "Angola", code: "AO" },
{ label: "Andorra", code: "AD" },
{ label: "Angola", code: "AO" },
{ label: "Anguilla", code: "AI" },
{ label: "Antarctica", code: "AQ" },
Expand Down
Loading