Skip to content

Commit

Permalink
Fix typeahead search logic when repeating a letter
Browse files Browse the repository at this point in the history
  • Loading branch information
rgossiaux committed Jan 22, 2022
1 parent bc809ef commit 5e795d2
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/lib/components/listbox/Listbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,22 @@
searchQuery += value.toLowerCase();
let match = options.findIndex(
let reorderedOptions =
activeOptionIndex !== null
? options
.slice(activeOptionIndex + 1)
.concat(options.slice(0, activeOptionIndex + 1))
: options;
let matchingOption = reorderedOptions.find(
(option) =>
!option.dataRef.disabled &&
option.dataRef.textValue.startsWith(searchQuery)
);
if (match === -1 || match === activeOptionIndex) return;
activeOptionIndex = match;
let matchIdx = matchingOption ? options.indexOf(matchingOption) : -1;
if (matchIdx === -1 || matchIdx === activeOptionIndex) return;
activeOptionIndex = matchIdx;
},
clearSearch() {
if (disabled) return;
Expand Down
40 changes: 40 additions & 0 deletions src/lib/components/listbox/listbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3125,6 +3125,46 @@ describe('Keyboard interactions', () => {
assertActiveListboxOption(options[1])
})
)

it(
'should be possible to search for the next occurence',
suppressConsoleLogs(async () => {
render(svelte`
<Listbox value={undefined} onChange={console.log}>
<ListboxButton>Trigger</ListboxButton>
<ListboxOptions>
<ListboxOption value="a">alice</ListboxOption>
<ListboxOption value="b">bob</ListboxOption>
<ListboxOption value="c">charlie</ListboxOption>
<ListboxOption value="d">bob</ListboxOption>
</ListboxOptions>
</Listbox>
`)

// Open listbox
await click(getListboxButton())

let options = getListboxOptions()

// Search for bob
await type(word('b'))

// We should be on the first `bob`
assertActiveListboxOption(options[1])

// Search for bob again
await type(word('b'))

// We should be on the second `bob`
assertActiveListboxOption(options[3])

// Search for bob once again
await type(word('b'))

// We should be back on the first `bob`
assertActiveListboxOption(options[1])
})
)
})
})

Expand Down
14 changes: 11 additions & 3 deletions src/lib/components/menu/Menu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,22 @@
search(value: string) {
searchQuery += value.toLowerCase();
let match = items.findIndex(
let reorderedItems =
activeItemIndex !== null
? items
.slice(activeItemIndex + 1)
.concat(items.slice(0, activeItemIndex + 1))
: items;
let matchingItem = reorderedItems.find(
(item) =>
item.data.textValue.startsWith(searchQuery) && !item.data.disabled
);
if (match === -1 || match === activeItemIndex) return;
let matchIdx = matchingItem ? items.indexOf(matchingItem) : -1;
if (matchIdx === -1 || matchIdx === activeItemIndex) return;
activeItemIndex = match;
activeItemIndex = matchIdx;
},
clearSearch() {
searchQuery = "";
Expand Down
40 changes: 40 additions & 0 deletions src/lib/components/menu/menu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2682,6 +2682,46 @@ describe('Keyboard interactions', () => {
assertMenuLinkedWithMenuItem(items[1])
})
)

it(
'should be possible to search for the next occurence',
suppressConsoleLogs(async () => {
render(svelte`
<Menu>
<MenuButton>Trigger</MenuButton>
<MenuItems>
<MenuItem as="a">alice</MenuItem>
<MenuItem as="a">bob</MenuItem>
<MenuItem as="a">charlie</MenuItem>
<MenuItem as="a">bob</MenuItem>
</MenuItems>
</Menu>
`)

// Open menu
await click(getMenuButton())

let items = getMenuItems()

// Search for bob
await type(word('b'))

// We should be on the first `bob`
assertMenuLinkedWithMenuItem(items[1])

// Search for bob again
await type(word('b'))

// We should be on the second `bob`
assertMenuLinkedWithMenuItem(items[3])

// Search for bob once again
await type(word('b'))

// We should be back on the first `bob`
assertMenuLinkedWithMenuItem(items[1])
})
)
})
})

Expand Down

0 comments on commit 5e795d2

Please sign in to comment.