forked from ruby-ui/web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombobox_controller.js
More file actions
176 lines (141 loc) · 4.35 KB
/
combobox_controller.js
File metadata and controls
176 lines (141 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { Controller } from "@hotwired/stimulus";
import { computePosition, autoUpdate, offset, flip } from "@floating-ui/dom";
// Connects to data-controller="ruby-ui--combobox"
export default class extends Controller {
static values = {
term: String
}
static targets = [
"input",
"toggleAll",
"popover",
"item",
"emptyState",
"searchInput",
"trigger",
"triggerContent"
]
selectedItemIndex = null
connect() {
this.updateTriggerContent()
}
disconnect() {
if (this.cleanup) { this.cleanup() }
}
inputChanged(e) {
this.updateTriggerContent()
if (e.target.type == "radio") {
this.closePopover()
}
if (this.hasToggleAllTarget && !e.target.checked) {
this.toggleAllTarget.checked = false
}
}
inputContent(input) {
return input.dataset.text || input.parentElement.textContent
}
toggleAllItems() {
const isChecked = this.toggleAllTarget.checked
this.inputTargets.forEach(input => input.checked = isChecked)
this.updateTriggerContent()
}
updateTriggerContent() {
const checkedInputs = this.inputTargets.filter(input => input.checked)
if (checkedInputs.length == 0) {
this.triggerContentTarget.innerText = this.triggerTarget.dataset.placeholder
} else if (checkedInputs.length === 1) {
this.triggerContentTarget.innerText = this.inputContent(checkedInputs[0])
} else {
this.triggerContentTarget.innerText = `${checkedInputs.length} ${this.termValue}`
}
}
openPopover(event) {
event.preventDefault()
this.updatePopoverPosition()
this.updatePopoverWidth()
this.triggerTarget.ariaExpanded = "true"
this.selectedItemIndex = null
this.itemTargets.forEach(item => item.ariaCurrent = "false")
this.popoverTarget.showPopover()
}
closePopover() {
this.triggerTarget.ariaExpanded = "false"
this.popoverTarget.hidePopover()
}
filterItems(e) {
if (["ArrowDown", "ArrowUp", "Tab", "Enter"].includes(e.key)) {
return
}
const filterTerm = this.searchInputTarget.value.toLowerCase()
if (this.hasToggleAllTarget) {
if (filterTerm) this.toggleAllTarget.parentElement.classList.add("hidden")
else this.toggleAllTarget.parentElement.classList.remove("hidden")
}
let resultCount = 0
this.selectedItemIndex = null
this.inputTargets.forEach((input) => {
const text = this.inputContent(input).toLowerCase()
if (text.indexOf(filterTerm) > -1) {
input.parentElement.classList.remove("hidden")
resultCount++
} else {
input.parentElement.classList.add("hidden")
}
})
this.emptyStateTarget.classList.toggle("hidden", resultCount !== 0)
}
keyDownPressed() {
if (this.selectedItemIndex !== null) {
this.selectedItemIndex++
} else {
this.selectedItemIndex = 0
}
this.focusSelectedInput()
}
keyUpPressed() {
if (this.selectedItemIndex !== null) {
this.selectedItemIndex--
} else {
this.selectedItemIndex = -1
}
this.focusSelectedInput()
}
focusSelectedInput() {
const visibleInputs = this.inputTargets.filter(input => !input.parentElement.classList.contains("hidden"))
this.wrapSelectedInputIndex(visibleInputs.length)
visibleInputs.forEach((input, index) => {
if (index == this.selectedItemIndex) {
input.parentElement.ariaCurrent = "true"
input.parentElement.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'nearest' })
} else {
input.parentElement.ariaCurrent = "false"
}
})
}
keyEnterPressed(event) {
event.preventDefault()
const option = this.itemTargets.find(item => item.ariaCurrent === "true")
if (option) {
option.click()
}
}
wrapSelectedInputIndex(length) {
this.selectedItemIndex = ((this.selectedItemIndex % length) + length) % length
}
updatePopoverPosition() {
this.cleanup = autoUpdate(this.triggerTarget, this.popoverTarget, () => {
computePosition(this.triggerTarget, this.popoverTarget, {
placement: 'bottom-start',
middleware: [offset(4), flip()],
}).then(({ x, y }) => {
Object.assign(this.popoverTarget.style, {
left: `${x}px`,
top: `${y}px`,
});
});
});
}
updatePopoverWidth() {
this.popoverTarget.style.width = `${this.triggerTarget.offsetWidth}px`
}
}