Correct or suppress all typescript errors

This commit is contained in:
Talia 2025-01-29 16:34:26 +01:00
parent 265ae85220
commit 86ac495feb
2 changed files with 40 additions and 17 deletions

View file

@ -3,6 +3,6 @@
"browser": "src/BetterSelect.js", "browser": "src/BetterSelect.js",
"type": "module", "type": "module",
"license": "MIT", "license": "MIT",
"version": "1.5.2", "version": "1.5.3",
"url": "https://darkwiiplayer.github.io/BetterSelect/" "url": "https://darkwiiplayer.github.io/BetterSelect/"
} }

View file

@ -3,12 +3,19 @@
*/ */
/** /**
* @param {function} fn * @template Result
* @typedef {((arr: TemplateStringsArray, ...params: String[])=>Result) & ((string: String)=>Result)} Template
*/
/**
* @template Type
* @param {(string: String)=>Type} fn
* @return {Template<Type>}
*/ */
const template = fn => { const template = fn => {
/** /**
* @param {TemplateStringsArray|String} arr * @param {String|TemplateStringsArray} arr
* @param {string[]} params * @param {String[]} params
*/ */
return (arr, ...params) => { return (arr, ...params) => {
if (arr instanceof Array) { if (arr instanceof Array) {
@ -67,6 +74,11 @@ export class BetterSelect extends HTMLElement {
#value = {} #value = {}
#internals = this.attachInternals() #internals = this.attachInternals()
/** @type {Set<String>} */
// @ts-ignore
#states = this.#internals.states
static formAssociated = true static formAssociated = true
static observedAttributes = Object.freeze(["placeholder", "search-placeholder", "required"]) static observedAttributes = Object.freeze(["placeholder", "search-placeholder", "required"])
@ -184,19 +196,25 @@ export class BetterSelect extends HTMLElement {
this.#internals.role = "combobox" this.#internals.role = "combobox"
this.options = this.getElementsByTagName("option") // @ts-ignore
this.options = /** @type {HTMLOptionElement[]} */(this.getElementsByTagName("option"))
// @ts-ignore
for (const element of this.shadowRoot.querySelectorAll(`[id]`)) { for (const element of this.shadowRoot.querySelectorAll(`[id]`)) {
this[element.id] = element this[element.id] = element
} }
this.shadowRoot.addEventListener("click", event => { this.shadowRoot.addEventListener("click", event => {
/** @type {HTMLLIElement} */
// @ts-ignore
const item = event.target.closest("#list > li") const item = event.target.closest("#list > li")
if (item) { if (item) {
this.setOption(item) this.setOption(item)
this.dispatchEvent(new InputEvent("input", {bubbles: true})) this.dispatchEvent(new InputEvent("input", {bubbles: true}))
this.close() this.close()
} else if (!this.#internals.states.has("--open")) { } else if (!this.#states.has("--open")) {
this.open() this.open()
// @ts-ignore
} else if (this.display.contains(event.target) || this.display.contains(event.target.closest("[slot]")?.assignedSlot)) { } else if (this.display.contains(event.target) || this.display.contains(event.target.closest("[slot]")?.assignedSlot)) {
this.close() this.close()
} }
@ -204,7 +222,7 @@ export class BetterSelect extends HTMLElement {
this.addEventListener("keydown", event => { this.addEventListener("keydown", event => {
const key = event.key const key = event.key
if (this.#internals.states.has("--open")) { if (this.#states.has("--open")) {
if (key == " " && this.list.contains(this.shadowRoot.activeElement)) { if (key == " " && this.list.contains(this.shadowRoot.activeElement)) {
this.close() this.close()
event.preventDefault() event.preventDefault()
@ -233,6 +251,7 @@ export class BetterSelect extends HTMLElement {
}) })
this.shadowRoot.addEventListener("input", event => { this.shadowRoot.addEventListener("input", event => {
// @ts-ignore
const item = event.target.closest("#input") const item = event.target.closest("#input")
if (item) { if (item) {
this.search(item.value) this.search(item.value)
@ -263,10 +282,9 @@ export class BetterSelect extends HTMLElement {
this.closedSearch(this.keyboardSearchBuffer) this.closedSearch(this.keyboardSearchBuffer)
} }
/** /** @param {string} search */
* @param {string} search
*/
closedSearch(search) { closedSearch(search) {
// @ts-ignore
for (const item of this.list.children) { for (const item of this.list.children) {
if (this.match(search, item)) { if (this.match(search, item)) {
this.setOption(item) this.setOption(item)
@ -312,22 +330,25 @@ export class BetterSelect extends HTMLElement {
}, {signal}) }, {signal})
this.dialog.show() this.dialog.show()
this.#internals.states.add("--open") this.#states.add("open")
if ("populate" in this) { if ("populate" in this) {
this.#internals.states.add("--loading") this.#states.add("--loading")
// @ts-ignore
await this.populate() await this.populate()
this.#internals.states.delete("--loading") this.#states.delete("--loading")
} }
} }
close() { close() {
this.input.value = null this.input.value = null
// @ts-ignore
for (const hidden of this.list.querySelectorAll("[hidden]")) for (const hidden of this.list.querySelectorAll("[hidden]"))
hidden.removeAttribute("hidden") hidden.removeAttribute("hidden")
this.#abortOpen?.abort() this.#abortOpen?.abort()
this.#abortOpen = null this.#abortOpen = null
this.#internals.states.delete("--open") // @ts-ignore
this.#states.delete("--open")
this.dialog.close() this.dialog.close()
} }
@ -335,18 +356,20 @@ export class BetterSelect extends HTMLElement {
/** @param {String} value */ /** @param {String} value */
search(value) { search(value) {
// @ts-ignore
for (const item of this.list.children) { for (const item of this.list.children) {
item.toggleAttribute("hidden", !this.match(value, item)) item.toggleAttribute("hidden", !this.match(value, item))
} }
} }
selectDefault() { selectDefault() {
if (this.shadowRoot.activeElement?.matches(`[part="item"]`)) { const active = this.shadowRoot.activeElement
this.setOption(this.shadowRoot.activeElement) if (active instanceof HTMLOptionElement && active?.matches(`[part="item"]`)) {
this.setOption(active)
this.close() this.close()
return return
} }
const candidates = [...this.list.children].filter(child => !child.hasAttribute("hidden")) const candidates = /** @type {HTMLElement[]} */(Array.from(this.list.children).filter(child => !child.hasAttribute("hidden")))
if (candidates.length) { if (candidates.length) {
this.setOption(candidates[0]) this.setOption(candidates[0])
this.close() this.close()