Add closed arrow key navigation

This commit is contained in:
Talia 2025-08-07 19:38:16 +02:00
parent 82ccdb3a55
commit aad018dad0
Signed by: darkwiiplayer
GPG key ID: 7808674088232B3E

View file

@ -126,6 +126,9 @@ export class BetterSelect extends HTMLElement {
#abortOpen #abortOpen
#value = {} #value = {}
/** @type {number} */
#index = undefined
#internals = this.attachInternals() #internals = this.attachInternals()
#states = new StateAttributeSet(this, this.#internals.states) #states = new StateAttributeSet(this, this.#internals.states)
@ -313,6 +316,14 @@ export class BetterSelect extends HTMLElement {
this.keyboardSearchAppend(key) this.keyboardSearchAppend(key)
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
} else if (key == "Delete") {
this.clear()
} else if (key == "ArrowDown") {
event.preventDefault()
this.next()
} else if (key == "ArrowUp") {
event.preventDefault()
this.previous()
} }
} }
}) })
@ -459,14 +470,17 @@ export class BetterSelect extends HTMLElement {
/** @param {HTMLElement} option */ /** @param {HTMLElement} option */
setOption(option) { setOption(option) {
return this.setValue(option.dataset.value, option.innerText) return this.setValue(Number(option.dataset.index), option.dataset.value, option.innerText)
} }
/** /**
* @param {number} index
* @param {string} value * @param {string} value
* @param {string} state * @param {string} state
*/ */
setValue(value, state=value) { setValue(index, value, state=value) {
this.#index = Number(index)
this.#value = {value, state} this.#value = {value, state}
this.dispatchEvent(new Event("change", {bubbles: true})); this.dispatchEvent(new Event("change", {bubbles: true}));
this.#internals.setFormValue(value, state) this.#internals.setFormValue(value, state)
@ -498,9 +512,10 @@ export class BetterSelect extends HTMLElement {
if (value === undefined) { if (value === undefined) {
this.clear() this.clear()
} else { } else {
let index = 0
for (const option of this.options) { for (const option of this.options) {
if (option.value === String(value)) { if (option.value === String(value)) {
this.setValue(option.value, option.innerText) this.setValue(index++, option.value, option.innerText)
return return
} }
} }
@ -512,8 +527,9 @@ export class BetterSelect extends HTMLElement {
setOptions() { setOptions() {
this.list.replaceChildren() this.list.replaceChildren()
let idx = 0
for (const option of this.options) { for (const option of this.options) {
const fragment = f`<li tabindex="0" part="item" data-value="${option.value}">${option.innerText}</li>` const fragment = f`<li data-index=${idx++} tabindex="0" part="item" data-value="${option.value}">${option.innerText}</li>`
const li = fragment.querySelector("li") const li = fragment.querySelector("li")
if (option.disabled) { if (option.disabled) {
li.part.add("disabled") li.part.add("disabled")
@ -555,7 +571,7 @@ export class BetterSelect extends HTMLElement {
get form() { return this.#internals.form } get form() { return this.#internals.form }
clear() { clear() {
this.setValue(undefined, "") this.setValue(undefined, undefined, "")
} }
/** /**
@ -571,6 +587,21 @@ export class BetterSelect extends HTMLElement {
return "Please select an option." return "Please select an option."
} }
next() {
const index = (this.#index ?? -1) + 1
const item = /** @type {HTMLElement} */(this.list.children[index])
if (item) {
this.setOption(item)
}
}
previous() {
const index = (this.#index ?? this.options.length) - 1
const item = /** @type {HTMLElement} */(this.list.children[index])
if (item) {
this.setOption(item)
}
}
setValidity() { setValidity() {
if (this.required) { if (this.required) {