Skip to content

Commit b7c025b

Browse files
authored
add toggle to combobox trigger (#321)
1 parent e377f1c commit b7c025b

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

lib/ruby_ui/combobox/combobox_controller.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export default class extends Controller {
2828
if (this.cleanup) { this.cleanup() }
2929
}
3030

31+
handlePopoverToggle(event) {
32+
// Keep ariaExpanded in sync with the actual popover state
33+
this.triggerTarget.ariaExpanded = event.newState === 'open' ? 'true' : 'false'
34+
}
35+
3136
inputChanged(e) {
3237
this.updateTriggerContent()
3338

@@ -62,9 +67,19 @@ export default class extends Controller {
6267
}
6368
}
6469

65-
openPopover(event) {
70+
togglePopover(event) {
6671
event.preventDefault()
6772

73+
if (this.triggerTarget.ariaExpanded === "true") {
74+
this.closePopover()
75+
} else {
76+
this.openPopover(event)
77+
}
78+
}
79+
80+
openPopover(event) {
81+
if (event) event.preventDefault()
82+
6883
this.updatePopoverPosition()
6984
this.updatePopoverWidth()
7085
this.triggerTarget.ariaExpanded = "true"

lib/ruby_ui/combobox/combobox_popover.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def default_attrs
1717
data: {
1818
ruby_ui__combobox_target: "popover",
1919
action: %w[
20+
toggle->ruby-ui--combobox#handlePopoverToggle
2021
keydown.down->ruby-ui--combobox#keyDownPressed
2122
keydown.up->ruby-ui--combobox#keyUpPressed
2223
keydown.enter->ruby-ui--combobox#keyEnterPressed

lib/ruby_ui/combobox/combobox_trigger.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def default_attrs
3131
data: {
3232
placeholder: @placeholder,
3333
ruby_ui__combobox_target: "trigger",
34-
action: "ruby-ui--combobox#openPopover"
34+
action: "ruby-ui--combobox#togglePopover"
3535
},
3636
aria: {
3737
haspopup: "listbox",

0 commit comments

Comments
 (0)