Skip to content
Closed
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
9 changes: 8 additions & 1 deletion assets/js/hooks/combobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,15 @@ export default {
if (!this.isOpen) {
return
}

const focusedOption = this.getCurrentFocusedOption()
if (e.key === KEYS.TAB && !this.getVisibleOptions().includes(focusedOption)) {
this.handleBlur()
return
}

e.preventDefault()
this.selectOption(this.getCurrentFocusedOption())
this.selectOption(focusedOption)
},

handleBackspace(e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<input id="previous-input" aria-label="Previous field" />

<.combobox id="demo-async-combobox" name="user[favourite_fruit]">
<.combobox_input
on_search="async_combobox_search"
Expand All @@ -19,3 +21,5 @@
<% end %>
</.combobox_options>
</.combobox>

<input id="next-input" aria-label="Next field" />
36 changes: 36 additions & 0 deletions demo/test/wallaby/demo_web/combobox_keyboard_navigation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,42 @@ defmodule DemoWeb.ComboboxKeyboardNavigationTest do
|> assert_has(@options_container |> Query.visible(false))
end

for {key, keys, target} <- [
{"Tab", [:tab], "next-input"},
{"Shift+Tab", [:shift, :tab], "previous-input"}
] do
@tag keys: keys, target: target
@feature_name "#{key} leaves an async combobox with no results"
feature @feature_name, %{
session: session,
keys: keys,
target: target
} do
input = Query.css("#demo-async-combobox input[data-prima-ref=search_input]")

session
|> visit_fixture("/fixtures/async-combobox", "#demo-async-combobox")
|> click(input)
|> click(Query.css("#demo-async-combobox [role=option][data-value=Cherry]"))
|> fill_in(input, with: "no matching fruit")
|> assert_has(Query.css("#demo-async-combobox-options", visible: true))
|> assert_has(Query.css("#demo-async-combobox [role=option]", count: 0))
|> send_keys(keys)
|> assert_has(Query.css("##{target}:focus"))
|> assert_has(Query.css("#demo-async-combobox-options", visible: false))
|> execute_script(
"""
const root = document.querySelector('#demo-async-combobox');
return {
search: root.querySelector('[data-prima-ref=search_input]').value,
selection: root.querySelector('[data-prima-ref=submit_input]').value
};
""",
fn values -> assert values == %{"search" => "Cherry", "selection" => "Cherry"} end
)
end
end

feature "ArrowDown opens combobox when closed and input is focused", %{session: session} do
session
|> visit_fixture("/fixtures/simple-combobox", "#demo-combobox")
Expand Down
Loading