mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-03-11 04:35:17 +09:00
45 lines
1.0 KiB
JavaScript
45 lines
1.0 KiB
JavaScript
export default class extends HTMLElement {
|
|
connectedCallback() {
|
|
if (!this.hasAttribute("role")) this.setAttribute("role", "listbox")
|
|
}
|
|
|
|
get optionElements() {
|
|
return this.querySelectorAll("suggestion-option")
|
|
}
|
|
|
|
get selectedIndex() {
|
|
const selected = this.querySelector("suggestion-option[selected]")
|
|
return selected?.index
|
|
}
|
|
|
|
set selectedIndex(value) {
|
|
const optionElements = this.optionElements
|
|
const optionCount = optionElements.length
|
|
|
|
if (!optionElements.length) return
|
|
|
|
Array.from(optionElements).forEach(option => {
|
|
option.selected = false
|
|
})
|
|
|
|
if (value === null || typeof value === "undefined") return
|
|
|
|
const index = Math.max(0, Math.min(optionCount - 1, parseInt(value, 10)))
|
|
optionElements[index].selected = true
|
|
}
|
|
|
|
get selectedOption() {
|
|
return this.optionElements[this.selectedIndex]
|
|
}
|
|
|
|
set selectedOption(option) {
|
|
if (option.selectElement === this) {
|
|
this.selectedIndex = option.index
|
|
}
|
|
}
|
|
|
|
get value() {
|
|
return this.selectedOption?.value
|
|
}
|
|
}
|