Skip to content

Commit

Permalink
Fix selection via enter key
Browse files Browse the repository at this point in the history
Regressed in #37
  • Loading branch information
josefarias committed Feb 29, 2024
1 parent 048830c commit 095cc2f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
14 changes: 9 additions & 5 deletions app/assets/javascripts/hw_combobox/models/combobox/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ Combobox.Selection = Base => class extends Base {
}
}

_selectFuzzyMatch() {
if (this._isFuzzyMatch) {
this._select(this._visibleOptionElements[0], { force: true })
_ensureSelection() {
if (this._shouldEnsureSelection) {
this._select(this._ensurableOption, { force: true })
this.filter(nullEvent)
}
}
Expand All @@ -82,7 +82,11 @@ Combobox.Selection = Base => class extends Base {
return this.hiddenFieldTarget.value && !this._selectedOptionElement
}

get _isFuzzyMatch() {
return this._isQueried && !!this._visibleOptionElements[0] && !this._isNewOptionWithPotentialMatches
get _shouldEnsureSelection() {
return this._isQueried && !!this._ensurableOption && !this._isNewOptionWithPotentialMatches
}

get _ensurableOption() {
return this._selectedOptionElement || this._visibleOptionElements[0]
}
}
4 changes: 0 additions & 4 deletions app/assets/javascripts/hw_combobox/models/combobox/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ Combobox.Toggle = Base => class extends Base {
return clientX >= left && clientX <= right && clientY >= top && clientY <= bottom
}

_ensureSelection() {
this._selectFuzzyMatch()
}

_openByFocusing() {
this._actingCombobox.focus()
}
Expand Down
13 changes: 13 additions & 0 deletions test/system/hotwire_combobox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ class HotwireComboboxTest < ApplicationSystemTestCase
assert_selected_option_with selector: ".hw-combobox__option--selected", text: "Florida"
end

test "selecting with the keyboard" do
visit html_options_path

open_combobox "#state-field"
type_in_combobox "#state-field", "mi"
type_in_combobox "#state-field", :down, :down
assert_selected_option_with text: "Mississippi"

type_in_combobox "#state-field", :enter
assert_closed_combobox
assert_combobox_display_and_value "#state-field", "Mississippi", "MS"
end

test "pressing enter locks in the current selection, but editing the text field resets it" do
visit html_options_path

Expand Down

0 comments on commit 095cc2f

Please sign in to comment.